Three Things to Know About 2-Pass x265 Encoding

Here are three things to know about 2-pass x265 encoding with FFmpeg.

  1.  If you’re using -pass 1 and -pass 2 in your command string, it’s probably not working. You must use x265-params 0:pass=1 and then x265-params 0:pass=2 in the second pass. In the way we absolutely love about FFmpeg, there’s no error message if you use -pass 1/-pass 2; it just takes twice as long, and you end up with the exact same file you would get if you simply used single-pass encoding.
  2. Unless you use the no-slow-firstpass=1 option, two-pass encoding will take about twice as long as 1-pass with no meaningful overall quality difference, though about a 10% advantage in the low-frame score. Since 1-pass is slightly better than 2x faster than two-pass and about the same quality, single-pass encoding is definitely worth considering.
  3. If you do use the no-slow-firstpass=1 option, it shaves about 14% of your encoding time with no quality penalty.

Let’s address point by point.

Perfecting the Two-Pass Command String

When x265 first became available in FFmpeg, you accessed most configurations vai the x265-params switch. If you didn’t, FFmpeg ran without error but simply ignored all the configuration options set without x265-params. Over time, more and more configuration options became accessible via normal FFmpeg commands, but apparently not passes.

When I first tested one-pass vs. two-pass I used the -pass command, and the two-pass file was the same as the 1-pass file, though it took twice as long to encode. I tried the x265-params switch, and though 2-pass again took twice as long as single pass, the files were different.

Note that with x264, 2-pass takes nowhere near twice as long to encode (see here). A bit of research revealed the no-slow-firstpass switch that I used to accelerate encoding by about 14%. In an attempt to determine if there were any faster options, I wrote a quick blog entitled, What’s the Best Setting for x265 Two-Pass Encoding? Asking whether anyone knew of any faster options. I heard nothing back.

I’ll show the complete command strings down below.

Accelerate Encoding Via the no-slow-firstpass=1 Option

As mentioned, once I got 2-pass working, I looked for options to speed it up, and found the no-slow-firstpass=1 option. Here’s the command string I used, showing both this switch and the correct configuration for two-pass encoding. I also closed the GOP with open-gop=0, which is recommended for ABR packaging (see here).

ffmpeg -y -i football.mp4 -c:v libx265 -preset medium -threads 8 -b:v 3500k -maxrate 7000k -bufsize 7000k -g 60 -x265-params open-gop=0:pass=1:no-slow-firstpass=1 -f mp4 NUL

ffmpeg -y -i football.mp4 -c:v libx265 -preset medium -threads 8 -b:v 3500k -maxrate 7000k -bufsize 7000k -g 60 -x265-params open-gop=0:pass=2 -an football_2pass_turbo.mp4

As I’ll show with the quality results below, this shaved from 14% – 22% of encoding time from the tested clips with similar or better quality than regular 2-pass encoding. If you’re encoding with regular 2-pass encoding with x265 in FFmpeg, this is a switch you should consider deploying.

The Results Please

I ran two sets of tests, one set on 23 files ranging from 1-5 minutes in five genres: animation, office, 30 fps sports, 60 fps sports, and general entertainment. The other set of tests was on two files about 12 minutes long; complete versions of Netflix’s Meridian and Blenders’ Tears of Steel. Table 1 shows results for the 23 mixed-genre files; note that file sizes were all within about 2%.

Table 1. Average encoding time, VMAF, and low-frame VMAF for 23 mixed genre files.

Figure 1 shows the data on a percentage basis. As stated, 1-pass delivered about the same overall quality as two-pass in slightly faster than 2x the encoding time. The low-frame score measures the potential for transient quality differences, and a roughly 10% delta translates to about 7 VMAF points, which a viewer might notice if the duration of the low quality region is more than 1-2 frames. If that concerns you, you can opt for the roughly 14% savings delivered by the turbo mode with insignificant differences in VMAF and low frame score.

Figure 1. Time and quality deltas between 1-pass, 2-pass, and 2-pass turbo encoding methods for 23 1-5 minute test files. Click the figure to see it at full resolution.

Meridian/Tears of Steel

Table 2 shows the results with the 12-minute-ish 1080p versions of Meridian and Tears of Steel.

Table 2 Average encoding time, VMAF, and low-frame VMAF for two 12-minute files.

Here, the time savings for 1-pass and 2-pass turbo were 48% and 22%, respectively, with nearly identical VMAF quality. In this case, the low-frame score for 2-pass turbo was higher than 2-pass regular speed, making the turbo mode the best overall option from both a quality and encoding speed perspective.

Figure 2. Time and quality deltas between 1-pass, 2-pass, and 2-pass turbo encoding methods for two 12-minute test files. Click the figure to see it at full resolution.

How to Use This Data

If you’re encoding with x265 using 2-pass mode, you should check if your results are consistent with mine and make whatever adjustments you see fit. If you have a technique that accelerates two-pass encoding even further, I’d appreciate hearing about it ([email protected]).

About Jan Ozer

Avatar photo
I help companies train new technical hires in streaming media-related positions; I also help companies optimize their codec selections and encoding stacks and evaluate new encoders and codecs. I am a contributing editor to Streaming Media Magazine, writing about codecs and encoding tools. I have written multiple authoritative books on video encoding, including Video Encoding by the Numbers: Eliminate the Guesswork from your Streaming Video (https://amzn.to/3kV6R1j) and Learn to Produce Video with FFmpeg: In Thirty Minutes or Less (https://amzn.to/3ZJih7e). I have multiple courses relating to streaming media production, all available at https://bit.ly/slc_courses. I currently work as www.netint.com as a Senior Director in Marketing.

Check Also

CSAI vs SSAI in Video Ad Insertion: A Comprehensive Guide with Recommendations

Introduction Ad insertion technologies play a crucial role in monetization strategies. Two primary methods dominate …

DCVC-B: A New Deep Learning Codec for Efficient B-Frame Compression

In a recent white paper titled Bi-Directional Deep Contextual Video Compression (DCVC-B), researchers Xihua Sheng, …

M3-CVC: A Glimpse into the Future of AI-Driven Video Compression

A new AI-based codec proved 18% more efficient than VVC but substantial decoding requirements will …

Leave a Reply

Your email address will not be published. Required fields are marked *