adding background music to video using ffmpeg - ffmpeg

I am trying to add bacground music to a video using ffmpeg and it is working fine but I want if length of video is more than the music file then music should start playing again till the video is over -
ffmpeg -i 1.mp4 -i 2.mp3 -map 0:v:0 -map 1:a:0 output.mp4
is there anyway to perform this action?

you can use the following example:
ffmpeg -i ./INPUUT_VIDEO.mp4 -filter_complex "amovie=./INPUT_MUSIC.M4A:loop=0,asetpts=N/SR/TB[aud];[0:a][aud]amix[a]" -map 0:v -map '[a]' -c:v copy -c:a aac -b:a 256k -shortest ./OUTPUT_VIDEO.mp4
you can set/change the audio bit rate by using -b:a flag, for more detail, take a look at this document.
EDIT:
for replacing new audio in the video (repeat it until the end of the video):
ffmpeg -i VIDEO.mp4 -stream_loop -1 -i MUSIC.mp3 -c copy -shortest -map 0:v:0 -map 1:a:0 OUTPUT.mp4
replacing new audio in the video (repeat video until the end of the audio):
ffmpeg -stream_loop -1 -i VIDEO.mp4 -i MUSIC.mp3 -c copy -shortest -map 0:v:0 -map 1:a:0 OUTPUT.mp4

Related

ffmpeg - cannot find matching stream for unlabeled input pad 1 on filter Parsed_amix_0

I have an mp4 and I am trying to merge multiple audio files into it. The command is generated dynamically given the number of audio and their offset. Everything works when I have multiple audios to merge but doesn't when I have one audio file to merge.
Here is the command to merge single audio into the mp4
ffmpeg -y -i ./video.mp4 -itsoffset 00:00:1.753 -i ./audio0.mp3 -filter_complex amix -map 0:v -map 1:a -c:v copy -async 1 -c:a aac -strict experimental -t 10 ./finalVideo.mp4
Here is the command to merge multiple audio files which works
ffmpeg -y -i ./video.mp4 -itsoffset 00:00:1.753 -i ./audio0.mp3 -itsoffset 00:00:0.113 -i ./audio/audio1.mp3 -filter_complex amix -map 0:v -map 1:a -map 2:a -c:v copy -async 1 -c:a aac -strict experimental -t 10 ./finalVideo.mp4

How to speed up sound and video and modify the pitch?

I want to increase speed on both the sound and the video, while adding pitch to the audio alone.
I have those two ffmpeg commands, but I don't know how to make them work together.
ffmpeg -i input.mkv -filter_complex "[0:v]setpts=0.94*PTS[v];[0:a]atempo=1.06[a]" -map "[v]" -map "[a]" output.mkv
and
ffmpeg -i input.mkv -filter:a "atempo=1.06,asetrate=44100*1.25" output.mkv
Combined command:
ffmpeg -i input.mkv -filter_complex "[0:v]setpts=0.94*PTS[v];[0:a]atempo=1.06,asetrate=44100*1.25[a]" -map "[v]" -map "[a]" output.mkv
Although I assume you want to reset the audio sample rate from 55125 back to 44100:
ffmpeg -i input.mkv -filter_complex "[0:v]setpts=0.94*PTS[v];[0:a]atempo=1.06,asetrate=44100*1.25,aformat=sample_rates=44100[a]" -map "[v]" -map "[a]" output.mkv
Alternatively, just add -ar 44100 output option to the first example.

FFMPEG map multiple audio input files to 1 single image file in order to create multiple video output files

I am attempting to have multiple output files in ffmpeg map to multiple inputs however instead of each input mapping to a unique output I get the first input mapping to the first video and then the next videos never get created at all, I will describe exactly what I am trying to achieve below,
I need to:
create multiple video output files
from multiple audio input files
which all use the same one common image file to create the
video
I will post my command below, any help would be greatly appreciated thanks
-y -i input1.mp3 -i input2.mp3 -f image2 -loop 1 -r 2 -i imagefile.png -shortest -c:a aac -c:v mpeg4 -crf 18 -preset veryfast -movflags faststart -map 0 output1.mp4 -map1 output2.mp4
Basic command
ffmpeg -i input1.mp3 -i input2.mp3 -loop 1 -framerate 10 -i imagefile.png -map 2:v -map 0:a -vf format=yuv420p -shortest output1.mp4 -map 2:v -map 1:a -vf format=yuv420p -shortest output2.mp4
The video is filtered and encoded once per output.
With the split filter
ffmpeg -i input1.mp3 -i input2.mp3 -loop 1 -framerate 10 -i imagefile.png -filter_complex "[2:v]format=yuv420p,split=outputs=2[v0][v1]" -map "[v0] -map 0:a -shortest -movflags +faststart output1.mp4 -map "[v1]" -map 1:a -shortest -movflags +faststart output2.mp4
The video is filtered once total, and encoded separately for each output.
Pipe
ffmpeg -y -v error -loop 1 -framerate 10 -i imagefile.png -filter_complex "[0:v]format=yuv420p[v]" -map "[v]" -c:v libx264 -f nut - | ffmpeg -y -i - -i input1.mp3 -i input2.mp3 -map 0:v -map 1:a -c:v copy -shortest -movflags +faststart output1.mp4 -map 0:v -map 2:a -c:v copy -shortest -movflags +faststart output2.mp4
The video is filtered and encoded only once and each output stream copies it.

Correct syntax for ffmpeg filter combination?

I'm playing with ffmpeg to generate a pretty video out of an mp3 + jpg.
I've managed to generate a video that takes a jpg as a background, and adds a waveform complex filter on top of it (and removes the black bg as an overlay).
This works:
ffmpeg -y -i 1.mp3 -loop 1 -i 1.jpg -filter_complex "[0:a]showwaves=s=1280x720:mode=cline,colorkey=0x000000:0.01:0.1,format=yuva420p[v];[1:v][v]overlay[outv]" -map "[outv]" -pix_fmt yuv420p -map 0:a -c:v libx264 -c:a copy -shortest output.mp4
I've been trying to add text somewhere in the generated video too. I'm trying the drawtext filter. I can't get this to work however, so it seems I don't understand the syntax, or how to combine filters.
This doesn't work:
ffmpeg -y -i 1.mp3 -loop 1 -i 1.jpg -filter_complex "[0:a]showwaves=s=1280x720:mode=line,colorkey=0x000000:0.01:0.1,format=yuva420p[v];[1:v][v]overlay[outv]" -filter_complex "[v]drawtext=text='My custom text test':fontcolor=White#0.5: fontsize=30:font=Arvo:x=(w-text_w)/5:y=(h-text_h)/5[out]" -map "[outv]" -pix_fmt yuv420p -map 0:a -c:v libx264 -c:a copy -shortest output.mp4
Would love some pointers!
Filteres operating in series should be chained together
ffmpeg -y -i 1.mp3 -loop 1 -i 1.jpg \
-filter_complex "[0:a]showwaves=s=1280x720:mode=line,colorkey=0x000000:0.01:0.1,
format=yuva420p[v];
[1:v][v]overlay,
drawtext=text='My custom text test':fontcolor=White#0.5:
fontsize=30:font=Arvo:x=(w-text_w)/5:y=(h-text_h)/5[outv]"
-map "[outv]" -pix_fmt yuv420p -map 0:a -c:v libx264 -c:a copy -shortest output.mp4
(You applied the drawtext onto the output of showwaves; it can be directly applied on the overlay output)

Merge video and audio while delaying audio by x seconds

This works for merging audio and video
ffmpeg -i video.mp4 -i audio.ogg -filter_complex "[0:a][1:a]amerge=inputs=2[a]" -map 0:v -map "[a]" -c:v copy -c:a libvorbis -ac 2 -shortest out.mp4 -y -nostdin
I can't figure out how to delay the audio so it starts x seconds into the video. I have tried -itsoffset but it doesn't work.
Use
ffmpeg -i video.mp4 -i audio.ogg -filter_complex "[1:a]adelay=1000|1000[a1];[0:a][a1]amerge=inputs=2[a]" -map 0:v -map "[a]" -c:v copy -c:a libvorbis -ac 2 -shortest out.mp4 -y -nostdin
The adelay adds 1000 ms of silence to both channels of the OGG.
This is more of a workaround, but you could concatenate 1 second of silence with your ogg first:
https://trac.ffmpeg.org/wiki/Concatenate

Resources