Skip to content

Instantly share code, notes, and snippets.

@mia-riezebos
Last active March 16, 2024 12:03
Show Gist options
  • Save mia-riezebos/9975172701dfce9db641d58e92b79719 to your computer and use it in GitHub Desktop.
Save mia-riezebos/9975172701dfce9db641d58e92b79719 to your computer and use it in GitHub Desktop.
My Obsessively Balanced & Optimized Transcode script, using FFMPEG.

OBOT

This ffmpeg script is using flags to optimize performance, hardware acceleration, quality and file size. I believe that these settings produce a highly optimized & balanced video with great visual clarity, while staying quite small in file size.

I have also added some flags like -g 30 -strict_gop 1 -forced-idr 1, which will make sure that the output video has regular I(DR) frames for improved scrubbing. This is useful when you are running a media server like Jellyfin or Plex, so you are able to start a decode stream more reliably (without having to backtrace to the last I-frame as much)

# [O]bsessively [B]alanced & [O]ptimized [T]ranscode script
ffmpeg -i <input> \ # select input video file
-vsync 0 # prevent FFmpeg from creating output YUV with duplicate and extra frames.
-hwaccel cuda # enable hardware acceleration using cuda
-hwaccel_output_format cuda # unclear- read https://docs.nvidia.com/video-technologies/video-codec-sdk/12.0/ffmpeg-with-nvidia-gpu/index.html
-c:v hevc_nvenc -preset p7 -tune hq \ # NVENC HEVC encoder, preset p7 (slowed, best quality), tune for high quality
-cq:v 23 \ # constant quality (CRF equivalent) of 23 (decent quality, great compression)
-rc:v vbr \ # use Variable Bit Rate (VBR) for rate control mode
-rc-lookahead:v 4 \ # look 4 frames ahead for VBR compression optimization
-g 30 \ # keyframe every 30 frames
-strict_gop 1 \ # minimize GOP-to-GOP rate fluctuations (keyframes are more evenly spaced & prefer 1/30)
-forced-idr 1 \ # make sure all keyframes are IDR frames so scrubbing is more responsive
-vf "scale_cuda='min(1920,iw)':-1" \ # resize to 1080p unless source is smaller (using scale_cuda for better hwaccel)
# -c:a libopus -ac 2 -b:a 320k \ # downmix to stereo 320k OPUS audio
-c:a copy \ # copy audio from source (I prefer this option to retain 5.1 surround)
-c:s copy \ # copy soft (embedded) subs if available
<output> # specify output file name
@mia-riezebos
Copy link
Author

mia-riezebos commented Nov 1, 2023

No changes to the output settings to report but I included hardware acceleration on the decode, and switched the native scale video filter to the scale_cuda one. this simple change made the transcoder significantly faster.

Source file: 2160p BluRay video, 40GB/h or a bitrate of 110M

  • CPU: Intel Core i7-7700k
  • GPU: NVIDIA GeForce RTX 3070 (Gigabyte / stock)
  • RAM: 2x8GB DDR4-2400
CPU decoding, CPU scaling, GPU encoding: *will report back* (preliminary: ~35fps)
  Final file size: *will report back*
GPU decoding, GPU scaling, GPU encoding: *will report back* (preliminary: ~150fps)
  Final file size: *will report back*

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