Escape Keys - TomdeMan's Blog
FFMPEG and ColdFusion

This topic seems to come up often.

I worked on a project recently that required the use of FFMPEG to convert videos into FLV. Your typical YouTube approach. There are some posts out there that offer what they can, but most using the native CFEXECUTE. Which I can say myself was not an effective solution.

It seems the thread would either close too early or not at all. No matter what you set for your timeout.

Our solution was to use the Java Runtime library.

The function looked like this:

(Keep in mind your arguments will vary depending on what you are trying to do here)

<cffunction name="convertVideo" access="public" output="false" returntype="struct">
<cfargument name="binPath" required="Yes" type="string" />
<cfargument name="srcPath" required="Yes" type="string" />
<cfargument name="destPath" required="Yes" type="string" />

<cfset var rtnStruct = structNew() />
<cfset var r = '' />
<cfset var ar_arg = arrayNew(1) />

<cfscript>
rtnStruct.success = false;
arrayappend(ar_arg, "-i");
arrayAppend(ar_arg, '"#arguments.srcPath#"');
arrayappend(ar_arg, "-g 300");
arrayappend(ar_arg, "-y");
arrayappend(ar_arg, "-s");
arrayappend(ar_arg, "320x240");
arrayappend(ar_arg, "-aspect");
arrayappend(ar_arg, "4:3");
arrayappend(ar_arg, "-f");
arrayappend(ar_arg, "flv");
arrayappend(ar_arg, "-ar");
arrayappend(ar_arg, "44100");
arrayAppend(ar_arg, '"#arguments.destPath#"');

try{
r = createObject('java','java.lang.Runtime').getRuntime();
r.exec('#arguments.binPath#\ffmpeg.exe #arraytoList(ar_arg,' ')#');
r.runFinalization();
r.freeMemory();
rtnStruct.success = true;
}

catch(excpt any){
rtnStruct.message = excpt.message;
}
</cfscript>

<cfreturn rtnStruct />
</cffunction>

Comments:

[Add Comment]

Don says:

I was trying your function, and for some reason the files ffmpeg created were only a second long, and ffmpeg stayed in memory. I copied the cmd string exactly as its sent to the java runtime and it worked, but it seems whenever I try to use the function to execute it, it would stop after a second, did you ever experience this?

6/28/08 7:14 PM

[Add Comment]