Thursday 14 April 2016

Converting videos to different format/resolution and creating thumbnails using FFMPEG in ColdFusion

If we are going to create a video player application in ColdFusion, then we have to work on different aspects of the video files to upload,play and store in server.

We have different formatted video files that we can upload to our application to play, but we have to make a default format file for all so that the video player will play that particular file easily.
So, the first process would be, we have to convert all uploaded files to a .mp4 file for example. If the user uploads an .AVI file or .DAT file, then we have to convert that file to .mp4 file for better
resolution and better picture quality.

So for converting a video file to .mp4 file we can use FFMPEG plug in. Below code we can use to do that.

<cfset argString = '/c #ARGUMENTS.serverPath#\manage\ffmpeg.exe -i "#fsVideoFile#" -ar 22050 "#ARGUMENTS.serverDirPath#\#dltSpaceServerFileName#.mp4" 2>&1'>

<cfexecute name="#ARGUMENTS.name#"
    arguments="#ARGUMENTS.argument#"
    timeout="3000"
    variable="jsonInfo"
    errorVariable="errorOut"
/>

ARGUMENTS.serverPath = The path wehere you store the ffmpeg.exe file(You can download this file form FFMPEG site).
fsVideoFile = The original file uploaded by the user.
ARGUMENTS.serverDirPath = The path where you can store the new converted video file.
dltSpaceServerFileName = The new video file name.


By using the above code you can convert the video files to .mp4. You can actually convert to any of the video format by changing the .ext to the new video file name.

If it necessary, then we can also get the length of the video file using the same API. For that, we can use below code.

<cfexecute name="#ARGUMENTS.serverPath#\manage\ffprobe.exe"
    arguments="#fsVideoFile# -v quiet -print_format json -show_format -show_streams"
    timeout="3000"
    variable="jsonInfo"
    errorVariable="errorOut"
/>

This above code is responsible for getting the length or duration of the video file. We can also get the resolution of that particular video file.
We would need the resolution of the video file to know whether it is a high resolution file or not. High resolution files need more internet speed to play. We can play the video by
depending upon our internet speed. If the internet is fast then we can play the high resolution or hd video files, if not the the medium or low resolution file would be played.

We can also create different resolution files by using this API. If the user upload a high resolution video file then, we can convert it to two different resolution files.


<cfexecute name="#ARGUMENTS.serverPath#\manage\ffmpeg.exe#"
    arguments="/c #ARGUMENTS.serverPath#\manage\ffmpeg.exe -i "#fsVideoFile#" -s 800x600 -ar 22050 "#fsVideoFileLarge#" 2>&1"
    timeout="3000"
    variable="jsonInfo"
    errorVariable="errorOut"
/>


<cfexecute name="#ARGUMENTS.serverPath#\manage\ffmpeg.exe#"
    arguments="/c #ARGUMENTS.serverPath#\manage\ffmpeg.exe -i "#fsVideoFile#" -s 480x320 -ar 22050 "#fsVideoFileLarge#" 2>&1"
    timeout="3000"
    variable="jsonInfo"
    errorVariable="errorOut"
/>


Here we are creating two different resolutions files with a resolution of 800x600 and 480x320. We can dynamically switch to the different resolution files depending upon the bandwidth.

There is also a mechanism we can extract the images from the video to show as a thumbnail.

<cfexecute name="#ARGUMENTS.serverPath#\manage\ffmpeg.exe#"
    arguments="-itsoffset -4  -i #fsVideoFile# -vcodec mjpeg -vframes 1 -an -f rawvideo -s 500x300 #ARGUMENTS.serverDirPath#\images\posters\poster_#dltSpaceServerFileName#.png"
    timeout="3000"
    variable="jsonInfo"
    errorVariable="errorOut"
/>

No comments:

Post a Comment