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.
+ Reply to Thread
Results 1 to 26 of 26
-
-
@ 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). -
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.
-
-
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
-
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? -
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
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. -
-
-
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
-
Thanks, but what would be the full command line? I tried to include this in the previous one but it doesn't work.
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"
– 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). -
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.
Yes.
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.
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. -
-
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
-
Last edited by Nico Darko; 12th Jul 2020 at 16:44.
-
Last edited by Nico Darko; 12th Jul 2020 at 17:48.
-
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. -
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. -
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. -
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...
-
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. -
It seems to have done the trick! Thank you!! ^^
And the report said: 0 frames successfully decoded, 0 decoding errors.
Similar Threads
-
hSplit - small command-line tool that lets you split and join files
By hubblec4 in forum EditingReplies: 4Last Post: 30th Sep 2020, 18:25 -
Imgburn Command Line
By sambat in forum Newbie / General discussionsReplies: 3Last Post: 6th Aug 2020, 15:04 -
Need a command line for ffmpeg
By NTradd in forum Video ConversionReplies: 11Last Post: 14th Oct 2019, 11:25 -
How to extract dvb subtitles from ts files using Subtitle Edit Command Line
By MounaLafi in forum SubtitleReplies: 0Last Post: 21st Jun 2019, 16:52 -
How to multiplex avi files on linux/mac/windows? (command line)
By Selur in forum Newbie / General discussionsReplies: 4Last Post: 27th Jan 2018, 08:30