FFmpeg is a free and open-source command line-based tool to handle video, audio, and other multimedia files. It packs many encoders and decoders, making it powerful enough to support almost all common and uncommon multimedia formats.
FFmpeg comes with a learning curve especially if you are new to a command line tool. You will need to type commands with your keyboard to transcode multimedia files and streams. So here in this post, I will introduce 31 most commonly used FFmpeg commands with examples to perform various tasks like converting media formats, trimming videos, extracting audio/video streams, etc.
Download and Install FFmpeg
If you are just getting started, first you'll need to download the source code or a build from the official download page, and install it on your device. Here at VideoProc, we also prepare you a step-by-step tutorial about how to install FFmpeg.
1. FFmpeg Get Video Info
First, you can easily get the information of a given media file (say input.mp4) using FFmpeg. Normally it will require to specify the input file and output file, but in this case, the output file can be omitted since we only need to get some basic information. You can run the following command:
ffmpeg -i input.mp4 -hide_banner
The "hide_banner" flag helps hide FFmpeg banners and other details, and only shows the media file information.
2. Preview or Test Video or Audio Files
FFmpeg framework consists of FFmpeg itself, ffprobe, and ffplay, the FFmpeg library-based media player. If you want to preview or test a media file before or after dealing with it, you can play it with ffplay by performing the following command:
Add "-fs" to the command to enter fullscreen mode.
3. FFmpeg MOV to MP4 (Convert Video/Audio to Another Format)
FFmpeg is extremely useful to convert your video file to another format. For example, to convert MOV to MP4 with FFmpeg, run the command below.
ffmpeg -i input.mov output.mp4
Similarly, you can transcode audio files with FFmpeg by specifying the output format. For example, FFmpeg will convert WAV to MP3 with the following command.
ffmpeg -i input.wav -vn -ar 44100 -ac 2 -b:a 192k output.mp3
4. FFmpeg Extract Audio (Convert Video to Audio)
In addition to converting the video/audio file to a different format, FFmpeg can also remove the video part or the audio part separately. The following command will remove the video stream from a video file, letting you extract audio from video.
ffmpeg -i input.mp4 -vn output.mp3
5. FFmpeg Remove Audio
Also, you can remove the audio stream and keep the video stream with FFmpeg using the command below. You can also set the output format (MP4 in this case) as other supported formats.
ffmpeg -i input.mp4 -an mute-output.mp4
6. Convert a Specific Portion of a Video
In addition to converting the format of the entire video, FFmpeg also allows you to convert a specific part of the video or audio format when needed. In this situation, you need to use the -t value to specify the time. For example, the following command will convert the first 10 seconds of the input.mp4 to MKV format. Note you can also specify the time in hh.mm.ss format.
ffmpeg -i input.mp4 -t 10 output.mkv
7. FFmpeg Trim Video
FFmpeg can also easily help with trimming video and audio files. Use the -ss value to set the start time of the clip, and -to value to set the end time. The -c value is to set the codec. Similarly, you can also use the -t value in the example above to set the duration of the clip.
ffmpeg -i input.mp4 -ss 00:01:54 -to 00:06:53 -c copy output.mp4
8. Cropping Videos
Use the command below to crop the frame in any dimension you want. In this case, -filter:v indicates the filter is for the video. Crop specifies the filter you need here is the crop filter. "w:h:x:y" represents the width, height, and position of the frame respectively.
ffmpeg -i input.mp4 -filter:v "crop=w:h:x:y" output.mp4
9. FFmpeg Resize Video
The following command will resize the video to a desired size. -vf in the command below is an alias for -filter:v in the example above.
ffmpeg -i input.mp4 -vf scale=1920:1080 output.mp4
10. FFmpeg Combine Audio and Video
If you have an audio and a video file, you can mix your video with the audio using the following command. Note you have to use the container format compatible with the coding formats.
ffmpeg -i audio.mp3 -i video.mp4 video_audio_mix.mkv
11. FFmpeg Concatenate Videos
FFmpeg will serve as a video merger. If you have several video clips encoded with the same codec, you can merge or concatenate these videos into one. Just create a .txt file with a list of all source files that you wish to concatenate, then run the command below.
ffmpeg -f concat -i file-list.txt -c copy output.mp4
12. FFmpeg Rotate Video
You can easily rotate a video clockwise and counter-clockwise using FFmpeg, as well as vertically and horizontally flip a video. The feature we use in this case is "Transpose". The following command will rotate the input video by 90 degrees clockwise.
ffmpeg -i input.mp4 -vf "transpose=1" output.mp4
The available parameters for transpose are 0, 1, 2, and 3.
- 0 - Rotate by 90 degrees counter-clockwise and flip vertically (default value).
- 1 - Rotate by 90 degrees clockwise.
- 2 - Rotate by 90 degrees counter-clockwise.
- 3 - Rotate by 90 degrees clockwise and flip vertically.
Note that you will need to mention the transpose parameter two times like below to rotate videos by 180 degrees clockwise.
ffmpeg -i input.mp4 -vf "transpose=2,transpose=2" output.mp4
13. FFmpeg Speed Up Video
FFmpeg helps change the playback speed of the video. You can speed up or slow down a video per your needs. For example, the following command will double the speed of the video. You can set the value according to your needs.
ffmpeg -i input.mp4 -vf "setpts=0.5*PTS" output.mp4
14. FFmpeg Compress Video
Compressing the video with FFmpeg will reduce the file size and save storage space. Run the following command to compress the input video.
ffmpeg -i input.mp4 -vf scale=1280:-1 -c:v libx264 -preset veryslow -crf 24 output.mp4
15. Zoom In and Zoom Out Videos
You can apply zoom and pan effect to the given video with the zoompan filter in FFmpeg. The following command will zoom in up to 1.5x and pan at the center of the picture.
- zoom, z - Set the zoom expression. Range is 1-10. Default is 1.
- d - Set the duration expression in number of frames. Default is 90.
- x, y - Set the x and y expression. Default is 0.
- in_w, iw, in_h, ih - Input width/height.
16. FFmpeg Loop
To loop a video with FFmpeg, you'll need to use the loop filter. For example, the following command will loop the input MP4 video for three times. The default of the number of loops is 0, meaning no loop. Use -1 for an infinite loop.
ffmpeg -stream_loop 2 -i input.mp4 -c copy output.mp4
17. Set Frames Per Second (FPS)
FFmpeg is also useful to change some parameters, including FPS and GOP. You can simply change the FPS for different uses without changing other parameters. Type and run the command below, and FFmpeg will change the original FPS to the value you set.
ffmpeg -i input.mp4 -vf "fps=30" output.mp4
18. Set Group of Pictures (GOP)
GOP means group of pictures, which is the distance between two keyframes. You can set the -g value as you want. Note that forcing too many keyframes is very harmful for the lookahead algorithms of certain encoders.
ffmpeg -i input.mp4 -g 300 output.mp4
19. Copy Metadata
You can also copy the metadata of a source media file with the following FFmpeg command. Note it might not copy all the metadata correctly since some videos store custom metadata.
ffmpeg -i input.mov -map_metadata 0 -movflags use_metadata_tags output.mp4
20. Add Subtitle to Video
FFmpeg can add a separate subtitle file to a video with the following command. Note to choose a compatible container format for the output video.
ffmpeg -i input.mp4 -i subtitle.srt -map 0 -map 1 -c copy -c:v libx264 -crf 23 -preset veryfast video-output.mkv
21. Keep Original Encoding
Sometimes you may just want to copy the original encoding. The command below will create a duplicate copy of the input video. The "-c copy" parameter makes ffmpeg omit the decoding and encoding step for the specified stream, so it does only muxing and demuxing. It is useful for only changing the container format or modifying container-level metadata.
ffmpeg -i input.mp4 -c copy output.mkv
22. Create Animated GIF
From here I will introduce some FFmpeg features surrounding image files.
First let's see how to create an animated GIF from a video using FFmpeg. The basic formula is as simple as shown below:
ffmpeg -i input.mp4 output.gif
You can also add some parameters to customize the output GIF image:
ffmpeg -ss 00:00:20 -i input.mp4 -to 10 -r 10 -vf scale=200:-1 output.gif
- -ss: indicates the start point of the GIF
- -to : indicates the end position of the GIF
- -r : means frame rate
- -vf scale: is to scale the GIF image in the desired size
23. FFmpeg Resize Image
Similar to resizing video in the above example, you can also use FFmpeg to resize images. The following command will resize the image to 1920:1080. You can set this value to any of your desired sizes.
ffmpeg -i input.png -vf "scale=1920:1080" output.png
24. FFmpeg Images to Video
FFmpeg can turn a series of images to a video. This is very helpful when you want to make a slideshow. For example, the following command will create a slideshow with a series of images named as img001.png, img002.png, etc. -r 1/5 indicates that each image will have a duration of 5 seconds.
ffmpeg -r 1/5 -i img%03d.png -c:v libx264 -r 30 -pix_fmt yuv420p output.mp4
25. Convert a Single Image Into a Video
Since video is composed of frames of images, it is possible to convert a single image into a video file with FFmpeg. Use the -t parameter to specify the duration of the video. You can also add other parameters as in the following command.
ffmpeg -loop 1 -i input.png -c:v libx264 -t 30 -pix_fmt yuv420p video.mp4
26. FFmpeg Extract Frames
Instead of converting a single image or a series of images to a video, you can also extract frames from a video. The following command will extract 1 frame every 1 second from your input video, and the exacted images will be named with 3 digits like 001.png, 002.png, etc. You can also add other parameters like -ss and -t.
ffmpeg -i input.mp4 -r 1 image-%03d.png
Refer to this guide on how to extract frames from video with FFmpeg for detailed information.
27. Change the Volume of Audio Files
The following are some commonly used FFmpeg commands for audio files.
One of the most commonly used is to change the volume of the audio. FFmpeg comes when the volume of the background music is too high or too low. You'll need the "volume filter" option. For example, the command below will decrease the audio volume by half. Of course, you can also set the value according to your needs.
ffmpeg -i input.mp3 -af 'volume=0.5' output.mp3
28. Compressing Audio Files
Just like compressing a video, you can also compress an audio to reduce the file size. The -ab flag of FFmpeg will specify the bit rate for the target audio and save you some storage space. The available audio bitrates are 96kbps, 112kbps, 128kbps, 160kbps, 192kbps, 256kbps, and 320kbps. Simply use a smaller value than the source file uses to compress the audio.
ffmpeg -i input.mp3 -ab 128k output.mp3
29. Increase/Decrease Audio Playback Speed
Similarly, we can also use FFmpeg to speed up or slow down the playback speed of the audio. In this case, we are going to use the "atempo" audio filter. Any value between 0.5 and 2.0 can be used for audio. For example, the command below will double the speed of audio.
ffmpeg -i input.mp3 -filter:a "atempo=2.0" -vn output.mp3
30. Add an Image to Audio
Have you ever tried to upload an MP3 audio to YouTube but failed? If the answer is yes, adding an image to the audio with FFmpeg may help with this situation. The following command will convert your audio to an MP4 video with a given image. You can also choose another compatible container based on the coding format of your audio.
ffmpeg -loop 1 -i image.jpg -i input.mp3 -c:v libx264 -c:a aac -strict experimental -b:a 192k -shortest output.mp4
31. Getting Help
You can always use -h to print help about a specific item in the ff* tools (including FFmpeg).
The above content covered some of the most commonly used FFmpeg flags with FFmpeg examples. For more information and use cases, you can always refer to the official FFmpeg Documentation.