FFmpeg: How to merge all audio streams into stereo - audio

I have 4 audio streams in my video file. They are from 4 microphones placed at 4 different people speaking. I want to transcode to a preview file that can be listened to on headphones where everybody's voice can be heard.
I have seen the -ac 2 option, but I can't tell if this will merge all the audio streams or just select the 1st two. I've also seen the amerge filter, but the docs say this will produce 4 audio channels in the output file. So I'm wondering how headphones will deal with the additional 2 channels

You have several options. This assumes each individual audio stream in in.mp4 is mono.
Mono
Using the amerge filter and -ac 1:
ffmpeg -i in.mp4 -filter_complex "[0:a]amerge=inputs=4[a]" -ac 1 -map 0:v -map "[a]" -c:v copy out.mp4
With amix:
ffmpeg -i in.mp4 -filter_complex "[0:a]amix=inputs=4[a]" -map 0:v -map "[a]" -c:v copy out.mp4
Stereo
With amerge and -ac 2:
ffmpeg -i in.mp4 -filter_complex "[0:a]amerge=inputs=4[a]" -ac 2 -map 0:v -map "[a]" -c:v copy out.mp4
Manually mixed stereo
Using amerge and pan with custom downmix:
Channel 0 will be 100% in FL
Channel 1 will be 75% in FL & 25% in FR
Channel 2 will be 25% in FL & 75% in FR
Channel 3 will be 100% in FR
ffmpeg -i in.mp4 -filter_complex "[0:a]amerge=inputs=4,pan=stereo|FL<c0+0.75*c1+0.25*c2|FR<0.25*c1+0.75*c2+c3[a]" -map 0:v -map "[a]" -c:v copy out.mp4

Try the amerge audio filter, used to solve this similar question: How do I use ffmpeg to merge all audio streams (in a video file) into one audio channel?
amix should even better should fit your purpose.

Related

ffmpeg: two videos side-by-side with audio1 lang=ger & audio2 lang=eng

I have two videos from two GoPro cameras. Both videos are rendered via ffmpeg side-by-side into a single video (left and right) and the audio is currently combined/mixed into two channels (stereo). I can hear both camera audio channels at the same time.
Two channels stereo:
ffmpeg -i video1.mp4 -i video2.mp4 -filter_complex "[0:v][1:v]hstack=inputs=2[v]; [0:a][1:a]amerge[a]" -map "[v]" -map "[a]" -ac 2 side-by-side.mp4
audio from both videos mixed into a single file with 2 channels - mediainfo
Now I want to switch between the two audio channels (cam1 or cam2) while I'm playing the side-by-side video.
My first try: With four channels (without -ac 2 parameter):
ffmpeg -i video1.mp4 -i video2.mp4 -filter_complex "[0:v][1:v]hstack=inputs=2[v]; [0:a][1:a]amerge[a]" -map "[v]" -map "[a]" side-by-side.mp4
audio from both videos mixed into a single file with 4 channels - mediainfo
But the most video players can't easily select the channels 1+2 or 3+4 while playing.
So I tried two languages:
ffmpeg -i video1.mp4 -i video2.mp4 -filter_complex "[0:v][1:v]hstack=inputs=2[v]; [0:a][1:a]amerge[a]" -map "[v]" -map "[a]" -metadata:s:a:0 language=ger -metadata:s:a:1 language=eng side-by-side.mp4
audio from both videos mixed into a single file with 4 channels with languages - mediainfo
But that's wrong. I can only see german with 4 channels. How can I put channels 1+2 into german and channels 3+4 into english? Afterwards I should be able to use the multi language feature from most video players to switch the audio between cameras.
Thank you,
Miriam
This worked for me
ffmpeg -i Video1.mp4 -i Video2.mp4 -filter_complex "[0:v][1:v]hstack=inputs=2[v]" -map [v] -map 0:a:0 -map 1:a:0 -metadata:s:a:0 language=ger -metadata:s:a:1 language=eng output.mp4
With this you should be able to switch between the audios

faster way to downmix 5.1 videos to stereo?

I have a hand full of movies that are all in 5.1 audio, i need there to be one audio track that is only in stereo. i have found a few suggestions as to how to do this but some dont work for me and others are way too slow.
the one i found that works the fastest is this:
ffmpeg -y -i "input" -map 0:v -c:v copy -map 0:a:0? -c:a:0 copy -map 0:a:0? -c:a:1 aac -ac 2 -metadata:s:a:1 title="Eng 2.0 Stereo" -map 0:a:1? -c:a:2 copy -map 0:a:2? -c:a:3 copy -map 0:a:3? -c:a:4 copy -map 0:a:4? -c:a:5 copy -map 0:a:5? -c:a:6 copy -map 0:a:6? -c:a:7 copy -map 0:s? -c:s copy "output"
sadly i do not remember where it came from.
the problem with this being that it creates a separate audio track, and for my purposes this does not work.
doing just the standard -ac 2 works but is way to slow, i estimated to take over 40 hours to go through all my movies.
edit:
a bit of extra information to throw in here
all the movies are mp4's
all except 2 movies are 5.1 audio (there is 1 7.1 and 1 mono. i dont really care about them tho)
the command i posted runs at about a 40x speed while just doing -ac 4 runs at 2x speed
Bottleneck is the audio encoding.
Command can just be:
ffmpeg -i input.mp4 -c:v copy -c:a aac -ac 2 output.mp4
Make sure your ffmpeg version 4.0 or newer or else encoding will be slower.
Using -c:a libfdk_aac -afterburner 0 may be 2x faster:
ffmpeg -i input.mp4 -c:v copy -c:a libfdk_aac -ac 2 output.mp4
Due to the license of libfdk_aac you need to compile ffmpeg to use this encoder.
Not much else you can do.

How to append 2 seconds of silence to an existing movie MP4 with ffmpeg?

I would like to append 2 seconds of silence to an existing video using ffmpeg.
I would like to keep the last frame displayed while the 2 seconds of video playsback not a black screen.
Thank you.
Use the tpad and apad filters:
ffmpeg -i input.mp4 -filter_complex "[0:v]tpad=stop_mode=clone:stop_duration=2[v];[0:a]apad=pad_dur=2[a]" -map "[v]" -map "[a]" output.mp4
A faster, but less compatible method is to stream copy the video and use the apad filter if your player and output container format supports dissimilar stream durations:
ffmpeg -i input.mp4 -filter_complex "[0:a]apad=pad_dur=2[a]" -map 0:v -map "[a]" -c:v copy output.mp4
If in doubt use the first command.

Include audio from FFmpeg overlay in output

I'm using the following command to combine two video files together, overlaying the second one at a certain point in the first file. The result is what I want except the audio from the overlayed file is missing.
ffmpeg.exe -y -hide_banner -ss 00:00:00.067 -i promo.mov -i tag.mov -filter_complex "[1:v]setpts=PTS+6.5/TB[a];[0:v][a]overlay=enable=gte(t\,6.5)[out]" -map [out] -map 0:a -map 1:a -c:v mpeg2video -c:a pcm_s16le -ar 48000 -af loudnorm=I=-20:print_format=summary -preset ultrafast -q:v 0 -t 10 complete.mxf
Without the -map 0:a I get no audio at all, but the second -map 1:a does not pass the audio from -i tag.mov
I have also tried amix but that combines audio from both clips starting at the beginning, and I want the audio from the second file to begin when that file starts overlaying.
It would also be helpful if I could make the audio from the first clip drop lower at the time of the overlay.
amix doesn't support introducing an input mid-way, so the workaround is to add leading silence. You can use the adelay filter to do this.
make the audio from the first clip drop lower at the time of the overlay
This is possible using a sidechaincompressor which takes two inputs and lowers the volume of the first input based on the volume of the second input.
So use,
ffmpeg.exe -y -hide_banner -ss 00:00:00.067 -i promo.mov -i tag.mov -filter_complex "[1:v]setpts=PTS+6.5/TB[1v];[0:v][1v]overlay=enable=gte(t\,6.5)[vout];[1:a]adelay=6.5s,apad,asplit=2[1amix][1aref];[0:a][1aref]sidechaincompress[0asc];[0asc][1amix]amix=inputs=2:duration=first[aout]" -map [vout] -map [aout] -c:v mpeg2video -c:a pcm_s16le -ar 48000 -af loudnorm=I=-20:print_format=summary -preset ultrafast -q:v 0 -t 10 complete.mxf

Ffmpeg to duplicate an audio stream and encode this new stream

I have some video files that I need to re-encode due to compatibility issues. They are currently mkv files with h.264 video and ac3-a52 audio. I want to keep the h.264 video, convert the container to m4v and create two audio tracks, one with the original ac3-a52 and one copied from that but in aac stereo.
I assume there has to be some sort of audio stream mapping command but I don't know how to map and re-encode at the same time. What command should I enter into ffmpeg to achieve this?
Also, what is the difference between ac3 and ac3-a52? Will an apple TV still be able to pass through ac3-a52 or does that have to be converted to ac3?
this works for me:
ffmpeg -y -i Source.mkv -map 0:v -c:v copy -map 0:a -c:a copy -map 0:a -strict -2 -c:a aac out.mkv
-y – A global option to overwrite the output file if it already exists.
-map 0:v – Designate the video stream(s) from the first input as a source for the output file.
-c:v copy – Stream copy the video. This just muxes the input to the output. No re-encoding occurs.
-map 0:a – Designate the audio stream(s) from the first input as a source for the output file.
-c:a copy – Stream copy the audio. This just muxes the input to the output. No re-encoding occurs.
-strict -2 -c:a aac – Use the native FFmpeg AAC audio encoder. -strict -2 is required as a way that you acknowledge that the encoder is designated as experimental. It is not a great encoder, but it is not too bad at higher bitrates.
According to wikipedia, there is no difference between AC3 and ATSC A/52: the 1st one is the name of the codec, the 2nd is the name of the standard specifying the AC3 codec. Maybe someone have more knowledge about it?
I'm doing the same as the OP, but with an m4v container. I'm using the MacPorts "nonfree" variant of ffmpeg so that I can use libfaac, which gives better audio quality than the built-in AAC encoder and also had the same issue as #dkam. The command line I ended using is like this:
ffmpeg -i input.m4v -map 0:v -c:v copy -map 0:a -c:a:0 copy -map 0:a -c:a:1 libfaac output.m4v
(The videos are for playback on an iPad, which doesn't seem to be able to handle ac3.)
This command will take a video with 1 audio stream, and downmix to stereo and convert the audio stream and add it as a 2nd audio stream. It will be in AAC 384k.
ffmpeg -i INPUT.mkv -strict -2 -map 0 -c copy -map 0:a:0 -c:a:1 aac -b:a 384k -ac 2 OUTPUT.mkv
Explanation of the command
ffmpeg -i INPUT.mkv The application and input file
-strict -2 Enable downmixing support
-map 0 Tell ffmpeg read all Video, Audio, and Subtitle streams for the following arguments
-c copy Copy everything
-map 0:a:0 Tell ffmpeg to read the first audio stream for the following arguments
-c:a:1 aac Output the audio to a 2nd audio channel (0 = first channel) in aac format. Important! You must change the output channel to a higher number if there are multiple audio streams to prevent overwriting them.
-b:a 384k 384k bitrate (I don't know what's good for aac stereo but this is really high since it's for 5.1 aac)
-ac 2 Downmix to stereo
OUTPUT.mkv Output file
More examples
A video with two audio streams. Creating a third audio stream by encoding the first.
ffmpeg -i INPUT.mkv -strict -2 -map 0 -c copy -map 0:a:0 -c:a:2 aac -b:a 384k -ac 2 OUTPUT.mkv
Again a video with two audio streams, but you want to encode the second one
ffmpeg -i INPUT.mkv -strict -2 -map 0 -c copy -map 0:a:1 -c:a:2 aac -b:a 384k -ac 2 OUTPUT.mkv

Resources