I've been trying to find a definitive answer to this question for some time now, but without much success. Any insight would be greatly appreciated.
I do all of my video transcoding with ffmpeg, mainly using a single pass, however for some videos I want to go the extra mile and use 2 pass encoding. I can do that well enough, however, I would like to improve the transcoding times.
My question is, does the first pass only care about the source (input) file as is, which would mean that I need not apply various filters (such as scale, hqdn3d, unsharp, gradfun, etc) in the first pass? Or does it work on the video stream *after* it's been processed by the filters and bitrate specifiers?
I have tried pass 1 with no filters and pass 2 with filters and the result does look pretty good, but I'd prefer to do it properly, even if it does take longer.
Thanks in advance.
+ Reply to Thread
Results 1 to 19 of 19
You must perform the filtering for each pass. ffmpeg doesn't remember the actual frames from the first pass, just some information about them. For that information to remain valid the two passes must receive the same video.
2 pass VBR encoding will not give you better image quality than CRF encoding. It gives you the ability to specify the file size. Or are you using single pass CBR encoding?
Thanks for the reply jagabo. It's funny, I did some testing by transcoding both ways - with filters (including a scale change) in pass 1 and without filters. I played both videos at the same time and could not perceive any difference by eye. I even extracted the same frame from both videos and compared them in an image editor using various distortions to enhance any differences between the two, but did not find any. Would you expect there to be a difference?
With respect to 2 pass vs single pass quality, I've noticed that for some videos (perhaps that start off with high bitrates?) that the first few seconds in the transcoded video can appear blocky and even a frame stutter can occur (viewed through Media Player Classic - Home Cinema).
If I do a 2 pass transcode, none of those problems are evident. It plays smoothly and no initial blockiness.
I'm not sure exactly what you mean by CBR encoding - I don't specify anything explicitly, I just use the presets, eg:
ffmpeg -i input.mkv -an -c:v libx264 -preset:v slow -profile:v high -level:v 4.0 -b:v %VBitRate% -aspect %Aspect% -vf "filters" -threads 0 output.mkv
ffmpeg settings are applied to the x264 encoder but I think that is using single pass variable bitrate. In that mode x264 varies the bitrate based on the compressibility of the each frame -- but it has to be very conservative about how much more or less than the average it can use because it doesn't know what future frames will look like. (Keep in mind that for every shot that gets more than the average bitrate some other shot(s) will have to get less.) That can lead to problems like you describe, a shot at the start not getting enough bitrate because the encoder doesn't know if future shots will be just as hard to compress or or easily compressed. In a case like this, 2-pass encoding can give you better results because during the second pass (where the actual encoding is being done) the encoder knows it can dedicate a lot of bits to that section because there are later shots that don't need a lot of bitrate.
You can avoid that type of problem and still encode in a single pass by using Constant Rate Factor encoding. In that mode you specify what "quality" you want and the encoder uses whatever bitrate is necessary at each frame to deliver that quality. The down side of CRF encoding is that you don't know what the final file size will be (and hence the average bitrate -- since Size = Bitrate * Time). You use "-crf X" on the ffmpeg command line to specify CRF encoding. Where X is a number between 0 (lossless but very large files) and 51 (very poor quality but very small files). Most people that use CRF encoding will use between 18 and 25 or thereabouts.
If you compress a video in CRF mode, then go back and compress the source again in 2-pass VBR mode at an average bitrate that matches the CRF result, the two videos will look nearly identical.
Basically, you have two methods of encoding. Bitrate and Quality. In Bitrate mode you know what the file size will be but you don't know what the quality will be. In Quality mode you know what the quality will be but you don't know what the bitrate will be.
Thanks again for an informative reply jagabo. I did some further tests with 2-pass encoding. I used some five filters in both the 1st and 2nd pass (case A) and compared the result with using no filters in pass 1 and full filters in pass 2 (case B).
The results were striking. Case A looked pretty bad, while case B looked pretty good. In case A, it looked like the filters were applied twice (even though encoding takes place only in pass 2). I think I will stick with case B...
I also carried out some tests with CRF, and as you say, it's somewhat of a lottery as to how big the resulting video will end up being and what the average bitrate will be. I tried to match a target bitrate and eventually came close by changing CRF values. The resulting video quality was of the same order as the 2-pass case B, and while it was just a single pass, the encoding time was slower - probably not much different than the 2-pass overall.
I guess my preference is to select an average bitrate, and therefore file size, than to be 'surprised' by what I get with CRF. This is especially so when I'm transcoding seasons of TV show episodes - I prefer them to be roughly the same size.
To each his own, of course.
Last edited by jagabo; 29th Nov 2013 at 22:25.
Originally Posted by _AI_
Anyway, I'll still do some more experimenting with various settings, etc.
Using CBR is a colossal waste of bitrate, but sure some use it every day, using editing softwares or even backing up TV, movies, but anyway using CBR, there could be scenes that need more bitrate.
Try this: take some clips from your videos, different video content, with filtering or without, so you have four videos. Encode them all CRF 18. In this case you should get balanced, relative quality the same in all clips. The only thing that is going to be different is bitrate. Now, how do you do that using 2pass encoding to average? Imposible, you can do it half a day and still not getting optimal bitrate.
CRF encoding can teach us what bitrate is needed for a scene, video content, for x264 encoder, very handy.
With 2pass VBR to average is difficult to guess how your encoder distributes bitrate. After denoising there is much less bitrate needed, sometimes drastically, difficult to guess how much. Good example is using very good bob deinterlacer QTGMC (creates double frame rate) , there was shock for me that less bitrate was needed than what I'd get encoding just interlace. It sounds like a joke first, but QTGMC uses a touch of denoise too, so hard to guess first. Using 2pass VBR with double frame rate filter , I'd still encode with too much higher bitrate until today because I'd just guess and it would look good. Not the end of the World sure, but again, no guessing now.
I do understand what you are saying _Al_ and I do agree with the theoretical basis. But I make a judgment based on what I can actually see while playing the content back, not so much on what the optimal bitrate should be for any second of content. If I can't tell the difference between say a DVD file and a transcoded version, then it's good enough for me. I'm not one of those people who fret about having 'lossless' quality or avoiding scaling away from 1:1, etc as any minor differences will either not be perceivable and/or be smeared out by the viewing distance from the screen, etc.
Sure, the more obvious differences will be in motion handling, especially fast motion, but if even those scenes look fine, then I'm happy with the encode.
However, it's nice to know that there is a fallback option (CRF) if I really need it. One further question I have is what role does "-b:v bitrate" play, if any, when using CRF?
I'm still not sure you get this 100%, CRF doesn't mean super quality, choosing something like CRF 25 and up will cause visual degradation more and more obvious, point is it is you who decides what quality you want, and that algorithm is watching it for you.
There is a switch needed in thinking, just take one episode, encode part of it with CRF 18, .., 20,..., 22 and watch it,...,Whatever number barely passes for you as OK you choose it. You reliaze that 21 works, 22 makes it visually somehow not that good for you so you choose 21. After this you have discovered YOUR number (quantizer) for your type of videos. Then you use that number all the time and you have constant watchdog quality/size working for you. No more guessing average 2pass. You can test filters and still using same CRF number. After filtering CRF just gives it less bitrate but keeps quality you decided that works for you.
Last edited by _Al_; 30th Nov 2013 at 21:33.
So given that I can not perceive any visual improvement in quality between the various CRF settings and the 2-pass version, I see not reason to pay the penalty in file sizes that are nearly doubled.
Originally Posted by jagabo
If you could limit the maximum bitrate while still using CRF, that might be a suitable compromise for me, but it seems that you can't really do this. I've tried playing around with maxbitrate and bufsize settings etc, however it still doesn't really respect the constraints (as I imagine these are somewhat mutually exclusive requirements for CRF encoding).
If the size is too large then use higher CRF value. Once you settle on a "quality" level that is good enough for you , that's all you need. People tend to use 2pass only when they need a specific filesize or bitrate (e.g. fixed media limits like blu-ray , or bandwidth restrictions like streaming)
You cannot make valid comparisons on "quality" at different bitrates . CRF won't look better than 2pass at the same bitrate. The point is it will look almost the same as 2pass at the same bitrate - with the benefit being faster since it's 1 pass instead of 2, and certainly better than 1pass ABR . Even though the 1st pass in a 2pass encode is usually done with quick settings (so it's not 2x faster), it's still much faster.
If you were to use CRF and achieve "x" bitrate , then encode 2pass with "x" bitrate and same settings otherwise - the video will look almost identical .
VBV certainly works with CRF using x264 . That's probably an issue with ffmpeg libx264 , or your binary has an issue. Personally I think that' s not the way to do it. Using VBV to "cap" can only reduce quality . I would just use a CRF value that's in the range of acceptable quality for you. If in your tests, the filesize was 2x of your 2pass bitrate runs when using CRF20-22 - that tells me right away your "acceptable" CRF value is going to be very high and you're not a very discerning or picky person
It sounds like there are other issues with your filtering as well. They should be applied to both passes. Just stop and think about it - how can the encoder properly allocate bitrate and frametypes if the 1st pass doesn't even receive those filters ? Something doesn't make sense about your observations
you need to go higher with that CRF, higher number - you get less quality. x264 encoders allows to set that number for CRF from 0 (lossless) to 52 (almost garbage) .Not sure what ffmpeg can do and not sure if CRF can be zero now, maybe CQ mode only, but that is not important here,
you need to find CRF that is ok and other CRF that is not ok for you, only then you know that "your" CRF number is in between , so narrow it down.
Last edited by _Al_; 1st Dec 2013 at 01:34.
Look in dark grainy areas -- that's one of x264 weakest points. You'll start seeing posterization artifacts there when you don't use sufficient bitrate. See the videos in this post as an example:
Originally Posted by poisondeathray
Originally Posted by poisondeathray
Originally Posted by _AI_
Originally Posted by jagabo
You must have set different settings or filtering, .., or ffmpeg works differently. Using x264 encoder gives very similar, almost the same results using CRF or 2pass and getting file that has the same volume.
Bitrate viewer shows bitrate distribution that tends to be exactly the same, except 2pass distribution seems to be somehow tiny bit smoother where scene changes (as if encoder knew what is going to follow). Not sure if this is good or bad thing. And also bitrate could be different at the times but margin is not that big at all to say something is visibly worse or better.