2

i have an android video:

$ mediainfo 20140324_192544.mp4
General
Complete name                            : 20140324_192544.mp4
Format                                   : MPEG-4
Format profile                           : Base Media
Codec ID                                 : isom
File size                                : 7.65 MiB
Duration                                 : 19s 861ms
Overall bit rate                         : 3 232 Kbps
Encoded date                             : UTC 2014-03-24 18:26:05
Tagged date                              : UTC 2014-03-24 18:26:05

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : Baseline@L1.0
Format settings, CABAC                   : No
Format settings, ReFrames                : 1 frame
Format settings, GOP                     : M=1, N=30
Codec ID                                 : avc1
Codec ID/Info                            : Advanced Video Coding
Duration                                 : 19s 780ms
Bit rate                                 : 3 112 Kbps
Width                                    : 640 pixels
Height                                   : 480 pixels
Display aspect ratio                     : 4:3
Frame rate mode                          : Variable
Frame rate                               : 30.283 fps
Minimum frame rate                       : 18.211 fps
Maximum frame rate                       : 30.405 fps
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.334
Stream size                              : 7.33 MiB (96%)
Title                                    : VideoHandle
Language                                 : English
Encoded date                             : UTC 2014-03-24 18:26:05
Tagged date                              : UTC 2014-03-24 18:26:05
Material_Duration                        : 19802
Material_StreamSize                      : 7693757
Material_FrameCount                      : 599

Audio
ID                                       : 2
Format                                   : AAC
Format/Info                              : Advanced Audio Codec
Format profile                           : LC
Codec ID                                 : 40

Duration                                 : 19s 861ms
Bit rate mode                            : Constant
Bit rate                                 : 128 Kbps
Nominal bit rate                         : 96.0 Kbps
Channel(s)                               : 1 channel
Channel positions                        : Front: C
Sampling rate                            : 48.0 KHz
Compression mode                         : Lossy
Stream size                              : 310 KiB (4%)
Title                                    : SoundHandle
Language                                 : English
Encoded date                             : UTC 2014-03-24 18:26:05
Tagged date                              : UTC 2014-03-24 18:26:05

Now, i make some agressive resize:

$ avconv -i 20140324_192544.mp4  -c:v libx264 -c:a copy -s:v 24x18 -b:v 3k 20140324_192544_2.mp4
  avconv version 0.8.10-4:0.8.10-0ubuntu0.12.04.1, Copyright (c) 2000-2013 the Libav developers
  built on Feb  6 2014 20:56:59 with gcc 4.6.3
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '20140324_192544.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 0
    compatible_brands: isom3gp4
    creation_time   : 2014-03-24 18:26:05
  Duration: 00:00:19.86, start: 0.000000, bitrate: 3231 kb/s
    Stream #0.0(eng): Video: h264 (Baseline), yuv420p, 640x480, 3108 kb/s, PAR 65536:65536 DAR 4:3, 30.25 fps, 90k tbr, 90k tbn, 180k tbc
    Metadata:
      creation_time   : 2014-03-24 18:26:05
    Stream #0.1(eng): Audio: aac, 48000 Hz, mono, s16, 128 kb/s
    Metadata:
      creation_time   : 2014-03-24 18:26:05
File '20140324_192544_2.mp4' already exists. Overwrite ? [y/N] y
[buffer @ 0x1bb3000] w:640 h:480 pixfmt:yuv420p
[scale @ 0x1baf7a0] w:640 h:480 fmt:yuv420p -> w:24 h:18 fmt:yuv420p flags:0x4
[libx264 @ 0x1bcb180] using SAR=1/1
[libx264 @ 0x1bcb180] using cpu capabilities: MMX2 SSE2Fast SSSE3 Cache64 SlowCTZ SlowAtom
[libx264 @ 0x1bcb180] profile Main, level 4.2
[libx264 @ 0x1bcb180] 264 - core 120 r2151 a3f4407 - H.264/MPEG-4 AVC codec - Copyleft 2003-2011 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x1:0x111 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=1 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=3 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=0 b_adapt=1 b_bias=0 direct=1 weightb=0 open_gop=1 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=abr mbtree=1 bitrate=3 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.25 aq=1:1.00
Output #0, mp4, to '20140324_192544_2.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 0
    compatible_brands: isom3gp4
    creation_time   : 2014-03-24 18:26:05
    encoder         : Lavf53.21.1
    Stream #0.0(eng): Video: libx264, yuv420p, 24x18 [PAR 1:1 DAR 4:3], q=-1--1, 3 kb/s, 180k tbn, 90k tbc
    Metadata:
      creation_time   : 2014-03-24 18:26:05
    Stream #0.1(eng): Audio: libvo_aacenc, 48000 Hz, mono, 128 kb/s
    Metadata:
      creation_time   : 2014-03-24 18:26:05
Stream mapping:
  Stream #0:0 -> #0:0 (h264 -> libx264)
  Stream #0:1 -> #0:1 (copy)
Press ctrl-c to stop encoding
frame=1779206 fps=2348 q=-1.0 Lsize=   44331kB time=19.77 bitrate=18370.2kbits/s dup=1778607 drop=0    
video:23072kB audio:310kB global headers:0kB muxing overhead 89.594479%
[libx264 @ 0x1bcb180] frame I:7117  Avg QP:51.00  size:    25
[libx264 @ 0x1bcb180] frame P:441244 Avg QP:51.00  size:    17
[libx264 @ 0x1bcb180] frame B:1330845 Avg QP:51.00  size:    12
[libx264 @ 0x1bcb180] consecutive B-frames:  0.0%  0.8%  0.0% 99.2%
[libx264 @ 0x1bcb180] mb I  I16..4: 100.0%  0.0%  0.0%
[libx264 @ 0x1bcb180] mb P  I16..4:  0.0%  0.0%  0.0%  P16..4:  0.0%  0.0%  0.0%  0.0%  0.0%    skip:100.0%
[libx264 @ 0x1bcb180] mb B  I16..4:  0.0%  0.0%  0.0%  B16..8:  0.0%  0.0%  0.0%  direct: 0.0%  skip:100.0%
[libx264 @ 0x1bcb180] final ratefactor: 128.61
[libx264 @ 0x1bcb180] coded y,uvDC,uvAC intra: 0.0% 38.5% 0.0% inter: 0.0% 0.0% 0.0%
[libx264 @ 0x1bcb180] i16 v,h,dc,p: 50%  0% 50%  0%
[libx264 @ 0x1bcb180] i8c dc,h,v,p: 100%  0%  0%  0%
[libx264 @ 0x1bcb180] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x1bcb180] kb/s:9560.28

And the result is a 45MB file:

$ mediainfo 20140324_192544_2.mp4
General
Complete name                            : 20140324_192544_2.mp4
Format                                   : MPEG-4
Format profile                           : Base Media
Codec ID                                 : isom
File size                                : 43.3 MiB
Duration                                 : 19s 862ms
Overall bit rate mode                    : Variable
Overall bit rate                         : 18.3 Mbps
Encoded date                             : UTC 2014-03-24 18:26:05
Tagged date                              : UTC 2014-03-24 18:26:05
Writing application                      : Lavf53.21.1

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : Main@L4.2
Format settings, CABAC                   : Yes
Format settings, ReFrames                : 3 frames
Codec ID                                 : avc1
Codec ID/Info                            : Advanced Video Coding
Duration                                 : 19s 769ms
Bit rate                                 : 9 563 Kbps
Nominal bit rate                         : 3 000 bps
Width                                    : 24 pixels
Height                                   : 18 pixels
Display aspect ratio                     : 4:3
Frame rate mode                          : Constant
Frame rate                               : 90 000.000 fps
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.246
Stream size                              : 22.5 MiB (52%)
Writing library                          : x264 core 120 r2151 a3f4407
Encoding settings                        : cabac=1 / ref=3 / deblock=1:0:0 / analyse=0x1:0x111 / me=hex / subme=7 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=0 / me_range=16 / chroma_me=1 / trellis=1 / 8x8dct=0 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=3 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=3 / b_pyramid=0 / b_adapt=1 / b_bias=0 / direct=1 / weightb=0 / open_gop=1 / weightp=2 / keyint=250 / keyint_min=25 / scenecut=40 / intra_refresh=0 / rc_lookahead=40 / rc=abr / mbtree=1 / bitrate=3 / ratetol=1.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / ip_ratio=1.25 / aq=1:1.00
Language                                 : English
Encoded date                             : UTC 2014-03-24 18:26:05
Tagged date                              : UTC 2014-03-24 18:26:05

Audio
ID                                       : 2
Format                                   : AAC
Format/Info                              : Advanced Audio Codec
Format profile                           : LC
Codec ID                                 : 40
Duration                                 : 19s 862ms
Bit rate mode                            : Variable
Bit rate                                 : 128 Kbps
Channel(s)                               : 1 channel
Channel positions                        : Front: C
Sampling rate                            : 48.0 KHz
Compression mode                         : Lossy
Stream size                              : 310 KiB (1%)
Language                                 : English
Encoded date                             : UTC 2014-03-24 18:26:05
Tagged date                              : UTC 2014-03-24 18:26:05

There should be something wrong with my parameters, right!? What I miss? Thank you.

csnemes
  • 21

2 Answers2

3

The size increase is because your output video has a framerate of over 90 fps. Don't ask me why this happens—it could very well be a bug with avconv, which already is available in a more recent version. They might have fixed it already.

If you want to reduce the file size of the original video I would recommend either downscaling the resolution (to something sane) while setting a bitrate explicitly, or just re-coding with a constant quality mode (CRF in x264), e.g.:

avconv -i input.mp4 -c:v libx264 -crf 28 -c:a copy output.mp4

CRF values of 28 or more will get you "lower quality". You have to play with these settings of course.

slhck
  • 228,104
  • It 90k fps, not 90. It tries to find an output fps that will allow it to put an output frame with a timestamp matching each input frame, so the output fps is something like the lowest common multiple of every input-frame interval. I'd also suggest using -preset slower -crf 23 or something. If you're encoding once and watching potentially multiple times, the improvement in space-vs.-quality (rate-distortion) tradeoff is worth the extra CPU time, IMO. And yeah, I'd agree that using CRF is almost always the right choice, rather than using a target bitrate (esp. without 2-pass.) – Peter Cordes Dec 26 '14 at 19:36
  • True, I had completely missed that frame rate issue. Indeed that would cause weird encoding behavior. – slhck Dec 26 '14 at 19:53
1

I think ffmpeg / avconv don't know how to put VFR h.264 into an MP4 container, so they have to duplicate a lot of frames. It uses a frame rate high enough to be able to have a frame in the output video for every frame in the input video. Since phones for some reason do true VFR, not just switching between a couple frame rates, avconv chooses 90 THOUSAND fps. Looks like the 90k came from the time-base numerator, actually. (90k tbn) I forget how ffmpeg / avconv internally store timestamps, I'd have to look at it again. And IDK if it matches how mp4 is designed.

This is why the average QP of your video is coming out to 51, since almost every frame is an exact duplicate of the one before, and there are so many frames that no matter what x264 does, a frame takes at least a few bytes. Just coding all those dup frames with every macroblock as a skip (i.e. same as before) is what's ballooning your bitrate up beyond all reason.

ffmpeg (and I assume avconv) can output VFR h.264 into a Matroska container. Your same command line with a filename ending in .mkv should do what you expect. IDK if it's then possible to use other tools to remux a VFR mkv into an mp4. mkv is a nice container format, just use it unless you need compatibility with something that annoyingly doesn't support it.

If you insist on losing some of the frame timing information by making a constant frame-rate output video, you'll need to find a different approach. Maybe a libav filter? Or maybe just using an avconv option to specify the output FPS you want. x264 can code a dup frame in only a few bytes, but if you have such a high FPS that there are a lot of dups, most of the potential reference frames will be duplicates, not actually useful. Increasing the number of consecutive b-frames is a good idea if you're feeding x264 input with many duplicate frames. It will use b frames to code the duplicates, which takes less space, and I think helps it keep non-duplicates as reference frames.

Peter Cordes
  • 5,900