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.
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:
This file called
test.mp4has 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)...
This files called
test1.webmhas 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
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:
-c copycopy the codecs without transcoding
-map 0:0select the 1st input file : 1st stream = the desired h.264 stream
-map 1:1select the 2nd input file : 2nd stream = the desired Opus stream
-shorteststop 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...
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.