1
votes

I have a video, video.mp4, of 30 seconds, and I have an audio that can change in length, audio.mp3.

My final idea is to have an output video of a loop of video.mp4 for the total length of the audio.mp3, and an overlay of the waveform of the audio.mp3. What I've done is this, in a bash script:

# calculate length of the audio and of the video
tot=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 audio.mp3)
vid=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 video.mp4)
# how many base video we need to loop into the waveform video?
repeattime=`echo "scale=0; ($tot+$vid-1)/$vid" | bc`

# ffmpeg final command
ffmpeg -stream_loop $repeattime -i video.mp4 -i audio.mp3 -filter_complex "[1:a]showwaves=s=1280x100:colors=Red:mode=cline:rate=25:scale=sqrt[outputwave]; [0:v][outputwave] overlay=0:main_h-overlay_h [out]" -map '[out]' -map '1:a' -c:a copy -y output.mp4

Is there a better way to do it in a single ffmpeg command? I know it exists the loop filter in ffmpeg, but it loops frames and I don't know the number of frames of the video.mp4. Also, using $repeattime can result in a number of loop longer then needed (because math calculation is done round up)

2

2 Answers

1
votes

-shortest helps you:

#!/bin/bash
ffmpeg -hide_banner -stream_loop -1 -i "input 1.mp4" -i "input 1.mp3" -filter_complex "
[1:a]showwaves=s=1280x100:colors=Red:mode=cline:rate=25:scale=sqrt[outputwave];
[0:v][outputwave] overlay=0:main_h-overlay_h [v]
" -map [v] -map 1:a -c:a copy -shortest -y output.mp4
0
votes

You can use the shortest option in overlay.

ffmpeg -stream_loop -1 -i video.mp4 -i audio.mp3 -filter_complex "[1:a]showwaves=s=1280x100:colors=Red:mode=cline:rate=25:scale=sqrt[outputwave]; [0:v][outputwave] overlay=0:main_h-overlay_h:shortest=1 [out]" -map '[out]' -map '1:a' -c:a copy -y output.mp4