I have 2 audio files in ac3 format, and both contain only one channel (audio1.ac3 and audio2.ac3)

On the other hand I have a mkv file that contains the video and 2 audio streams (video.mkv)

Audio stream 1 is in ac3 format and has 4 channels (let's call them L1 R1 C1 LFE1)

Audio stream 2 is in ac3 format and has 4 channels too. (let's call them L2 R2 C2 LFE2)

I want to substitute the central channel in audio stream 1 and audio stream 2, using the ac3 files I mentioned before. I want as the final output the mkv file with these 2 audio streams:

Audio stream 1 -> L1 R1 (audio1.ac3 as center C1) LFE1

Audio stream 2 -> L2 R2 (audio2.ac3 as center C2) LFE2

I would like to preserve the metadata tags that existed already in the mkv file for the names of these audio streams, and to do all the process with no quality loss and using FFmpeg from the command line (I am on Windows but can do it from Ubuntu too)

All the information I have found is regarding the complete substitution of the stream, but not on the channel level. Maybe it is something that can't be done.

Note: All the audio files have the same sample rate, parameters as the streams in the mkv file.


Substitute channel in audio using external audio files

One method is to use the join filter:

ffmpeg -i input.mkv -i audio1.ac3 -i audio2.ac3 -filter_complex "[0:a:0][1:a]join=inputs=2:channel_layout=5.1:map=0.0-FL|0.1-FR|1.0-FC|0.3-LFE|0.4-BL|0.5-BR[a0];[0:a:1][2:a]join=inputs=2:channel_layout=5.1:map=0.0-FL|0.1-FR|1.0-FC|0.3-LFE|0.4-BL|0.5-BR[a1]" -map 0:v -map "[a0]" -map "[a1]" -c:v copy -c:a flac output.mkv

I made some assumptions about the channel ordering. You may have to adjust. See ffmpeg -layouts for channel layout info.

Preserve the metadata tags

From the documentation (under the -map_metadata option):

By default, global metadata is copied from the first input file, per-stream and per-chapter metadata is copied along with streams/chapters.

You didn't provide any detailed info on your files or the metadata (the complete output of ffmpeg -i input.mkv -i audio1.ac3 -i audio2.ac3 would have sufficed) so I'll assume the default behavior should work.

No quality loss

Filtering requires re-encoding, so a lossless format (flac) is used in the example.

| improve this answer | |
  • Thank you for your detailed answer. As I need AC3 as output, should I use your method and then convert the FLAC to AC3? – tom_len Oct 10 '18 at 9:53
  • @tom_len You can also use -c:a ac3 instead of -c:a flac. – slhck Oct 10 '18 at 10:17
  • @tom_len I suggested flac because you had a no quality loss requirement. ac3 is lossy. – llogan Oct 10 '18 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.