VideoHelp Forum
+ Reply to Thread
Results 1 to 15 of 15
Thread
  1. Member Budman1's Avatar
    Join Date
    Jul 2012
    Location
    NORTHWEST ILLINOIS, USA
    Search Comp PM
    Good morning. I am trying to join 2 mp4s. Video 1 is a 10 second video created with ffmpeg from a single image and has a silent audio created with anullsrc. Video 2 is, unfortunately, vfr and since ffmpeg does not like to create vfr files, at least to match the format of another. I tried to convert video 2 to cfr but audio sync errors occur.

    Does anyone know the best way to join these 2 together. If they both must be reencoded then how can i do this and keep audio in sync. I would like to use ffmpeg and/or avisynth since they are cli.

    Thank you
    Quote Quote  
  2. I think you will have to convert both files to something that has the exact same specs and then they can be appended together. If there is another method I am sure someone will help you find it.
    Quote Quote  
  3. I would recommend using the Video To Video Converter for some of your converting tasks.
    Quote Quote  
  4. Have you tried MKVToolNix? It has several options for joining video/audio files in the same container. If it works you can use mkvmerge in CLI.
    Quote Quote  
  5. Member Budman1's Avatar
    Join Date
    Jul 2012
    Location
    NORTHWEST ILLINOIS, USA
    Search Comp PM
    I think you will have to convert both files
    I tried but having trouble keeping audio sync when converting the VFR to CFR.

    I would recommend using the Video To Video Converter
    Does it have cli option? Would prefer cli option to convert.

    Have you tried MKVToolNix
    Would prefer to keep final output as mp4

    Thanks for replies.
    Quote Quote  
  6. Would prefer to keep final output as mp4
    Depends on what you exactly understand by "joining 2 videos". If you just want to pack 2 vids in one container as 2 seperate streams you'll probably stuck here. But if you want to join these vids to one stream you can try doing that with MKVToolNix, then demux the resulting MKV and remux to MP4. These steps can easily be done by CLI batch files.
    Quote Quote  
  7. Originally Posted by Budman1 View Post
    Have you tried MKVToolNix
    Would prefer to keep final output as mp4
    Well, you could cat in mkvtoolnix. Then convert to mp4 using ffmpeg.

    Or you can try the cat features provided by ffmpeg:
    https://trac.ffmpeg.org/wiki/Concatenate
    Quote Quote  
  8. Member
    Join Date
    Mar 2008
    Location
    United States
    Search Comp PM
    For anybody who would like to try Avisynth, here's a script I had that joins two files:
    In my case the files had slightly different properties (but same aspect ratio). The code modifies
    the size, frame rate and audio sample rate so they match. If the two files have the same properties, this matching code
    does nothing.



    LoadCPlugin("I:\mypath\ffms2.dll")

    Video2 = dss2("S:\mypath\music video one.avi")
    Audio2 = directshowsource("S:\mypath\music video one.avi", video=no)

    Audio1 = FFAudioSource("S:\mypath\music video two.mkv", track=-1)
    Video1 = FFVideoSource("S:\mypath\music video two.mkv", track=-1)

    Video1 = Video1.ConvertToYV12()
    Video2 = Video2.ConvertToYV12()

    a=audiodub(video1,audio1)
    b=audiodub(video2,audio2)

    b=b.blackmanresize(a.width,a.height) # fix mismatch size
    b=b.assumefps(a.framerate,sync_audio=true).Resampl eAudio(a.AudioRate) # fix mismatch frame rate and audio sample rate

    return a+b
    Last edited by davexnet; 16th Feb 2018 at 14:39.
    Quote Quote  
  9. Member Budman1's Avatar
    Join Date
    Jul 2012
    Location
    NORTHWEST ILLINOIS, USA
    Search Comp PM
    Davexnet... i definitely can make avisynth work since it is cli and and i can fix size, etc ss you describe. I will have to check if it works fixing 'b' since my problem is the Vfr rate messing up yhe2 audio sync. Thanks for the script.
    Quote Quote  
  10. One way would be do extract VFR timecodes, edit them by shifting them and adding the 1st part. It might be easy if there are only a few different sections, or it can be difficult if you have very variable file. Join the elementary streams, then use mp4fpsmod to mux the timecodes


    How did you "try to convert video 2 to cfr" with the sync problems ? Which methods did you try ?
    Quote Quote  
  11. Member
    Join Date
    Mar 2008
    Location
    United States
    Search Comp PM
    Some notes about the above script, it wont work as-is if the amount of audio channels don't match, also in some cases,
    changefps (or convertfps) will be better than assumefps.
    Also, you can use what ever source filter you like. The only reason I used DSS2/directshowsource was because
    FFmpegsource would not work on my partitcular AVI file.
    Quote Quote  
  12. Member Budman1's Avatar
    Join Date
    Jul 2012
    Location
    NORTHWEST ILLINOIS, USA
    Search Comp PM
    How did you "try to convert video 2 to cfr" with the sync problems ? Which methods did you try ?
    I used ffmpeg with the same parameters as the 10 second video created from a single image so both would match. I also tried to extract audio from video 2, create both with ffmpeg without audio in cfr format and then mux it back to the fombined video1 video 2 but with 10 second delay. The audio came out variable rate so -c:a copy may not have been the way to go.
    Quote Quote  
  13. Originally Posted by Budman1 View Post
    How did you "try to convert video 2 to cfr" with the sync problems ? Which methods did you try ?
    I used ffmpeg with the same parameters as the 10 second video created from a single image so both would match. I also tried to extract audio from video 2, create both with ffmpeg without audio in cfr format and then mux it back to the fombined video1 video 2 but with 10 second delay. The audio came out variable rate so -c:a copy may not have been the way to go.
    It depends on what type of "VFR" (there are different variants). The most common would be things like camera phones, webcaptures, internet video, etc.. Those are actually missing frames and there are dips in the framerate , sometimes speed ups, which keep everything in sync

    For those types, a proper CFR conversion inserts duplicate frames in approximately the correct locations where they were dropped in the first place. Most reliable way in my experience is DirectShowSource in avisynth with convertfps=true . You would use whatever the base frame rate was . If it was 30.0, that' s what you would enter

    eg
    Code:
    DirectShowSource("video.mp4", fps=30.0, convertfps=true, audio=false)
    Audio doesn't really have a "variable" frame rate. There is no frame rate to audio. Only duration. (there is a variable sample rate, but that's something else entirely). 1 hour of audio is 1 hour of audio. It's the video that is variable in these common types of VFR . And the timecodes dictate the display time per frame. The audio is constant

    Some people would say ffms2 with fpsnum and fpsden . In my experience, not as reliable. You need it as accurate as possible when editing videos, and I've done thousands of these. The ffms2 conversion is slightly off and errors propogate. It might be ok for a one off, but when you edit dozens of videos in a project, those slightly more off frames are a nightmare. (And I can't believe I'm saying this, but this is the 1 case where DirectShowSource is an actually preferred source filter; but it requires that you keep your directshow filters in order too...)
    Quote Quote  
  14. Member Budman1's Avatar
    Join Date
    Jul 2012
    Location
    NORTHWEST ILLINOIS, USA
    Search Comp PM
    Thanks everyone for help. I tried all possibilities since this was a good test video being VFR and a little odd working with it and playing compared to others. I was able to modify an avisynth script using a combination of the posts by Poisondeathray and Davexnet.

    Code:
    video2 = directshowsource("c:\users\bud\desktop\[dp]Manjandani-1.mp4", fps=23.976, convertfps=true, audio=true)
    Video1 = directshowsource("c:\users\bud\desktop\output.mp4")
    Video1 = Video1.ConvertToYV12()
    Video2 = Video2.ConvertToYV12()
    a=video1
    b=video2
    b=b.blackmanresize(a.width,a.height) # fix mismatch size
    b=b.assumefps(a.framerate,sync_audio=true).ResampleAudio(a.AudioRate) # fix mismatch frame rate and audio sample rate
    return a+b
    Then I used a crude ffmpeg script just to test and got a video that is perfectly in sync now and the correct size.

    Image
    [Attachment 44711 - Click to enlarge]


    The number of frames shown above for the 10 second video was 240 (23.976 x 10) but I assume thats as close as it could get to 239.76 frames and only results in an extra 11 ms for video 1. It doesn't seem to affect video 2 after it is joined so no biggie.

    Thanks again everyone who replied and helped!
    Last edited by Budman1; 17th Feb 2018 at 00:43. Reason: Chrome dropped unexpectedly before I finished.
    Quote Quote  
  15. Originally Posted by Budman1 View Post
    Does anyone know the best way to join these 2 together.
    Code:
    MP4Box -cat video1.mp4 -cat video2.mp4 -new new_video.mp4
    Code:
    MP4Box -add video1.mp4 -cat video2.mp4 -new new_video.mp4
    Code:
    mencoder video1.mp4 video2.mp4 -ovc copy -oac copy -of lavf format=mp4 -o new_video.mp4
    You can get MP4Box from here.
    You can get mencoder from here.

    Or you can use My-MP4Box-GUI
    Quote Quote  



Similar Threads

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