Search code examples
ffmpegffmpeg-php

FFMPEG replace video audio with filter_complex


I wants such a output video where audio of output is created using ffmpeg -filter_complex mechanism,

/usr/local/Cellar/ffmpeg/3.2.2/bin/ffmpeg 
-i /uploads/videos/1487684390-lg9htt0RW2.mov 
-i /uploads/audios/1487664761-SCPbo6Tkac.mp3 
-filter_complex "
[0:a]atrim=0:8.70824980736,asetpts=PTS-STARTPTS[aud1]; 
[1:a]atrim=0:12.9567301273,asetpts=PTS-STARTPTS[aud2];
[0:a]volume=0.3,atrim=start=8.70824980736:21.6649799347,asetpts=PTS-STARTPTS[slow_aud]; 
[aud2][slow_aud] amerge=inputs=2[a_merged];
[0:a]atrim=start=21.6649799347:31.6410098076 [remaining_audio]; 
[aud1][a_merged][remaining_audio]concat=n=3:v=0:a=1[aout]" 
-map 0:v -map "[aout]" -c:v copy -acodec mp3 
/uploads/output/1487684390-lg9htt0RW2.mov

Original Audio Recorded Based On UTC timestamp Vs Original Video Recorded Based on UTC timestamp

            13:00-------- Original Event Audio -------- 13:20
       12:50------------- Event Video Recorded --------------13:30

This is my requirement

So the audio of the output video should contains

  1. First 10 seconds(12:50 - 13:00) are Audio of Event Video Recorded
  2. Next 20 seconds (13:00 -13:20) are merged audio(Original Audio+ Original Video where Original video's audio volume is .3)
  3. Remaining 10 seconds(13:21-13:30) of video will play remaing audio of video

What I am getting by above commands

  1. First 10 seconds(12:50 - 13:00) are Audio of Event Video Recorded Achieved
  2. Next 20 seconds (13:00 -13:20) are merged audio(Original Audio+ Original Video where Original video's audio volume is .3) Achieved
  3. Remaining 10 seconds(13:21-13:30) of video will play remaining audio of video Not Achieved

Solution

  • You haven't reset the timestamps of the remaining audio, as the concat filter requires. So, it should be

    [0:a]atrim=start=21.6649799347:31.6410098076,asetpts=PTS-STARTPTS[remaining_audio]; 
    

    A shorter way of achieving the same result is

    -filter_complex
    "[1:a]adelay=12956.7301273|12956.7301273[mp3];
     [0:a]volume=0.3:enable='between(t,8.70824980736,21.6649799347)'[vid];
     [vid][mp3]amix[aout]"