VideoHelp Forum

Try DVDFab and download streaming video, copy, convert or make Blu-rays,DVDs! Download free trial !
+ Reply to Thread
Results 1 to 26 of 26
Thread
  1. Hi.
    I have a bunch of mkv files where I need to add a second audio track. All the tracks are ac3 and have the same name as the mkvs.
    Could someone tell me what the command line would be to merge the mkvs with the tracks, placing the new audio as the 1st track? In batch mode.

    Thank you.
    Quote Quote  
  2. Very similar to your last request:

    Code:
    md New
    for %%F in (*.mkv) do ( 
        ffmpeg -i "%%~nxF" -i "%%~nF.ac3" -c:v copy -c:a copy -map 0:v -map 1:a -map 0:a "New\%%~nxF"
    )
    pause
    Last edited by jagabo; 6th Jul 2020 at 18:45. Reason: Changed mistaken mp4 references to mkv
    Quote Quote  
  3. @ OP :
    Just replace "mp4" by "mkv" in the script above (I haven't seen the "last request" so perhaps I'm missing something).

    @ jagabo :
    As far as I know the MP4 container doesn't support AC3 audio, or two audio tracks (perhaps it's just a typo and you meant "mkv" indeed).
    With recent versions of ffmpeg it works with "-c copy" (all streams are copied as-is).
    Quote Quote  
  4. Sorry, yes, I forget to change the mp4 extensions to mkv (the previous thread was about mp4). I'll edit the post to prevent confusion in the future.

    AC3 was officially added to the supported audio codecs in mp4 a few years ago. Older devices may not support it.

    Also, although the new command line works, delivering the correct streams in the correct order, when I play the resulting files with MPCHC it plays the second audio track (aac in my test cases), not the first (ac3).
    Last edited by jagabo; 6th Jul 2020 at 18:47.
    Quote Quote  
  5. Thank you both!

    Originally Posted by jagabo View Post
    Also, although the new command line works, delivering the correct streams in the correct order, when I play the resulting files with MPCHC it plays the second audio track (aac in my test cases), not the first (ac3).
    Yeah it works fine, but I noticed that as well, MPCHC plays the second track by default (also ac3 in my case). Is there a way to change that? If not I'll just use JMkvpropedit to make the new track the default one.
    Quote Quote  
  6. Found it: Using the -disposition option lets you specify the default audio track:

    Code:
    md New
    for %%F in (*.mkv) do ( 
        ffmpeg -y -i "%%~nxF" -i "%%~nF.ac3" -c copy -map 0:v -map 1:a -map 0:a -disposition:a:0 default -disposition:a:1 none "New\%%~nxF"
    )
    pause
    Quote Quote  
  7. Perfect thanks!

    Ok one more thing. I also have some mkv files for which I have 2 audio tracks to add, and I'd like to strip the audio from the original mkv.
    The thing is that sometimes those 2 new tracks are both ac3 (how do I name the second one?) and sometimes one of them is mp3. How could I change the command line for all that?
    Quote Quote  
  8. If you need to convert files with a different layout / diffent format, it gets tricky to process them all with a single script.
    The general usage of the "map" syntax is : first input track named in the command gets mapped to first track in the output, second input track named in the command gets mapped to second track, and so on... Then "-map 0:0" means "first track of the first input file" (because the track numbers start from 0, it's a bit confusing at first), "-map 0:1" means "second track of the first input file", "-map 1:0" means "first track of the second input file", and so on...
    So to add the video track from the video file, and two extra audio tracks, without the original audio from the video file, it would be :
    Code:
    -i "%%~nxF -i "%%~nF-1.ac3" -i "%%~nF-2.ac3 -map 0:0 -map 1:0 -map 2:0
    In this case the AC3 audio files simply have "-1" and "-2" added at the end of the name. There's no "-map 0:1" (or "-map 0:a") so the audio from the source video file won't be muxed to the output.
    I don't know about the "disposition" option, so I won't risk typing something wrong.
    I also didn't know about the "-map 0:a" / "-map 0:v" notation, it seems convenient in case input files are mapped differently (sometimes MKV have an audio track before the video track), but what happens when an input file has more than one audio track and "-map 0:a" is used ?

    Another possibility would be to prepare one conversion with MKVToolNix, once all the options are correctly set, go to "Multiplexer" => "Show command line", and then create a batch script based on it. It probably has more options and flexibility than ffmpeg, although I couldn't specify what / how.
    Quote Quote  
  9. Originally Posted by Nico Darko View Post
    Ok one more thing. I also have some mkv files for which I have 2 audio tracks to add, and I'd like to strip the audio from the original mkv.
    The thing is that sometimes those 2 new tracks are both ac3 (how do I name the second one?) and sometimes one of them is mp3. How could I change the command line for all that?
    So a folder has two audio files for each video file, sometimes two AC3 files, sometimes one AC3 file and one MP3 file. But never only one audio file? When there are two AC3 files what is the naming convention?
    Quote Quote  
  10. Originally Posted by abolibibelot View Post
    If you need to convert files with a different layout / diffent format, it gets tricky to process them all with a single script.
    The general usage of the "map" syntax is : first input track named in the command gets mapped to first track in the output, second input track named in the command gets mapped to second track, and so on... Then "-map 0:0" means "first track of the first input file" (because the track numbers start from 0, it's a bit confusing at first), "-map 0:1" means "second track of the first input file", "-map 1:0" means "first track of the second input file", and so on...
    So to add the video track from the video file, and two extra audio tracks, without the original audio from the video file, it would be :
    Code:
    -i "%%~nxF -i "%%~nF-1.ac3" -i "%%~nF-2.ac3 -map 0:0 -map 1:0 -map 2:0
    Thanks, but what would be the full command line? I tried to include this in the previous one but it doesn't work.

    Originally Posted by jagabo View Post
    Originally Posted by Nico Darko View Post
    Ok one more thing. I also have some mkv files for which I have 2 audio tracks to add, and I'd like to strip the audio from the original mkv.
    The thing is that sometimes those 2 new tracks are both ac3 (how do I name the second one?) and sometimes one of them is mp3. How could I change the command line for all that?
    So a folder has two audio files for each video file, sometimes two AC3 files, sometimes one AC3 file and one MP3 file. But never only one audio file? When there are two AC3 files what is the naming convention?
    Correct. For the two ac3 files, they're just "name.1" and "name.2"
    Quote Quote  
  11. This batch file ignores the source audio and adds the AC3 and MP3 files if there is an MP3 file. Otherwise it adds two AC3 files:

    Code:
    md New
    for %%F in (*.mkv) do ( 
        IF EXIST "%%~nF.mp3" ( ffmpeg -y -i "%%~nxF" -i "%%~nF.ac3" -i "%%~nF.mp3" -c copy -map 0:v -map 1:a -map 2:a -disposition:a:0 default -disposition:a:1 none "New\%%~nxF" ) ^
        ELSE ( ffmpeg -y -i "%%~nxF" -i "%%~nF.1.ac3" -i "%%~nF.2.ac3" -c copy -map 0:v -map 1:a -map 2:a -disposition:a:0 default -disposition:a:1 none "New\%%~nxF" )
    )
    pause
    Quote Quote  
  12. Thanks, but what would be the full command line? I tried to include this in the previous one but it doesn't work.
    I typed this not knowing what the extracted files were named by default. For it to work you'd have to manually rename the files accordingly, or to correct the command to reflect the actual naming scheme. The argument "%%~nF" means "the name of the *.mkv file before the extension", so if the input video file is named "this is an example.mkv" and the audio tracks are named "this is an example - audio 1.ac3" and "this is an example - audio 2.ac3", "%%~nF" will be expanded as "this is an example", so you need to complete with the part of the name that is different, in this case "%%~nF - audio track 1.ac3" and "%%~nF - audio track 2.ac3". Once you understand how this works you can create custom commands regardless of the particular naming scheme of the files you want to process, without a reference to an existing template.

    You could read this to get further explanations :
    https://ss64.com/nt/for.html
    https://ss64.com/nt/syntax-args.html
    Or read the integrated help for the "FOR" command :
    Code:
    for /? # to read it within the command prompt
    for /? >"E:\CMD FOR help.txt # to save it to a local file for future reference  you can change the name and location of course

    @ jagabo :
    It would be wiser to add something to the name of the output file to indicate that it is the remuxed one, even if it's placed in a dedicated subfolder, as it could be moved later on, resulting in a name conflict. For instance :
    Code:
    "New\%%~nF - remuxed%%~xF"
    Isn't "%%~nxF" equivalent to "%%F" ?
    How does the "disposition" option work ? In the humongous integrated help (798KB text file) it's only mentioned once and there's no description.
    Apparently "-y" does "overwrite output files", why is it there ? I'm generally wary of letting any program overwrite anything (I live in SNAFU-land).
    Quote Quote  
  13. Originally Posted by abolibibelot View Post
    @ jagabo :
    It would be wiser to add something to the name of the output file to indicate that it is the remuxed one, even if it's placed in a dedicated subfolder, as it could be moved later on, resulting in a name conflict.
    I was thinking he might want to delete the old files (after verifying the new files are the way he wants them) by moving the new ones over them. He can change the output spec to whatever he wants. How to do it should be pretty obvious by now.

    Originally Posted by abolibibelot View Post
    Isn't "%%~nxF" equivalent to "%%F" ?
    Yes.

    Originally Posted by abolibibelot View Post
    How does the "disposition" option work ? In the humongous integrated help (798KB text file) it's only mentioned once and there's no description.
    Sets the indicated output track to default or not. For example "-disposition:a:0 default" marks the first (0) audio (a) track "default" (you can see this in a MediaInfo report), "-disposition:a:1 none" sets the second audio track to not-default.

    Originally Posted by abolibibelot View Post
    Apparently "-y" does "overwrite output files", why is it there ? I'm generally wary of letting any program overwrite anything (I live in SNAFU-land).
    When I'm creating a script like this it's a pain to delete all the new files after a failure. So I just set the script to overwrite them automatically. The OP can remove that option if he wants.
    Quote Quote  
  14. Originally Posted by jagabo View Post
    This batch file ignores the source audio and adds the AC3 and MP3 files if there is an MP3 file. Otherwise it adds two AC3 files:

    Code:
    md New
    for %%F in (*.mkv) do ( 
        IF EXIST "%%~nF.mp3" ( ffmpeg -y -i "%%~nxF" -i "%%~nF.ac3" -i "%%~nF.mp3" -c copy -map 0:v -map 1:a -map 2:a -disposition:a:0 default -disposition:a:1 none "New\%%~nxF" ) ^
        ELSE ( ffmpeg -y -i "%%~nxF" -i "%%~nF.1.ac3" -i "%%~nF.2.ac3" -c copy -map 0:v -map 1:a -map 2:a -disposition:a:0 default -disposition:a:1 none "New\%%~nxF" )
    )
    pause
    It works perfectly, thank you!!

    The files do have subtitles though, I tried to include them by adding
    Code:
    -c:s copy
    but it doesn't work, I guess I added it at the wrong place?
    Quote Quote  
  15. To include subs (if any) from the first file:

    Code:
    md New
    for %%F in (*.mkv) do ( 
        IF EXIST "%%~nF.mp3" ( ffmpeg -y -i "%%~nxF" -i "%%~nF.ac3" -i "%%~nF.mp3" -c copy -map 0:v -map 1:a -map 2:a -map 0:s -disposition:a:0 default -disposition:a:1 none "New\%%~nxF" ) ^
        ELSE ( ffmpeg -y -i "%%~nxF" -i "%%~nF.1.ac3" -i "%%~nF.2.ac3" -c copy -map 0:v -map 1:a -map 2:a  -map 0:s -disposition:a:0 default -disposition:a:1 none "New\%%~nxF" )
    )
    pause
    Quote Quote  
  16. Originally Posted by jagabo View Post
    To include subs (if any) from the first file:

    Code:
    md New
    for %%F in (*.mkv) do ( 
        IF EXIST "%%~nF.mp3" ( ffmpeg -y -i "%%~nxF" -i "%%~nF.ac3" -i "%%~nF.mp3" -c copy -map 0:v -map 1:a -map 2:a -map 0:s -disposition:a:0 default -disposition:a:1 none "New\%%~nxF" ) ^
        ELSE ( ffmpeg -y -i "%%~nxF" -i "%%~nF.1.ac3" -i "%%~nF.2.ac3" -c copy -map 0:v -map 1:a -map 2:a  -map 0:s -disposition:a:0 default -disposition:a:1 none "New\%%~nxF" )
    )
    pause
    Ok there's an issue when I add
    Code:
    -map 0:s
    It says "starting new cluster due to timestamp" during the creation of the files, and when checking that new file, the new track is faulty. Everything seems fine if I remove the subtitle part.
    Last edited by Nico Darko; 12th Jul 2020 at 16:44.
    Quote Quote  
  17. Originally Posted by Nico Darko View Post
    the new track is faulty
    The sub track?
    Quote Quote  
  18. Originally Posted by jagabo View Post
    Originally Posted by Nico Darko View Post
    the new track is faulty
    The sub track?
    No the new audio tracks, both mp3 and ac3, they're faulty in the new file, with the sound going and off.

    But if I remove
    Code:
    -map 0:s
    I don't get the "starting new cluster due to timestamp" message and the new audio tracks are fine.
    Last edited by Nico Darko; 12th Jul 2020 at 17:48.
    Quote Quote  
  19. There's no alternative code to include the subtitles? to see if that maybe fixes the issue?
    Although I don't get why including the subtitles messes up with the new audio.
    Quote Quote  
  20. Maybe try adding "-c:s copy" ? I don't think that will make a difference because "-c copy" is already specified.

    If it's only one particular file I would try using mkvtoolnix. Even if it's multiple files try using mkvtoolnix for one that fails with ffmpeg and see what command line it generates. Or demux the sub and take a look at it, looking for irregularities.
    Quote Quote  
  21. Yeah adding "c-s copy" doesn't make a difference.
    And I just checked, I seem to get that "starting new cluster due to timestamp" with every file, even another random one I tried just to see if it was different.

    edit : I'll try mkvtoolnix.
    Quote Quote  
  22. There's no issue after creating the files with mkvtoolnix, subs included and all. And I demuxed the subs, nothing wrong with them. So if anyone knows why ffmpeg messes-up...
    Quote Quote  
  23. Try adding "-max_interleave_delta 0" found here: https://www.reddit.com/r/ffmpeg/comments/efddfs/starting_new_cluster_due_to_timestamp/

    You can try setting the log level to debug (-loglevel debug) to get more thorough error reports. Maybe you'll see something there. You'll probably have to redirect output to a file to see it all.
    Quote Quote  
  24. It seems to have done the trick! Thank you!! ^^

    And the report said: 0 frames successfully decoded, 0 decoding errors.
    Quote Quote  



Similar Threads