0

I'm trying to re-encode an HEVC/x265 file yet I'm getting an error.

I used these commands and it's not working

D:\FFmpeg\bin>ffmpeg -i output2.mkv -c:v libx265 -preset slow -b:v 3000k -x265-params "lossless=1:pass=2" -acodec copy output3.mkv

ffmpeg version 2021-10-03-git-2761a7403b-full_build-www.gyan.dev Copyright (c) 2000-2021 the FFmpeg developers
  built with gcc 10.3.0 (Rev5, Built by MSYS2 project)
  configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-lzma --enable-libsnappy --enable-zlib --enable-librist --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray --enable-libcaca --enable-sdl2 --enable-libdav1d --enable-libzvbi --enable-librav1e --enable-libsvtav1 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx --enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-libglslang --enable-vulkan --enable-opencl --enable-libcdio --enable-libgme --enable-libmodplug --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libshine --enable-libtheora --enable-libtwolame --enable-libvo-amrwbenc --enable-libilbc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-ladspa --enable-libbs2b --enable-libflite --enable-libmysofa --enable-librubberband --enable-libsoxr --enable-chromaprint
  libavutil      57.  7.100 / 57.  7.100
  libavcodec     59. 10.100 / 59. 10.100
  libavformat    59.  5.100 / 59.  5.100
  libavdevice    59.  0.101 / 59.  0.101
  libavfilter     8. 11.100 /  8. 11.100
  libswscale      6.  1.100 /  6.  1.100
  libswresample   4.  0.100 /  4.  0.100
  libpostproc    56.  0.100 / 56.  0.100
Input #0, matroska,webm, from 'output2.mkv':
  Metadata:
    DESCRIPTION     : Packed by Bilibili XCoder v2.0.2
    MAJOR_BRAND     : iso5
    MINOR_VERSION   : 1
    COMPATIBLE_BRANDS: avc1iso5dsmsmsixdash
    ENCODER         : Lavf59.5.100
  Duration: 03:57:16.79, start: -0.007000, bitrate: 11269 kb/s
  Stream #0:0: Video: hevc (Main), yuv420p(tv, bt709, progressive), 1920x1080 [SAR 1:1 DAR 16:9], 30 fps, 30 tbr, 1k tbn (default)
    Metadata:
      HANDLER_NAME    : VideoHandler
      VENDOR_ID       : [0][0][0][0]
      ENCODER         : Lavc59.10.100 libx265
      DURATION        : 03:57:16.440000000
  Stream #0:1: Audio: opus, 48000 Hz, stereo, fltp
    Metadata:
      HANDLER_NAME    : SoundHandler
      VENDOR_ID       : [0][0][0][0]
      ENCODER         : Lavc59.10.100 libopus
      MAJOR_BRAND     : iso5
      MINOR_VERSION   : 1
      COMPATIBLE_BRANDS: avc1iso5dsmsmsixdash
      COMMENT         : Packed by Bilibili XCoder v2.0.2
      DURATION        : 03:57:16.792000000
Stream mapping:
  Stream #0:0 -> #0:0 (hevc (native) -> hevc (libx265))
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
x265 [info]: HEVC encoder version 3.5+15-4bf31dc15
x265 [info]: build info [Windows][GCC 10.3.0][64 bit] 8bit+10bit+12bit
x265 [info]: using cpu capabilities: MMX2 SSE2Fast LZCNT SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
x265 [info]: Main profile, Level-8.5 (Main tier)
x265 [info]: Thread pool created using 8 threads
x265 [info]: Slices                              : 1
x265 [info]: frame threads / pool features       : 3 / wpp(17 rows)
x265 [error]: unable to open file x265_2pass.log
[libx265 @ 000001a6470f76c0] Cannot open libx265 encoder.
Error initializing output stream 0:0 -- Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height
Conversion failed!

While this command works: ffmpeg -i output2.mkv -c:v libx265 -preset slow -b:v 3000k -acodec copy output3.mkv

I'm aware that there's an error with the video (stream:0), yet I'm confused since I didn't change anything in the resolution (width and height), I don't know what rate it is pertaining to, and the bit rate is already specified.

I only have limited knowledge in encoding so please bear with me. Thank you!

UPDATE: So what I've done, I used

ffmpeg -i output2.mkv -c:v libx265 -preset slow -b:v 3000k -x265-params "pass=1:pass=2" -acodec copy output3.mkv

and it worked. I also learned that lossless can't go together with the pass option

Kyle
  • 1
  • 2
  • The error is quite clear, no? You are passing advanced parameters and they’re not working. You probably cannot use the “lossless” option with multi-pass encoding. You also probably should not be using this option if you actually want to reduce the size. – Daniel B Jun 09 '22 at 19:17
  • Furthermore, there’s the “unable to open file x265_2pass.log” error. – Daniel B Jun 09 '22 at 19:17
  • Hi! Thank you for your response! I also tried removing the "lossless" option too yet still didn't work. Also what does the "unable to open file x265_2pass.log” error means? – Kyle Jun 09 '22 at 19:22
  • You have to run pass 1 first. Are you even actually trying to do multi-pass encoding or did that option slip in there by accident? – Daniel B Jun 09 '22 at 19:23
  • Ohh, so you mean that I should use the `pass=1` first, then another run for the 2nd pass and use `pass=2`?? I'm really sorry. I'm still trying to learn to encode through the CLI since it is more powerful that the GUI. In the GUI that I've been using, I can just check the 2 pass filter and the avg bitrate I wish to. – Kyle Jun 09 '22 at 19:36
  • UPDATE: So what I've done, I used `ffmpeg -i output2.mkv -c:v libx265 -preset slow -b:v 3000k -x265-params "pass=1:pass=2" -acodec copy output3.mkv` and it worked. Thank you! I thought I should run them separately. – Kyle Jun 09 '22 at 19:52

1 Answers1

2

To do multi-pass encoding, you need to run each pass individually, with the correct parameters. When using a GUI encoder, it may do this automatically under the hood. On the command line, you need to do it manually.

You tried to start with pass 2 (the last pass) without running any other pass first. As such, you received this error message:

x265 [error]: unable to open file x265_2pass.log

To create this log file, you need to run at least pass 1:

ffmpeg -y -i input.mkv -c:v libx265 -preset slow -b:v 3000k -x265-params pass=1 -an -f null NUL

Note that we discard the video output, because it is not relevant. (On Linux/Unix, instead of NUL, use /dev/null.)

Afterwards, you can create the final output:

ffmpeg -i input.mkv -c:v libx265 -preset slow -b:v 3000k -x265-params pass=2 -c:a copy output.mkv

Lossless encoding is not compatible with rate control options (says the x265 manual). What this means regarding multi-pass encoding I don’t know, but it’s probably pointless to use them together. Lossless encoding will most likely also not reduce the file size but may even increase it instead.

You can find this information in the FFmpeg H.265 encoding guide.


The error “Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height” doesn’t refer to any specific parameter. It just means the encoder couldn’t be initialized. Luckily in this case, we have additional output identifying the exact problem.

Daniel B
  • 60,360
  • 9
  • 122
  • 163