Encoder libx265 multi pass issue: 1st pass ends prematurely

I have multiple AVC videos I’m converting into HEVC with half their original bit rate using libx265 “turbo multi pass” on ffmpeg 8.1.1 full build (Windows 11).

Found some encoder settings online to make the video look better & ran them on one minute of two different videos: they worked great.

THE ISSUE: When I try to encode the same two videos fully using 1st pass command

.ffmpeg -y -i "d:Mediavideo.mkv" -c:v libx265 -preset superfast -b:v 12000k -g 240 -x265-params no-open-gop=1:gop-lookahead=12:bframes=6:weightb=1:hme=1:strong-intra-smoothing=0:rect=0:aq-mode=4:pass=1:no-slow-firstpass=1 -an -sn -f null NUL

it keeps the *2pass.log files as *.temp (‘x265_2pass.log.cutree.temp’ & ‘x265_2pass.log.temp’). So when I try to run the 2nd pass command:

.ffmpeg -i "d:Mediavideo.mkv" -map_metadata -1 -c:v libx265 -preset superfast -b:v 12000k -g 240 -x265-params no-open-gop=1:gop-lookahead=12:bframes=6:weightb=1:hme=1:strong-intra-smoothing=0:rect=0:aq-mode=4:pass=2 -an -sn out.mkv

I get:

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

Removing .temp from file names & re-running 2nd pass command I get:

x265 [error]: bad frame number (198168) at stats line 198162

Ends Prematurely: After finishing the 1st pass it should automatically remove the “.temp” from *.2pass files. Also my input file has only one stream, video, should have 205626 frames (duration x FPS), but the “x265_pass.log” files ends like this:

in:198168 out:198162 type:P q:11.75 q-aq:8.96 q-noVbv:11.75 q-Rceq:0.98 tex:1214319 mv:20719 misc:12359 icu:286.10 pcu:1478.62 scu:6395.27 sc:0 ;
in:198165 out:198163 type:B q:12.89 q-aq:13.00 q-noVbv:12.89 q-Rceq:0.98 tex:351530 mv:10798 misc:11120 icu:0.00 pcu:1287.21 scu:6872.79 sc:0 ;
in:198162 out:198164 type:b q:14.02 q-aq:14.00 q-noVbv:14.02 q-Rceq:0.98 tex:378362 mv:11935 misc:11205 icu:0.00 pcu:1331.79 scu:6828.21 sc:0 ;
in:198163 out:198165 type:b q:14.02 q-aq:14.00 q-

Here is the full 1st pass command and its output:

PS D:PortableProgramsffmpeg-8.1.1-full_buildbin> .ffmpeg -y -i "d:Mediavideo.mkv" -c:v libx265 -preset superfast -b:v 12000k -g 240 -x265-params no-open-gop=1:gop-lookahead=12:bframes=6:weightb=1:hme=1:strong-intra-smoothing=0:rect=0:aq-mode=4:pass=1:no-slow-firstpass=1 -an -sn -f null NUL
ffmpeg version 8.1.1-full_build-www.gyan.dev Copyright (c) 2000-2026 the FFmpeg developers
  built with gcc 15.2.0 (Rev13, Built by MSYS2 project)
  configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-cairo --enable-fontconfig --enable-iconv --enable-gnutls --enable-lcms2 --enable-libxml2 --enable-gmp --enable-bzlib --enable-lzma --enable-libsnappy --enable-zlib --enable-librist --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-libbluray --enable-libcaca --enable-libdvdnav --enable-libdvdread --enable-sdl2 --enable-libaribb24 --enable-libaribcaption --enable-libdav1d --enable-libdavs2 --enable-libopenjpeg --enable-libquirc --enable-libuavs3d --enable-libxevd --enable-libzvbi --enable-liboapv --enable-libqrencode --enable-librav1e --enable-libsvtav1 --enable-libvvenc --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxeve --enable-libxvid --enable-libaom --enable-libjxl --enable-libsvtjpegxs --enable-libvpx --enable-mediafoundation --enable-libass --enable-frei0r --enable-libfreetype --enable-libfribidi --enable-libharfbuzz --enable-liblensfun --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-dxva2 --enable-d3d11va --enable-d3d12va --enable-ffnvcodec --enable-libvpl --enable-nvdec --enable-nvenc --enable-vaapi --enable-libshaderc --enable-vulkan --enable-libplacebo --enable-opencl --enable-libcdio --enable-openal --enable-libgme --enable-libmodplug --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libshine --enable-libtheora --enable-libtwolame --enable-libvo-amrwbenc --enable-libcodec2 --enable-libilbc --enable-libgsm --enable-liblc3 --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-ladspa --enable-libbs2b --enable-libflite --enable-libmysofa --enable-librubberband --enable-libsoxr --enable-chromaprint --enable-whisper
  libavutil      60. 26.101 / 60. 26.101
  libavcodec     62. 28.101 / 62. 28.101
  libavformat    62. 12.101 / 62. 12.101
  libavdevice    62.  3.101 / 62.  3.101
  libavfilter    11. 14.101 / 11. 14.101
  libswscale      9.  5.101 /  9.  5.101
  libswresample   6.  3.101 /  6.  3.101
Input #0, matroska,webm, from 'd:Mediavideo.mkv':
  Metadata:
    ENCODER         : Lavf62.12.101
  Duration: 02:22:54.90, start: 0.000000, bitrate: 23550 kb/s
  Chapters:
    Chapter #0:0: start 0.000000, end 482.315167
    Chapter #0:1: start 482.315167, end 709.166792
    Chapter #0:2: start 709.166792, end 918.751167
    Chapter #0:3: start 918.751167, end 1233.482250
    Chapter #0:4: start 1233.482250, end 1384.257875
    Chapter #0:5: start 1384.257875, end 1779.444333
    Chapter #0:6: start 1779.444333, end 2243.991750
    Chapter #0:7: start 2243.991750, end 2567.982083
    Chapter #0:8: start 2567.982083, end 3022.269250
    Chapter #0:9: start 3022.269250, end 3664.661000
    Chapter #0:10: start 3664.661000, end 4445.107333
    Chapter #0:11: start 4445.107333, end 5281.234292
    Chapter #0:12: start 5281.234292, end 5900.561333
    Chapter #0:13: start 5900.561333, end 6340.167167
    Chapter #0:14: start 6340.167167, end 6675.085083
    Chapter #0:15: start 6675.085083, end 7067.769042
    Chapter #0:16: start 7067.769042, end 7478.679542
    Chapter #0:17: start 7478.679542, end 7779.396625
    Chapter #0:18: start 7779.396625, end 7990.691042
    Chapter #0:19: start 7990.691042, end 8574.899667
  Stream #0:0: Video: h264 (High), yuv420p(progressive), 1920x1080 [SAR 1:1 DAR 16:9], 23.98 fps, 23.98 tbr, 1k tbn
    Metadata:
      DURATION        : 02:22:54.899000000
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> hevc (libx265))
Press [q] to stop, [?] for help
x265 [info]: HEVC encoder version 4.2+3-3f4120d
x265 [info]: build info [Windows][GCC 15.2.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-4 (Main tier)
x265 [info]: Thread pool created using 16 threads
x265 [info]: Slices                              : 1
x265 [info]: frame threads / pool features       : 4 / wpp(34 rows)
x265 [warning]: Gop-lookahead cannot be greater than (rc-lookahead - length of the mini-gop); Clipping gop-lookahead to 2
x265 [info]: Coding QT: max CU size, min CU size : 32 / 8
x265 [info]: Residual QT: max TU size, max depth : 32 / 1 inter / 1 intra
x265 [info]: HME L0,1,2 / range / subpel / merge : hex, umh, umh / 48 / 1 / 1
x265 [info]: Keyframe min / max / scenecut / bias  : 23 / 240 / 40 / 5.00
x265 [info]: Lookahead / bframes / badapt        : 10 / 6 / 0
x265 [info]: b-pyramid / weightp / weightb       : 1 / 0 / 1
x265 [info]: References / ref-limit  cu / depth  : 1 / off / off
x265 [info]: AQ: mode / str / qg-size / cu-tree  : 4 / 0.0 / 32 / 1
x265 [info]: Rate Control / qCompress            : ABR-12000 kbps / 0.60
x265 [info]: tools: rd=2 psy-rd=2.00 early-skip rskip mode=1 signhide tmvp
x265 [info]: tools: fast-intra lslices=6 deblock stats-write dhdr10-info
Output #0, null, to 'NUL':
  Metadata:
    encoder         : Lavf62.12.101
  Chapters:
    Chapter #0:0: start 0.000000, end 482.315167
    Chapter #0:1: start 482.315167, end 709.166792
    Chapter #0:2: start 709.166792, end 918.751167
    Chapter #0:3: start 918.751167, end 1233.482250
    Chapter #0:4: start 1233.482250, end 1384.257875
    Chapter #0:5: start 1384.257875, end 1779.444333
    Chapter #0:6: start 1779.444333, end 2243.991750
    Chapter #0:7: start 2243.991750, end 2567.982083
    Chapter #0:8: start 2567.982083, end 3022.269250
    Chapter #0:9: start 3022.269250, end 3664.661000
    Chapter #0:10: start 3664.661000, end 4445.107333
    Chapter #0:11: start 4445.107333, end 5281.234292
    Chapter #0:12: start 5281.234292, end 5900.561333
    Chapter #0:13: start 5900.561333, end 6340.167167
    Chapter #0:14: start 6340.167167, end 6675.085083
    Chapter #0:15: start 6675.085083, end 7067.769042
    Chapter #0:16: start 7067.769042, end 7478.679542
    Chapter #0:17: start 7478.679542, end 7779.396625
    Chapter #0:18: start 7779.396625, end 7990.691042
    Chapter #0:19: start 7990.691042, end 8574.899667
  Stream #0:0: Video: hevc, yuv420p(tv, progressive), 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 12000 kb/s, 23.98 fps, 23.98 tbn
    Metadata:
      encoder         : Lavc62.28.101 libx265
      DURATION        : 02:22:54.899000000
    Side data:
      CPB properties: bitrate max/min/avg: 0/0/12000000 buffer size: 0 vbv_delay: N/A
PS D:PortableProgramsffmpeg-8.1.1-full_buildbin> 4 bitrate=N/A speed=2.48x elapsed=0:55:38.28

Note: got tired of letting 1st pass run for >4hrs with -preset veryslow just to find I couldn’t use 2nd pass so I switch to -preset superfast for testing. Both fail in the same manner.

These same settings worked on 4 other files so far. I thought “well I guess these two not working files are corrupted” but 1st I can play them back without issue and 2nd I was able to encode them fully with hardware encode command:

.ffmpeg -i "d:Mediavideo.mkv" -map 0:0 -map_metadata -1 -c:v hevc_amf -quality 0 -latency false -rc 5 -b:v 12000k -high_motion_quality_boost_enable true -gops_per_idr 240 -preencode true -vbaq true out.mkv

but the above output video is 5.8Mbps instead of the requested 12Mbps and it looks bad: I’m trying to preserve image quality as much as possible with half the original bit rate.

QUESTION:

Any suggestions on how to fix the 1st pass ending prematurely issue on libx265?