Skip to content

Instantly share code, notes, and snippets.

@nicolas-cusan
Last active March 3, 2023 10:19
Show Gist options
  • Save nicolas-cusan/04e167183b317154b701d5fba858671d to your computer and use it in GitHub Desktop.
Save nicolas-cusan/04e167183b317154b701d5fba858671d to your computer and use it in GitHub Desktop.

Video encoding and coversion with ffmpeg

Based on https://evilmartians.com/chronicles/better-web-video-with-av1-codec

h264

ffmpeg -i SOURCE.mov -map_metadata -1 -c:a libfdk_aac -c:v libx264 -crf 24 -preset veryslow -profile:v main -pix_fmt yuv420p -movflags +faststart -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" video.h264.mp4

h264 / no sound, low quality, resize to 640x360.

  • change -crf 26 to change quality (higher number = lower quality)
  • Change 640 in -vf "scale=640:trunc(ow/a/2)*2" to change size (upsacling is not possible)
ffmpeg -i SOURCE.mov -map_metadata -1 -an -c:v libx264 -crf 26 -preset veryslow -profile:v main -pix_fmt yuv420p -movflags +faststart -vf "scale=640:trunc(ow/a/2)*2" video.h264.mp4

av1

ffmpeg -i SOURCE.mov -map_metadata -1 -c:a libopus -c:v libaom-av1 -crf 34 -b:v 0 -pix_fmt yuv420p -movflags +faststart -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -strict experimental video.av1.mp4

av1 / no sound, low quality, resize to 640x360.

  • Change 640 in -vf "scale=640:trunc(ow/a/2)*2" to change size (upsacling is not possible)
  • change -crf 40 to change quality (higher number = lower quality)
ffmpeg -i SOURCE.mov -map_metadata -1 -an -c:v libaom-av1 -crf 40 -b:v 0 -pix_fmt yuv420p -movflags +faststart -vf "scale=640:trunc(ow/a/2)*2" -strict experimental video.av1.mp4

hevc

ffmpeg -i SOURCE.mov -map_metadata -1 -c:a libfdk_aac -c:v libx265 -crf 24 -preset veryslow -pix_fmt yuv420p -movflags +faststart -tag:v hvc1 -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" video.hevc.mp4

hevc / no sound, low quality, resize to 640x360.

  • change -crf 26 to change quality (higher number = lower quality)
  • Change 640 in -vf "scale=640:trunc(ow/a/2)*2" to change size (upsacling is not possible)
ffmpeg -i SOURCE.mov -map_metadata -1 -an -c:v libx265 -crf 26 -preset veryslow -pix_fmt yuv420p -movflags +faststart -tag:v hvc1 -vf "sscale=640:trunc(ow/a/2)*2" video.hevc.mp4

HTML

<video controls width="600" height="400">
  <source
    src="video.hevc.mp4"
    type="video/mp4; codecs=hevc,mp4a.40.2"
  />
  <source
    src="video.av1.mp4"
    type="video/mp4; codecs=av01.0.05M.08,opus"
  />
  <source
    src="video.h264.mp4"
    type="video/mp4; codecs=avc1.4D401E,mp4a.40.2"
  />
</video>

Optimized versions:

<video controls width="600" height="400">
  <source
    src="video.hevc.mp4"
    type="video/mp4; codecs=hevc"
  />
  <source
    src="video.av1.mp4"
    type="video/mp4; codecs=av01.0.05M.08"
  />
  <source
    src="video.h264.mp4"
    type="video/mp4; codecs=avc1.4D401E"
  />
</video>

Quality conversion CRF values comparison

H.264 AV1
19 27
23 33
27 39
31 45
35 51
39 57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment