VideoHelp Forum




+ Reply to Thread
Results 1 to 9 of 9
  1. TL; DR

    How to convert this .ogm file to an .mp4 file using ffmpeg, such that audio and video are properly synced?

    My attempts

    The problem seems to be that the audio in the .ogm file is a non-monotonous DTS. My first attempt was naive:

    Code:
    ffmpeg -i inp.ogm -max_muxing_queue_size 100000 -c:v h264_qsv -global_quality 18 -look_ahead 1 -pix_fmt nv12 out.mp4
    However, video frames are duplicated and the latency accumulates. The input video is 23:11 and the output is 27:35. The output video and audio are out of sync, but the difference is not constant, it accumulates through the video. I tried delaying audio/video by a constant time, but it doesn't solve the problem, since the difference is not constant.

    I tried all combinations of "-vsync X -async Y", where X and Y are 0, 1, or 2. None of that works. When "async" is set to 1, video and audio are synced, but the problem is that the audio is interspersed by periods of silence after every ~0.5 seconds (non constant). The second problem is that the output video is again 27:35 long, instead of 23:11.

    Also tried changing "atempo", redefining DTS timestamps using PTS, changing output framerate, but none of that works. It results in weird audio/video glitches.

    My understanding is that the audio consists of chunks, each chunk contains the starting time and the audio data. However, the length of the audio data is longer than the video frame duration (inverse of the framerate), so ffmpeg fills the gaps with duplicated frames. With "-async 1", it doesn't duplicate frames, but it leaves periods of silence (this seems counter-intuitive to me, I don't actually know why it happens).

    The .ogm file plays perfectly fine using ffplay. My goal is to convert the .ogm file to an .mp4 file that plays just like ffplay plays the .ogm file.
    Image Attached Files
    Last edited by Patrick1389; 18th Aug 2023 at 05:09.
    Quote Quote  
  2. Member
    Join Date
    Mar 2008
    Location
    United States
    Search Comp PM
    What do you intend to do aboout the interlacing/combing in the source?
    Leave it and encode it as is?
    Or perhaps reduce it to 25 (or 23.976) to recover the underlying frame rate?
    Quote Quote  
  3. Thank you for the reply. I just want to create an .mp4 file from the .ogm file that plays exactly the same as ffplay plays the .ogm file. That is, the output file duration should be 23:11 and the audio and video should be synchronized. Naively re-encoding both streams results in a longer video, many frames are duplicated and audio is out of sync.

    I've managed to achieve what I want by first extracting audio and video streams separately, then re-encoding video only, and finally combining the streams into one file:

    Code:
    ffmpeg -i inp.ogm -c:v copy -an v0.mp4
    ffmpeg -i v0.mp4 -max_muxing_queue_size 100000 -c:v h264_qsv -global_quality 18 -look_ahead 1 -pix_fmt nv12 v.mp4
    ffmpeg -i inp.ogm -vn -c:a copy a.mp4
    ffmpeg -i v.mp4 -i a.mp4 -c copy out.mp4
    rm v0.mp4 v.mp4 a.mp4
    Even though this works, I still wonder if this can be achieved using a single invocation of ffmpeg.
    Quote Quote  
  4. Member
    Join Date
    Mar 2008
    Location
    United States
    Search Comp PM
    Even a simple command using Libx264 results in a good file.
    Code:
    ffmpeg.exe  -i "C:\Users\davex\Downloads\inp\inp.mkv"    -c:v libx264 -preset faster -c:a copy out.mp4
    perhaps it's something related to your HW encoding
    Quote Quote  
  5. I don't know what the problem with ffmpeg is but this works well in AviSynth:

    Code:
    v = LWLibavVideoSource("inp.ogm") 
    a = LWLibavAudioSource("inp.ogm") 
    AudioDub(v,a)
    DelayAudio(0.2) # 200 ms
    AssumeTFF()
    QTGMC(preset="fast")
    SRestore(frate=25)
    Audio is clean and in sync from beginning to end. In addition panning is smooth and blended frames have been removed. Low bitrate result attached.
    Image Attached Files
    Quote Quote  
  6. Member
    Join Date
    Feb 2006
    Location
    United States
    Search Comp PM
    Originally Posted by Patrick1389 View Post
    TL; DR

    How to convert this .ogm file to an .mp4 file using ffmpeg, such that audio and video are properly synced?

    My attempts

    The problem seems to be that the audio in the .ogm file is a non-monotonous DTS. My first attempt was naive:

    Code:
    ffmpeg -i inp.ogm -max_muxing_queue_size 100000 -c:v h264_qsv -global_quality 18 -look_ahead 1 -pix_fmt nv12 out.mp4
    However, video frames are duplicated and the latency accumulates. The input video is 23:11 and the output is 27:35. The output video and audio are out of sync, but the difference is not constant, it accumulates through the video. I tried delaying audio/video by a constant time, but it doesn't solve the problem, since the difference is not constant.

    I tried all combinations of "-vsync X -async Y", where X and Y are 0, 1, or 2. None of that works. When "async" is set to 1, video and audio are synced, but the problem is that the audio is interspersed by periods of silence after every ~0.5 seconds (non constant). The second problem is that the output video is again 27:35 long, instead of 23:11.

    Also tried changing "atempo", redefining DTS timestamps using PTS, changing output framerate, but none of that works. It results in weird audio/video glitches.

    My understanding is that the audio consists of chunks, each chunk contains the starting time and the audio data. However, the length of the audio data is longer than the video frame duration (inverse of the framerate), so ffmpeg fills the gaps with duplicated frames. With "-async 1", it doesn't duplicate frames, but it leaves periods of silence (this seems counter-intuitive to me, I don't actually know why it happens).

    The .ogm file plays perfectly fine using ffplay. My goal is to convert the .ogm file to an .mp4 file that plays just like ffplay plays the .ogm file.
    see this thread post #10 - https://forum.videohelp.com/threads/361242-Help-ffmpeg-bat-ogm-mp4
    Quote Quote  
  7. Originally Posted by davexnet View Post
    Even a simple command using Libx264 results in a good file.
    Code:
    ffmpeg.exe  -i "C:\Users\davex\Downloads\inp\inp.mkv"    -c:v libx264 -preset faster -c:a copy out.mp4
    perhaps it's something related to your HW encoding
    Doesn't seem to be a HW encoding problem. Running your command exactly, I get a 27:35 video, which is undesired. What does ffprobe output for your out.mp4 if you run that command on your machine? If the duration is 27:35, then your command doesn't produce a "good" file. If the duration is 23:11, then my installation of ffmpeg is broken for some reason.

    Originally Posted by october262 View Post
    That thread is unrelated.
    Quote Quote  
  8. Originally Posted by october262 View Post
    The command there didn't work. There's just something odd about the timecodes in the OP's mp4 file. ffmpeg gives a video that's about 4 minutes too long.

    But I was able to get the right result by remuxing to MKV first (with lots of timecode warnings), then converting that MKV using the OP's command line.

    Code:
    ffmpeg -y -fflags +genpts -i inp.ogm -vcodec copy -acodec copy inp.mkv
    ffmpeg -y -i inp.mkv -max_muxing_queue_size 100000 -c:v h264_qsv -global_quality 18 -look_ahead 1 -pix_fmt nv12 out.mp4
    The audio still needs a ~200 ms delay in my opinion. And, of course, the video is still full of interlacing and blending.
    Quote Quote  
  9. Member
    Join Date
    Mar 2008
    Location
    United States
    Search Comp PM
    Yes I converted to MKV myself (forgot to mention, mux in MKVtoolnix). I think just doing that fixes some issues.
    As you can see, mine (the encode using LIbx264) looks good:
    Code:
    Video
    ID                                       : 1
    Format                                   : AVC
    Format/Info                              : Advanced Video Codec
    Format profile                           : High@L3
    Format settings                          : CABAC / 4 Ref Frames
    Format settings, CABAC                   : Yes
    Format settings, Reference frames        : 4 frames
    Codec ID                                 : avc1
    Codec ID/Info                            : Advanced Video Coding
    Duration                                 : 23 min 11 s
    Bit rate                                 : 887 kb/s
    Width                                    : 720 pixels
    Height                                   : 480 pixels
    Display aspect ratio                     : 3:2
    Frame rate mode                          : Constant
    Frame rate                               : 29.970 (29970/1000) FPS
    Standard                                 : NTSC
    Color space                              : YUV
    Chroma subsampling                       : 4:2:0
    Bit depth                                : 8 bits
    Scan type                                : Progressive
    Bits/(Pixel*Frame)                       : 0.086
    Stream size                              : 147 MiB (86%)
    Quote Quote  



Similar Threads

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