Mixing two wav files in FFmpeg

2019-09-09 12:25发布

I have 2 wav files wav1 and wav2.I want to play wav2 every 45 seconds over the wav1 using FFmpeg.

标签: ffmpeg
2条回答
虎瘦雄心在
2楼-- · 2019-09-09 12:29

Ok, first create a 45-second file to loop of wav2

ffmpeg -i wav2 -af apad -t 45 wav2-padded.wav

Now, the mix

ffmpeg -i wav1 -f lavfi -i amovie=wav2-padded:loop=9999 -filter_complex [0][1]amix[out] -map [out] -shortest mixed.wav
查看更多
一夜七次
3楼-- · 2019-09-09 12:47

I think this should accomplish what you want

#!/bin/bash

interval_millis=45000

regex="Duration: ([0-9:.]+)"
[[ $(ffmpeg -i $1 2>&1) =~ $regex ]]

duration_millis=$(echo "${BASH_REMATCH[1]}" | \
    awk -F: '{ print 1000 * (($1 * 3600) + ($2 * 60) + $3) }')

duration_counter=$interval_millis
i=1

while [[ $duration_counter -lt $duration_millis ]]; do
    inputs+=" -i $2"
    delay_params+="[$i]adelay=$duration_counter[del$i];"
    amix_params+="[del$i]"

    ((i++))
    ((duration_counter += $interval_millis))
done

ffmpeg -i $1 $inputs -filter_complex \
    "$delay_params[0]${amix_params}amix=inputs=$i:duration=first" out.wav

I had some problems with adelay on a 2015 build of ffmpeg, so make sure you have an up-to-date version to run this. You call the script as

./script.sh long-file short-file
查看更多
登录 后发表回答