How can I use a single ffmpeg.exe command to copy an audio track from one MP4 file to another without re-encoding?
I have two MP4 files:
- 1080.mp4 - video without sound
- 360.mp4 - video with sound
I need to copy the audio track from the 360.mp4 file to the 1080.mp4 file without re-encoding the audio. I know this can be done in two commands:
ffmpeg -i 360.mp4 -vn -acodec copy sound.aac
ffmpeg -i 1080.mp4 -i sound.aac -c copy 1080_sound.mp4
However, I need to accomplish this task with a single ffmpeg command, passing both video files through the -i key and getting the result in a single program run.
What ffmpeg command should I use to copy the audio track from one MP4 file to another without intermediate files and re-encoding?
Use the following command to transfer an audio track from one MP4 file to another without re-encoding:
ffmpeg -i 1080.mp4 -i 360.mp4 -map 0:v:0 -map 1:a:0 -c copy 1080_sound.mp4
Table of Contents
- Main Command
- Command Component Breakdown
- Alternative Options
- Handling Multiple Audio Tracks
- Checking the Result
- Conclusion
Main Command
To transfer the audio track from the 360.mp4 file to the 1080.mp4 file without re-encoding, use:
ffmpeg -i 1080.mp4 -i 360.mp4 -map 0:v:0 -map 1:a:0 -c copy 1080_sound.mp4
This command combines the video from the first file with the audio from the second file, preserving both streams without re-encoding.
Command Component Breakdown
Let’s break down each component of the command:
-i 1080.mp4- first input file (video without sound)-i 360.mp4- second input file (video with sound)-map 0:v:0- take the video stream (v) with index 0 from the first file (0)-map 1:a:0- take the audio stream (a) with index 0 from the second file (1)-c copy- copy both streams without re-encoding1080_sound.mp4- output file
Important: The
-mapkey allows you to precisely specify which streams from which files to use in the output file.
Alternative Options
More Concise Notation
You can use a more concise form of the command:
ffmpeg -i 1080.mp4 -i 360.mp4 -map 0:v -map 1:a -c copy 1080_sound.mp4
In this case, FFmpeg will automatically select the first video stream and the first audio stream from the specified files.
Explicit Codec Specification
For greater clarity, you can specify codecs separately:
ffmpeg -i 1080.mp4 -i 360.mp4 -map 0:v:0 -map 1:a:0 -c:v copy -c:a copy 1080_sound.mp4
This is equivalent to -c copy but more explicitly shows that both video and audio are copied without changes.
Handling Multiple Audio Tracks
If the 360.mp4 file contains multiple audio tracks, you can select the desired one using its index:
# Second audio track (index 1) from the second file
ffmpeg -i 1080.mp4 -i 360.mp4 -map 0:v:0 -map 1:a:1 -c copy 1080_sound.mp4
To view the available streams in the files, use:
ffmpeg -i 360.mp4
This will show all streams with their indices.
Checking the Result
After running the command, you can check the result:
ffprobe 1080_sound.mp4
This command will show the structure of the output file, including the presence of video and audio streams.
You can also check the audio quality using:
ffmpeg -i 1080_sound.mp4 -f null -
If the command executes without errors, it means the file has a correct structure.
Conclusion
Using a single FFmpeg command to transfer an audio track between files has several advantages:
- Efficiency - no need to create intermediate files
- Speed - the process is faster as there’s no re-encoding step
- Quality - audio is preserved in its original quality without loss
- Convenience - just one command instead of two
The main command for your task:
ffmpeg -i 1080.mp4 -i 360.mp4 -map 0:v:0 -map 1:a:0 -c copy 1080_sound.mp4
Sources
- r/ffmpeg on Reddit: Adding Audio Track from one mp4 to another mp4
- Super User: How to use ffmpeg to rip audio from mp4 and add it to another
- Stack Overflow: FFMPEG mux video and audio (from another video) - mapping issue
- JSON2Video: FFmpeg - How to extract audio from video
- Mapping multiple input files - python-ffmpeg