Cool FFmpeg Tips and Tricks
UPDATE: This article was a precursor to my book FFmpeg Quick Hacks. It was originally published in the Open Source For You magazine in 2016. It has been followed up with another article titled More Cool FFmpeg Tricks.
Sometimes, fans of free software have to jump through many hoops to get at the good stuff. FFMPEG is one such example. Like the VLC media player, FFMPEG can handle almost any audio or video format you throw at it. Yet, most people have not even heard about it. Once you have compiled and installed it, it will prove to be just as useful.
What is FFMPEG?
FFMPEG bills itself as a complete solution to record, convert and stream audio and video. The FFMPEG project provides four simple apps -
ffserver. Many audio/video applications use it internally. If not directly, then via
mencoder, ffmpeg is used by lots of media players, browser plugins, audio/video editors and other multimedia software. This article will focus on the
ffmpeg binary executable, which can be used to convert audio/video files from one format to another.
Because many audio/video codecs are encumbered by "software patents", GNU/Linux distributions have had to omit FFMPEG or bundle a less-capable FFMPEG version of it. Even if it were compiled with support for non-free encoders/decoders, there was a risk of a license violation. (Recently, FFMPEG released its Version 3 with a totally native AAC encoder but more work remains.) The solution was for users to do their own compilation. The FFMPEG Wiki site has compilation steps for several distributions. The compilation process does take a while to finish but it will eventually create the four binary executable files. Copy them to your
/usr/bin directory. If you don't want to disturb the FFMPEG installation that came with your Linux distribution, then you can access the binaries by providing their full path from a different directory.
FFMPEG Compilation for AMV Support
While this FFMPEG installation covers most formats, I found that it would not encode to the audio codec of the undocumented AMV format. AMV is used by many cheap Chinese-made media players. These tiny devices are primarily FM and MP3 players with the video option added as a please-ignore-it extra. The video resolution is a luxurious 160x120! I bought one of these players without realizing all this. Fortunately, there was a Google Code project called amv-codec-tools that provided support for AMV using an old version of FFMPEG. If you need to work with this format, then build a custom FFMPEG binary, specially made for AMV, using the following commands. Copy the
amv-ffmpeg that these steps generate to the
git clone https://github.com/vsubhash/amv-ffmpeg.git cd amv-ffmpeg ./configure make
- List supported codecs: The
-formatsoptions will list you all the codecs that are supported. Each codec is referred by a special name. This name will have to be used in your FFMPEG commands when a particular codec is required to be used. The
-encodersoption will list all codecs which FFMPEG can convert to, i.e., the output formats. The
-decodersoption will list all codecs which FFMPEG can convert from, i.e., the input formats.
- Check the codecs used in a media file: Use the
-ioption to specify the input file. This will display the streams available in the file and the settings with which each stream was encoded. Please remember that file extensions can be misleading. MP4, AVI or WMV files are merely containers for multiple content streams. There is no guarantee that a WMV file will have a stream in Windows Media Audio format. It could very well have MPEG-3 audio.
ffmpeg -i duffy.ogv
- Convert a file from one format to another: Linux programs typically create OGV/OGG files. However, very few consumer electronic devices support these formats. You can use FFMPEG to convert to a device-compatible format. FFMPEG can guess the output format based on the file extension you have used.
ffmpeg -i duffy.ogv duffy.mp4
- Convert to AMV format: First, convert the video to WMV format. Then, use the AMV FFMPEG binary built earlier to convert the WMV file to AMV.
ffmpeg -i duffy.ogv duffy.wmv amv-ffmpeg -i duffy.wmv -f amv \ -s 160x120 -r 16 -ac 1 -ar 22050 -qmin 3 -qmax 3 \ tank.amv
- Explicitly specify conversion settings: When you let FFMPEG guess the output format and do the conversion, it will choose its own default codec settings. This may not always be what you want. Sometimes, you may have very different requirements. In this example, the container format is MP4. The video stream is set to MPEG4 codec, 30 frames per second and 500K bitrate. The audio stream is set to LAME MP3 codec, 44.1 kHz sampling rate and 96K bitrate. Here, mpeg4 and libmp3lame are the codec names.
ffmpeg -i duffy.ogv \ -vcodec mpeg4 -s 352x480 -r 30 -b:v 500k \ -acodec libmp3lame -ar 44100 -b:a 96k \ duffy.mp4
- Extract audio from a video file: You can use the
-vnoption to disable the video stream and save the remaining audio stream to an MP3 file.
ffmpeg -i duffy.ogv \ -vn \ -acodec libmp3lame -ar 44100 -b:a 96k \ duffy.mp3
- Listen to YouTube and other online video sites:: I use the
youtube-dlprogram to "listen" to YouTube videos. (Many TV programmes, such as the Keiser Report, upload their shows to YouTube so that viewers who miss the broadcast schedule can catch up later. With an audio file, you can continue to work while you listen. To install
youtube-dl, use the command
sudo apt-get install youtube-dl.) The first youtube-dl command here uses the
-Foption (uppercase F) to list all formats in which a particular video is available. The second youtube-dl command uses the
# list the available formats youtube-dl -F \ https://www.youtube.com/watch?v=_juHR5uodjU # download the video in M4A audio format youtube-dl -f 140 \ https://www.youtube.com/watch?v=_juHR5uodjU ffmpeg -i *.m4a play.ogg audacious play.ogg & # return to prompt
- Fix errors:
recordMyDesktopis a great screen-capturing program but it sometimes creates video files with incorrect keyframes. These errors can make conversions or editing difficult or impossible. FFMPEG can fix these errors if you ask it simply
copythe audio and video streams to a new file.
ffmpeg -i duffy.ogv \ -c:v copy \ -c:a copy \ duffy-fixed.ogv
- Rotate video: Sometimes, the videos that people take from a mobile phone are 90 or 180 degrees rotated. You can fix it by specifying a transpose filter. The transpose value is in multiples of 90.
ffmpeg -i tank.mp4 \ -vf "transpose=1" \ tank-rotated.mp4
- Cut a part of a video: To extract a part of a video, you need to specify the start time using
-ssoption and the duration using
-toption. (Use hh:mm:ss format.) If you don't want the file to be re-encoded, then "copy" the streams.
# start from 1 min 12 sec and go for 2 min 50 sec ffmpeg -i tank.mp4 \ -ss 1:12 \ -t 2:50 \ tank-cut-recoded.mp4 ffmpeg -i tank.mp4 \ -c:v copy \ -c:a copy \ -ss 1:12 \ -t 2:50 \ tank-cut.mp4
- Append a video to another video: FFMPEG offers several methods for merging. The simplest method is to use input videos that use the same codec and preferably the same resolution and frame rate. The following commands first create a text file containing the pathnames of the input files. The FFMPEG commands merges the files specified in the text file.
echo "file 'tank-cut-recoded.mp4'" > file-list.txt echo "file 'tank.mp4'" >> file-list.txt ffmpeg -f concat -i file-list.txt -c copy tank-mix.mp4
- Perform a 2-pass encoding: With a two-pass encoding, you can get a higher quality video file with lesser artifacts. The data obtained from the first pass is recorded in a log file and then used in the second pass for a more efficient conversion.
ffmpeg -y -i duffy.ogv -f mp4 \ -pass 1 -passlogfile duffy-log-file \ -vcodec mpeg4 -s 320x240 -r 25 -b:v 512K \ -an /dev/null ffmpeg -i duffy.ogv -f mp4 \ -pass 2 -passlogfile duffy-log-file \ -vcodec mpeg4 -s 320x240 -r 25 -b:v 512K \ -acodec libmp3lame -ar 44100 -b:a 96K \ duffy.mp4
Klaatu (of the Gnu World Order podcast) once provided a great video-editing tip. First downsample (lower bitrate with same resolution) the input video and create your video edits. When the video editing project file is finalized, replace the downsampled video file with the original bigger video file. This way, the video editing process and your test renderings are faster. The final rendering can take hours to finish but you do not waste much time waiting before that. You can use FFMPEG to downsample the video.
If you have Caja Actions or Nautilus Actions Configuration installed, then you can simply right-click media files to convert them. You will have to first write some BASH scripts that will automate the FFMPEG conversions. Nautilus Actions Configuration (Gnome 2/3) is available in most software repositories. My March 2016 article has steps for compiling Caja Actions (Mate).