This is an old revision of the document!
ffmpeg is a swiss army knife for everything audio/video. It can do practically every task under the sun, and in fact powers most major dedicated “video players” (VLC, MPC-HC, built-in players in Chrome and Firefox…)1)
If you're on Windows, it's technically possible to install ffmpeg
and use it directly 2), but since the windows Command Prompt sucks ass comfort-wise and scripting-wise, it's recommended to just install Ubuntu as part of the Windows Subsystem for Linux, and then apt-get install ffmpeg
.
If you're on Linux, you know what to do
(All commands are expected to be ran in bash
or a similar Linux shell.)
fffmpeg
is pretty clever, it can correctly guess the codecs and reasonable default settings by the file extension, so all of the following will work as expected (and retain metadata3)!):
ffmpeg -i video.avi video.mp4 ffmpeg -i video.mp4 video_sound_only.wav ffmpeg -i video_sound_only.wav video_sound_only.mp3 ffmpeg -i song.flac song.mp3
“Reasonable” might not be what you want though, especially in the case of mp3, where the default bitrate is V4 (!), i.e. 140-185 kbps.
If you want, for example, V0, use the -q:a
4) option, like so:
ffmpeg -i song.flac -q:a 0 song.mp3
More info at: https://trac.ffmpeg.org/wiki/Encode/MP3
Since container/format ≠ codec, you might want to select the codec manually.
While it can reasonably assumed that mp4
≅ h264
, avi
is a bit more complex. You can list all the supported codecs with ffmpeg -codecs
5), but since there's several hundreds, you better have an idea of what you want to do in the first place.
For example, if you want an .avi
with xvid codec, you just do:
ffmpeg -i original.mp4 -c:v libxvid output.avi
This StackOverflow post explains everything: https://stackoverflow.com/a/20587693/3833159
ffmpeg
needs a list of images in a text file in a specific format in order to convert them to a video. There's a couple ways to do this:
ls *.jpg | xargs -I xyz echo "file 'xyz'" > list.txt
for f in *.jpg; do echo "file '$f'" >> list.txt; done
It's up to preference, all end up with a list of all JPGs in current directory, in list.txt
.
ffmpeg -f concat -r 30 -i list.txt out.mp4
-f concat
tells ffmpeg
to handle list.txt
as a list.
-r 30
specifies resulting FPS (30 FPS)
out.mp4
is output file - autodetected as h264-encoded. (out.avi
, out.gif
, etc. also work - refer to ffmpeg manual)
ffmpeg -i FILE image%05d.png
Where FILE
is the video file, and image%05d.png
is the format string for image filenames; this will create image00001.png
, image00002.png
, image00123.png
, etc. (%05d
means pad with 5
zeroes; %010d
for padding with 10
zeroes…)
ffmpeg
can also smoothly handle streams, so basic stream capture is pretty trivial, provided you grabbed the playlist/HLS url from somewhere6):
ffmpeg -i "https://example.com/playlist.m3u8" my_stream.mp4
-vframes 1
is the option that tells ffmpeg
to just capture one (i.e. the first) frame of the video - in the case of streams, this means the latest one anyway.
ffmpeg -i "https://example.com/playlist.m3u8" -vframes 1 capture.jpg
h264 also has “profiles”, basically sets of features - and it turns out this can make the difference between a file working and not working on some crappy embedded media players, like TVs or pico projectors.
The-profile:v
option limits the output to a specific H.264 profile. Some devices (mostly very old or obsolete) only support the more limited Constrained Baseline or Main profiles. You can set these profiles with-profile:v baseline
or-profile:v main
.
And apparently, some players are also sensitive to the pixel format, refer to https://trac.ffmpeg.org/wiki/Encode/H.264#Encodingfordumbplayers
No silver bullet, you'll just have to try different things for different devices. A database of crappy players and appropriate ffmpeg
settings would be great.
ffmpeg -formats