Opened 2 years ago
Last modified 14 months ago
#10830 new defect
Bug: MPEG-1/2 encoder output video missing end code
| Reported by: | Nebulas | Owned by: | |
|---|---|---|---|
| Priority: | wish | Component: | avcodec |
| Version: | git-master | Keywords: | mpegvideo |
| Cc: | Blocked By: | ||
| Blocking: | Reproduced by developer: | no | |
| Analyzed by developer: | no |
Description
Summary of the bug:
When the MPEG-1/MPEG-2 (PS) video stream file (*.m1v/*.m2v) is encoded by the mpegvideo encoder, the end code of the video sequence (sequence_end_code, As defined in ISO/IEC 11172-2 2.4.2.2, or ISO/IEC 13818-2 6.2.2, “the bit string '000001B7' in hexadecimal”) in the output file is missing.
How to reproduce:
% ffmpeg -i in.mp4 -an -c:v mpeg1video out.m1v
or
% ffmpeg -i in.mp4 -an out.m2v
ffmpeg version:
Windows builts from gyan.dev, “ffmpeg-2024-01-24-git-00b288da73-full_build”
Change History (3)
comment:1 by , 2 years ago
| Version: | unspecified → git-master |
|---|
comment:2 by , 2 years ago
| Component: | undetermined → avcodec |
|---|---|
| Priority: | important → wish |
Note:
See TracTickets
for help on using tickets.



I just wanted to note that i also came across this issue while i was re-encoding a demuxed Playstation 2 JRPG .m2v video. It was quite difficult to figure this out, since 'Vegas Pro 22' was able to properly re-encode the MPEG-2 .m2v with the sequence_end_code.
This "bug" causes the video in an Playstation 2 JRPG to stay "black" and not close properly, so the player has to click a button to "skip" the video and go back to the actual game.
Vegas Pro 22 added the sequence_end_code and the video properly closed itself.
So, the bug still exists in the latest FFmpeg version. For reference, these are the commands i used to re-encode the video and then add the missing bytes:
Write-Host "`n[i] Add subtitles to 00000006_video_0.m2v" cmd.exe /C "ffmpeg.exe" -nostdin -i "00000006_video_0.m2v" -vf subtitles=00000006.MOVIE.srt -c mpeg2video -b:v 6342k -minrate 5700k -maxrate 15000k -bufsize 1835008 -s 640x448 -r 30000/1001 -g 18 -bf 2 -sc_threshold 1G -pix_fmt yuv420p -profile:v main -level:v main -color_primaries smpte170m -color_trc smpte170m -colorspace smpte170m -intra_matrix 16,17,17,18,18,18,19,19,19,19,20,20,20,20,20,21,21,21,21,21,21,22,22,22,22,22,22,22,23,23,23,23,23,23,23,23,24,24,24,25,24,24,24,25,26,26,26,26,25,27,27,27,27,27,28,28,28,28,30,30,30,31,31,33 -video_format ntsc -f mpeg2video "00000006_subtitled.m2v" Write-Host "`n[i] Add missing mpeg2 sequence end code 0x00 0x00 0x01 0xB7 at the end of the file" $file = "00000006_subtitled.m2v" Write-Output "File size = $((Get-Item $file).Length) bytes"; ($fs = [System.IO.File]::OpenRead($file)).Seek(-4, [System.IO.SeekOrigin]::End) | Out-Null; $b = [byte[]]::new(4); $fs.Read($b, 0, 4) | Out-Null; Write-Output ($b | ForEach-Object { $_.ToString('X2') } | Join-String -Separator ' '); $fs.Close() ($fs = [System.IO.File]::OpenWrite($file)).Seek(-4, [System.IO.SeekOrigin]::End) | Out-Null; $fs.Write(([byte[]]@(0x00,0x00,0x01,0xB7)), 0, 4); $fs.Close() Write-Output "File size = $((Get-Item $file).Length) bytes"; ($fs = [System.IO.File]::OpenRead($file)).Seek(-4, [System.IO.SeekOrigin]::End) | Out-Null; $b = [byte[]]::new(4); $fs.Read($b, 0, 4) | Out-Null; Write-Output ($b | ForEach-Object { $_.ToString('X2') } | Join-String -Separator ' '); $fs.Close()[i] Add subtitles to 00000006_video_0.m2v ffmpeg version 2025-05-26-git-43a69886b2-essentials_build-www.gyan.dev Copyright (c) 2000-2025 the FFmpeg developers built with gcc 15.1.0 (Rev4, 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-bzlib --enable-lzma --enable-zlib --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-sdl2 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx --enable-mediafoundation --enable-libass --enable-libfreetype --enable-libfribidi --enable-libharfbuzz --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-openal --enable-libgme --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libtheora --enable-libvo-amrwbenc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-librubberband libavutil 60. 3.100 / 60. 3.100 libavcodec 62. 3.101 / 62. 3.101 libavformat 62. 0.102 / 62. 0.102 libavdevice 62. 0.100 / 62. 0.100 libavfilter 11. 0.100 / 11. 0.100 libswscale 9. 0.100 / 9. 0.100 libswresample 6. 0.100 / 6. 0.100 Input #0, mpegvideo, from 'C:\a\00000006_video_0.m2v': Duration: N/A, bitrate: N/A Stream #0:0: Video: mpeg2video (mpeg1video) (Main), yuv420p(tv, smpte170m, progressive), 640x448 [SAR 1:1 DAR 10:7], 25 fps, 29.97 tbr, 1200k tbn Side data: cpb: bitrate max/min/avg: 15000000/0/0 buffer size: 1835008 vbv_delay: N/A [Parsed_subtitles_0 @ 0000026d4eff4880] libass API version: 0x1703020 [Parsed_subtitles_0 @ 0000026d4eff4880] libass source: commit: 0.17.3-81-g695509365f152bd28720a0c0e036d46836ee9345 [Parsed_subtitles_0 @ 0000026d4eff4880] Shaper: FriBidi 1.0.16 (SIMPLE) HarfBuzz-ng 11.2.1 (COMPLEX) [Parsed_subtitles_0 @ 0000026d4eff4880] Using font provider directwrite (with GDI) Stream mapping: Stream #0:0 -> #0:0 (mpeg2video (native) -> mpeg2video (native)) [Parsed_subtitles_0 @ 0000026d50d71e00] libass API version: 0x1703020 [Parsed_subtitles_0 @ 0000026d50d71e00] libass source: commit: 0.17.3-81-g695509365f152bd28720a0c0e036d46836ee9345 [Parsed_subtitles_0 @ 0000026d50d71e00] Shaper: FriBidi 1.0.16 (SIMPLE) HarfBuzz-ng 11.2.1 (COMPLEX) [Parsed_subtitles_0 @ 0000026d50d71e00] Using font provider directwrite (with GDI) [mpeg2video @ 0000026d4eff3b80] Warning min_rate > 0 but min_rate != max_rate isn't recommended! Output #0, mpeg2video, to 'C:\a\00000006_subtitled.m2v': Metadata: encoder : Lavf62.0.102 Stream #0:0: Video: mpeg2video (Main), yuv420p(tv, smpte170m, progressive), 640x448 [SAR 1:1 DAR 10:7], q=2-31, 6342 kb/s, 29.97 fps, 29.97 tbn Metadata: encoder : Lavc62.3.101 mpeg2video Side data: cpb: bitrate max/min/avg: 15000000/5700000/6342000 buffer size: 1835008 vbv_delay: N/A [Parsed_subtitles_0 @ 0000026d50d71e00] fontselect: (Arial, 400, 0) -> ArialMT, 0, ArialMT frame= 416 fps=0.0 q=2.0 size= 9984KiB time=00:00:13.84 bitrate=5906.5kbits/s speed=26.7x elapsed=0:00:00.51 frame= 816 fps=783 q=2.5 size= 20480KiB time=00:00:27.19 bitrate=6169.5kbits/s speed=26.1x elapsed=0:00:01.04 frame= 1363 fps=879 q=2.5 size= 34816KiB time=00:00:45.44 bitrate=6275.9kbits/s speed=29.3x elapsed=0:00:01.55 [out#0/mpeg2video @ 0000026d4efcd140] video:40377KiB audio:0KiB subtitle:0KiB other streams:0KiB global headers:0KiB muxing overhead: 0.000000% frame= 1589 fps=919 q=2.0 Lsize= 40377KiB time=00:00:52.98 bitrate=6242.6kbits/s speed=30.7x elapsed=0:00:01.72 [i] Add missing mpeg2 sequence end code 00 00 01 B7 at the end of the file File size = 41346435 bytes 00 00 00 00 File size = 41346435 bytes 00 00 01 B7