Skip to content

Instantly share code, notes, and snippets.

@Aldaviva
Last active May 24, 2023 01:46
Show Gist options
  • Save Aldaviva/3646af3cb34e1538cbc38fd10022d4cb to your computer and use it in GitHub Desktop.
Save Aldaviva/3646af3cb34e1538cbc38fd10022d4cb to your computer and use it in GitHub Desktop.
Video encoding with FFmpeg, a great tool with the shittiest user interface you can imagine: lots of confusing, unmemorable, unintuitive, undiscoverable command-line flags

Remux without transcoding

ffmpeg -i input.ts -c copy output.mp4

x264, AAC

ffmpeg -i input.avi -c:v libx264 -preset slow -crf 23 -pix_fmt yuvj420p -c:a aac -b:a 160k output.mp4

x265, AAC

ffmpeg -i input.avi -c:v libx265 -preset slow -crf 28 -x265-params range=full -vf scale=-1:1080 -c:a aac -b:a 128k output.mp4

HEVC_NVENC, pass-through audio

ffmpeg -i input.avi -c:v hevc_nvenc -preset slow -rc vbr_hq -cq 28 -tier high -dst_range 1 -c:a copy output.mp4
REM Convert a Fraps capture to a Ut Video file with the trimming, cropping, and resizing you want, using a tool like TMPGEnc.
REM Export as AVI file output with UtVideo YUV420 BT.709 VCM (ULH0).
REM Specifying -pix_fmt yuvj420p will preserve the full range of lightness values, instead of washed-out blacks and whites.
ffmpeg -i input.avi -pix_fmt yuvj420p -c:v libx264 -preset medium -crf 23 -c:a aac -b:a 128k output.mp4
REM You can also use YUV422 and H.265:
ffmpeg -i input.avi -pix_fmt yuvj422p -c:v libx265 -preset fast -crf 26 -c:a aac -b:a 128k output.mp4
REM Fraps to H.265 with NVENC:
ffmpeg -i input.avi -ss 00:00:01.000 -to 00:05:00.000 -c:v hevc_nvenc -preset slow -rc vbr_hq -cq 27 -c:a aac -b:a 128k -dst_range 1 output.mp4
  1. Export from Premiere to a lossless, compressed AVI.
  2. Transcode AVI to H.264 using FFmpeg.

If you don't have the time or space to render a temporary lossless copy of your file on disk, you can use a frameserver instead.

Premiere export settings

Video

Setting Value
Format AVI
Video Codec UtVideo YUV420 BT.709 VCM
Width 1920
Height 1080
Frame Rate 29.97
Field Order Progressive
Aspect Square Pixels (1.0)

Audio

Setting Value
Sample Rate 48000 Hz
Channels Stereo
Sample Size 16 bit
@Aldaviva
Copy link
Author

When using UTVideo BT.709 YUV422, FFmpeg seems to figure out the pixel formats correctly and doesn't need to have a pix_fmt specified to get rich blacks.

@Aldaviva
Copy link
Author

@Aldaviva
Copy link
Author

QuickTime requires 4:2:0 subsampling, so remember to use -pix_fmt yuv420p or similar. It can't open 4:2:2 video.

@Aldaviva
Copy link
Author

Aldaviva commented Jan 4, 2020

@jeffward01
Copy link

Video encoding with FFmpeg, a great tool with the shittiest user interface you can imagine: lots of confusing, unmemorable, unintuitive, undiscoverable command-line flags

haha, so true! I am glad that someone finally said this! Also, without looking at the documentation, even as a somewhat savvy user, I have no idea what this means:

ffmpeg -i input.avi -pix_fmt yuvj422p -c:v libx265 -preset fast -crf 26 -c:a aac -b:a 128k output.mp4

Shame on you ffmpeg... But also as developers its sad that Ffmpeg is the only option out there.

Big thank you for the scripts! I have not tried them, i will let you know how it works for me!

For my business logic, I have some very large .flv files that I have to compress in a lossless manner. I found some scripts here and they were definitely not lossless..

Thanks!

@Aldaviva
Copy link
Author

Aldaviva commented Jan 8, 2022

@jeffward01 Best of luck. The output codecs I listed above are typically lossy (H.264 and H.265). If you want lossless, or at least very close to lossless, you may want something like Ut Video, Apple ProRes, or Avid DNxHD instead. The best explanation of this concept that I have found is Warren Young's comprehensive answer to "How to create an uncompressed AVI from a series of 1000's of PNG images using FFMPEG".

@Aldaviva
Copy link
Author

Aldaviva commented Feb 2, 2022

If you only want to encode the first several seconds of a video, perhaps to compare it to another video, you can use ffmpeg -t 120s -i ....

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