Given an MP3 I would like to extract the waveform from the file into an image (.png)
Is there a package that can do what I need ?
FFmpeg showwavespic
FFmpeg can do it in a single command as usual:
Sample command:
sudo apt install ffmpeg
ffmpeg -i in.flac -filter_complex "showwavespic=s=640x320:colors=black" \
-frames:v 1 out.png
You can also set colors
in RGB colors=0x0088FF
: Using hex colors with ffmpeg's showwaves
Sample test data of me saying "Hello my name is Ciro Santilli" with two identical stereo channels:
wget -O in.flac https://raw.githubusercontent.com/cirosantilli/media/d6e9e8d0b01bccef4958eb8b976c3b0a34870cd3/Hello_my_name_is_Ciro_Santilli.flac
Output:
Background color
The background is transparent by default, but:
and so we reach:
ffmpeg -i in.flac -f lavfi -i color=c=black:s=640x320 -filter_complex \
"[0:a]showwavespic=s=640x320:colors=white[fg];[1:v][fg]overlay=format=auto" \
-frames:v 1 out.png
Added to the Wiki now ;-)
For the uninitiated, that CLI creates a processing graph:
black background (1:v) ------------------------> overlay ----> out.png
^
|
in.flac (0:a) ----> showwavespic ----> (fg) -------+
where e.g. the overlay
filter takes two image inputs and produces the desired output, and fg
is just a name assigned to an intermediate node.
Split channels
The tutorial also covers other options such as split channels with -filter_complex "showwavespic=s=640x480:colors=black:split_channels=1"
:
gnuplot plot with axes
OK, I'll admit it, FFmpeg can't do this alone (yet!). But the Wiki already provides a data export method to gnuplot that works:
ffmpeg -i in.flac -ac 1 -filter:a aresample=8000 -map 0:a -c:a pcm_s16le -f data - | \
gnuplot -p -e "set terminal png size 640,360; set output 'out.png'; plot '<cat' binary filetype=bin format='%int16' endian=little array=1:0 with lines;"
Video representations
See: https://superuser.com/questions/843774/create-a-video-file-from-an-audio-file-and-add-visualizations-from-audio
Tested on Ubuntu 20.04, FFmpeg 4.2.4.