Video Conversion (FFMPEG)

FFMPEG is the most popular library for video manipulation. FFMPEG provides large set of commands for media processing. You can join video files,audio files , can convert video files in different formats, generate thumbnails, generate gif and many more. FFMPEG is a complete solution for any manipulation for audio and video files.

Here I am writing some most useful commands that you can use in your project.
For details you can visit:

Example of generating thumb of video and converting all videos to mp4:


/* generating thumbnail from a video */
$thumbname='thumb_'.$filename.'.jpg'; //give a name to thumb
$size = "400*400"; //size of thumb
$getFromSecond = 1; //from which second you want to capture thumb
$uploadThumbPath='/var/www/html/test/videoprocessed/thumbs/'.$thumbname; //path to upload thumb on server
$cmd = shell_exec("ffmpeg -i $tempLocation -an -ss $getFromSecond -s $size $uploadThumbPath 2>&1");

Getting Deatils Of Video
//getting video information
ffprobe command is used to get detailed info about a media. You can all info in json format:

$videoInfo=shell_exec("ffprobe -v quiet -print_format json -show_format -show_streams $tempLocation 2>&1");								
$videoHeight=$videoInfoArray->streams[0]->height; //480
$videoWidth=$videoInfoArray->streams[0]->width; //720  

Coversion of video format:

//converting video in another format
//converting video in mp4 and set resolution to 720*480 
$cmd=shell_exec("ffmpeg -i $tempLocation -c:v libx264 -s 720*480 $uploadfilePathMp4 2>&1");     
//just converting video in mp4
$cmd=shell_exec("ffmpeg -i $tempLocation -c:v libx264  $uploadfilePathMp4 2>&1");     

You can use these two commands accordingly by getting video current resolution by ffprobe


if($videoWidth>720 || $videoHeight>720) //if video resolution is high then convert video in 720*480
$cmd=shell_exec("ffmpeg -i $tempLocation -c:v libx264 -s 720*480 $uploadfilePathMp4 2>&1");                                                        
else{ //just change format
$cmd=shell_exec("ffmpeg -i $tempLocation -c:v libx264  $uploadfilePathMp4 2>&1");