FFmpeg useful commands like generate preview image from video, jpg to png, overlay image on video, merge video, extract video and create video from the single image.
How to generate reverse video
Below command explodes frame and join by reverse order so it will be converted to reverse video.
1 2 3 4 5 6 |
$command-="ffmpeg -i /Users/ankit/Downloads/a.mp4 -filter_complex "[0:v]setpts=2.0*PTS,reverse[v];[0:a]atempo=0.5,areverse[a]" -map "[v]" -map "[a]" /Users/ankit/Downloads/ad.mp4"; shell_exec($command); |
How to generate a preview image from the video
it will help to generate a preview image from the video, just pass the screen size of the screenshot & which second of video.
1 2 3 4 5 6 7 8 9 10 |
//PHP CODE $size = "640x480"; $getfromsecondvideo = 0; ///ROTATE WITH SCRREEN SHOT $command = 'ffmpeg -i '.$videofile.' -vf "transpose=1" -an -ss 0 -s 640x480 image.png'; $command = 'ffmpeg -i ' . $videofile . ' -vf "transpose=0" -an -ss ' . $getfromsecondvideo . ' -s ' . $size . ' ' . $saved_imagefile; shell_exec($command); |
How to convert jpg to png
1 2 3 4 5 6 7 8 9 |
//PHP CODE $jpg_image_path = "fd/image.jpg"; $png_image_path = "fd/image.png"; $command = "ffmpeg -i " . $jpg_image_path . " " . $png_image_path; shell_exec($command); |
How to set overlay image on the video
1 2 3 4 5 6 7 8 9 10 11 |
//PHP CODE $image = "fd/image.png"; $video = "fd/video.mp4"; $saved_video_path="fd/new_video.mp4"; $i = "'"; $command = 'ffmpeg -i ' . $video . ' -i ' . $image . ' -filter_complex "[0:v][1:v] overlay=25:25:enable=' . $i . 'between(t,0,1)' . $i . '" '.$saved_video_path; shell_exec($command); |
How to merge videos
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// PHP CODE $video1 = "moment_video/75.mp4"; $video2 = "moment_video/76.mp4"; $command1 = 'ffmpeg -i ' . $video1 . ' -c copy -bsf:v h264_mp4toannexb -f mpegts moment_video/intermediate1.ts'; $command2 = 'ffmpeg -i ' . $video2 . ' -c copy -bsf:v h264_mp4toannexb -f mpegts moment_video/intermediate2.ts'; $command3 = 'ffmpeg -i "concat:moment_video/intermediate1.ts|moment_video/intermediate2.ts" -c copy -bsf:a aac_adtstoasc moment_video/7576.mp4'; shell_exec($command1); shell_exec($command2); shell_exec($command3); unlink("intermediate1.ts"); unlink("intermediate2.ts"); |
How to extract images from video
1 2 3 4 5 6 7 8 9 10 |
//PHP CODE //$video=video path //10 is number of image from video $video = "video.mp4"; $command = "ffmpeg -i " . $video . " -an -r 10 -y -s 640x480 video%d.jpg"; shell_exec($command); |
How to create video from image
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//PHP CODE //$image_path=image path $image_path = "images/image1.png"; //$timer=number of second $timer="3"; //$result_video_path= generated video path $result_video_path="images/video.mp4"; $command_1 = "ffmpeg -loop 1 -i " . $image_path. " -c:v libx264 -t " . $timer . " -pix_fmt yuv420p " . $result_video_path . ""; shell_exec($command_1); |
How to mute a video
1 2 3 4 5 6 7 8 9 10 |
//PHP CODE //$video=video path // $video = "video.mp4"; $command = "ffmpeg -i ".$video." -an mute_video.mp4"; shell_exec($command); |
How to get audio from video
1 2 3 4 5 6 7 8 9 10 |
//PHP CODE //$video=video path // $video = "video.mp4"; $command = "ffmpeg -i ".$video." -vn -ab 256 audio.mp3"; shell_exec($command); |
More Stories
How to set EC2 Ubuntu default password
CPU & Memory usage in PHP
Install PHP mcrypt extension on Ubuntu