Sunday, January 26, 2020

FFMpeg Audio Waveform

If you've spent any time on this blog you've likely seen many instances of using Ffmpeg on video.  Ffmpeg also provides functionality for audio and this post will focus on generating a graphical waveform from the audio and overlaying it alongside the video.

For this, we need Johnny...Johnny Cash.

What we're gonna do here is: download the video, extend the video height to accommodate a graphical waveform on the bottom, generate a waveform video and overlay it along the bottom of the output video.

This is our bogey;

Let's download the video;
$ youtube-dl https://www.youtube.com/watch?v=8AHCfZTRGiI

Next, generate the audio waveform from the video, note the same width of the input video and a fixed height of 120;
$ ffmpeg -i input.mp4 -filter_complex "[0:a]showwaves=s=854x120:mode=line,format=yuv420p[v]" -map "[v]" -map 0:a -c:v libx264 -c:a copy waveform.mp4

Lastly, overlay the waveform on the bottom of the frame after padding the bottom to account for the additional video space, note the use of the waveform video height (e.g. 120);

$ ffmpeg -i input.mp4 -vf 'pad=iw:ih+120 [top]; movie=waveform.mp4 [bottom];[top][bottom] overlay=0:main_h-120' -acodec copy output.mp4

You're left with Johnny and his amazing vocal range co-star'ing;

Now go make something cool.



1 comment: