ffmpeg - h264 - multiple resolutions without scaling

2

I have an MP4 which contains an H264/AVC stream, which contains multiple resolutions.

Below is the output of mediainfo on this file:

General
Complete name                            : known.mp4
Format                                   : MPEG-4
Format profile                           : Base Media
Codec ID                                 : isom
File size                                : 661 KiB
Duration                                 : 15s 960ms
Overall bit rate                         : 340 Kbps
Writing application                      : Lavf54.49.102

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : Baseline@L2.0
Format settings, CABAC                   : No
Format settings, ReFrames                : 1 frame
Codec ID                                 : avc1
Codec ID/Info                            : Advanced Video Coding
Duration                                 : 15s 960ms
Bit rate                                 : 338 Kbps
Width                                    : 176 pixels
Original width                           : 352 pixels
Height                                   : 144 pixels
Original height                          : 288 pixels
Display aspect ratio                     : 1.222
Frame rate mode                          : Constant
Frame rate                               : 25.000 fps
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.534
Stream size                              : 659 KiB (100%)

After some research, the value Lavf54.49.102 appears to indicate that ffmpeg was used to encode this file.

Below is the output of ffplay on this file:

ffplay known.mp4 
ffplay version 2.4.3 Copyright (c) 2003-2014 the FFmpeg developers
  built on Nov  9 2014 17:21:35 with gcc 4.8 (Ubuntu 4.8.2-19ubuntu1)
  configuration: --extra-libs=-ldl --prefix=/opt/ffmpeg --enable-avresample --disable-debug --enable-nonfree --enable-gpl --enable-version3 --enable-x11grab --enable-libpulse --enable-libopencore-amrnb --enable-libopencore-amrwb --disable-decoder=amrnb --disable-decoder=amrwb --enable-libx264 --enable-libx265 --enable-libfdk-aac --enable-libvorbis --enable-libmp3lame --enable-libopus --enable-libvpx --enable-libspeex --enable-libass --enable-avisynth --enable-libsoxr --enable-libxvid
  libavutil      54.  7.100 / 54.  7.100
  libavcodec     56.  1.100 / 56.  1.100
  libavformat    56.  4.101 / 56.  4.101
  libavdevice    56.  0.100 / 56.  0.100
  libavfilter     5.  1.100 /  5.  1.100
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  0.100 /  3.  0.100
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  0.100 / 53.  0.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'known.mp4':=    0B f=0/0   
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf54.49.102
  Duration: 00:00:15.96, start: 0.000000, bitrate: 339 kb/s
    Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yuv420p, 176x144, 338 kb/s, 25 fps, 25 tbr, 1200k tbn, 2400k tbc (default)
    Metadata:
      handler_name    : VideoHandler
[h264 @ 0x7f756800b540] Reinit context to 352x288, pix_fmt: yuv420p 

Note the last line [h264 @ 0x7f756800b540] Reinit context to 352x288, pix_fmt: yuv420p; at this point ffplay changes size to match this new resoltuion.

I am currently trying to reproduce this "multiple resolution" effect, firstly on the same video by decoding and then reencoding it, and once that is working I plan to encode another video in the same way to (hopefully) achieve the same. Once that works I shall try to achieve the same with other codecs that support this.

My efforts so far, even reencoding the same video using ffmpeg, have yielded a video where one resolution is simply scaled to be the same size as the other, rather than actually encoding the stream with two different resolutions.

I've tried everything I can think of, ranging from a simple reencode to piping the output of ffmpeg back into ffmpeg, forcing the profile to baseline, converting the stream to mpg, and so on. Nothing appears to be working.

My question is, how can I preserve a resoltuion change within ffmpeg*, rather than it being scaled?

Thank you for any help in advance!


* although if the same result can be achieved with another encoder, that's good too!

OMGtechy

Posted 2014-11-11T12:01:16.080

Reputation: 285

Answers

1

The answer was not to use ffmpeg at all. It turns out that H264/AVC streams support concatination, and so simply taking two seperate streams at different resolutions and using cat works perfectly.

So, with two streams at different resolutions...

cat stream_a.h264 stream_b.h264 >> stream_c.h264

Then wrap it up in whatever container you'd like.

OMGtechy

Posted 2014-11-11T12:01:16.080

Reputation: 285

This works because the stream consists of special NAL units which indicate the frame size for the following sequence. When you wrap it in a container, these NAL units are removed and the information about when the frame size changes is put in the container's global header. I do not think x264 can switch this on the fly, for example. Further reading: http://www.szatmary.org/blog/25

– slhck – 2014-11-11T14:52:07.263

@slhck thank you, I'll read that shortly. I've now extended this to H265 and that also works fine. It seems odd though; all the MP4 muxers I've found cannot cope with two resolutions (although I did find an mkv muxer that could). – OMGtechy – 2014-11-11T16:11:42.873

It's not very common, I suppose. I have yet to see such a file and I've worked with video a lot. – slhck – 2014-11-11T16:14:54.273

@slhck Until yesterday I'd never of such things, but as I've recently started working in SQA revolving around video encoders and decoders I'm seeing all sorts...you learn something every day. That article is helpful thank you, I may even share it around the office. – OMGtechy – 2014-11-11T16:18:07.963