12

I a trying to convert my high quality movie collection to save some disk space. And I learned that 2 Pass encoding will do the job better. And this is the command line that I am using to encode videos with 2 pass.

ffmpeg -i input.mkv -c:v libx264 -vf "scale=1920x1080" -vb 3.5M -pass 1 -an pass1.mp4
ffmpeg -i input.mkv -c:v libx264 -vf "scale=1920x1080" -vb 3.5M -pass 2 -c:a copy pass2.mp4

And this works fine. And I know that this command will give the output for the first pass also. That's fine. I will delete them. My doubt is, I read many forums that people use -y and -passlogfile commands in 2 pass encoding. What does -y and -passlogfile do?

Also is there any other setting can I add to improve the quality?

llogan
  • 57,139
  • 15
  • 118
  • 145
RvidD
  • 364
  • 2
  • 4
  • 12

1 Answers1

17

tl;dr - Just use -crf with 1-pass unless you need a specific file size

  • 1 pass is faster than 2 passes.
  • 2-pass does not make a better quality or smaller file: it only lets you set the output file size (but not the quality), whereas -crf lets you choose the quality (but not the file size).

Questions

I learned that 2 Pass encoding will do the job better.

Two-passes will to a better job targeting a specific output file size. Otherwise, just do a single pass with -crf.

I know that this command will give the output for the first pass also. That's fine. I will delete them.

You could output to /dev/null (Linux & macOS) or NUL (Windows) instead and avoid making a temporary file:

ffmpeg -y -i input.mkv -c:v libx264 -vf "scale=1920:1080" -b:v 3.5M -pass 1 -an -f mp4 /dev/null
ffmpeg -y -i input.mkv -c:v libx264 -vf "scale=1920:1080" -b:v 3.5M -pass 2 -c:a aac output.mp4

See FFmpeg Wiki: H.264 - Two-Pass for more info and examples.

What does -y and -passlogfile do?

  • -y Overwrite output files without asking for confirmation. Makes sense for the first pass, otherwise it will ask you File '/dev/null' already exists. Overwrite ? [y/N]. Yes, you can "overwrite" /dev/null.

  • -passlogfile Sets the two-pass log file name. You don't need to use this option unless you want to use a custom log file name instead of the default (ffmpeg2pass-0.log).

See the ffmpeg documentation for more info on these options.

Also is there any other setting can I add to improve the quality?

  • Use the slowest preset you have patience for. See FFmpeg Wiki: H.264.
  • Increase the bitrate.
  • Use a more efficient encoder, such as libx265. See FFmpeg Wiki: H.265. Be aware that it is slow.
  • Avoid upscaling. Experiment with different scaling algorithms to see what looks best to you: "scale=1920:-2:flags=lanczos".
  • Buy another drive and don't re-encode.
llogan
  • 57,139
  • 15
  • 118
  • 145
  • **1. "Use the slowest preset you have patience for"** - From the FFmpeg documentation, I thought those presets were only useful for crf encoding. So slower preset will also improve 2 pass. Right? **2. "scale=1920:-2:flags=lanczos"** - I will definitely try different scaling algorithms. And what does -2 in the height do here? Is that same as -1? – RvidD Oct 01 '18 at 21:57
  • 1
    @RvidD A slower preset will use a more efficient set of options which means that it will be slower to encode, but you will generally have a better quality per bit ratio. You can use it with any rate control method. The `-2` is like `-1`, but will make the value divisible by 2 which is needed for outputting H.264/H.265 with 4:2:0 chroma subsampling. – llogan Oct 01 '18 at 23:07
  • Can I run FFmpeg GPU assisted? My GPU is Geforce GTX 970m. Is that even a good idea to run FFmpeg with GPU assistance? – RvidD Oct 02 '18 at 06:31
  • 1
    @RvidD Yes, see https://trac.ffmpeg.org/wiki/HWAccelIntro – particularly the part about NVENC. Some more info here: https://superuser.com/q/1296374/48078. This will improve encoding speed, but I don't know if NVENC for H.264 gives you better quality for the same bitrate, compared to libx264. – slhck Oct 02 '18 at 12:01
  • 1
    @LordNeckbeard I encoded 2 pass video with slowpreset, placebo and without giving any preset. The results were different. slowpreset performed slightly worse than No preset. And placebo performed slightly worse than slowpreset. I also tested them with different videos and different bitrates. I took many screenshots and compared them and the results were same. This maybe because the second pass of the encoding has predefined parameters and may not need any preset setting that is required for crf encoding. I am using latest version of FFmpeg (Windows 64-bit, Static, nightly git) – RvidD Oct 03 '18 at 04:41
  • @slhck I tried Nvidia NVENC. It is super fast (like 12-14x). But doesn't support 2-pass encoding and also quality is not as good as 2 pass encoded video for the same size. – RvidD Oct 03 '18 at 04:47
  • I don't see how this can work. The command above does not create a stats file. If there is no stats file, there's no way for the 2nd pass to use the information from the 1st pass to make compression decisions. I know this all comes from some of the guides found on ffmpeg's website but there's definitely something missing because this essentially produces a single-pass CBR file. – dsanchez Dec 16 '20 at 03:55
  • @dsanchez It creates 2 stats files for me: `ffmpeg2pass-0.log` & `ffmpeg2pass-0.log.mbtree`. The command in the answer is only 1 of 2 commands needed for 2-pass encoding (see the question for command #2 or maybe I'll update the answer to include both). – llogan Dec 16 '20 at 19:07
  • @llogan The command line in this answer only creates 1 file for me, the log file. However, that file is empty. This command is missing the -x265-params option, which is what actually creates the proper stats files. – dsanchez Dec 16 '20 at 19:16
  • @dsanchez The question, command, and answer are referring to libx264, not libx265. – llogan Dec 16 '20 at 19:23
  • @llogan Ah, that's the difference. Thanks! – dsanchez Dec 16 '20 at 19:27