How make audio to start from the specified point in the video and last specified amount of time

Viewed 53

Lets say I have sample video named "samplevideo" and sample audio named "sampleaudio". I want to replace the audio of the samplevideo with the sampleaudio starting from 5 sec to 12 sec. Until 5 sec mark the video audio won't change, then sampleaudio starts and lasts for 7 sec.

I tried to come up with ffmpeg command that solves this problem:

ffmpeg -i samplevideo.mp4  -i sampleaudio.aac -ss 20 -map 0:v  -map 1:a -c:v copy output.mp4

Problem:
It cuts video starting from 20 sec time mark. What I want is to persist the video and the original audio stream until it hits desired time mark and then use sampleaudio until desired end time.

I would appreciate any help.

3 Answers

Just use amix with selective muting.

ffmpeg -i samplevideo.mp4  -i sampleaudio.aac \
       -filter_complex "[0:a]volume=0:enable=between(t\,5\,12)[a0];\
                        [1:a]atrim=0:7,adelay=5s:all=1[a1];\
                        [a0][a1]amix=normalize=0:duration=first[aout]"
       -map 0:v  -map [aout] -c:v copy output.mp4

Use ffmpeg 5.0

You need to splice audio using filtergraph. Try this:

ffmpeg -i samplevideo.mp4  -i sampleaudio.aac \
       -filter_complex "[1:a]asendcmd='5.0 astreamselect map 1;\
                                      12.0 astreamselect map 0',\
                        [0:a]astreamselect=inputs=2:map=0[aout]"
       -map 0:v  -map [aout] -c:v copy output.mp4
Related