FFmpeg Recipes
FFmpeg is an open-source software framework capable of "decode, encode, transcode, mux, demux, stream, filter, and play pretty much anything that humans and machines have created".
FFmpeg does a lot of things, it can be difficult to get a handle on all the different use cases and options. But for a handfulcommon scenario, all you need is quick pre-made FFmpeg template so you can copy-paste them to the command line. This article will show you a few FFmpeg recipe to combine a video file with one or multiple audio file. You can also merge an audio-less video with an audio file as well.
To get a better understanding of FFmpeg and it's benefits read my Blog post about FFmpeg.
To get to know how to install FFmpeg on your system ream my Tutorial for installing FFmpeg on many operating systems.
Basic Tasks
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:
ffplay input.mp4
Add "-fs
" to the command to enter fullscreen mode.
3. 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
4. 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
5. 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
6. 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
7. 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
Manipulate Video
1. FFmpeg MOV to MP4 (Convert Video 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,
2. 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
3. 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
4. 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
5. 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
6. 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
7. 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
8. 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
9. 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
10. 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
11. 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.
zoompan=z='min(zoom+0.0015,1.5)':d=700:x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)'
- 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.
12. 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
13. 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
14. 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
15. 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
16. 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
17. 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.
Manipulate Audio
1. FFmpeg WAV to MP3 (Convert Audio to Another Format)
FFmpeg will convert WAV to MP3 with the following command.
ffmpeg -i input.wav -vn -ar 44100 -ac 2 -b:a 192k output.mp3
2. 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
3. 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
4. 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
5. 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 128 output.mp3
6. 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
7. 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
Streaming Video and Audio Using FFmpeg
01. Stream Video
ffmpeg -stream_loop -1 -re -i music.mp3 -c:v libx264 -preset fast -b:v 5000k -maxrate 5000k -bufsize 7000k -pix_fmt yuv420p -g 50 -c:a aac -b:a 160k -ac 2 -ar 44100 -f flv URL{rtmps://d-1.rtmp.me/s/16359sOU3w}
02. Stream Audio
ffmpeg -stream_loop -1 -re -i music.mp3 -c:a aac -b:a 128k -ac 2 -ar 44100 -f flv {URL}
03. Stream Audio with Image
ffmpeg -loop 1 -f image2 -i img.jpg -f lavfi -i amovie=./music.mp3:loop=999,asetpts=N/SR/TB -vf realtime,scale=600:480,format=yuv420p -r 30 -g 60 -c:v libx264 -x264-params keyint=60 -bufsize 500k -c:a aac -ar 44100 -b:a 128k -f flv {URL}
04. Stream YT Video Again
ffmpeg -re -i https://URL.m3u8 -c:v copy -c:a aac -ar 44100 -ab 128k -ac 2 -strict -2 -flags +global_header -bsf:a aac_adtstoasc -bufsize 2500k -f flv {URL}
Advance Tasks
Now Let's see how to use above commands to do a bit advance tasks.
Combine separate video and audio using FFmpeg
If you have separate audio and video file, and the video file contains no audio, you can use this command to combine them into one video file.
Please do note that the container (file extension) must accept the video and audio codec. To ensure compatibility, we recommend using MP4 or MKV as the container.
Let's suppose our video file name is INPUT_FILE.mp4 and the audio file name is AUDIO.wav. If you want to combine them AND re-encode the audio to AAC format, you can use this command :
ffmpeg -i INPUT_FILE.mp4 -i AUDIO.wav -c:v copy -c:a aac OUTPUT_FILE.mp4
-i INPUT_FILE.mp4
specify INPUT_FILE.mp4
as an input source
Similarly, -i AUDIO.wav
tells FFmpeg to take AUDIO.wav
as an input source.
-c:v copy
-codec:v copy
which means copy the video stream from the source files to the destination file.
c:a aac
means select all the audio stream from source files, then encode it with AAC encoder.
In case you don't want any audio conversion, just drop the aac
part in the command and replace it with copy
, so it would look like this
ffmpeg -i INPUT_FILE.mp4 -i AUDIO.aac -c:v copy -c:a copy OUTPUT_FILE.mp4
Extract video from file and combine with another audio file using FFmpeg
If your video file already contains one or more audio stream and you want to replace the default audio with another (external) audio file, you need to use FFmpeg's -map
option.
The -map
option is used to choose which streams from the input/inputs to be included in the output/outputs. The -map
option can also be used to exclude specific streams with negative mapping.
Let's suppose we have a video file with audio named video.mp4
and an audio file named audio.aac
encoded with the AAC codec. The example will replace the audio in video.mp4
with audio.aac
and output OUTPUT_FILE.mp4
.
ffmpeg -i INPUT_FILE.mp4 -i AUDIO.aac -c:v copy -c:a copy -map 0:v:0 -map 1:a:0 OUTPUT_FILE.mp4
-map 0:v:0
means that select the first input file (INPUT_FILE.mp4), then select the first (0) video stream. The first number (0) is the index of the first input file, the latter is the index number of the video stream.
map 1:a:0
, similarly, means select from the second file (index number 1) the first audio stream (index number 0) to include in the output file.
Replace audio in files with another audio stream using FFmpeg
With the -map
option, you are able to select any stream, no matter it's video, audio, subtitle or metadata, to include in the output file.
Just like replacing audio stream with a separate audio file, you can leverage -map
option to simultaneously extract audio stream in another video file and combine with the video stream to make another file. A command line example would look like this :
ffmpeg -i INPUT_FILE1.mp4 -i INPUT_FILE2.mp4 -c:v copy -c:a copy -map 0:v:0 -map 1:a:0 OUTPUT_FILE.mp4
map 0:v:0
means that select the first input file (INPUT_FILE.mp4, index number 0), then select the first (0) video stream. The first number (0) is the index of the first input file, the latter is the index number of the video stream.map 1:a:0
, similarly, means select from the second input file (INPUT_FILE2.mp4, index number 1) the first audio stream (index number 0) to include in the output file.
To re-encode the audio or video stream with a different codec, replace copy
with the name of an audio encoder, such as aac
or libvorbis
.
Combine video with multiple audio using FFmpeg
If you read the examples above, you will now have a glimpse of how -map
works.
You can put literally unlimited number of input files and leverage -map
to select separate streams from those files and include them in the output file.
Typically, a video file contains only one video stream. On the other hand, it can contains multiple audio stream as well as subtitles, metadatas.
Below is an example where we take the video stream from INPUT_FILE1.mp4
and pair it with the audio streams from INPUT_FILE2.mp4
and AUDIO2.aac
to make a new video file with multiple audio. You can clearly see how we select each input file (index 0,1,2) and decompose them with -map
.
ffmpeg -i INPUT_FILE1.mp4 -i INPUT_FILE2.mp4 -i AUDIO2.aac -c:v copy -c:a copy -map 0:v:0 -map 1:a:0 -map 2:a:0 OUTPUT_FILE.mp4
Replace audio in video file using FFmpeg
You can use the command in the third example to replace the default audio stream in a video file using FFmpeg. The process basically is selecting video and audio streams from input files and pair them together.
We will recite the command so you don't have to scroll up.
ffmpeg -i INPUT_FILE1.mp4 -i INPUT_FILE2.mp4 -c:v copy -c:a copy -map 0:v:0 -map 1:a:0 OUTPUT_FILE.mp4
Final Thoughts
Feel free to explore more about this amazing tool. Grab a couple of demo media files and practice with them to master FFmpeg.