1

I have a raw h264 bitstream that I'd like to put into MP4 using ffmpeg but ffmpeg always produces it with a constant frame rate and I need it to be the variable frame rate.

So lets say I have an MP4 with variable frame rate (output from MediaInfo):

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : Baseline@L4.2
Format settings                          : 1 Ref Frames
Format settings, CABAC                   : No
Format settings, Reference frames        : 1 frame
Codec ID                                 : avc1
Codec ID/Info                            : Advanced Video Coding
Duration                                 : 17 min 38 s
Bit rate                                 : 233 kb/s
Width                                    : 1 920 pixels
Height                                   : 1 080 pixels
Display aspect ratio                     : 16:9
Frame rate mode                          : Variable
Frame rate                               : 27.879 FPS
Minimum frame rate                       : 2.545 FPS
Maximum frame rate                       : 43.478 FPS
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.004
Stream size                              : 29.4 MiB (99%)
Codec configuration box                  : avcC

and I extract H264 out of it:

ffmpeg -i input.mp4 -vcodec copy -an -bsf:v h264_mp4toannexb input.h264
``

Just to make it Mp4 once again:

ffmpeg -i input.h264 -c copy output.mp4 ``

output.mp4 is now a constant frame rate:

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : Baseline@L4.2
Format settings                          : 1 Ref Frames
Format settings, CABAC                   : No
Format settings, Reference frames        : 1 frame
Codec ID                                 : avc1
Codec ID/Info                            : Advanced Video Coding
Duration                                 : 19 min 40 s
Bit rate                                 : 209 kb/s
Width                                    : 1 920 pixels
Height                                   : 1 080 pixels
Display aspect ratio                     : 16:9
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.004
Stream size                              : 29.4 MiB (100%)
Codec configuration box                  : avcC

My question is: Is it possible to force ffmpeg somehow to make it variable, the same way the source file was?

I was experimenting with ffmpeg's vsync options but with no luck.

I think I know where the problem is in general - timing information should be handled outside of h264 bitstream (i.e. via RTP Timestamps) and lets say I do have these timestamp (my app uses RTP and this is how I get the h264 bitstream) - how I can use these timestamps in such a flow to pass them to ffmpeg when creating this mp4?

Adam Szmyd
  • 2,765
  • 2
  • 24
  • 32
  • Have you seen [this Super User post](https://superuser.com/questions/908295)? – kesh Mar 10 '22 at 15:16
  • I actually did and tried a bunch of combinations but none of them leaded to producing variable framerate mp4 output. I tried i.e.: `ffmpeg -r 30 -vsync 2 -i input.h264 -c copy output.mp4`, `ffmpeg -vsync 2 -i input.h264 -c copy output.mp4` also with `-copyts` and `-fflags +genpts` - everything results in the same - CFR output. – Adam Szmyd Mar 11 '22 at 09:18
  • And ffmpeg is actually showing this warning: `[mp4 @ 0x560f0a1c4b80] Timestamps are unset in a packet for stream 0. This is deprecated and will stop working in the future. Fix your code to set the timestamps properly` and AFAIR its because raw h264 bitstream do not contain timestamps? Maybe my whole approach is bad and I should somehow get leverage of the RTP Timestamps I have but I'm not sure how to do it and create a VFR output out of such RTP video stream (btw I kinda need to keep it VFR to keep separate Audio and Video streams in sync) – Adam Szmyd Mar 11 '22 at 09:20

0 Answers0