Introduction to ffmpeg
Calculating Bitrate
Assuming your video duration is 1 hour 50 minutes (i.e., 110 minutes = 6600 seconds), and the target file size is 5GB (5GB = 5000MB, actual 1GB = 1024MB)
Target Bitrate (kbps) = (File Size (MB) × 8192) ÷ Duration (seconds) = (5120 × 8192) ÷ 6600 ≈ 6355 kbps (For convenience, we’ll just use 6000k later)
First Encoding Pass
- First pass: ffmpeg analyzes the entire video content, records the complexity and motion information of each frame, but does not generate the final file.
ffmpeg -y -i input.mp4 -c:v libx264 -b:v 6000k -preset slow -pass 1 -an -f mp4 /dev/null
-y: automatically overwrite existing files-i input.mp4: input file-c:v libx264: use H.264 encoder-b:v 6000k: set video bitrate to 6000 kbps-preset slow: slow encoding speed, high compression, better quality-pass 1: first pass encoding (only analysis, no final video generated)-an: no audio processing-f mp4 /dev/null: output format mp4 but discard output (/dev/null)
Second Encoding Pass
- Second pass: uses data collected in the first pass; ffmpeg can allocate bitrate more reasonably—more bitrate is assigned to complex scenes, less to simple scenes, thus maximizing overall quality within the file size limit.
ffmpeg -i input.mp4 -c:v libx264 -b:v 6000k -preset slow -pass 2 -c:a aac -b:a 192k output.mp4
-pass 2: second pass encoding (uses first pass analysis data to optimize bitrate allocation)-c:a aac: audio encoding to AAC-b:a 192k: audio bitrate 192 kbpsoutput.mp4: output filename
Adjusting Multi-Core Parallelism
By default, ffmpeg calls the maximum number of cores available

You can modify the number of cores with -threads
For example, for the first pass encoding, if we don’t want to fully utilize all 10 cores
ffmpeg -y -i input.mp4 -c:v libx264 -b:v 6000k -preset slow -pass 1 -an -f mp4 /dev/null
Change it to:
ffmpeg -y -i input.mp4 -c:v libx264 -b:v 6000k -thread 8 -preset slow -pass 1 -an -f mp4 /dev/null
More
- https://wangjunjian.com/ffmpeg/2023/03/04/use-ffmpeg-on-macbook-pro-m2-max.html
- https://www.reddit.com/r/ffmpeg/comments/9w2ofl/mac_users_anyone_using_hevc_videotoolbox_to/?tl=zh-hans
- https://www.reddit.com/r/PleX/comments/1fwoipz/when_is_x265_worth_it_over_x264/?tl=zh-hans
- https://pixpark.net/h264-h265-av1-codec-test.html