Loop movie and mix with audio by ffmpeg - audio

I have a movie file and an audio file with different length.
Audio file is longer than video file.
I want to loop video and mix with audio, then set length same as audio file.
ffmpeg -i "Pentagon_VJ_background_loop.mp4" -stream_loop -1 -t 60 -i "Out1.wav" -map 0:0 -map 1:0 output.mp4
With this I have -stream_loop -1 but movie doesn't loop. and have no way to set the length to audio file(I use -t 60 instead).
Does anyone help??

ffmpeg -stream_loop -1 -i "Pentagon_VJ_background_loop.mp4" -i "Out1.wav" -shortest -map 0:0 -map 1:0 output.mp4
Explanation:
-stream_loop -1 must be placed before the file that you want to loop
-shortest tells ffmpeg that output.mp4 should have the duration of the shortest input. Since the video is now an endless video, the shortest input is the audio file.

Related

Why is adding background music to video using `ffmpeg -i input.mp4 -i music.mp3 output.mp4` not working?

I explored google and StackOverflow for how to add background music to the video and many of them suggested to use
ffmpeg -i input.mp4 -i audio.mp3 -shortest output.mp4
I have been trying to achieve this but it just does not work. When I try to add map like
ffmpeg -i "input.mp4" -i bg.mp3 -map 0:v:0 -map 1:a:0 oo.mp4
The video sound is replaced by the bg.mp3
And if I try -map 0 -map 1:a:0 or not provide map, the audio is not added at all.
How do I add the background music? I don't also get any error.
-map is a selector; select a type of stream from an input "file". To merge two audio streams, you need an audio filter:
ffmpeg -i input.mp4 -i audio.mp3 -lavfi "[0:a][1:a]amerge[out]" -map 0:v -map [out]:a -shortest output.mp4
-lavfi: Same as -filter_complex, because you have two inputs
[0:a][1:a] take audio stream from the first and second inputs
-map 0:v select the video stream from the first input without processing
-map [out]:a select the audio stream from the filtergraph (processed)
The shortest option in the amerge filter is set by default.
If you have problems, you might want to check also the amix filter, the audio codecs of your files, and the volume filter to adjust the volume of the inputs in the filtergraph.
Additional references:
https://ffmpeg.org/ffmpeg-filters.html#amerge
https://ffmpeg.org/ffmpeg-filters.html#amix
https://ffmpeg.org/ffmpeg-filters.html#volume
If the video length is longer than music you can add "-stream_loop -1" to repeat music until end of video
ffmpeg -i video_with_audio.mkv -stream_loop -1 -i background_music.mp3 -lavfi "[0:a][1:a]amerge[out]" -map 0:v -map [out]:a -shortest video_with_audio_and_background_music.mkv
If you want increase or decrease the volume, follow this command:
ffmpeg -i video_with_audio.mkv -stream_loop -1 -i background_music.mp3 -lavfi "[1:a]volume=0.2,apad[A];[0:a][A]amerge[out]" -map 0:v -map [out]:a -shortest video_with_audio_and_background_music.mkv

ffmpeg - add 3 audio streams to video

I have the following problem.
In the folder there is video.mp4 file (contain 1 audio stream). There are also 3 different files audio1.wav, audio2.wav, audio3.wav. These files I need to 'attach' as multi stream to the video file - so the user can choose the audio language in VLC player or similar. Result must be one audio at the time - no mixing it all together.
Now, I've done it via Premiere Pro with multitrack (quicktime export to mov), and then I run a script to change audio stream names to correspond with the audio language (iso 639-2 ) and output the mp4 file. All works well, but I wonder if there is simple way to do everything via ffmpeg ( .bat script ). I have a working script for replacing audio in the video, but I need to add few additional .wav to the video file as separate audio tracks.
Any help will be appreciated!
To add a new audio track into an existing video with audio, use
the -i parameter to specify all the input files (original video and additional audios)
the -map option to manually select the tracks of each input (https://trac.ffmpeg.org/wiki/Map)
in your case,
-map 0 to copy all streams from the input #0 (video)
-map 1:a to include all audio streams from input#1 file (audio1)
-map 2:a to include all audio streams from input#2 file (audio2)
and so on
and
-shortest to crop the output to the shortest input
and additionally you may want to use
-c:v copy to copy the video stream without reencoding.
so, try this (line split for readability)
ffmpeg -i video.mp4 -i input1.mp3 -i input2.mp3
-map 0 -map 1:a -map 2:a
-c:v copy -shortest
output.mp4
and (addording to your comment) adding metadata for the audio tracks
ffmpeg -i video.mp4 -i input1.mp3 -i input2.mp3
map 0 -map 1:a -map 2:a
-metadata:s:a:0 language=eng
-metadata:s:a:1 language=ger
-metadata:s:a:2 language=fra
-disposition:a:0 default
-c:v copy -shortest
output.mp4

ffmpeg HLS segmentation audio and video files

I want to perform HLS segmentation and get audio and video in separated files for each segment from .mp4 video with audio. No need to adapt bitrate, because that will cause an extra delay.
I tried to use
ffmpeg -i videoInput.mp4 -c:v copy –c:a copy -hls_segment_type mpegts –map a:0 –map v:0 -hls_time 1 out.m3u8 audio.ts
Which generates the video files perfectly but just one audio file.
Thank you.
From the FFmpeg documentation I understand:
When there are two or more variant streams, the output filename
pattern must contain the string "%v", this string specifies the
position of variant stream index in the output media playlist
filenames. The string "%v" may be present in the filename or in the
last directory name containing the file. If the string is present
in the directory name, then sub-directories are created after
expanding the directory name pattern. This enables creation of
variant streams in subdirectories.
'''
For me:
ffmpeg -hide_banner -t 5:00 -i "$IN" -c copy -map a:0 -map v:0 -hls_segment_type mpegts -hls_time 1 -f hls -var_stream_map "a:0 v:0" v_%v/file.m3u8
creates both streams in v_0 & v_1.
I don't know the exact solution, but as a workaround, you can extract the audio first with:
ffmpeg -i video.mp4 -c:a copy -vn audio.aac
Then apply segmentation for video and audio files separately
Audio:
ffmpeg -i audio.aac -c:a copy -hls_segment_type mpegts -map a:0 -hls_time 1 out_aud.m3u8
Video:
ffmpeg -i video.mp4 -c:v copy -hls_segment_type mpegts -map v:0 -hls_time 1 out_vid.m3u8

Singler line FFMPEG cmd to Merge Video /Audio and retain both audios

I have a project that requires merging of a video file with another audio file. The expected out put is an video file that will have both the audio from actual video and the merged audio file. The length of the output video file will be same to the size of the actual video file.
Is there a single line FFMPEG command to achieve this using copy and -map parameters ?
The video form I will be using is either flv or mp4
And the audio file format will be mp3
There can be achieved without using map also.
ffmpeg -i video.mp4 -i audio.mp3 output.mp4
In case you want the output.mp4 to stop as soon as one of the input stops (audio/video)
then use
-shortest
For example: ffmpeg -i video.mp4 -i audio.mp3 -shortest output.mp4
This will make sure that the output stops as and when any one of the inputs is completed.
Since you have asked that you want to do it with map. this is how you do it:
ffmpeg -i video.mp4 -i audio.mp3 -map 0:0 -map 1:0 -shortest output.mp4
Now, since you want to retain the audio of the video file, consider you want to merge audio.mp3 and video.mp4. These are the steps:
Extract audio from the video.mp4
ffmpeg -i video.mp4 1.mp3
Merge both audio.mp3 and 1.mp3
ffmpeg -i audio.mp3 -i 1.mp3 -filter_complex amerge -c:a libmp3lame -q:a 4 audiofinal.mp3
Remove the audio from video.mp4 (this step is not required. but just to do it properly)
ffmpeg -i video.mp4 -an videofinal.mp4
Now merge audiofinal.mp3 and videofinal.mp4
ffmpeg -i videofinal.mp4 -i audiofinal.mp3 -shortest final.mp4
note: in the latest version of ffmpeg it will only prompt you to use '-strict -2' in case it does then use this:
ffmpeg -i videofinal.mp4 -i audiofinal.mp3 -shortest -strict -2 final.mp4
hope this helps.
You can not do that using one cmd.
1. Get the audio from video file, the audio file name is a.mp3
ffmpeg.exe -i video.mp4 a.mp3
2. Merge two audio files(audio.mp3+a.mp3=audiofinal.mp3)
ffmpeg.exe -i audio.mp3 -i a.mp3 -filter_complex amerge -c:a libmp3lame -q:a 4 audiofinal.mp3
3. Merge video file and audio file(video.mp4+audiofinal.mp3=output.mp4)
ffmpeg.exe -i video.mp4 -i audiofinal.mp3 -map 0:v -map 1:a -c copy -y output.mp4
I don't think extracting the audio from the video is necessary. We can just use -filter_complex amix to merge both audios:
ffmpeg -i videowithaudio.mp4 -i audiotooverlay.mp3 -filter_complex amix -map 0:v -map 0:a -map 1:a -shortest videowithbothaudios.mp4
-filter_complex amix overlays the audio from the first input file on top of audio in the second input file.
-map 0:v video stream of the first input file.
-map 0:a audio stream of the first input file.
-map 1:a audio stream of the second input file.
-shortest the length of the output is the length of the shortest input
Use case:
add music to your background
you rendered a video, but muted some part of it, so you don't want to render it again(coz it's too long), instead you render only audio track(fast) and wanna merge it with original video.
Assuming
you have your video with you speech (or just audio track, whatever)
your music_file is not loud. Otherwise, you will not hear yourself D:
Steps:
1) Extract audio from the video
ffmpeg -i test.mp4 1.mp3
test.mp4 - your file
2) Merge both audio.mp3 and 1.mp3
ffmpeg -i audio.mp3 -i 1.mp3 -filter_complex amerge -c:a libmp3lame -q:a 4 audiofinal.mp3
audiofinal.mp3 - audio with music
3) Delete audio from original
ffmpeg -i example.mkv -c copy -an example-nosound.mkv
example-nosound.mkv - your video without audio
4) Merge with proper audio
ffmpeg -i audiofinal.mp3 -i example-nosound.wmv -c:v copy -vcodec copy final.wmv
final.wmv - your perfect video.
This is very easy with FFmpeg:
ffmpeg -i vid.mp4 -i audio.mp3 -codec:a libmp3lame -ar 44100 -ab 64k -ac 1 -q:v 1 -pix_fmt yuv420p -map 0:0 -map 1:0
First remove the sound from video if you are not able to merge video and audio by using this command:
ffmpeg -i video.mp4 -an videofinal.mp4

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