Skip to content

Instantly share code, notes, and snippets.

@Orangestar12
Last active August 9, 2024 01:55
Show Gist options
  • Save Orangestar12/a6593a093d8728a268a799ff9003d815 to your computer and use it in GitHub Desktop.
Save Orangestar12/a6593a093d8728a268a799ff9003d815 to your computer and use it in GitHub Desktop.
quick ffmpeg cheat sheet

If this is too much for you, check out ffmpeg.lav.io

These are a few quick easy ffmpeg command lines that can be used to make oft-used video formats. I use them a lot so I wrote them down in a txt file but I converted it to markdown to upload here and access on all my pcs.

Feel free to use 'em. I've gathered them through superuser posts, wiki trawls, and personal experience.


General notes

  • Add -movflags faststart to make mp4 files have their headers at the beginning of the file, allowing them to be streamed (i.e. played even if only part of the file is downloaded). EDIT 3/26/2022: I actually haven't used this flag in a long time. I think ffmpeg does it by default now.
  • The MP4 container supports MP3 files, so if libfdk_aac isnt available (it's the only good AAC enc) use libmp3lame.
    • Update: unless you're uploading them to Twitter. Twitter doesn't like MP4 files with MP3 audio. Fall back to AAC and provide a higher quality alternative somewhere else.
    • libfdk_aac is actually extremely uncommon in ffmpeg builds because >nonfree and a lot of sites don't like libmp3lame audio in MP4 containers. If you MUST use MP4 presumably the quality loss you'd get from using aac is acceptable. Otherwise, use MP3 if you need quality and need an MP4. Otherwise, try another container. (I keep my source videos in Matroska, personally.)
  • For MP4 encoders, use -preset X to use MP4 enc presets, like slow or superfast. (veryfast or fast is ok).
    • h264_nvenc has its own presets, profiles, etc, and they're different for each GPU. You can check these with ffmpeg -h encoder=h264_nvenc
  • c:v refers to the video codec used (codec: video). Likewise, c:a is audio. If you're using -map or something, this can be extended (-c:a:0: codec: audio: stream 0; -map 0:a:0: file 0: audio: stream 0)
  • If you leave out any option, ffmpeg will either use a default or try to guess what you want based on the other options. I.e. if the container is mp4 it'll try encoding your audio with aac unless you specify.
  • To get rid of a stream, do -an or -vn, which will output a file with no audio or video stream respectively. If your audio file is silent you will save a LOT of filesize doing -an.
  • To hide the gigantic banner that shows at the start of every ffmpeg session, pass -hide_banner.

TOC


Generic conversions

To MP4

ffmpeg -i file.mkv -c:v libx264 -c:a aac output.mp4

If you have an Nvidia card and are running proprietary Nvidia drivers, you can use NVENC to speed up your encoding by an absolute crapton. (This is the same tech that runs "shadowplay" on Windows).

ffmpeg -i file.mkv -c:v h264_nvenc -c:a aac output.mp4

Intel users can use "QuickSync" for hardware acceleration.

ffmpeg -hwaccel qsv -c:v h264_qsv -i file.mkv -c:a aac output.mp4

For AMD... it's complicated. Check the wiki.

To WebM

The default WebM audio encoder is libopus but you can also use libvorbis, which I personally prefer since it lets you easily stream copy them to ogg files.

There are actually two WebM video encoders: The newer, faster, smaller VP9 codec and the older but more compatible VP8 codec.

VP9

ffmpeg -i file.mkv -c:v libvpx-vp9 -crf 28 -c:a libvorbis output.webm

Turn the -crf parameter down to increase the quality at the expense of filesize.

VP9 is more useful with a two-pass encoding, but holy crap is it confusing and time-consuming.

VP8

ffmpeg -i file.mkv -c:v libvpx -qmin 4 -qmax 24 -c:a libvorbis output.webm

As far as I'm aware the only time you'd need to use VP8 in the current year is uploading a WebM to 4chan.

-qmin and -qmax set the file size.

  • Turn -qmax down if you need more quality.
  • Turn -qmin up if you need a smaller file.
  • The range is 0-63.

You should always specify at least a -qmax because the default -qmax is 63, which means that if you don't specify it ffmpeg will literally try to compress the video as much as possible which will look like garbage.

If you want more info, check the section on video quality.

Discard video, save audio to MP3

ffmpeg -i file.mkv -vn -c:a libmp3lame -qscale:a # output.mp3

Where # is the audio quality, from 0-9.

Directory of images to video w/ audio:

ffmpeg -r [Framerate (24)] \
    -f image2 -s [Image size (1280x720)] -i image%04d.png \
    -c:v libx264 -crf 25 -pix_fmt yuv420p \
    -i audio.wav -c:a aac output.mp4

%04d represents how many 0s trail the end of your filenames before the extension. i.e. filenames that look like houston000004.png would be houston%05d.png

-crf controls quality. if you're having trouble getting a good picture try fiddling with it.

If things are coming out really wrong, double check to make sure your pixel format is yuv420p, if not you might double check the proper pix_fmt command for your images, or just batch convert them with ImageMagick or smth idk

If your directory of images doesn't start at 0, you need to include -start_number [index] so image2 knows which image to start at.

The above command, without fields marked in braces

ffmpeg -r 24 -f image2 -s 1280x720 -i image%04d.png \
    -c:v libx264 -crf 25 -pix_fmt yuv420p \
    -i audio.wav -c:a aac output.mp4

Merge one video file and one audio file together

ffmpeg -i video.mp4 -i audio.mp3 -c:v libx264 -c:a libmp3lame out.mp4

Though, that's a little overkill. If you're using an mp4 and an mp3 you can just use the copy codec, which is faster, since mp4 container supports mp3.

ffmpeg -i video.mp4 -i audio.mp4 -c:v copy -c:a copy output.mp4

Containers like MKV can handle a lot of codecs, so copy is pretty versatile there.

If your video file already has audio

Use -map.

ffmpeg -i video.mp4 -i audio.mp3 \
    -c:v libx264 -c:a libmp3lame \
    -map 0:v:0 -map 1:a:0 out.mp4

One image to the entire length of audio

ffmpeg -loop 1 -i image.png -i audio.wav \
    -c:a aac -c:v libx264 -tune stillimage \
    -shortest -pix_fmt yuv420p out.mp4

-shortest makes sure it stops when the audio stream ends and doesn't just encode until it runs out of space or crashes.

You may need to do -s, -pix_fmt, etc depending on your image.


Other goofy stuff

Add album art to MP3 file

ffmpeg -i file.mp3 -i artwork.png -map 0:0 -map 1:0 \
    -c copy -id3v2_version 3 -metadata:s:v title="Album cover" \
    -metadata:s:v comment="Cover (Front)" out.mp3

There's a lot more metadata that can be added to an mp3 file but they're all obscure and weird and Cover is basically the only one you'll ever see used besides Lyrics (though I think lyrics are on mp4, not mp3)

Convert from X to Y and keep the metadata

ffmpeg -i "whatever.ogg" -ab 320k -map_metadata 0:s:0 "whatever.mp3"

-map_metadata is a nice little command that does just what it says: maps the metadata from one stream to another. In this case we're maping the metadata from output stream 0 into input stream 0. It works like the normal -map command.


More on video quality

This was touched on earlier but here's a full bit on making the most of the video quality settings.

Constant bitrate with -b

The easiest way to mess with quality is to specify a bitrate with -b:v or -b:a (again, video and audio respectively.) This results in a constant bitrate (CBR): I.E. the video streams exactly that many bits every second. If your video traffic is generally the same throughout the entire video or you don't want to exceed some encoding limit for your hardware, CBR will work fine for you.

ffmpeg -i some_high_quality_source_video_thats_like_700mb.mkv -b:v 5000k -c:v libx264 a_much_smaller_mp4_file.mp4

Pros

  • Predictable filesize because you know exactly how many bits are going into the video (5Mbps + 10sec video == 50Mb video)
  • High-traffic segments get compressed more
  • As long as your encoder can handle the input bitrate, it will never choke
  • Easier command to remember

Cons

  • Low-traffic sections are not as compressed
  • High-traffic sections can look like a blurry or blocky mess (the "Twitch stream FIREBLU" problem)
  • During a real-time stream or recording, if your encoder can't compress a frame fast enough, the buffer will keep doubling in size until it can start compressing two frames in less time than the input frames are being recorded.

Variable bitrate

Ok here's the hard one. First, why you should use VBR:

If you have a section that has a lot of low traffic and a section that has a lot of high traffic, then to get the most optimal picture and filesize you'd want the video file to send less data during the low traffic areas. For example, a video of a computer mouse moving around on a desktop would only contain the data for changing those pixels that the mouse is moving past, but footage from a video game being launched from that desktop would need much more data being streamed. VBR is ideal for things like screencasts and other VOD recordings where you want everything to be crisp and clear.

The issue is that there's no standard for what parameters each encoder uses, so you'll just have to look around online to figure out how to use it. For example, libx264 uses a single -crf flag to determine the quality, which is a number between 0 and 51 (lower number == higher quality)...

ffmpeg -i some_high_quality_source_video_thats_like_700mb.mkv -c:v libx264 -preset slow -crf 20 a_compressed_boi.mp4

...but h264_nvenc uses the -qmin and -qmax flags to tell the encoder the minimum and maximum compression you want applied to the file.

ffmpeg -i some_high_quality_source_video_thats_like_700mb.mkv -c:v h264_nvenc -qmin 18 -qmax 24 this_is_probably_fine.mp4

slhck has a great page here detailing how to turn on VBR for the most popular software encoders.

Pros

  • High quality footage on every frame.
  • Lower filesize on average than CBR videos
  • Standard on the MP4 container, so you can expect high support

Cons

  • Filesize is mostly unpredictable. Treat it like a black box. If your encoding settings are too good for your filesize budget you won't know until you've already waited for it to encode the whole thing (or at least up to the -fs parameter)
  • Higher chance to get "in over your head" and choke the encoder when encoding real-time streams. Highly recommend that you do not use VBR with real-time streams. Do a two-pass with a lossless source recording first.

Quick editing commands

If you're actually gonna do extensive video editing, you should probably use the Blender VSE or something. But if you only need to do a little bit of work on a file, these should help.

Trimming and seeking

Seek

To start encoding a file part of the way through, use -ss to seek to it.

ffmpeg -ss 00:00:15 -i input.mp4 out.mp4

You can use -sseof to seek from the end of the file

ffmpeg -sseof 00:00:15 -i input.mp4 out.mp4

Trim

To stop encoding after a set amount of time, use -t to specify a duration.

ffmpeg -t 00:00:30 -i input.mp4 out.mp4

This is similar to setting an egg timer to stop after 30 seconds.

To stop encoding after a set time in the video has passed, use -to.

ffmpeg -to 00:08:37 -i input.mp4  out.mp4

This is like scheduling a DVR to record from 6:00 PM to 6:30 PM.

Note that without -ss, -t and -to are functionally similar.

Seeking vs Reading

When -ss, -to, etc is placed before an input file, ffmpeg won't try to "read" those first X seconds of video first, which makes encoding start faster. Ffmpeg will "seek" to that position in the file first before trimming or whatever you're doing to the file. By contrast, doing ffmpeg -i input.mp4 -ss 00:00:15 out.mp4 will read the first 15 seconds, then start encoding.

If your input stream has timestamps already set right (it should) then seeking should work fine, but I have noticed some strange recording software (like OBS) will sometimes output FLV files with broken or missing timestamps, in which case you'll get a radically different time if you try to seek it. In these cases, reading should be used.

This results in a few caveats:

  • ffmpeg -ss 0:30 -to 0:35 -i ... will seek to 0:30 and record to 0:35 based on the timestamps in the file.
  • ffmpeg -ss 0:30 -i whatever.mp4 -to 0:35 will seek to 0:30, then continue the encoding operation for 35 seconds. This is because you've reset the internal "timer" to 0 by seeking, then reading the file. In this instance, -t and -to are functionally identical.
  • -t basically does the same thing regardless of where you put it.

I don't know how this is handled regarding multiple inputs yet (-ss 0:30 -i file.mp4 -ss 0:40 -i file.mp3 I think seeks to 30 seconds in the video then 40 seconds in the audio) so I'm open for comments helping me out here.

Basically, if your video file isn't malformed in some way, just make sure to put -ss, -t, and -to before the -i command.

File Size

If you want to limit your recording/encoding/transcoding to a file size instead, use -fs.

ffmpeg -i input.mp4 -fs 5M out.mp4

This would stop copying input.mp4 when the file reached 5 megabytes.

Putting them all together

You can combine these commands to get clips out of videos.

ffmpeg -i input.mp4 -t 00:02:00 -fs 1MB out.mp4

This would trim your video to 2 minutes or 1 megabyte, whichever happens first.

ffmpeg -ss 00:30:00 -i input.mp4 -to 02:00:00 out.mp4

This would make you a new version of input.mp4 that lasts 1 hour and 30 minutes, and starts 30 minutes in.

-fs is extremely useful if your hosting service has a strong limit on your filesize and/or duration, but you want to press your luck to see how good a quality video you can get under those limits.

ffmpeg -ss 00:02:52 -i input.mp4 -preset fast -fs 2M f.mp4
ffmpeg -ss 00:02:52 -i input.mp4 -preset veryfast -fs 2M vf.mp4
ffmpeg -ss 00:02:52 -i input.mp4 -preset superfast -fs 2M sf.mp4
ffmpeg -ss 00:02:52 -i input.mp4 -preset ultrafast -fs 2M uf.mp4

other cool use cases for -fs:

  • you're on a storage budget and don't want to eat all your drive space with video files
  • you're getting some kind of infinite stream (like recording your desktop) and you want to upload the file in some hosting service that doesn't care about durations (like 4chan's webms or something)
  • your input stream has sections of "heavy traffic" that make the file size more unpredictable to change by simply using an mp4 preset or lowering the time.

Scaling

Scaling is a video filter, so the syntax is -vf. This is an older syntax (ffmpeg used to use -vc for "video codec", and will still recognize what you meant if you use -vc.) so pardon the confusion with -c. Part of this is because -f is already used for "Format" and can't be used for "Filter". Enough semantics.

If you know precisely what size you want the output to be, you can just specify it.

ffmpeg -i input.mp4 -vf scale=1280:720 out.mp4

(Some OSes, like Windows, freak out if you use colons on them. If you're running one of those, do -vf "scale=1280:720" instead.)

If you want to retain the aspect ratio, use -1 to make ffmpeg calculate the proper height or width respectively.

ffmpeg -i input.mp4 -vf scale=1280:-1 out.mp4
ffmpeg -i input.mp4 -vf scale=-1:720 out.mp4

The width and height are calculated equations, and the scale filter exposes a set of constants you can use. Basically this means that you can write simple expressions using the original image's height and width (among other things) to do specific options.

Doubling or halving the size of an image using iw and ih, the "input width" and "height":

ffmpeg -i input.mp4 -vf scale=iw*2:ih*2 out.mp4
ffmpeg -i input.mp4 -vf scale=iw*.5:iw*.5 out.mp4

A list of constants and options for scale are available here

The default is a bicubic scaler. You can make the encoding faster with a lower quality scaler:

ffmpeg -i input.mp4 -vf scale=1280:-1,sws_flags="neighbor" out.mp4

or more beautiful with a higher quality one:

ffmpeg -i input.mp4 -vf scale=1280:-1,sws_flags="lanczos" out.mp4

A list of all options are available here

If you're scaling at anything that isn't the same aspect ratio, you will need to change the video's SAR and DAR. This is easy enough by adding the setsar=1 flag to your filters.

ffmpeg -i input.mp4 -vf scale=1280:-1,setsar=1 out.mp4

Some distortion can be expected since, you know, you're discarding, substituting, or duplicating pixels to achieve the effect, but it generally looks fine if you're scaling down. (Example: NVIDIA Share refuses to acknowledge one of my monitors is portrait-rotated, so it always records videos at 16:10 instead of 10:16. I can squish the width to get a 656x1050 video which has just enough clarity to be visible.)

@lancejpollard
Copy link

Nice stuff! Will definitely help immerse me into the ffmpeg CLI!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment