To merge video and audio streams into a single HEVC file, use FFmpeg to remux the streams without re-encoding. This process is ideal when you need to correct an incorrect audio track or simply combine separate media files into one container like MP4 or MKV. By using the
-c copy flag, FFmpeg copies the existing video and audio data directly into the new file, preserving the original HEVC quality while ensuring the audio stream is correctly attached. This method is significantly faster than re-encoding because it skips the computationally expensive decoding and encoding steps.
Understanding Stream Remuxing vs. Re-encoding
When working with multimedia files, it is crucial to distinguish between remuxing and re-encoding. Remuxing involves changing the container format (such as from MKV to MP4) or selecting specific streams within that container without altering the actual codec data. Re-encoding, on the other hand, decodes the video and audio back to raw data and then compresses them again, which results in generation loss and takes considerable time. For most users who have already encoded their video in HEVC but made a mistake with the audio selection, remuxing is the only logical choice. It allows you to swap out a bad audio track for a good one instantly while keeping the video intact.
If you are looking to combine multiple video segments rather than just fixing audio tracks, you might want to check our guide on
How to Merge/Concatenate Video Files in Part1 Folder of Parent Folder Alpha. However, for simple stream correction, the remuxing approach described here is superior.
Prerequisites and Installation
Before proceeding, ensure you have FFmpeg installed on your system. FFmpeg is a powerful command-line tool available for Windows, macOS, and Linux. You can download the latest static builds from the official FFmpeg website. Once installed, verify the installation by opening your terminal or command prompt and typing
ffmpeg -version. If the version number appears, you are ready to proceed.
You will also need two input files:
1. The video file containing the HEVC video stream.
2. The audio file containing the desired audio stream (e.g., AAC, AC3, or FLAC).
Ensure both files are accessible from the directory where you plan to run the command. It is helpful to keep them in the same folder to simplify path references.
Executing the Merge Command
The core command for merging these streams relies on the
-i flag to specify inputs and
-c copy to instruct FFmpeg to copy streams without re-encoding. Here is the standard syntax:
ffmpeg -i video.mp4 -i audio.ac3 -c copy output.mkv
In this example,
video.mp4 is your source video, and
audio.ac3 is your source audio. The output file is named
output.mkv. MKV is often preferred for this task because it supports a wider variety of codecs and metadata than MP4, reducing the risk of compatibility issues during the muxing process.
If your audio file is in a different format, such as MP3 or WAV, FFmpeg will still handle it seamlessly due to the
-c copy instruction, provided the audio codec is compatible with the output container. If you encounter errors regarding unsupported codecs, you may need to specify the codec explicitly using
-c:a aac for audio, though this triggers re-encoding.
Handling Multiple Audio Streams
Sometimes, your video file may contain multiple audio tracks, and you need to select only one for the final output. You can identify the index of each audio stream by running:
ffprobe -i video.mp4
Look for lines indicating
Stream #0:1(eng): Audio: ac3. The number after the colon indicates the index. To select only the first English audio track and discard others, use the
-map option:
ffmpeg -i video.mp4 -i audio_track.m4a -map 0:v:0 -map 0:a:0 -map 1:a:0 -c copy final_output.mp4
This command maps the video from the first input (
0:v:0), the primary audio from the first input (
0:a:0), and the new audio from the second input (
1:a:0). This gives you precise control over which streams end up in the final file.
For more complex data manipulation tasks involving lists or tables, you might find our article on
How to Move Certain Lines to the Top of a List in Tech Support useful for organizing your media file metadata manually if needed.
Troubleshooting Common Issues
If the output file plays but has no sound, check the stream mapping. Ensure that the audio stream was correctly mapped using
-map. Another common issue is synchronization problems, which rarely occur with
-c copy but can happen if the timestamps differ significantly between sources. In such cases, adding
-async 1 to the command can help align the audio and video clocks.
Always verify the output file by playing it in a media player like VLC. Check the properties to ensure the video codec remains HEVC and the audio codec matches your expectations. If the file size seems unexpectedly large, confirm that no unintended high-bitrate streams were included.
Frequently Asked Questions
Will using -c copy reduce the video quality?
No, using the
-c copy flag ensures that the video stream is copied exactly as it is without any re-encoding. This means there is zero loss in quality, making it the safest method for preserving your original HEVC content.
Can I merge an MP4 video with an MP3 audio file?
Yes, FFmpeg can merge almost any combination of video and audio formats. As long as the output container (like MKV or MP4) supports the resulting codecs, the merge will succeed. Note that some older MP4 containers may not support certain audio codecs, so MKV is often a safer default.
Why is my merged file larger than the original?
The file size increase is usually due to added overhead from the container format or additional metadata. If you added a new audio track that was higher bitrate than the original, the total size will naturally increase. Otherwise, the difference should be minimal.
What should I do if the audio is out of sync?
If you experience sync issues, try adding the
-async 1 parameter to your FFmpeg command. This helps adjust the audio clock to match the video. If the problem persists, consider checking the timestamps of the source files using
ffprobe before merging.
Is it possible to remove all audio tracks except one?
Yes, you can use the
-map option to selectively include only the desired audio stream. By specifying
-map 0:v:0 for video and
-map 0:a:0 for a specific audio index, you exclude all other streams from the output file.