Logo

dev-resources.site

for different kinds of informations.

Adding sound wave overlays to videos and pictures using FFMPEG

Published at
8/31/2023
Categories
ffmpeg
videoediting
cli
tips
Author
codepo8
Categories
4 categories in total
ffmpeg
open
videoediting
open
cli
open
tips
open
Author
7 person written this
codepo8
open
Adding sound wave overlays to videos and pictures using FFMPEG

In order to fix a broken video where the camera stopped recording, I wanted to replace the part with a video with a sound wave overlay. There are many services for that, and it is also a feature of video editing software. But I am stingy and a geek, so I wanted to use FFMPEG on the command line instead. This also allows for batch processing.

Note: I didn't embed the videos in this post but GIFs instead. The real videos play the video and audio and the sound wave moves accordingly.

Let's say this is our original video:

Video of me talking about public speaking

This is what it looked like in the end:

The same video with a sound wave on top of it

Here's the command I used to create the above version:

ffmpeg -i Understandable.mp4 \
 -filter_complex "[0:a][email protected]\
 :scale=sqrt:mode=cline,format=yuva420p[v];\
 [v]scale=1280:400[bg];\
 [v][bg]overlay=(W-w)/2:H-h[outv]"\
  -map "[outv]" -map 0:a -c:v libx264 -c:a copy \
  waveform-sqrt-cline.mp4
Enter fullscreen mode Exit fullscreen mode

OK, let's unwrap this:

  • Understandable.mp4 is the input file
  • [email protected] is the hexadecimal colour of the waves I want to create. The @0.3 is the opacity, 30%.
  • The first scale is how the bars should be resized to stay in a certain range. I found squareroot to look the best. Other options are available in the documentation
  • mode is what soundwave you want. This version, cline is a centered, filled line. Other options are point, line and p2p.
  • scale is the size of the generated video of the wave, in this case 1280x400 pixels (as the video is 1280x1080)
  • The overlay defines where on the background video the wave should appear. In this case centred in the horizontal direction and on the bottom of the video.
  • waveform-sqrt-cline.mp4 is the name of the final video.

Change location of the sound wave

You can also center the sound wave in both directions using overlay=(W-w)/2:(H-h)/2:

ffmpeg -i Understandable.mp4 \
 -filter_complex "[0:a][email protected]\
 :scale=sqrt:mode=cline,format=yuva420p[v];\
 [v]scale=1280:400[bg];\
 [v][bg]overlay=(W-w)/2:(H-h)/2[outv]"\
  -map "[outv]" -map 0:a -c:v libx264 -c:a copy \
  waveform-sqrt-cline-centered.mp4
Enter fullscreen mode Exit fullscreen mode

And the result looks like this:

Video showing the sound wave in the vertical center

Other sound wave styles

And you can change the output type to line and change the colour:

ffmpeg -i Understandable.mp4 \
 -filter_complex "[0:a][email protected]\
 :scale=sqrt:mode=line,format=yuva420p[v];\
 [v]scale=1280:400[bg];\
 [v][bg]overlay=(W-w)/2:H-h[outv]"\
  -map "[outv]" -map 0:a -c:v libx264 -c:a copy \
  waveform-sqrt-line.mp4
Enter fullscreen mode Exit fullscreen mode

Video showing the sound wave as lines

This is what the point to point mode looks like:

ffmpeg -i Understandable.mp4 \
 -filter_complex "[0:a]showwaves=colors=0xffffff\    
 :scale=sqrt:mode=p2p,format=yuva420p[v];\ 
 [v]scale=1280:400[bg];\
 [v][bg]overlay=(W-w)/2:H-h[outv]"\
  -map "[outv]" -map 0:a -c:v libx264 -c:a copy \
 waveform-sqrt-p2p.mp4 
Enter fullscreen mode Exit fullscreen mode

Video with point to point line

Adding a sound wave to a static image

If you only want the audio from a video and use a background image instead, this is what to use. This creates a 400x400 pixel video with the image as the background and the sound wave on top:

ffmpeg -i Understandable.mp4  -i chris.jpg\
 -filter_complex "[0:a][email protected]\
 :scale=sqrt:mode=cline,format=yuva420p[v];\
 [1:v]scale=400:400[bg];\
 [bg][v]overlay=(W-w)/2:(H-h)/2[outv]"\
  -map "[outv]" -map 0:a -c:v libx264 -c:a copy \
  static-image.mp4
Enter fullscreen mode Exit fullscreen mode

Soundwave on top of a static image

FFMPEG is amazing

Granted, the syntax of FFMPEG is pretty nuts, but it is also super powerful. Want to know for example how I created the GIFs of this post?

First, I resized all the MP4s in the folder to 1/3 of their size and saved them as small*:

for f in *.mp4 ; \
do ffmpeg -i "$f" -vf scale=w=iw/3:h=ih/3 "small-$f" ;\
done             
Enter fullscreen mode Exit fullscreen mode

Then I took those and created gifs

for f in small-*.mp4 ; \
do ffmpeg -i "$f" -sws_dither ed -ss 5 -t 1 "e-$f.gif" ; \
done      
Enter fullscreen mode Exit fullscreen mode

In English: Get all small-*.mp4s in the current folder, and create a GIF from second 5 of the video, one second long, using error diffusion.

Automate all the things!

Soundwave from transformers pressing a button

videoediting Article's
26 articles in total
Favicon
Davinci Resolve 19.1.2 x Linux Mint 22 AAC Audio Workaround
Favicon
Top Video Editing Apps for Android: A Comprehensive Guide to the Best Tools
Favicon
How to Install DaVinci Resolve 19 (studio) on Linux Mint 22 with AMD Radeon Graphics Card with a Script fast 2025
Favicon
Video Editing and Graphic Design Course
Favicon
Introducing Videmix: The Effortless Online Video Editing Platform
Favicon
Submagic: Turn Videos Viral with AI Captions
Favicon
Upgrade Your Social Media with Amazing Video Editing!
Favicon
Effortless Content Creation with EngagexAI: Videos and Podcasts Made Easy!
Favicon
How we edited 175 conference videos in 5 hours
Favicon
How to Install DaVinci Resolve 19 (beta) on Linux Mint 22 with AMD Radeon Graphics Card
Favicon
Mastering Adobe Premiere Pro A Beginner-Friendly Tutorial for Professional-Looking Videos
Favicon
How to cancel capcut pro subscription
Favicon
13 Best Video Transitions that’ll Make You a PRO Video Creator
Favicon
VideoProc: The Must-Have Tool for Modern Creators
Favicon
Get Professional-Quality Videos in Minutes with the Amazing AI Video Editor
Favicon
Title: Exploring the World of 3D Video Editing: Techniques and Tips
Favicon
Adding sound wave overlays to videos and pictures using FFMPEG
Favicon
Top 10 Best Video Editing Apps
Favicon
Edit a video from start to finish by joining ADMEC's Video Editing Course
Favicon
Appwrite Loves Open Source: Why I Choose To Support Kdenlive
Favicon
How to Crop Videos Quickly
Favicon
Best Photo & video Editing Software's for Android?
Favicon
Get started editing videos with the VEED API.
Favicon
How to Install DaVinci Resolve 16 on Linuxmint 20 with AMD Radeon Graphic Card
Favicon
How to Edit your videos for youtube using iMovie
Favicon
Creating a video editing tool in python (part 1 of many)

Featured ones: