How to apply audio track to a mute h264/mp4-video losslessly?

4

3

I have a MP4/h.264-video with no sound. With ffmpeg or similar, how can I apply an audio track to that MP4-file without degrading the quality of the video (i.e. without recoding the video-track)?

I'd prefer solutions involving free software, available for either Win7, Ubuntu or FreeBSD.

poplitea

Posted 2012-11-30T16:33:09.140

Reputation: 756

Answers

10

The correct FFmpeg command is:

ffmpeg -i video.mp4 -i audio.mp4 -map 0:0 -map 1:0 -c copy output.mp4

This will supply two input streams, the video you already have and the audio, e.g. from an MP4 file with AAC audio, and merge them together using the -map options.

Here, the first number in 0:0 is the input file (0 for the video file and 1 for the audio file), and the second number is the stream from that file (0 since there's only one stream each, video or audio). The two streams will be mapped to the one output file, so first video, then audio.

The bitstreams will be copied and not re-encoded using the -c copy option. You can observe this in the FFmpeg output:

Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #1:0 -> #0:1 (copy)

slhck

Posted 2012-11-30T16:33:09.140

Reputation: 182 472

Sweet! Especially nice that you explained the arguments and the usage of the mapping. – poplitea – 2012-11-30T17:20:01.513

4Also you may want -shortest if you want it only to be the duration of the shortest input (helpful if the audio is music that is longer than the video). – mark4o – 2012-11-30T17:34:17.103

1

You can use ffmpeg to accomplish that. I'm just doing it from the back of my weakened brain but maybe something like this would work:

ffmpeg -i movie-file.mp4 -i audio-file.mp3 -c:v copy -c:a copy output.mp4

thornomad

Posted 2012-11-30T16:33:09.140

Reputation: 136

This is what I'm looking for. Although I've used ffmpeg before, I can't remember its arguments. I suppose they have to do with codec/format, but what do -v:c, -a:c and -an stand for? – poplitea – 2012-11-30T17:14:18.950

2The options should be -c:v and -c:a, not -v:c and -a:c. Also, with -an you disable audio output, so the result file will have no audio again. If you merge files, you will have to map them explicitly unless FFmpeg can guess what you want (e.g. merging AVI video + WAV audio) /cc @poplitea – slhck – 2012-11-30T17:17:06.707

Ooops - got those switched in my brain. Nice to learn something new about the mapping. Thanks. – thornomad – 2012-11-30T18:10:14.190

-1

There are a few open source video editors.

On windows there's Windows movie maker or microsoft live movie maker. I don't have experience qith these though.

On linux there's OpenShot which is quite easy to use.

To Do

Posted 2012-11-30T16:33:09.140

Reputation: 1 772

The problem with nonlinear editors is that they won't allow you to do a bitstream copy very easily. If you could show an example of how to do that, it'd be very helpful though – I'm not sure it's doable. – slhck – 2012-11-30T21:47:57.733

You import your video file and your audio file. Then you simply drag it to the bottom (the timeline). You do the same with your audio file. Then you export your project, selecting the settings you desire. you may preview it before exporting. Not sure if this is what you ask. – To Do – 2012-12-01T22:13:53.303

What I mean is, when you export the project, most editors will re-encode the video and audio streams (based on the export settings, e.g. to some h.264 / AAC). They won't however copy the bitstream as-is. Re-encoding makes the quality worse, and the OP wanted to do a bitstream copy. Do you know any editors that do this, or can Windows Movie Maker / OpenShot do it? – slhck – 2012-12-01T22:25:16.663

I used pitivi on Ubuntu which had a "direc" output option for both video and audio. I think that this simply copies the original format because the encoding was over much quicker than with the normal settings. Said that Pitivi was very unstable, at least back then (1 year ago). – To Do – 2012-12-02T13:56:43.333