4

I have a video file encoded as H.264 in an mp4 file and audio encoded as Opus in a webm file. What would be the best way to combine these files into one mp4? I don't want re encode the files and lose quality.

3

This can be done relatively easily with FFmpeg. If you are using Xenial Xerus LTS simply run the following:

sudo apt-get install ffmpeg

If however you are running Trusty Tahr LTS you would be best to build your own from these simple copy and paste instructions...

To demonstrate the technique I have created 2 test files:

  1. This file called test.mp4 has the h.264 stream that you want and is in the following format, take careful note of the stream identifiers:

    Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661) ...
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D)...
    
  2. This files called test1.webm has the Opus stream that you want and is in the following format, again take careful note of the stream identifiers:

    Stream #0:0: Video: vp9 (Profile 0)...
    Stream #0:1: Audio: opus, 48000 Hz, stereo, fltp (default)
    

Note: In your example the webm container may have only an audio stream with no video stream. In this case your Opus audio stream would have the identifier Stream #0:0.

Bear in mind as well that at the moment you cannot put Opus Audio in an mp4 container but it sits quite happily in an mkv container. The following is the simplest method:

ffmpeg -i test.mp4 -i test1.webm -c copy -map 0:0 -map 1:1 -shortest out.mkv

This accomplishes the following:

  1. -c copy copy the codecs without transcoding
  2. -map 0:0 select the 1st input file : 1st stream = the desired h.264 stream
  3. -map 1:1 select the 2nd input file : 2nd stream = the desired Opus stream
  4. -shortest stop encoding when the shortest input stream ends

For more elaborate methods that will be required with multi-stream files see the link in my references...

References:

  • I'm getting Stream map '1:1' matches no streams. – Qwertie May 7 '16 at 7:37
  • Perhaps your webm file has only audio? Can you pastebin the results of ffmpeg -i my_second_file? If you know the second file has only audio make the second map option -map 1:0 – andrew.46 May 7 '16 at 7:40
  • Yeah 1:0 works. – Qwertie May 7 '16 at 7:51
  • I have added some information about the possibility of a single stream Opus / webm file. You have to love FFmpeg don't you :) – andrew.46 May 7 '16 at 7:53
2

Since Opus doesn't conform to the MP4 container standard and H.264 is similarly out of place in WebM (a restricted MKV) containers you have to use MKV and hope that the players support Opus audio tracks.

Remuxing can be as simple as opening Mkvmerge GUI dragging both files in and selecting the particular tracks you want to have in the output. But nobody stops you from researching all the options involved for the command line utility (the new GUI should still have an option to copy the entire command) or learning to use ffmpeg.

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.