How can I create a waveform image of an MP3 in Linux?

前端 未结 7 1095
旧时难觅i
旧时难觅i 2021-02-01 21:03

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 ?

相关标签:
7条回答
  • 2021-02-01 21:50

    FFmpeg showwavespic

    FFmpeg can do it in a single command as usual:

    • tutorial on wiki at: https://trac.ffmpeg.org/wiki/Waveform
    • documentation for the filter: https://ffmpeg.org/ffmpeg-filters.html#showwavespic

    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:

    • the Wiki teaches us how to add a image background: https://trac.ffmpeg.org/wiki/Waveform#Addingabackground
    • Overlay timelapse video on solid background colour using ffmpeg teaches us how to generate a solid color for an image

    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.

    0 讨论(0)
提交回复
热议问题