VideoHelp Forum




+ Reply to Thread
Results 1 to 27 of 27
  1. Anonymous543
    Guest
    hello guys
    i have a input.mkv which has constant frame rate

    Image
    [Attachment 63796 - Click to enlarge]



    when am changing wrapper MKV to MP4
    with this code
    ffmpeg -i input.mkv -c copy output.mp4
    it copies all streams but it makes my output video stream's frame rate variable


    Image
    [Attachment 63797 - Click to enlarge]


    why this happening?
    Quote Quote  
  2. Member hydra3333's Avatar
    Join Date
    Oct 2009
    Location
    Australia
    Search Comp PM
    +1
    I have a similar but no doubt unrelated issue with output from v4l2 encoder=h264_v4l2m2m
    Quote Quote  
  3. The default time base for MP4 is 90000. That is not evenly divisible by a frame rate of 24000/1001. You get 90000 / 24000 * 1000 = 3753.75, ie, each frame needs to be displayed for 3753.75/90000 seconds. But frame durations can only be expressed as integers so the muxer alternates between 3753/90000 (one time) and 3754/90000 (3 times) to make an average of 3753.75.

    Actually, given the minimum frame rate of 23.564 and maximum frame rate of 24.390 some time base other than 90000 is being used (I think 16000). But the issue is the same. The muxer is alternating between two different values to achieve the exact average frame rate.

    And MKV does the same thing, it just doesn't label the file as variable frame rate. Drag/drop Batch file:

    You can use ffprobe to see the timestamps. pkt_dts is the time at which each frame is displayed (pkt_dts / timebase).

    Code:
    ffprobe -v 32 -stats -y -hide_banner -i %1 -select_streams v -print_format csv -of csv -show_entries "frame=pkt_dts" >> "%~nx1.csv"
    That gives the display time of each frame (time base not shown). The resulting CSV file can be opened in Notepad or imported into a spreadsheet.
    Last edited by jagabo; 14th Mar 2022 at 08:22.
    Quote Quote  
  4. Anonymous543
    Guest
    so how can i convert a mkv video which has constant frame rate to mp4 without messing frame rate or time code?
    Quote Quote  
  5. remux it simply and that's it
    Quote Quote  
  6. Mediainfo is wrong about "contant frame rate".

    MKV is technically always VFR for "23.976" (24000/1001) . The problem is the MKV container. The MKV container specs stipulate a timebase of 1/1000s only, so there is "jitter" and rounding errors in the timestamps. FFmpeg is copying those rounding errors to the MP4 container. (You can set the ffmpeg output MP4 container timebase to 24000 using -video_track_timescale 24000 , but it won't fix it because it copies the input MKV's already rounded timestamps)

    If you extract a 24000/1001 MKV timestamps with mkvextract, you get this. There is rounding up and down in the delta between frames. It's not really "constant" framerate, because there are 1ms rounding deviations

    Code:
    # timestamp format v2
    0
    42
    83
    125
    167
    209
    250


    In contrast, a 24000/1001 MP4 has a timebase of 1/24000s , so there are no ms rounding errors. It looks like this

    Code:
    # timecode format v2
    0
    41.7083333333333
    83.4166666666667
    125.125
    166.833333333333
    208.541666666667
    250.25

    One way to "fix" the ffmpeg issue is to use mp4fpsmod. It also supports in-place editing (modify file directly), but you could argue that it's safer to use streamcopy

    Code:
    "mp4fpsmod" "input.mp4" --fps 0:24000/1001 -o "output.mp4"
    Quote Quote  
  7. Member hydra3333's Avatar
    Join Date
    Oct 2009
    Location
    Australia
    Search Comp PM
    Hello. Similarly, with this one the input is 25fps interlaced CFR and always produces VFR and I am not sure why.
    The filter_complex stuff is just tinkering to see what happens but the result is the same VFR.
    (It always produces "CBR" but that's another issue).
    (The extra commandline options are due to a recently submitted patch but have zero effect on the outcome).
    Suggestions welcomed.

    Code:
    /usr/local/bin/ffmpeg -hide_banner -stats -v debug \
    	-sws_flags lanczos+accurate_rnd+full_chroma_int+full_chroma_inp \
    	-fflags +igndts \
    	-i "./some_test_input_file_tiny_2.mp4" \
    	-vsync cfr \
    	-strict experimental \
    	-filter_complex "[0:v]yadif=0:0:0,format=pix_fmts=yuv420p[vid0];[vid0]setpts=PTS-STARTPTS[vid1];[vid1]setpts=N/(25*TB)[vidX];[0:a]asetpts=PTS-STARTPTS[aud0];[aud0]asetpts=N/(SR*TB)[aud1];[aud1]aresample=async=1000[audX]" -map [vidX] -map [audX] \
    	-c:v h264_v4l2m2m \
    	-pix_fmt yuv420p \
    	-rc VBR \
    	-b:v 4000000 \
    	-qmin 10 -qmax 51 \
    	-profile:v high \
    	-level 4.2 \
    	-shm separate_buffer \
    	-rsh 0 \
    	-g:v 25 \
    	-movflags +faststart+write_colr \
    	-c:a libfdk_aac -cutoff 20000 -ab 256k -ar 48000 \
    	-y "./some_test_input_file_tiny_transcoded_h264_VBR_g25_b_YEScorrections_v4l2m2m.mp4"
    Code:
    General
    Count                                    : 331
    Count of stream of this kind             : 1
    Kind of stream                           : General
    Kind of stream                           : General
    Stream identifier                        : 0
    Count of video streams                   : 1
    Count of audio streams                   : 1
    Video_Format_List                        : AVC
    Video_Format_WithHint_List               : AVC
    Codecs Video                             : AVC
    Audio_Format_List                        : AAC LC
    Audio_Format_WithHint_List               : AAC LC
    Audio codecs                             : AAC LC
    Complete name                            : ./some_test_input_file_tiny_transcoded_h264_VBR_g25_b_YEScorrections_v4l2m2m.mp4
    Folder name                              : .
    File name extension                      : some_test_input_file_tiny_transcoded_h264_VBR_g25_b_YEScorrections_v4l2m2m.mp4
    File name                                : some_test_input_file_tiny_transcoded_h264_VBR_g25_b_YEScorrections_v4l2m2m
    File extension                           : mp4
    Format                                   : MPEG-4
    Format                                   : MPEG-4
    Format/Extensions usually used           : braw mov mp4 m4v m4a m4b m4p m4r 3ga 3gpa 3gpp 3gp 3gpp2 3g2 k3g jpm jpx mqv ismv isma ismt f4a f4b f4v
    Commercial name                          : MPEG-4
    Format profile                           : Base Media
    Internet media type                      : video/mp4
    Codec ID                                 : isom
    Codec ID                                 : isom (isom/iso2/avc1/mp41)
    Codec ID/Url                             : http://www.apple.com/quicktime/download/standalone.html
    CodecID_Compatible                       : isom/iso2/avc1/mp41
    File size                                : 995559
    File size                                : 972 KiB
    File size                                : 972 KiB
    File size                                : 972 KiB
    File size                                : 972 KiB
    File size                                : 972.2 KiB
    Duration                                 : 2040
    Duration                                 : 2 s 40 ms
    Duration                                 : 2 s 40 ms
    Duration                                 : 2 s 40 ms
    Duration                                 : 00:00:02.040
    Duration                                 : 00:00:02:02
    Duration                                 : 00:00:02.040 (00:00:02:02)
    Overall bit rate mode                    : CBR
    Overall bit rate mode                    : Constant
    Overall bit rate                         : 3904153
    Overall bit rate                         : 3 904 kb/s
    Frame rate                               : 25.490
    Frame rate                               : 25.490 FPS
    Frame count                              : 52
    Stream size                              : 4001
    Stream size                              : 3.91 KiB (0%)
    Stream size                              : 4 KiB
    Stream size                              : 3.9 KiB
    Stream size                              : 3.91 KiB
    Stream size                              : 3.907 KiB
    Stream size                              : 3.91 KiB (0%)
    Proportion of this stream                : 0.00402
    HeaderSize                               : 2651
    DataSize                                 : 992908
    FooterSize                               : 0
    IsStreamable                             : Yes
    File last modification date              : UTC 2022-03-14 19:30:58
    File last modification date (local)      : 2022-03-15 06:00:58
    Writing application                      : Lavf59.18.101
    Writing application                      : Lavf59.18.101
    
    Video
    Count                                    : 378
    Count of stream of this kind             : 1
    Kind of stream                           : Video
    Kind of stream                           : Video
    Stream identifier                        : 0
    StreamOrder                              : 0
    ID                                       : 1
    ID                                       : 1
    Format                                   : AVC
    Format                                   : AVC
    Format/Info                              : Advanced Video Codec
    Format/Url                               : http://developers.videolan.org/x264.html
    Commercial name                          : AVC
    Format profile                           : High@L4.2
    Format settings                          : CABAC / 1 Ref Frames
    Format settings, CABAC                   : Yes
    Format settings, CABAC                   : Yes
    Format settings, Reference frames        : 1
    Format settings, Reference frames        : 1 frame
    Internet media type                      : video/H264
    Codec ID                                 : avc1
    Codec ID/Info                            : Advanced Video Coding
    Duration                                 : 2040
    Duration                                 : 2 s 40 ms
    Duration                                 : 2 s 40 ms
    Duration                                 : 2 s 40 ms
    Duration                                 : 00:00:02.040
    Duration                                 : 00:00:02:02
    Duration                                 : 00:00:02.040 (00:00:02:02)
    Duration_FirstFrame                      : -40
    Duration_FirstFrame                      : -40 ms
    Duration_FirstFrame                      : -40 ms
    Duration_FirstFrame                      : -40 ms
    Duration_FirstFrame                      : -00:00:00.040
    Bit rate mode                            : CBR
    Bit rate mode                            : Constant
    Bit rate                                 : 3631337
    Bit rate                                 : 3 631 kb/s
    Nominal bit rate                         : 4000000
    Nominal bit rate                         : 4 000 kb/s
    Maximum bit rate                         : 4000000
    Maximum bit rate                         : 4 000 kb/s
    Width                                    : 1920
    Width                                    : 1 920 pixels
    Height                                   : 1080
    Height                                   : 1 080 pixels
    Stored_Height                            : 1088
    Sampled_Width                            : 1920
    Sampled_Height                           : 1080
    Pixel aspect ratio                       : 1.000
    Display aspect ratio                     : 1.778
    Display aspect ratio                     : 16:9
    Rotation                                 : 0.000
    Frame rate mode                          : VFR
    Frame rate mode                          : Variable
    Frame rate                               : 25.490
    Frame rate                               : 25.490 FPS
    Minimum frame rate                       : 25.000
    Minimum frame rate                       : 25.000 FPS
    Maximum frame rate                       : 25.049
    Maximum frame rate                       : 25.049 FPS
    Original frame rate                      : 30.000
    Original frame rate                      : 30.000 FPS
    Frame count                              : 52
    Color space                              : YUV
    Chroma subsampling                       : 4:2:0
    Chroma subsampling                       : 4:2:0
    Bit depth                                : 8
    Bit depth                                : 8 bits
    Scan type                                : Progressive
    Scan type                                : Progressive
    Bits/(Pixel*Frame)                       : 0.069
    Stream size                              : 925998
    Stream size                              : 904 KiB (93%)
    Stream size                              : 904 KiB
    Stream size                              : 904 KiB
    Stream size                              : 904 KiB
    Stream size                              : 904.3 KiB
    Stream size                              : 904 KiB (93%)
    Proportion of this stream                : 0.93013
    Buffer size                              : 62499840
    colour_description_present               : Yes
    colour_description_present_Source        : Container
    Color range                              : Limited
    colour_range_Source                      : Container
    colour_primaries_Source                  : Container
    transfer_characteristics_Source          : Container
    matrix_coefficients_Source               : Container
    Codec configuration box                  : avcC
    
    Audio
    Count                                    : 282
    Count of stream of this kind             : 1
    Kind of stream                           : Audio
    Kind of stream                           : Audio
    Stream identifier                        : 0
    StreamOrder                              : 1
    ID                                       : 2
    ID                                       : 2
    Format                                   : AAC
    Format                                   : AAC LC
    Format/Info                              : Advanced Audio Codec Low Complexity
    Commercial name                          : AAC
    Format_AdditionalFeatures                : LC
    Codec ID                                 : mp4a-40-2
    Duration                                 : 2040
    Duration                                 : 2 s 40 ms
    Duration                                 : 2 s 40 ms
    Duration                                 : 2 s 40 ms
    Duration                                 : 00:00:02.040
    Duration                                 : 00:00:02:02
    Duration                                 : 00:00:02.040 (00:00:02:02)
    Source duration                          : 2083
    Source duration                          : 2 s 83 ms
    Source duration                          : 2 s 83 ms
    Source duration                          : 2 s 83 ms
    Source duration                          : 00:00:02.083
    Source_Duration_LastFrame                : -8
    Source_Duration_LastFrame                : -8 ms
    Source_Duration_LastFrame                : -8 ms
    Source_Duration_LastFrame                : -8 ms
    Source_Duration_LastFrame                : -00:00:00.008
    Bit rate mode                            : CBR
    Bit rate mode                            : Constant
    Bit rate                                 : 256985
    Bit rate                                 : 257 kb/s
    Channel(s)                               : 2
    Channel(s)                               : 2 channels
    Channel positions                        : Front: L R
    Channel positions                        : 2/0/0
    Channel layout                           : L R
    Samples per frame                        : 1024
    Sampling rate                            : 48000
    Sampling rate                            : 48.0 kHz
    Samples count                            : 97920
    Frame rate                               : 46.875
    Frame rate                               : 46.875 FPS (1024 SPF)
    Frame count                              : 96
    Source frame count                       : 98
    Compression mode                         : Lossy
    Compression mode                         : Lossy
    Stream size                              : 65560
    Stream size                              : 64.0 KiB (7%)
    Stream size                              : 64 KiB
    Stream size                              : 64 KiB
    Stream size                              : 64.0 KiB
    Stream size                              : 64.02 KiB
    Stream size                              : 64.0 KiB (7%)
    Proportion of this stream                : 0.06585
    Source stream size                       : 66902
    Source stream size                       : 65.3 KiB (7%)
    Source stream size                       : 65 KiB
    Source stream size                       : 65 KiB
    Source stream size                       : 65.3 KiB
    Source stream size                       : 65.33 KiB
    Source stream size                       : 65.3 KiB (7%)
    Source_StreamSize_Proportion             : 0.06720
    Default                                  : Yes
    Default                                  : Yes
    Alternate group                          : 1
    Alternate group                          : 1
    Source_Delay                             : -43
    Source_Delay_Source                      : Container
    Quote Quote  
  8. It's a kludge, but the easiest way I know of to convert NTSC frame rate MKV files to CFR MP4 files is to remux from MKV to AVI, then from AVI to MP4.

    Code:
    ffmpeg -i 24p.mkv -c copy -r 24000/1001 -bsf:v h264_mp4toannexb 24p.avi
    ffmpeg -loglevel error -i 24p.avi -c copy 24p.mp4
    del 24p.avi
    MediaInfo will show it as CFR and it the timestamps will increase by 1001 (ie, 1001/24000, 2002/24000, 3003/24000, etc).

    Slightly less kludgy, demux to a h264 elementary stream, then remux to mp4:

    Code:
    ffmpeg -i 24p.mkv -c copy -f h264 24p.h264
    ffmpeg -loglevel error -r 24000/1001 -i 24p.h264 -c copy 24p.mp4
    del 24p.h264
    Last edited by jagabo; 14th Mar 2022 at 18:18. Reason: added h264 elementary stream method
    Quote Quote  
  9. Anonymous543
    Guest
    Too much rocket science behind this
    i only understood that when we convert MKV which has 24000/1001 constant framerate to MP4 it messes some frame time codes and makes constant frame rate to variable

    remux it simply and that's it
    you mean like this?
    ffmpeg -i input.mkv output.mp4
    this works well output frame rate remains constant 24000/1001 in mp4 wrapper
    Image
    [Attachment 63809 - Click to enlarge]

    but.. in this process video gets rencoded and thats why it may lose some quality and also output file size higher then input
    so this is not a good solution

    when we remux containers it rencodes video
    so i did one more experiment with input.mkv(which has constant frame rate)
    and did 2 pass encoding with output of mp4 container
    Code:
    ffmpeg -y -i input.mkv -c:v libx265 -preset ultrafast -b:v 336k -x265-params pass=1 -an -sn -f null NUL && ^
    ffmpeg -i input.mkv -c:v libx265 -preset ultrafast -b:v 336k -x265-params pass=2 -an -sn output.mp4
    this encoding was also failed with incomplete cu tree file error



    question is still there
    i have mkv video file which has constant frame rate and i wanna copy video streams in mp4 container without messing frame rate with ffmpeg
    Last edited by Anonymous543; 15th Mar 2022 at 02:02. Reason: added remuxed output mediainfo
    Quote Quote  
  10. Originally Posted by kirito View Post
    when we remux containers it rencodes video
    No

    If you want constant framerate in mp4 (no advantage at all) follow jagabo's instructions:
    1) remux your mkv to avi
    2) remux the avi to mp4

    Just tested it with clever FFmpeg-GUI
    Quote Quote  
  11. Anonymous543
    Guest
    https://files.videohelp.com/u/301063/movie.mkv
    i want this video in mp4 wrapper without rencode with constant frame rate

    tried this code
    ffmpeg -i movie.mkv -c copy output.mp4
    but it makes frame rate mode variable

    and
    ffmpeg -i movie.mkv -c copy output.avi
    this makes video unplayable
    Quote Quote  
  12. Here you go
    Image Attached Files
    Quote Quote  
  13. Anonymous543
    Guest
    Originally Posted by ProWo View Post
    Here you go
    how did you!?
    what code you used?
    Quote Quote  
  14. Here's a drag/drop batch file that uses the AVI intermediate method in post #8.

    Code:
    ffmpeg -i "%~dpnx1" -c copy -r 24000/1001 -bsf:v hevc_mp4toannexb -vtag hevc "%~dpn1.avi%"
    ffmpeg -loglevel error -i "%~dpn1.avi" -c copy "%~dpn1".mp4
    del "%~dpn1.avi"
    It needed a slight modification to work with HEVC. And it works specifically for 24000/1001 fps. I'm sure you can figure out how to modify it for other frame rates.
    Last edited by jagabo; 15th Mar 2022 at 07:22.
    Quote Quote  
  15. Originally Posted by kirito View Post
    how did you!?
    what code you used?
    1) ffmpeg -r 24000/1001 -i movie.mkv -c copy -vtag hvc1 movie.avi
    2) ffmpeg -i movie.avi -c copy movie.mp4
    Quote Quote  
  16. Anonymous543
    Guest
    Originally Posted by jagabo View Post
    Here's a drag/drop batch file that uses the AVI intermediate method in post #8.

    Code:
    ffmpeg -i "%~dpnx1" -c copy -r 24000/1001 -bsf:v hevc_mp4toannexb -vtag hevc "%~dpn1.avi%"
    ffmpeg -loglevel error -i "%~dpn1.avi" -c copy "%~dpn1".mp4
    del "%~dpn1.avi"
    It needed a slight modification to work with HEVC. And it works specifically for 24000/1001 fps. I'm sure you can figure out how to modify it for other frame rates.
    i never used batch/.bat files for encoding thats why....its liltle confusing
    -r 24000/1001 -bsf:v hevc_mp4toannexb -vtag hevc
    -loglevel error
    explain this commands..whats its doing?
    Quote Quote  
  17. Instead of always asking new questions, you should first say thank you and learn for yourself.
    Quote Quote  
  18. Anonymous543
    Guest
    Instead of always asking new questions, you should first say thank you and learn for yourself.
    I don't know what exactly those command will do with my video stream I'll thank you guys later when I'll know
    -r is for framerate i guess
    i have no idea of
    -bsf:v hevc_mp4toannexb
    -vtag hevc
    -loglevel error
    Quote Quote  
  19. -r 24000/1001 = set constant frame rate to 24000/1001 for AVI file, https://ffmpeg.org/ffmpeg.html#Video-Options

    -bsf:v hevc_mp4toannexb = adjustment of hevc stream organization, https://ffmpeg.org/ffmpeg-bitstream-filters.html#hevc_005fmp4toannexb

    -vtag hevc = set AVI fourcc (codec identifier) to hevc, https://ffmpeg.org/ffmpeg.html#Advanced-Video-options and https://docs.microsoft.com/en-us/windows/win32/directshow/avi-riff-file-reference

    -loglevel error = only show error message otherwise lots of warning messages about unset timestamps will slow the processing, https://ffmpeg.org/ffmpeg.html#Generic-options
    Quote Quote  
  20. Anonymous543
    Guest
    Thanks for info✨
    Quote Quote  
  21. Anonymous543
    Guest
    @jagabo @ProWo
    i did liltle research and made one more method to keep frame rate constant in mp4 which is similar to yours methods

    https://superuser.com/questions/678897/extract-hevc-bitstream-with-ffmpeg

    here goes my explaination
    step 1 : extract raw hevc stream from MKV file which is called Annex B format
    by this command
    Code:
    ffmpeg -i input.mkv -c copy -bsf hevc_mp4toannexb out.h265
    note : - output file name out.h265


    step 2 : remux output of step 1 to mp4 with this commands
    Code:
    ffmpeg -i out.h265 -c copy output.mp4

    thats all works well




    by the way why did you suggested to remux to avi first and then mp4?
    and also in my method specifying -r also not needed ,all gets copied automatically i guess

    i think our all methods are same?
    Last edited by Anonymous543; 16th Mar 2022 at 06:46.
    Quote Quote  
  22. Originally Posted by kirito View Post
    by the way why did you suggested to remux to avi first and then mp4?
    Audio can be remuxed along with the video. With an elementary stream you need some extra commands in the final mux to include the audio:

    Code:
    ffmpeg -i input.mkv -c copy -bsf hevc_mp4toannexb out.h265
    ffmpeg -i out.h265 -i input.mkv -c copy -map 0:v -map 1:a output.mp4
    del out.h265
    Also, I noticed that when using an AVI intermediate the output mp4 comes out VFR if the source video is HEVC. I was testing with an AVC source where it came out CFR. I'm not sure why there is a difference.
    Quote Quote  
  23. Anonymous543
    Guest
    Also, I noticed that when using an AVI intermediate the output mp4 comes out VFR if the source video is HEVC. I was testing with an AVC source where it came out CFR. I'm not sure why there is a difference.
    Ohh yea this ffmpeg codecs surprises me always with output results

    May be that's why i like CLI of ffmpeg ,i can choose easy way of GUI but in that no new options,
    But in CLI i discover something new and different everyday
    Quote Quote  
  24. Alternatively, you could also use MP4FPSMod. I had this exact same problem a while back, and this is what fixed it.

    Use this command:
    Code:
    "mp4fpsmod.exe" --fps 0:24000/1001  "file.mp4" -o "file_fixed.mp4"
    Doesn't re-encode, just fixes the timestamps.
    Quote Quote  
  25. Anonymous543
    Guest
    I have heard about mp4fpsmod and i don't remember properly someone said about mp4box also is this tools are like ffmpeg??
    Quote Quote  
  26. Originally Posted by jagabo View Post
    Also, I noticed that when using an AVI intermediate the output mp4 comes out VFR if the source video is HEVC. I was testing with an AVC source where it came out CFR. I'm not sure why there is a difference.
    No, see my post above #15
    Quote Quote  
  27. Member
    Join Date
    Feb 2008
    Location
    United States
    Search Comp PM
    I realize this is an older thread, but i recently had this variable frame rate issue and found an easy solution.

    If you have VideoReDo, open the mkv in the Quick Stream Fixer and set the output profile for mp4. The mp4 created will be constant frame rate. The file will just be remuxed, not re-encoded.
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!