Log in

View Full Version : FFmpeg/x264 2 pass encoding to TS


Aegwyn11
15th April 2012, 18:08
Hi there. First let me apologize if this is a noob question...I've searched a ton all over the place and figured a lot out on my own, but I'm sort of stuck. I'm slowing figuring out FFmpeg and x264, but need a little help. I've gotten to a point where I can make good single pass encodes, but need some help understanding two pass. I understand the 'reason' for using two pass is to achieve a desired bitrate with max quality, which is why I'm interested in it.

So I've figured out how to use the VBV/HRD settings, as well as all the necessary stuff to make interlaced work (my source in this case is interlaced). I've searched a ton about 2 pass encoding and find lots of references along the lines of "if you don't care about file size, use CRF; if you need a specific bitrate, use 2pass" but noone ever really talks very specifically about -how- to use two pass. I understand the the second pass should be done using -bitrate for rate control, but what about the first pass?

For the application that's in front of me right now, my source is a Prores 422 file that I decoded to v210 using QT Pro (avcodec's Prores decoder doesn't handle stream problems nearly as elegantly as QT). I use FFmpeg compiled with 10-bit x264 with the v210 file as an input to encode h.264 and mux into a TS. Basically what I've been doing is setting CRF to a low low number (1) and letting x264 control the rate using VBV. I need an average bitrate no more than 80 Mbps (video) and it needs to adhere to Hi422P at (no more than) level 4.2, but the video can go above 80 at times if needed. VBR video is totally fine, as long as the resulting TS is CBR.

So I tried doing a first pass encode setting -muxrate 87000000 -x264opts crf=1:vbv-maxrate=85000:vbv-bufsize=70000:nal-hrd=vbr. Once this completed, I ran the second pass, just changing crf=1 to bitrate=80000. As the encode ran, I got a bunch of warnings that pts<dts, invalid TS from time to time (same thing you get if you set a muxrate and too low CRF without VBV).

So I guess my question is - for my application (highest quality possible, 80 Mbps average bitrate, Hi422P@L4.2 or lower, muxed into a valid CBR transport stream), what kind of two pass settings should I be using?

nm
15th April 2012, 18:17
I understand the the second pass should be done using -bitrate for rate control, but what about the first pass?

It's best to use the same average bitrate in both passes!

Remember to use parameter -pass 1 (or maybe it's better to use -x264opts pass=1:... with x264opts) in the first pass command line and -pass 2 in the second pass. Otherwise you weren't doing 2-pass at all but first encoded one file with CRF=1 and then overwrote that with a 1-pass ABR encode.

Aegwyn11
15th April 2012, 18:24
It's best to use the same average bitrate in both passes!

Remember to use -pass 1 for the first pass and -pass 2 for the second. Otherwise you weren't doing 2-pass at all but first encoded one file with CRF=1 and then overwrote that with a 1-pass ABR encode.

Oops, sorry about that...I am using pass=1 and pass=2 for 1st and 2nd pass.

When you say its best to use the same average bitrate in both passes, what does that mean in regards to settings? I don't understand.

nm
15th April 2012, 18:28
First pass: -bitrate 80000k
Second pass: -bitrate 80000k

Here's one "official" 2-pass encoding example: http://www.x264bluray.com/home/1080i

Aegwyn11
15th April 2012, 18:31
First pass: -bitrate 80000k
Second pass: -bitrate 80000k

Here's one "official" 2-pass encoding example: http://www.x264bluray.com/home/1080i

I think I get it...so you're saying to just forget about using CRF alltogether and use -bitrate for both passes? Sorry for being obtuse, I just want to make sure I understand.

nm
15th April 2012, 18:32
Exactly.

Aegwyn11
15th April 2012, 20:09
Okay I'm a little lost. The first pass completes just dandy, no errors no warnings, finishes the whole file, but my 2nd pass always gets lots of the following warnings:

dts < pcr, TS is invalid
[mpegts @ 00000000003BF5C0] dts < pcr, TS is invalid

And then a couple of minutes into the 23 minute file, it just stopped encoding.

Here's my command line:
ffmpeg64-10 -top 1 -i "e:\trimmed v210.mov" -r:v 29.97 -flags +ilme+ildct -pix_fmt yuv422p10le -muxrate 87000000 -map 0:1 (lots of audio settings) -vf setsar=1:1 -vcodec libx264 -preset medium -tune film -top 1 -x264opts pass=1:stats="1stpass.log":bitrate=80000:threads=0:vbv-maxrate=85000:vbv-bufsize=70000:nal-hrd=vbr "Encode.ts" -vstats_file "log.txt"

ffmpeg64-10 -top 1 -i "e:\trimmed v210.mov" -r:v 29.97 -flags +ilme+ildct -pix_fmt yuv422p10le -muxrate 87000000 -map 0:1 (lots of audio settings) -vf setsar=1:1 -vcodec libx264 -preset medium -tune film -top 1 -x264opts pass=2:stats="1stpass.log":bitrate=80000:threads=0:vbv-maxrate=85000:vbv-bufsize=70000:nal-hrd=vbr "Encode-2.ts" -vstats_file "log-2.txt"