List of useful and most common commands used in ffmpeg

A list of mostly all of the ffmpeg commands that can be used in daily use for conversion, creating images, editing videos . Read the post below for more details.

Resize a video

-s: switch to resize a video.

ffmpeg -i input.mp4 -s 480x320 -c:a copy output.mp4

Convert video from one format to another

ffmpeg -i flash_stream.flv -c:v libx264 converted.mp4
ffmpeg -i Wildlife.wmv -c:v libx264 -preset ultrafast sample.mp4

Information of a video

ffmpeg -i video.mp4

concatenate video files

ffmpeg -f concat -i file-list.txt -c copy output.mp4

Remove the audio from a clip

Use the -an parameter to disable the audio portion of a video stream.

ffmpeg -i video.mp4 -an mute-video.mp4

Extract the audio from video

-vn :  extracts  audio portion from a video save it using the -ab switch as a 256kbps MP3 audio file.

ffmpeg -i video.mp4 -vn -ab 256 audio.mp3

Convert a video into animated GIF

ffmpeg -i video.mp4 -vf scale=500:-1 -t 10 -r 10 image.gif

Extract image frames from a video

ffmpeg -ss 00:00:15 -i video.mp4 -vf scale=800:-1 -vframes 1 image.jpg

Convert Video into Images

This command saves image frame after every 4 seconds.

ffmpeg -i movie.mp4 -r 0.25 frames.png

Create video slideshow from images

This command creates a video slideshow using a series of images that are named as img001.png, img002.png, etc. Each image will have a duration of 5 seconds (-r 1/5).

ffmpeg -r 1/5 -i img%03d.png -c:v libx264 -r 30 -pix_fmt yuv420p slideshow.mp4

Split a video into multiple parts

This command will split the source video into 2 parts – one ending at 30s from the start and the other beginning at 30s and ending at the end of the input video.

ffmpeg -i video.mp4 -t 00:00:30 -c copy part-1.mp4 -ss 00:00:30 -codec copy part-2.mp4

Cut video file into a smaller file

-ss: specify the start time stamp in format
-t: specify duration of the video in seconds.

ffmpeg -i input.mp4 -ss 00:00:50.0 -codec copy -t 20 output.mp4

Use the -t parameter to specify the duration of the video.

ffmpeg -loop 1 -i image.png -c:v libx264 -t 30 -pix_fmt yuv420p video.mp4

Add subtitles to a movie

This will take the subtitles from the .srt file. FFmpeg can decode most common subtitle formats.

ffmpeg -i movie.mp4 -i -map 0 -map 1 -c copy -c:v libx264 -crf 23 -preset veryfast output.mkv

Crop an audio file

This will create a 30 second audio file starting at 90 seconds from the original audio file without transcoding.

ffmpeg -ss 00:01:30 -t 30 -acodec copy -i inputfile.mp3 outputfile.mp3

Change the audio volume

You can use the volume filter to alter the volume of a media file using FFmpeg. This command will half the volume of the audio file.

ffmpeg -i input.wav -af 'volume=0.5' output.wav

Rotate a video

This command will rotate a video clip 90° clockwise. You can set transpose to 2 to rotate the video 90° anti-clockwise.

ffmpeg -i input.mp4 -filter:v 'transpose=1' rotated-video.mp4

This will rotate the video 180° counter-clockwise.

ffmpeg -i input.mp4 -filter:v 'transpose=2,transpose=2' rotated-video.mp4

Speed up or Slow down the video

You can change the speed of your video using the setpts (set presentation time stamp) filter of FFmpeg. This command will make the video 8x (1/8) faster or use setpts=4*PTS to make the video 4x slower.

ffmpeg -i input.mp4 -filter:v "setpts=0.125*PTS" output.mp4

Speed up or Slow down the audio

For changing the speed of audio, use the atempo audio filter. This command will double the speed of audio. You can use any value between 0.5 and 2.0 for audio.

ffmpeg -i input.mkv -filter:a "atempo=2.0" -vn output.mkv

 Merge an audio and video file

You can also specify the -shortest switch to finish the encoding when the shortest clip ends.
ffmpeg -i video.mp4 -i audio.mp3 -c:v copy -c:a aac -strict experimental part1.mp4
ffmpeg -i video.mp4 -i audio.mp3 -c:v copy -c:a aac -strict experimental -shortest part2.mp4


Extract Frames from Video
1.1 Extract the first frame from video
ffmpeg –vframes 1 –i input_video_file_name –f image2 output_frame.bmp
ffmpeg also supports other image formats like .png or .jpg.
1.2 Extract all frames from video
ffmpeg –i input_video_file_name –f image2 output_file_name_format.bmp
Note that output_file_name_format.bmp can be, for example, %05d.bmp

Crop Video
ffmpeg –i input_video_file_name –vf crop=w:h:x:y output_file_name
Note that w and h are width and height, and x, y is the position of the top-left corner before crop. vf means video filter. crop is kind of video filter in ffmpeg.

Convert Image between Different Formats
ffmpeg –i input_image_file_name.ext1 output_image_file_name.ext2

Create Video from Sequences of Images
ffmpeg –r 20 –i %input_image_file_name_format.ext video_file_name.ext2

As an example, ffmpeg –r 20 –i %05.bmp test.avi

Convert Video to Different Container Formats
ffmpeg –i input_video_file_name.ext1 output_video_file_name.ext2

Example 1:  Convert Video from AVI to FLV
Sometimes ffmpeg cannot figure out everything itself, we may need to supply more info. I encountered an example where I’ll need to specify the output video, audio codec and scale.
ffmpeg -i recording_3.avi -f flv -vcodec copy -acodec copy -s 1280×720 recording3.flv
Note that the command above only changes the container format, it specifies same audio/video codec (using the “copy” option) as the avi file for the output flv file. This command runs very fast as it left the codec untouched.

Convert Video to Different Codec (Transcoding)
ffmpeg -i input_video_file_name.ext1 -vcodec xxx -r xx -s aaaaxbbbb -aspect xx:xx -b xxxxk output_file_name.ext2
As an example,
ffmpeg -i h1.mp4 -vcodec mpeg4 -r 30 -s 1280×720 -aspect 16:9 -b 10000k h1_1280_720.mp4
This command takes input h1.mp4 (it has h264 codec in my case), transcode it to mpeg4 codec, with frame rate 30 fps, resolution 1280×720, aspect ratio 16:9, bitrate of 10000 kbits/s, and output it to h1_1280_720.mp4 file.


This entry was posted in webdevelopment. Bookmark the permalink.