Skip to content

Instantly share code, notes, and snippets.

@sturmen
Last active March 16, 2024 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sturmen/03ac5d686547bf06729c30a7122578a5 to your computer and use it in GitHub Desktop.
Save sturmen/03ac5d686547bf06729c30a7122578a5 to your computer and use it in GitHub Desktop.
Useful ffmpeg Commands

Useful FFmpeg Commands

Often we use ffmpeg commands that have esoteric or unmemorizable options. This is my dumping grounds for all the ones I need to remember somewhere. I am posting this publicly in the hope that it can help other people. If anyone has specific questions/suggestions/improvements for a command, please comment. If you're seeking help creating your own ffmpeg command, I recommend using ChatGPT. It may be bad at math, but it's actually really good at coming up with CLI usages, and even writing quick scripts for their use.

Video

Lossless Transcoding For Archive (plus lossy for viewing) [REQURIES NVIDIA GPU]

Encode a video losslessly for archiving and as a deinterlaced H.264+AAC file for viewing. This was designed for taking the uncompressed (!) Quicktime MOV output of Blackmagic Media Express and shrinking it to something more manageable for archiving as well as viewing. Note the "seek start" -ss and duration -t placeholder values.

ffmpeg -hide_banner -fflags +genpts+igndts -hwaccel cuda -hwaccel_output_format cuda -ss 00:00:00.0 -t 2:00:00.0 -filter_complex "[0:v]hwupload_cuda,split=2[o1][i2];[i2]bwdif_cuda=mode=send_field:parity=auto:deint=all,scale_cuda=format=yuv420p[o2]" -i inputfile.mov -map "[o1]" -map 0:a -c:v hevc_nvenc -preset p7 -tune lossless -c:a copy -vtag hvc1 archivefile.mov -map "[o2]" -map 0:a -c:v h264_nvenc -preset slow -tune hq -rc vbr -cq 28 -b:v 0 -rc-lookahead 32 -multipass fullres -c:a libfdk_aac -b:a 256k -movflags +faststart outputfile.mp4

Lossless Decklink Capture [REQUIRES DECKLINK DEVICE]

Save losslessly input from your Decklink and monitor it while it's recording. Make sure your adjust for your Decklink device and what format it's in (it's picky) using these other commnads: DeckLink FFmpeg Reference

Note: on my Windows PC, this has the unfortunate behavior where ffplay freezes if I select any other window. That's why I switched to using Blackmagic Media Express and postprocessing with the command above.

ffmpeg -hide_banner -fflags +genpts+igndts -hide_banner -re -f decklink -rtbufsize 500M -format_code ntsc -i "Intensity Pro 4K" -c:v ffv1 -c:a copy outputfile.mov -pix_fmt yuv420p -f sdl "DeckLink Preview"

Lossy Encoding with NVENC [REQURIES NVIDIA GPU]

This is just a generic command that can be used for downscaling+encoding any video up to 8192x8192 down to tablet-sized viewing.Remove the video filter entirely if your output is already less than 2560 pixels wide. Remove :format=p010le if you don't want 10-bit, but be aware that 10-bit is still helpful for preserving quality when downscaling from higher-resolution sources because, for example, averaging two adjacent 8-bit values (such as 128 and 129) might result in a number between them (such as 128.5) which can be properly stored in 10-bit but not 8-bit.

ffmpeg.exe -hide_banner -hwaccel cuda -hwaccel_output_format cuda -i input.mov -vf "hwupload_cuda,scale_cuda=2560:-2:interp_algo=lanczos:format=p010le" -c:v hevc_nvenc -preset:v p7 -tune:v hq -rc:v vbr -cq:v 19 -b:v 0 -c:a libfdk_aac -b:a 256k -vtag hvc1 output.mp4

Split SBS Stereoscopic Video Files

This is a loop command to convert all of the stereoscopic full SBS files in the current directory into separate left and right video files, without losing quality. The intention is to take files directly captured by stereoscopic cameras like the QooCam EGO and convert them into formats suitable for editing in Davinci Resolve.

for file in *.mp4; do basename="${file%.mp4}"; mkdir -p left right; ffmpeg -hide_banner -i "$file" -vf "stereo3d=sbsl:ml,format=yuv420p10le" -c:v prores_videotoolbox -c:a pcm_s24le "left/$basename.mov" -vf "stereo3d=sbsl:mr,format=yuv420p10le" -c:v prores_videotoolbox -c:a pcm_s24le "right/$basename.mov"; done

Audio

Shrink Audiobooks to Books.app Format [REQURIES macOS]

Encoding Audiobooks in a format that can be played with the macOS/iOS/iPadOS Books app. I find most audiobooks I come across are of terrible audio quality anyway, so this converts them to mono, high-efficiency low-bitrate AAC that shouldn't harm them much, if at all. (reference on encoding HE-AACv1)

ffmpeg -hide_banner -i input.wav -ac 1 -c:a aac_at -profile:a 4 -aac_at_mode abr -b:a 64k output.m4b

Decode Audible audiobooks

This maintains what little quality the audiobook already has, and also preserves chapters and metadata. Find your activation bytes here.

ffmpeg -hide_banner -y -activation_bytes xxxxxxx -i input.aax -codec copy output.m4b

Additional Tips

  • MKV is a great format, use it when you just need to store data for later (such as archiving) because it seems to preserve just about everything. However, if you want to work with the files, like edit them in an NLE or upload them somewhere, see if you can't use use MOV instead since it's more widely compatible but nearly as flexible.

  • I compared various lossless but widely compatible video formats for archiving, and I found that FFV1 offered the best compression, but H.264 wasn't too far behind. HEVC was a distant 3rd. H.264/HEVC can be encoded losslessly with NVENC and the output is slightly larger but still comparable to x264/x265 in output file size. QuickTime can't play lossless H.264 directly because it uses the obscure "High 4:4:4 Predictive" profile, but it is readable by professional Mac applications like Final Cut Pro X and Compressor. HEVC seems to support lossless encoding using "range extensions," AKA "RExt," which is much more widely supported, including in QuickTime. FFV1 is completely unsupported by both. If you're an anti-patent purist, FFV1 is still your best bet, but if you want compatibility with any of the Apple video tools including QuickTime player, best to go with HEVC lossless (tagged with hvc1, as below). As a result: for my archiving, I'm using hevc_nvenc in lossless mode.

  • When using MOV or MP4, make sure to add -movflags +faststart. It's a post processing step that takes a few seconds but it's worth it, especially when uploading to video sharing sites because it allows them to begin their transcoding immediately.

  • In my amateur opinion, bwdif is the best deinterlacer in ffmpeg and honestly, it's good enough for most use cases. It's also great there's a CUDA implementation that you can use in a completely NVIDIA-hardware-accelerated pipeline like I do above.

  • As of 2023: when it comes to hardware-accelerated video encoding, NVENC and prores_videotoolbox are good, but for H.264/H.265/AV1 encoding where latency is not a concern, software encoding is almost always worth it over QSV/AMF/VideoToolbox.

  • prores_videotoolbox claims to support 8-bit input but actually doesn't. Use the format filter to convert it to 10-bit:

    • 10-bit 4:2:0: yuv420p10le
    • 10-bit 4:2:2: yuv422p10le
    • 10-bit 4:4:4: yuv444p10le
  • Don't forget to add -tag:v hvc1 to your H.265 encodes. (source)

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