4. September 2009 11:59 by Markus Wollny

UDF to grab a frame from an FLV to JPG

4
Sep/09
0

This requires FFMPEG to be installed on your server. Here’s the UDF:

<cffunction name="flvgrabber" access="public" output="no" returntype="void" hint="grabs a frame from an FLV at a specified second and renders it as a JPG">
	<cfargument name="strPathToFLV" type="string" required="yes" hint="absolute path to the source flv">
	<cfargument name="strPathToJPG" type="string" required="yes" hint="absolute path to the target JPG; if file exists and is writeable, it will be overwritten">
	<cfargument name="strFrameAtTime" type="string" required="no" default="00:00:05" hint="time at which frame should be grabbed in format hh:mm:ss">
	<cfscript>
		var strTMPPath = '/tmp/';
		var strUniqueFname = CreateUUID();
		var strPathToFFMPEG = '/usr/bin/ffmpeg';
		var strArguments = '';
		var qTempFile = '';
	</cfscript>
 
	<cfif not DirectoryExists(getDirectoryFromPath(arguments.strPathToJPG))>
		<cfthrow message="target directory does not exist">
	</cfif>
	<cfif not FileExists(arguments.strPathToFLV)>
		<cfthrow message="source FLV does not exist">
	</cfif>
	<cfif not RefindNoCase('\.flv$',arguments.strPathToFLV)>
		<cfthrow message="source file must be an .flv">
	</cfif>
	<cfif not RefindNoCase('\.jpg$',arguments.strPathToJPG)>
		<cfthrow message="target file must be a .jpg">
	</cfif>		
	<cfif not RefindNoCase('^\d\d:\d\d:\d\d$',arguments.strFrameAtTime)>
		<cfthrow message="time must be set as hh:mm:ss">
	</cfif>
	<cfset strArguments = "-i ""#arguments.strPathToFLV#"" -an -ss #arguments.strFrameAtTime# -an -r 1 -vframes 1 -y #strTMPPath##strUniqueFname#-%d.jpg">		
 
	<cfexecute name="#strPathToFFMPEG#" arguments="#strArguments#" timeout="30"></cfexecute>
	<cfdirectory name="qTempFile" action="list" directory="#strTMPPath#" filter="#strUniqueFname#-*.jpg" listinfo="name" recurse="no" type="file">
	<cffile action="move" source="#strTMPPath##qTempFile.name#" destination="#arguments.strPathToJPG#">		
</cffunction>

And here’s how to use it:

<cfscript>
flvgrabber(strPathToFLV='/some/path/some.flv',strPathToJPG='/some/path/some.jpg',strFrameAtTime='00:00:03');
</cfscript>

Have fun!

Tagged as: , , , ,