View Single Post
Old 09-15-2019, 11:26 AM   #6
Human being with feelings
Join Date: Dec 2011
Location: Norway
Posts: 88
Default Encode Videos with FFmpeg

This is a good one for: Encode Videos with FFmpeg

I have copy / paste this:

6. Add/Remove Audio from a Video

While adding audio, there are 2 possibilities depending on the expected end result.

A. Replace the original Audio
You can overwrite the original audio with a new audio as shown below. By default, the duration of the output will be that of the longer input. That means that if the audio is longer than the video, the video will freeze when it reaches its end whereas the audio will continue until it reaches the end of its duration, and vice-versa.

To avoid that, you could use either use the -shortest option which truncates the output to the same duration as the shortest input or use the -t 300 option which will truncate the output at the 300th second.

1 ffmpeg -i inputAudio.mp3 -i inputVideo.avi outputVideo.avi
2 ffmpeg -i inputAudio.mp3 -i inputVideo.avi -shortest outputVideo.avi
3 ffmpeg -i inputAudio.mp3 -i inputVideo.avi -t 300 outputVideo.avi
4 /**
5 * -i => Input file
6 * -shortest => Makes output to be the same duration as the shortest input
7 * -t => time in seconds
8 */

B. Add an additional Audio track

If you wish to conserve the original audio, and only add a new audio track, it can be done by mapping the audio streams.

1 ffmpeg -i input_video_with_audio.avi -i new_audio.ac3 -map 0 -map 1 -c copy outputVideo.avi
2 /**
3 * -i => Input file
4 * -c => Codec (both audio + video)
5 * -map => maps the input files
6 */

Here, -map 0 copies all streams(audio + video) from the first input file (input_video_with_audio.avi) and -map 1 copies all streams (in this case, one i.e. audio) from the second input file (new_audio.ac3). Hence the output video will now have 2 audio streams. During playback, the user can choose the audio track to listen. (Works with players that support it, like VLC player) By default, it uses the first audio track that was mapped. (-map 0, where 0 refers to the first input file)

Remove Audio
To strip the Audio, use the -an option which tells FFmpeg to exclude all audio streams from the input.

1 ffmpeg -i inputVideo.avi -c copy -an outputVideo.avi
2 /**
3 * -i => Input file
4 * -c => codec (audio + video)
5 * -an => exclude audio streams
6 */
sveinpetter is offline   Reply With Quote