[FFmpeg-user] Set target file size

Takashi ffmpeg-list at dubistmeinheld.de
Tue May 27 12:49:45 CEST 2014


I would like to convert a file to a given target file size (quality is
less important than file size in this case).

As far as I understand, the best approach for this is to use two pass
encoding. I followed the instructions here:
http://trac.ffmpeg.org/wiki/x264EncodingGuide (Two-pass Example)

The problem is that my converted file is always smaller than intended.

How can I fix that?

Example calculation (target file size = 8MB):
(8 MB * 8192 [converts MB to kilobits]) / 55 seconds = ~1192 kilobits/s
total bitrate
1192k - 128k (desired audio bitrate) = 1064k video bitrate

Source:
# ls -s Promo\ video\ for\ Android.mp4
3420 Promo video for Android.mp4
After conversion:
# ls -s output.mp4
7848 output.mp4

# ffmpeg -y -i Promo\ video\ for\ Android.mp4 -c:v libx264 -preset
medium -b:v 1064k -pass 1 -an -f mp4 /dev/null && ffmpeg -i Promo\
video\ for\ Android.mp4 -c:v libx264 -preset medium -b:v 1064k -pass 2
-c:a libfaac -b:a 128k output.mp4
ffmpeg version N-63468-ge64e079 Copyright (c) 2000-2014 the FFmpeg
developers
  built on May 27 2014 12:31:00 with gcc 4.8 (SUSE Linux)
  configuration: --enable-gpl --enable-version3 --enable-libopenjpeg
--enable-nonfree --enable-postproc --enable-libfaac --enable-libmp3lame
--enable-libopus --enable-libopencore-amrnb --enable-libopencore-amrwb
--enable-libvorbis --enable-libtheora --enable-libvpx --enable-libspeex
--enable-libxvid --enable-libx264 --disable-network --disable-stripping
--enable-libschroedinger --enable-libgsm --enable-avfilter
--enable-pthreads --disable-ffserver --extra-cflags=-I/usr/local/include
--extra-ldflags=-L/usr/local/lib --disable-protocol=httpproxy
--enable-bzlib --enable-libvpx --enable-zlib
  libavutil      52. 87.100 / 52. 87.100
  libavcodec     55. 65.100 / 55. 65.100
  libavformat    55. 41.100 / 55. 41.100
  libavdevice    55. 13.101 / 55. 13.101
  libavfilter     4.  5.100 /  4.  5.100
  libswscale      2.  6.100 /  2.  6.100
  libswresample   0. 19.100 /  0. 19.100
  libpostproc    52.  3.100 / 52.  3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'Promo video for Android.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    creation_time   : 1970-01-01 00:00:00
    encoder         : Lavf53.3.1
  Duration: 00:00:55.33, start: 0.000000, bitrate: 505 kb/s
    Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p,
432x320, 374 kb/s, 15 fps, 15 tbr, 15 tbn, 30 tbc (default)
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo,
fltp, 127 kb/s (default)
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : SoundHandler
[libx264 @ 0x379f760] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
[libx264 @ 0x379f760] profile Main, level 2.1
[libx264 @ 0x379f760] 264 - core 142 r2431 ac76440 - H.264/MPEG-4 AVC
codec - Copyleft 2003-2014 - http://www.videolan.org/x264.html -
options: cabac=1 ref=1 deblock=1:0:0 analyse=0x1:0 me=dia subme=2 psy=1
psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=0
cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=12
lookahead_threads=2 sliced_threads=0 nr=0 decimate=1 interlaced=0
bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1
b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250
keyint_min=15 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=abr
mbtree=1 bitrate=1064 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4
ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to '/dev/null':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf55.41.100
    Stream #0:0(und): Video: h264 (libx264) ([33][0][0][0] / 0x0021),
yuv420p, 432x320, q=-1--1, pass 1, 1064 kb/s, 15 fps, 15360 tbn, 15 tbc
(default)
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : VideoHandler
      encoder         : Lavc55.65.100 libx264
Stream mapping:
  Stream #0:0 -> #0:0 (h264 -> libx264)
Press [q] to stop, [?] for help
frame=  830 fps=0.0 q=-1.0 Lsize=    6839kB time=00:00:55.20
bitrate=1015.0kbits/s
video:6831kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB
muxing overhead: 0.120833%
[libx264 @ 0x379f760] frame I:14    Avg QP: 2.16  size: 16132
[libx264 @ 0x379f760] frame P:526   Avg QP: 2.91  size: 10889
[libx264 @ 0x379f760] frame B:290   Avg QP: 4.35  size:  3589
[libx264 @ 0x379f760] consecutive B-frames: 48.1% 13.5%  7.6% 30.8%
[libx264 @ 0x379f760] mb I  I16..4: 38.4%  0.0% 61.6%
[libx264 @ 0x379f760] mb P  I16..4: 18.4%  0.0%  0.0%  P16..4: 32.3%
0.0%  0.0%  0.0%  0.0%    skip:49.2%
[libx264 @ 0x379f760] mb B  I16..4:  0.9%  0.0%  0.0%  B16..8: 25.8%
0.0%  0.0%  direct: 9.7%  skip:63.6%  L0:47.1% L1:35.8% BI:17.1%
[libx264 @ 0x379f760] final ratefactor: 3.77
[libx264 @ 0x379f760] coded y,uvDC,uvAC intra: 69.8% 27.0% 23.0% inter:
24.4% 12.0% 8.1%
[libx264 @ 0x379f760] i16 v,h,dc,p: 55% 30% 10%  5%
[libx264 @ 0x379f760] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 37% 29%  8%  4%  4%
 4%  4%  5%  5%
[libx264 @ 0x379f760] i8c dc,h,v,p: 79%  8% 11%  2%
[libx264 @ 0x379f760] Weighted P-Frames: Y:14.1% UV:3.6%
[libx264 @ 0x379f760] kb/s:1011.19
ffmpeg version N-63468-ge64e079 Copyright (c) 2000-2014 the FFmpeg
developers
  built on May 27 2014 12:31:00 with gcc 4.8 (SUSE Linux)
  configuration: --enable-gpl --enable-version3 --enable-libopenjpeg
--enable-nonfree --enable-postproc --enable-libfaac --enable-libmp3lame
--enable-libopus --enable-libopencore-amrnb --enable-libopencore-amrwb
--enable-libvorbis --enable-libtheora --enable-libvpx --enable-libspeex
--enable-libxvid --enable-libx264 --disable-network --disable-stripping
--enable-libschroedinger --enable-libgsm --enable-avfilter
--enable-pthreads --disable-ffserver --extra-cflags=-I/usr/local/include
--extra-ldflags=-L/usr/local/lib --disable-protocol=httpproxy
--enable-bzlib --enable-libvpx --enable-zlib
  libavutil      52. 87.100 / 52. 87.100
  libavcodec     55. 65.100 / 55. 65.100
  libavformat    55. 41.100 / 55. 41.100
  libavdevice    55. 13.101 / 55. 13.101
  libavfilter     4.  5.100 /  4.  5.100
  libswscale      2.  6.100 /  2.  6.100
  libswresample   0. 19.100 /  0. 19.100
  libpostproc    52.  3.100 / 52.  3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'Promo video for Android.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    creation_time   : 1970-01-01 00:00:00
    encoder         : Lavf53.3.1
  Duration: 00:00:55.33, start: 0.000000, bitrate: 505 kb/s
    Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p,
432x320, 374 kb/s, 15 fps, 15 tbr, 15 tbn, 30 tbc (default)
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo,
fltp, 127 kb/s (default)
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : SoundHandler
File 'output.mp4' already exists. Overwrite ? [y/N] y
[libx264 @ 0x1bca760] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
[libx264 @ 0x1bca760] profile High, level 2.1
[libx264 @ 0x1bca760] 264 - core 142 r2431 ac76440 - H.264/MPEG-4 AVC
codec - Copyleft 2003-2014 - http://www.videolan.org/x264.html -
options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7
psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1
8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2
threads=12 lookahead_threads=2 sliced_threads=0 nr=0 decimate=1
interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2
b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250
keyint_min=15 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=2pass
mbtree=1 bitrate=1064 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4
cplxblur=20.0 qblur=0.5 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'output.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf55.41.100
    Stream #0:0(und): Video: h264 (libx264) ([33][0][0][0] / 0x0021),
yuv420p, 432x320, q=-1--1, pass 2, 1064 kb/s, 15 fps, 15360 tbn, 15 tbc
(default)
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : VideoHandler
      encoder         : Lavc55.65.100 libx264
    Stream #0:1(und): Audio: aac (libfaac) ([64][0][0][0] / 0x0040),
44100 Hz, stereo, s16, 128 kb/s (default)
    Metadata:
      creation_time   : 1970-01-01 00:00:00
      handler_name    : SoundHandler
      encoder         : Lavc55.65.100 libfaac
Stream mapping:
  Stream #0:0 -> #0:0 (h264 -> libx264)
  Stream #0:1 -> #0:1 (aac -> libfaac)
Press [q] to stop, [?] for help
frame=  830 fps=382 q=-1.0 Lsize=    7846kB time=00:00:55.24
bitrate=1163.5kbits/s
video:6956kB audio:863kB subtitle:0kB other streams:0kB global
headers:0kB muxing overhead: 0.344019%
[libx264 @ 0x1bca760] frame I:14    Avg QP: 1.10  size: 18539
[libx264 @ 0x1bca760] frame P:526   Avg QP: 1.31  size: 10965
[libx264 @ 0x1bca760] frame B:290   Avg QP: 2.52  size:  3775
[libx264 @ 0x1bca760] consecutive B-frames: 48.1% 13.5%  7.6% 30.8%
[libx264 @ 0x1bca760] mb I  I16..4: 39.9%  2.9% 57.2%
[libx264 @ 0x1bca760] mb P  I16..4:  5.3%  1.4% 12.5%  P16..4: 15.7%
9.8%  3.5%  0.0%  0.0%    skip:51.6%
[libx264 @ 0x1bca760] mb B  I16..4:  0.2%  0.1%  0.8%  B16..8: 20.3%
8.1%  3.0%  direct: 3.0%  skip:64.5%  L0:59.8% L1:33.4% BI: 6.7%
[libx264 @ 0x1bca760] 8x8 transform intra:6.8% inter:12.2%
[libx264 @ 0x1bca760] coded y,uvDC,uvAC intra: 68.8% 32.8% 28.8% inter:
18.7% 10.9% 8.0%
[libx264 @ 0x1bca760] i16 v,h,dc,p: 66% 21%  7%  6%
[libx264 @ 0x1bca760] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 36% 31% 21%  2%  2%
 1%  2%  3%  4%
[libx264 @ 0x1bca760] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 36% 29%  9%  4%  4%
 4%  4%  6%  5%
[libx264 @ 0x1bca760] i8c dc,h,v,p: 75% 10% 12%  3%
[libx264 @ 0x1bca760] Weighted P-Frames: Y:14.1% UV:3.6%
[libx264 @ 0x1bca760] ref P L0: 77.7% 10.7%  8.2%  3.2%  0.2%
[libx264 @ 0x1bca760] ref B L0: 95.3%  4.3%  0.4%
[libx264 @ 0x1bca760] ref B L1: 99.2%  0.8%
[libx264 @ 0x1bca760] kb/s:1029.65


More information about the ffmpeg-user mailing list