Opened 8 years ago

Last modified 8 years ago

#6217 new defect

Ffmpeg ignores bitrate parameter when encoding with libxvid at 2nd pass

Reported by: danieldc Owned by:
Priority: normal Component: ffmpeg
Version: unspecified Keywords: ffmpeg libxvid dual pass
Cc: Blocked By:
Blocking: Reproduced by developer: no
Analyzed by developer: no

Description

Summary of the bug: with some really old version of ffmpeg I would use 2 pass encoding to convert anyfile to AVI/XVID at medium/low resolution (900k), but all newer versions of FFMPEG ignores "bitrate" parameter at "2nd pass" encoding and this result in a file bigger and at high bitrate (as if using some low quantizer).

How to reproduce:

ffmpeg-last64.exe  -i "teste.mkv" -vcodec libxvid -vtag DIVX -b:v 900k -an -pass 1 -passlogfile logfile -f rawvideo -y nul

ffmpeg-last64.exe -i "teste.mkv" -vcodec libxvid -vtag DIVX -b:v 900k -acodec libmp3lame -b:a 96k -ac 2 -pass 2 -passlogfile logfile  -f avi -y newfile.avi


FFMPEG version (N-74645-g4c39892)

Thank you,
from Brazil


1st pass:

ffmpeg-last64.exe  -v 9 -loglevel 99  -i "teste.mkv" -vcodec libxvid -vtag DIVX -b:v 900k -an -pass 1 -

passlogfile logfile -f rawvideo -y nul

ffmpeg version N-74645-g4c39892 Copyright (c) 2000-2015 the FFmpeg developers
  built with gcc 4.9.3 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-av
isynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enab
le-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --
enable-libdcadec --enable-libfreetype --enable-libgme --enable-libgsm --enable-l
ibilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enab
le-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --en
able-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --ena
ble-libtwolame --enable-libvidstab --enable-libvo-aacenc --enable-libvo-amrwbenc
 --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enabl
e-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-lzma --ena
ble-decklink --enable-zlib
  libavutil      54. 31.100 / 54. 31.100
  libavcodec     56. 58.100 / 56. 58.100
  libavformat    56. 40.101 / 56. 40.101
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 37.100 /  5. 37.100
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  2.101 /  1.  2.101
  libpostproc    53.  3.100 / 53.  3.100
Splitting the commandline.
Reading option '-v' ... matched as option 'v' (set logging level) with argument '9'.
Reading option '-loglevel' ... matched as option 'loglevel' (set logging level) with argument '99'.
Reading option '-i' ... matched as input file with argument 'teste.mkv'.
Reading option '-vcodec' ... matched as option 'vcodec' (force video codec ('copy' to copy stream)) with argument 

'libxvid'.
Reading option '-vtag' ... matched as option 'vtag' (force video tag/fourcc) with argument 'DIVX'.
Reading option '-b:v' ... matched as option 'b' (video bitrate (please use -b:v)) with argument '900k'.
Reading option '-an' ... matched as option 'an' (disable audio) with argument '1'.
Reading option '-pass' ... matched as option 'pass' (select the pass number (1 to 3)) with argument '1'.
Reading option '-passlogfile' ... matched as option 'passlogfile' (select two pass log file name prefix) with 

argument 'logfile'.
Reading option '-f' ... matched as option 'f' (force format) with argument 'rawvideo'.
Reading option '-y' ... matched as option 'y' (overwrite output files) with argument '1'.
Reading option 'nul' ... matched as output file.
Finished splitting the commandline.

Parsing a group of options: global .
Applying option v (set logging level) with argument 9.
Applying option y (overwrite output files) with argument 1.
Successfully parsed a group of options.
Parsing a group of options: input file teste.mkv.
Successfully parsed a group of options.
Opening an input file: teste.mkv.
Probing matroska,webm score:100 size:2048
Probing mp3 score:1 size:2048

[matroska,webm @ 0000000003140280] Format matroska,webm probed with size=2048 and score=100
[matroska,webm @ 0000000003140280] Unknown entry 0x55B0
st:0 removing common factor 1000000 from timebase
st:1 removing common factor 1000000 from timebase
[matroska,webm @ 0000000003140280] Before avformat_find_stream_info() pos: 5227 bytes read:32768 seeks:0
[h264 @ 0000000003141d60] no picture
[matroska,webm @ 0000000003140280] All info found
[matroska,webm @ 0000000003140280] 0: start_time: 0.000 duration: -9223372036854.775
[matroska,webm @ 0000000003140280] 1: start_time: 0.000 duration: -9223372036854.775
[matroska,webm @ 0000000003140280] stream: start_time: 0.000 duration: 30.003 bitrate=1212 kb/s
[matroska,webm @ 0000000003140280] After avformat_find_stream_info() pos: 7061 bytes read:32768 seeks:0 frames:9
Input #0, matroska,webm, from 'teste.mkv':
  Metadata:
    ENCODER         : Lavf57.63.100
  Duration: 00:00:30.00, start: 0.000000, bitrate: 1212 kb/s
    Stream #0:0, 4, 1/1000: Video: h264 (High), 4 reference frames, yuv420p(left), 720x404 (720x416) [SAR 1:1 DAR 

180:101], 1/48, 24 fps, 24 tbr, 1k tbn, 48 tbc (default)
    Metadata:
      title           : Video
      ENCODER         : Lavc57.75.100 libx264
      DURATION        : 00:00:30.003000000
    Stream #0:1, 5, 1/1000: Audio: vorbis, 44100 Hz, stereo, fltp (default)
    Metadata:
      title           : Audio
      ENCODER         : Lavc57.75.100 libvorbis
      DURATION        : 00:00:30.003000000
Successfully opened the file.
Parsing a group of options: output file nul.
Applying option vcodec (force video codec ('copy' to copy stream)) with argument libxvid.
Applying option vtag (force video tag/fourcc) with argument DIVX.
Applying option b:v (video bitrate (please use -b:v)) with argument 900k.
Applying option an (disable audio) with argument 1.
Applying option pass (select the pass number (1 to 3)) with argument 1.
Applying option passlogfile (select two pass log file name prefix) with argument logfile.
Applying option f (force format) with argument rawvideo.
Successfully parsed a group of options.

Opening an output file: nul.
Successfully opened the file.

detected 4 logical cores
[graph 0 input from stream 0:0 @ 0000000003149700] Setting 'video_size' to value '720x404'
[graph 0 input from stream 0:0 @ 0000000003149700] Setting 'pix_fmt' to value '0'
[graph 0 input from stream 0:0 @ 0000000003149700] Setting 'time_base' to value'1/1000'
[graph 0 input from stream 0:0 @ 0000000003149700] Setting 'pixel_aspect' to value '1/1'
[graph 0 input from stream 0:0 @ 0000000003149700] Setting 'sws_param' to value'flags=2'
[graph 0 input from stream 0:0 @ 0000000003149700] Setting 'frame_rate' to value '24/1'
[graph 0 input from stream 0:0 @ 0000000003149700] w:720 h:404 pixfmt:yuv420p tb:1/1000 fr:24/1 sar:1/1 

sws_param:flags=2
[format @ 00000000031e9f40] compat: called with args=[yuv420p]
[format @ 00000000031e9f40] Setting 'pix_fmts' to value 'yuv420p'
[AVFilterGraph @ 00000000031eb320] query_formats: 4 queried, 3 merged, 0 already done, 0 delayed
Output #0, rawvideo, to 'nul':
  Metadata:
    encoder         : Lavf56.40.101
    Stream #0:0, 0, 1/24: Video: mpeg4 (libxvid), 1 reference frame (DIVX / 0x58
564944), yuv420p(left), 720x404 [SAR 1:1 DAR 180:101], 1/24, q=2-31, pass 1, 900
 kb/s, 24 fps, 24 tbn, 24 tbc (default)
    Metadata:
      title           : Video
      DURATION        : 00:00:30.003000000
      encoder         : Lavc56.58.100 libxvid
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> mpeg4 (libxvid))
Press [q] to stop, [?] for help
[h264 @ 0000000005b12200] no picture
[h264 @ 0000000005a51d60] no picture
*** 1 dup!
frame=  160 fps=0.0 q=2.0 size=    1079kB time=00:00:06.66 bitrate=1326.2kbits/s
frame=  272 fps=271 q=2.0 size=    3358kB time=00:00:11.33 bitrate=2427.5kbits/s
frame=  408 fps=271 q=2.0 size=    4794kB time=00:00:17.00 bitrate=2309.9kbits/s
frame=  561 fps=280 q=2.0 size=    5579kB time=00:00:23.37 bitrate=1955.3kbits/s
[output stream 0:0 @ 00000000031e9aa0] EOF on sink link output stream 0:0:defaul
t.
No more output streams to write to, finishing.
frame=  720 fps=292 q=2.0 Lsize=    6396kB time=00:00:30.00 bitrate=1746.5kbits/s dup=1 drop=0
video:6396kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxingoverhead: 0.000000%
Input file #0 (teste.mkv):
  Input stream #0:0 (video): 719 packets read (4107831 bytes); 719 frames decoded;
  Input stream #0:1 (audio): 5 packets read (884 bytes);
  Total: 724 packets (4108715 bytes) demuxed
Output file #0 (nul):
  Output stream #0:0 (video): 720 frames encoded; 720 packets muxed (6549199 bytes);
  Total: 720 packets (6549199 bytes) muxed
719 frames successfully decoded, 0 decoding errors
[AVIOContext @ 00000000031e80e0] Statistics: 0 seeks, 741 writeouts
[AVIOContext @ 0000000003149a40] Statistics: 4548715 bytes read, 0 seeks

2nd pass:

ffmpeg-last64.exe -v 9 -loglevel 99  -i "teste.mkv" -vcodec libxvid -vtag DIVX -b:v 900k -acodec libmp3lame -b:a 

96k -ac 2 -pass 2 -passlogfile logfile  -f avi -y newfile.avi

ffmpeg version N-74645-g4c39892 Copyright (c) 2000-2015 the FFmpeg developers
  built with gcc 4.9.3 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-

fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --

enable-libcaca --enable-libdcadec --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --

enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg 

--enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora 

--enable-libtwolame --enable-libvidstab --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --

enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-

libxvid --enable-lzma --enable-decklink --enable-zlib
  libavutil      54. 31.100 / 54. 31.100
  libavcodec     56. 58.100 / 56. 58.100
  libavformat    56. 40.101 / 56. 40.101
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 37.100 /  5. 37.100
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  2.101 /  1.  2.101
  libpostproc    53.  3.100 / 53.  3.100
Splitting the commandline.
Reading option '-v' ... matched as option 'v' (set logging level) with argument'9'.
Reading option '-loglevel' ... matched as option 'loglevel' (set logging level)with argument '99'.
Reading option '-i' ... matched as input file with argument 'teste.mkv'.
Reading option '-vcodec' ... matched as option 'vcodec' (force video codec ('copy' to copy stream)) with argument 

'libxvid'.
Reading option '-vtag' ... matched as option 'vtag' (force video tag/fourcc) with argument 'DIVX'.
Reading option '-b:v' ... matched as option 'b' (video bitrate (please use -b:v)) with argument '900k'.
Reading option '-acodec' ... matched as option 'acodec' (force audio codec ('copy' to copy stream)) with argument 

'libmp3lame'.
Reading option '-b:a' ... matched as option 'b' (video bitrate (please use -b:v)) with argument '96k'.
Reading option '-ac' ... matched as option 'ac' (set number of audio channels) with argument '2'.
Reading option '-pass' ... matched as option 'pass' (select the pass number (1 to 3)) with argument '2'.
Reading option '-passlogfile' ... matched as option 'passlogfile' (select two pass log file name prefix) with 

argument 'logfile'.
Reading option '-f' ... matched as option 'f' (force format) with argument 'avi'.
Reading option '-y' ... matched as option 'y' (overwrite output files) with argument '1'.
Reading option 'newfile.avi' ... matched as output file.
Finished splitting the commandline.

Parsing a group of options: global .
Applying option v (set logging level) with argument 9.
Applying option y (overwrite output files) with argument 1.
Successfully parsed a group of options.

Parsing a group of options: input file teste.mkv.
Successfully parsed a group of options.
Opening an input file: teste.mkv.
Probing matroska,webm score:100 size:2048
Probing mp3 score:1 size:2048
[matroska,webm @ 00000000031d0820] Format matroska,webm probed with size=2048 and score=100
[matroska,webm @ 00000000031d0820] Unknown entry 0x55B0st:0 removing common factor 1000000 from timebasest:1 

removing common factor 1000000 from timebase
[matroska,webm @ 00000000031d0820] Before avformat_find_stream_info() pos: 5227bytes read:32768 seeks:0
[h264 @ 00000000031e4820] no picture
[matroska,webm @ 00000000031d0820] All info found
[matroska,webm @ 00000000031d0820] 0: start_time: 0.000 duration: -9223372036854.775
[matroska,webm @ 00000000031d0820] 1: start_time: 0.000 duration: -9223372036854.775
[matroska,webm @ 00000000031d0820] stream: start_time: 0.000 duration: 30.003 bitrate=1212 kb/s
[matroska,webm @ 00000000031d0820] After avformat_find_stream_info() pos: 7061 bytes read:32768 seeks:0 frames:9
Input #0, matroska,webm, from 'teste.mkv':
  Metadata:
    ENCODER         : Lavf57.63.100
  Duration: 00:00:30.00, start: 0.000000, bitrate: 1212 kb/s
    Stream #0:0, 4, 1/1000: Video: h264 (High), 4 reference frames, yuv420p(left), 720x404 (720x416) [SAR 1:1 DAR 

180:101], 1/48, 24 fps, 24 tbr, 1k tbn, 48 tbc (default)
    Metadata:
      title           : Video
      ENCODER         : Lavc57.75.100 libx264
      DURATION        : 00:00:30.003000000
    Stream #0:1, 5, 1/1000: Audio: vorbis, 44100 Hz, stereo, fltp (default)
    Metadata:
      title           : Audio
      ENCODER         : Lavc57.75.100 libvorbis
      DURATION        : 00:00:30.003000000
Successfully opened the file.

Parsing a group of options: output file newfile.avi.
Applying option vcodec (force video codec ('copy' to copy stream)) with argument libxvid.
Applying option vtag (force video tag/fourcc) with argument DIVX.
Applying option b:v (video bitrate (please use -b:v)) with argument 900k.
Applying option acodec (force audio codec ('copy' to copy stream)) with argument libmp3lame.
Applying option b:a (video bitrate (please use -b:v)) with argument 96k.
Applying option ac (set number of audio channels) with argument 2.
Applying option pass (select the pass number (1 to 3)) with argument 2.
Applying option passlogfile (select two pass log file name prefix) with argument logfile.
Applying option f (force format) with argument avi.
Successfully parsed a group of options.

Opening an output file: newfile.avi.
[AVIOContext @ 00000000031d91e0] Statistics: 17370 bytes read, 0 seeks
Successfully opened the file.
detected 4 logical cores
[graph 0 input from stream 0:0 @ 000000000592f4c0] Setting 'video_size' to value '720x404'
[graph 0 input from stream 0:0 @ 000000000592f4c0] Setting 'pix_fmt' to value '0'
[graph 0 input from stream 0:0 @ 000000000592f4c0] Setting 'time_base' to value'1/1000'
[graph 0 input from stream 0:0 @ 000000000592f4c0] Setting 'pixel_aspect' to value '1/1'
[graph 0 input from stream 0:0 @ 000000000592f4c0] Setting 'sws_param' to value'flags=2'
[graph 0 input from stream 0:0 @ 000000000592f4c0] Setting 'frame_rate' to value '24/1'
[graph 0 input from stream 0:0 @ 000000000592f4c0] w:720 h:404 pixfmt:yuv420p tb:1/1000 fr:24/1 sar:1/1 

sws_param:flags=2
[format @ 0000000005927740] compat: called with args=[yuv420p]
[format @ 0000000005927740] Setting 'pix_fmts' to value 'yuv420p'
[AVFilterGraph @ 00000000031d92e0] query_formats: 4 queried, 3 merged, 0 already done, 0 delayed
[graph 1 input from stream 0:1 @ 000000000592d620] Setting 'time_base' to value'1/44100'
[graph 1 input from stream 0:1 @ 000000000592d620] Setting 'sample_rate' to value '44100'
[graph 1 input from stream 0:1 @ 000000000592d620] Setting 'sample_fmt' to value 'fltp'
[graph 1 input from stream 0:1 @ 000000000592d620] Setting 'channel_layout' to value '0x3'
[graph 1 input from stream 0:1 @ 000000000592d620] tb:1/44100 samplefmt:fltp samplerate:44100 chlayout:0x3
[audio format for output stream 0:1 @ 000000000327a900] Setting 'sample_fmts' to value 's32p|fltp|s16p'
[audio format for output stream 0:1 @ 000000000327a900] Setting 'sample_rates' to value '44100|48000|32000|

22050|24000|16000|11025|12000|8000'
[audio format for output stream 0:1 @ 000000000327a900] Setting 'channel_layouts' to value '0x3'
[AVFilterGraph @ 0000000005925920] query_formats: 4 queried, 9 merged, 0 already done, 0 delayed

Output #0, avi, to 'newfile.avi':
  Metadata:
    ISFT            : Lavf56.40.101
    Stream #0:0, 0, 1/24: Video: mpeg4 (libxvid), 1 reference frame (DIVX / 0x58564944), yuv420p(left), 720x404 

[SAR 1:1 DAR 180:101], 1/24, q=2-31, pass 2, 900 kb/s, 24 fps, 24 tbn, 24 tbc (default)
    Metadata:
      title           : Video
      DURATION        : 00:00:30.003000000
      encoder         : Lavc56.58.100 libxvid
    Stream #0:1, 0, 32/1225: Audio: mp3 (libmp3lame) (U[0][0][0] / 0x0055), 4410
0 Hz, stereo, fltp, 96 kb/s (default)
    Metadata:
      title           : Audio
      DURATION        : 00:00:30.003000000
      encoder         : Lavc56.58.100 libmp3lame

Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> mpeg4 (libxvid))
  Stream #0:1 -> #0:1 (vorbis (native) -> mp3 (libmp3lame))

Press [q] to stop, [?] for help
[h264 @ 0000000005a6e0e0] no picture
[h264 @ 0000000005a6ed80] no picture
frame=  108 fps=0.0 q=2.0 size=     471kB time=00:00:04.59 bitrate= 840.1kbits/s
frame=  184 fps=183 q=2.0 size=    1913kB time=00:00:07.75 bitrate=2019.7kbits/s
frame=  253 fps=168 q=2.0 size=    3222kB time=00:00:10.63 bitrate=2482.9kbits/s
frame=  346 fps=172 q=2.0 size=    4239kB time=00:00:14.49 bitrate=2395.4kbits/s
frame=  441 fps=176 q=2.0 size=    5125kB time=00:00:18.44 bitrate=2276.4kbits/s
frame=  544 fps=181 q=2.0 size=    5658kB time=00:00:22.75 bitrate=2037.1kbits/s
frame=  648 fps=185 q=2.0 size=    6210kB time=00:00:27.08 bitrate=1878.0kbits/s

[output stream 0:1 @ 0000000005930060] EOF on sink link output stream 0:1:default.
[output stream 0:0 @ 0000000005927680] EOF on sink link output stream 0:0:default.
No more output streams to write to, finishing.

[libmp3lame @ 00000000059e51a0] Trying to remove 687 more samples than there are in the queue
frame=  719 fps=185 q=2.0 Lsize=    6643kB time=00:00:30.01 bitrate=1813.0kbits/s
video:6236kB audio:352kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.828692%

Input file #0 (teste.mkv):
  Input stream #0:0 (video): 719 packets read (4107831 bytes); 719 frames decoded;
  Input stream #0:1 (audio): 1538 packets read (419597 bytes); 1537 frames decoded (1323008 samples);
  Total: 2257 packets (4527428 bytes) demuxed

Output file #0 (newfile.avi):
  Output stream #0:0 (video): 719 frames encoded; 719 packets muxed (6385808 bytes);
  Output stream #0:1 (audio): 1149 frames encoded (1323008 samples); 1150 packets muxed (360490 bytes);
  Total: 1869 packets (6746298 bytes) muxed

2256 frames successfully decoded, 0 decoding errors

[AVIOContext @ 00000000031d91e0] Statistics: 12 seeks, 1892 writeouts
[AVIOContext @ 00000000031d9fe0] Statistics: 4548715 bytes read, 0 seeks

Change History (3)

comment:1 by edumj, 8 years ago

Hi,

it also happens to me, and only works fine with mpeg4 library (not libxvid) or only with older versions from 2014!

comment:2 by edumj, 8 years ago

And it gets worse without -g 250 (using default gop size and bitrate 1500k, results in 2617k! with a maxium Peak of 19524k!!):

libxvid 1500k 2 Pass -g 250 -> Bitrate 1917k! and Peak 18793k!
libxvid 1500k 2 Pass (-g 12)-> Bitrate 2617k!! and Peak 19524k!

But, only with 1 pass and a normal gop of 250, it doesn't reach desired bitrate at all:

libxvid 2000k 1 Pass -g 250 -> Bitrate 1572k!
libxvid 2000k 1 Pass (-g 12) -> Bitrate 1969k

comment:3 by edumj, 8 years ago

Well, 2014 versions also lacks bitrate enforcement with 2 pass, but there was no such great difference between desired bitrate and the resulting one:

libxvid (2017) 1.111k 2 pass (-g 12) -> Bitrate 2.875k!! and Peak 23.133k!!!!
libxvid (2014) 1.111k 2 pass (-g 12) -> Bitrate 1.466k! and Peak 9.671k

And, with a gop of 250:

libxvid (2017) 1.111k 2 pass -g 250 -> Bitrate 2.201k!! and Peak 22.937k!!!!
libxvid (2014) 1.111k 2 pass -g 250 -> Bitrate 1.283k! and Peak 11.854k!

Also, any ideas how to control maximum bitrate peaks?

Thanks.

Note: See TracTickets for help on using tickets.