Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tkt028/e9d662a39622cae302c503b2ef17240e to your computer and use it in GitHub Desktop.
Save tkt028/e9d662a39622cae302c503b2ef17240e to your computer and use it in GitHub Desktop.
Some FFMpeg commands I need to remember for converting footage for video editing. http://bit.ly/vidsnippets

Quick commands for encoding video-only source footage

All commands need to start with the path to your copy of ffmpeg. E.g. on windows d:\ffmpegfolder\ffmpeg.exe [command below]

Also, be sure to change input.mkv and outputFile as needed for your file.

Lossless h264

-i input.mkv -pix_fmt yuv420p -c:v libx264 -crf 0 -g 1 -an outputFile.mp4

Lossy good-enough h264

-i input.mkv -pix_fmt yuv420p -c:v libx264 -crf 14 -g 1 -preset faster -an outputFile.mp4

Prores HQ

-i input.mkv -pix_fmt yuv422p10le -c:v prores_ks -profile:v 3 -vendor ap10 -an outputFile.mov

Prores Ultra (for VFX and keeping Alpha Channel)

-i input.mkv -pix_fmt yuv444p10  -c:v prores_ks -profile:v 4 -vendor ap10 -an outputFile.mov

DNxHD 1080p 23.976 or 24fps

If framesize matches

-i input.mkv -pix_fmt yuv422p -c:v dnxhd -b:v 175M -an outputFile.mov

else

-i input.mkv -pix_fmt yuv422p -filter:v "scale=(sar*iw)*min(1920/(sar*iw)\,1080/ih):ih*min(1920/(sar*iw)\,1080/ih), pad=1920:1080:(1920-(sar*iw)*min(1920/(sar*iw)\,1080/ih))/2:(1080-ih*min(1920/(sar*iw)\,1080/ih))/2" -c:v dnxhd -b:v 175M -an outputFile.mov

DNxHD 720p 23.976 or 24fps

If framesize matches

-i input.mkv -pix_fmt yuv422p -c:v dnxhd -b:v 90M -an outputFile.mov

else

-i input.mkv -pix_fmt yuv422p -filter:v "scale=(sar*iw)*min(1280/(sar*iw)\,720/ih):ih*min(1280/(sar*iw)\,720/ih), pad=1280:720:(1280-(sar*iw)*min(1280/(sar*iw)\,720/ih))/2:(720-ih*min(1280/(sar*iw)\,720/ih))/2" -c:v dnxhd -b:v 90M -an outputFile.mov

UTVideo

-i input.mkv -c:v utvideo -an outputFile.avi

MJpeg

-i input.mkv -c:v mjpeg -q:v 1 -an outputFile.avi

FFMpeg Clipping Commands

Convert only a part of the file

--ss is the start time and -t is the duration of the clip. Alternatively you can use -to to set the end timestamp of the clip.

Quick seeking (inaccurate, snaps to nearest keyframe):

--ss 00:01:23.123 -i input.mkv -t 20 [... remaining commands]

or

--ss 00:01:23.123 -i input.mkv -to 00:01:43.123 [... remaining commands]

Accurate seeking

-i input.mkv --ss 00:01:23.123 -t 20 [... remaining commands]

or

-i input.mkv --ss 00:01:23.123 -to 00:01:43.123 [... remaining commands]

Split file based on its keyframes

-i input.mp4 -f segment -c:v copy -c:a copy  -reset_timestamps 1 -map 0 outputFile%d.mp4

Split file into time-based segments

Example below splits into 5 minute chunks

-i input.mp4 -f segment -segment_time 0:05:00 -c:v copy -c:a copy -reset_timestamps 1 -map 0 outputFile%d.mp4

Detailed Source Conversion Guide

Breakdown of an FFMpeg Command

ffmpeg -i inputfile1.mkv [Source Settings] [Video Settings] [Audio Settings] [Container Settings] output.ext

Source Settings

-pix_fmt - forces the colorspace you wish to work in

Not usually required - ffmpeg will often choose the correct option for the codec(s) involed but some encoders can support multiple colorspaces so you sometimes need to be specific. Here are some common ones:

  • -pix_fmt yuv420p - same as DVD and BLu-ray footage. Common in most compressed streaming media. Use this for encoding for the web
  • -pix_fmt yuv422p - More chroma samples, used for common intermediate and editing codecs
  • -pix_fmt yuv444p - 'Lossless' colorspace used for high quality editing codec profiles when using raw footage.
  • -pix_fmt yuv444p10 - as above but using 10bits for the luma channel instead of 8. Better for VFX work - smoother gradients etc.

Resizing source video before encoding

Before scaling, set the rsize algorithm: -sws_flags spline or -sws_flags lanczos will do.

Then, resize either with the scale video filter: -filter:v scale

Examples:

Scale to a particular width, auto-calculate the appropriate height:

-vf scale=1280:-1

Scale to a particular height, auto-calculate the appropriate width:

-vf scale=-1:1080

Scale to fit into a 1920x1080 frame and add borders if needed (useful for DXnHD etc):

-filter:v "scale=(sar*iw)*min(1920/(sar*iw)\,1080/ih):ih*min(1920/(sar*iw)\,1080/ih), pad=1920:1080:(1920-(sar*iw)*min(1920/(sar*iw)\,1080/ih))/2:(1080-ih*min(1920/(sar*iw)\,1080/ih))/2"

Generic pattern, for any size - just replace XXXX with the width and YYYY with the Y

-filter:v "scale=(sar*iw)*min(XXXX/(sar*iw)\,YYYY/ih):ih*min(XXXX/(sar*iw)\,YYYY/ih), pad=XXXX:YYYY:(XXXX-(sar*iw)*min(XXXX/(sar*iw)\,YYYY/ih))/2:(YYYY-ih*min(XXXX/(sar*iw)\,YYYY/ih))/2" 

Video Settings

Choosing a video codec in ffmpeg generally uses the format -c:v followed by the codec

Here is a summary of the most important codecs for conversion:

  • libx264 - The open-source encoder for h264 encoding (mpeg4), usually for web distribution but also can be set as a high quality lossy intermediate codec for editing on some systems.
  • prores and prores_ks - ffmpeg's implementation of Apple's ProRes codec. Fairly industry standard for Final Cut usage - works in all pro editing systems. Main downside is the ffmpeg implementation is slow as an encoder so processing source may take longer than other options.
  • dxnhd - Avid's editing codec. Just as well supported as ProRes and encodes faster with ffmpeg, which is a plus. However, this is profile-based so will only allow certain resolution, fps and colorspace combinations. If you source isn't exactly 720p or 1080p or what have you you will need to add borders to be able to convert using this codec.
  • utvideo - Lossless codec, excellent quality but only really supported by Windows version of video editors. Can be sometimes slow to read making quick editing trickier but the compressed size is fairly similar to high quality lossy codecs!
  • mjpeg - low quality traditional format that reads and writes really fast so good as a proxy source for video editors that allow low-quality proxies to be used.

h264

The main aspects here are -crf which controlls quality (0 being lossless and 22 being the default). -g controls GOP length so setting this to 1 makes every frame a keyframe which is ideal for editing.

Lossless h264

-c:v libx264 -crf 0 -g 1

Lossy encoding with every frame a keyframe

-c:v libx264 -crf 16 -g 1

or, if you don't care about the file size that much and would rather it encode quicker:

-c:v libx264 -crf 14 -preset faster -g 1

16 is medium quality but lower numbers would be very good quality. The lower the number, the larger the file until you reach lossless.

ProRes

Note the -vendor ap10 part below is only needed if working with Final Cut, but it does no harm otherwise.

High Quality 4444 - usually overkill unless doing VFX work

-pix_fmt yuv444p10  -c:v prores_ks -profile:v 4 -vendor ap10

High Quality 422 (84 Mbit/s) - use this one

-c:v prores_ks -profile:v 3 -vendor ap10

Standard Quality 422 - or this if you are low on space and need something 'good enough'

-c:v prores_ks -profile:v 2 -vendor ap10

Proxy Quality - For proxy files only, looks crappy but edits fast and is small

-c:v prores_ks -profile:v 0 -vendor ap10

DNxHD

First look up the bitrate you need to set on this table based on the framesize, fps and desired quality level. For each there is a standard and a high quality version. The high quality version is preferred:

Project Format Resolution Frame Size Bits FPS
1080i / 59.94 DNxHD 220 1920 x 1080 8 29.97 220M
1080i / 59.94 DNxHD 145 1920 x 1080 8 29.97 145M
1080i / 50 DNxHD 185 1920 x 1080 8 25 185M
1080i / 50 DNxHD 120 1920 x 1080 8 25 120M
1080p / 25 DNxHD 185 1920 x 1080 8 25 185M
1080p / 25 DNxHD 120 1920 x 1080 8 25 120M
1080p / 25 DNxHD 36 1920 x 1080 8 25 36M
1080p / 24 DNxHD 175 1920 x 1080 8 24 175M
1080p / 24 DNxHD 115 1920 x 1080 8 24 115M
1080p / 24 DNxHD 36 1920 x 1080 8 24 36M
1080p / 23.976 DNxHD 175 1920 x 1080 8 23.976 175M
1080p / 23.976 DNxHD 115 1920 x 1080 8 23.976 115M
1080p / 23.976 DNxHD 36 1920 x 1080 8 23.976 36M
1080p / 29.7 DNxHD 45 1920 x 1080 8 29.97 45M
720p / 59.94 DNxHD 220 1280x720 8 59.94 220M
720p / 59.94 DNxHD 145 1280x720 8 59.94 145M
720p / 50 DNxHD 175 1280x720 8 50 175M
720p / 50 DNxHD 115 1280x720 8 50 115M
720p / 23.976 DNxHD 90 1280x720 8 23.976 90M
720p / 23.976 DNxHD 60 1280x720 8 23.976 60M

Then add the bitrate to this command (using 1080p 24fps as an example):

-c:v dnxhd -b:v 175M

UTVideo

UTVideo, being lossless, can generally handle any input format, so the command is simply:

-c:v utvideo

MJpeg

Where qscale is the quantizer to use, 1 being the highest. Still not great quality. For shitty proxy files you can drop qscale to 4 or lower to make small but ugly and fast files.

-c:v mjpeg -q:v 1

Audio Settings

Stripping Audio

Simply add this shorthand

-an

AAC (for mp4 video files)

Stereo Bitrates: 128k, 160k, 192k, 224k, 320k

224 is what iTunes uses if that gives any idea

-c:a aac -b 224k

Container Settings and Extension

You can determine a container format either by specifying it on the commandline with -f otherwise ffmpeg will use the filename to determine the container:

Encoding h264

-f mp4 or simply outputFilname.mp4

Encoding ProRes

-f mov or outputFilename.mov

Encoding DXnHD

-f mxf or outputFilename.mxf or -f mov or outputFilename.mov

Encoding UTVideo or MJpeg

-f avi or outputFilename.avi

FFMpeg Encoding for online sharing

Making h264 mp4 files

Basic no-brainer encoding options

The -crf 18 option here is very high quality. 20 would be lower quality. The default is actually 23. You can also use fractional amounts e.g. 18.5

ffmpeg -i inputfile.avi -pix_fmp yuv420p -c:v libx264 -crf 18 -preset slower -c:a aac -b:a 224k -movflags +faststart output.mp4

This is where I usually start, though for 1080p encodes this often creates a file that's way too big so I then reduce the number and find the sweet spot where it's not too large and not too bad-looking.

High quality but with some bitrate limits

It's possible you may have a target device (like, I dunno, some streaming box) that chokes on high birtates you can set rate limits in addition to keeping your -crf value.

ffmpeg -i inputfile.avi -pix_fmp yuv420p -c:v libx264 -crf 18 -maxrate 7M -bufsize 10M -preset slower -c:a aac -b:a 224k -movflags +faststart output.mp4

Adding Metadata (Title, Author, etc)

ffmpeg [settings above except output.mp4] -metadata title="Video Title" -metadata author="Author Name" output.mp4

Webm (using VP9)

ffmpeg -i inputfile.avi -c:v libvpx-vp9 -crf 30 -b:v 0 -c:a libopus -b:a 224k output.webm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment