日本語

How to Make a Video of Audio Waveforms with FFmpeg showwaves

​​FFmpeg​audio,command line,video

The showwaves filter in FFmpeg can turn an audio file into a video in which the waveform moves with the sound. This is useful when you want to post a podcast or a recorded audio-only file to a video site or social media as a video. For making a single image of the whole waveform, see my other article on this blog, "How to Make an Image of an Audio Waveform with FFmpeg." When making a video, you also need to watch for three things: how faint the waveform color is, how much is drawn in one frame, and the offset between the sound and the waveform.

Here is the command I used to export a 10-second test audio to MP4 and confirm that the waveform appears at the same time as the sound.

bash
ffmpeg -i voice.wav -filter_complex "[0:a]aformat=channel_layouts=mono,showwaves=s=1280x360:mode=cline:rate=30:colors=0x00c8e8:draw=full,setpts=PTS-STARTPTS[w];color=white:s=1280x360:r=30[bg];[bg][w]overlay=format=auto:shortest=1,format=yuv420p[v]" -map "[v]" -map 0:a -c:v libx264 -c:a aac -b:a 192k out.mp4

The following is a GIF of the first 4.5 seconds of the resulting video. The playback position display at the top was added when making the GIF.

A video in which a light-blue waveform stretches and shrinks vertically in the middle of a white background. While the playback position moves from 0 to 4.5 seconds, large waveforms appear every 0.5 seconds and shrink; from 3 to 4 seconds there is no line; from 4 seconds on, the waveform is a thin band
The first 4.5 seconds of the video exported with the final command
Note

The test environment was Ubuntu 24.04.5, FFmpeg 6.1.1 (Ubuntu package 6.1.1-3ubuntu5), and a virtual machine with 4 CPU cores and 16 GB of memory. The audio was synthetic (a sine wave and pink noise generated with FFmpeg, 44.1 kHz WAV), and the video was encoded to H.264 with libx264. I did not test recorded human voice or music, compressed audio input such as MP3, other FFmpeg versions, or actual uploads to video sites or social media.

The test audio

I made a 44.1 kHz stereo WAV divided into the following sections. The amplitude is given relative to a maximum sound level of 1.

Section Left Right
0 to 3 seconds A 220 Hz tone with amplitude 0.8 that plays every 0.5 seconds and decays Same as left
3 to 4 seconds Silence Silence
4 to 7 seconds A small 440 Hz tone with amplitude 0.05 Same as left
7 to 10 seconds Silence A 330 Hz tone with amplitude 0.4, whose loudness slowly fluctuates

Without options, the output is 600×240 at 25 fps

The shortest form is:

bash
ffmpeg -i voice.wav -filter_complex showwaves basic.mp4

This produces an MP4. I did not specify -map, but the original audio was also converted to AAC and included together with the video. The video properties from ffprobe were:

Item Value
Size 600×240
Frame rate 25 fps
Pixel format yuv444p
H.264 profile High 4:4:4 Predictive
Duration 10.12 seconds (253 frames; the audio is 10.000 seconds)

According to the source code of showwaves in FFmpeg 6.1.1, the only video formats showwaves can output are RGBA and gray. In this test, the output was converted to yuv444p and encoded as H.264. In the table of H.264 profiles in MDN's guide to video codecs, only High 4:4:4 Predictive supports 4:4:4, while Main and High support up to 4:2:0. If you want a video with the Main or High profile, add format=yuv420p at the end of the filter. When I did, the profile became High and the pixel format became yuv420p.

The reason the video is 0.12 seconds longer than the audio is explained in the section below, "When the waveform appears later than the sound, use setpts to start the timestamps at 0."

If the waveform is too faint to see, use draw=full

When I exported with the size and frame rate specified, the waveform was drawn in a very faint color. The top image below is one frame exported with the default draw=scale, and the bottom one uses draw=full. Both were drawn in light blue (colors=0x00c8e8) on a white background, with a width of 760 and rate=30.

Two waveform images stacked vertically. The top one, with draw=scale, is a row of dots so faint that the waveform is barely visible. The bottom one, with draw=full, shows the same waveform in clear light blue
Comparison of draw=scale and draw=full (mode=line, a frame at 1.0 second)

When I counted pixel colors, with draw=scale, 3,267 of the 3,368 waveform pixels were (249, 254, 255), which was almost indistinguishable from white. With draw=full, all 3,368 pixels were the specified light blue (0, 200, 232).

draw=scale is a drawing method that divides the color value by the number of samples per column, n, and the number of channels (the relevant part of the FFmpeg 6.1.1 source code). The official documentation for showwaves also says that setting draw is useful when n is large. If you do not specify n, it is determined by "sampling frequency ÷ width ÷ frame rate," which was about 1.93 in this case. If you keep draw=scale and specify n=2 or n=3, most waveform pixels become (1, 198, 230) or (0, 198, 231), so the color was a deep light blue close to the specified value, unlike the case at about 1.93. However, specifying n makes the frame rate an awkward value; for example, 44,100 ÷ 760 ÷ 2 gives about 29 fps. If you want to decide the frame rate and make a video, add draw=full so that the specified color comes out as is.

Four modes compared

You can choose one of four ways to draw the waveform with mode. I compared them using the same 1.1-second frame with draw=full.

Four waveform images stacked vertically. point draws a sine wave as thin dots. line fills from the center line up and down. p2p draws thin lines connecting the points of point. cline shows spindle shapes filled symmetrically up and down around the center
Comparison of mode=point, line, p2p, and cline (a frame at 1.1 seconds)
mode How it draws (official documentation) Number of waveform pixels
point (default) Draws a dot for each sample 1,357
line Draws a vertical line for each sample 23,953
p2p Draws dots and connects them with lines 1,637
cline Draws a vertical line for each sample, centered 47,894

point and p2p are thin lines, and p2p increased the pixel count from 1,357 to 1,637 because of the connecting lines. line fills from the center up and down, and cline fills symmetrically up and down around the center. The final command in this article uses cline, which has the largest filled area.

With rate=30, about 0.06 seconds of waveform fit in one frame

A single showwaves frame draws the waveform for only a short section of the audio. To see how long that section is, I counted using a 100 Hz tone (100 wave peaks per second). The width was 1280.

rate Peaks in one frame Length drawn (peaks ÷ 100)
30 6 About 0.06 seconds
10 12 About 0.12 seconds
5 21 About 0.21 seconds

The lower the rate, the longer the section drawn in one frame, so the wave shape appears to change more slowly. However, the rate value is used directly as the video's frame rate, so rate=5 produces a 5 fps video.

The fact that rate=30 draws about 0.06 seconds, which is longer than one frame (0.033 seconds), is because the number of samples per column is rounded up to an integer. In the source code, each time a sample is drawn, 1/n is added; when the value reaches 1 or more, it returns to 0 and moves on to the next column (the relevant part of the FFmpeg 6.1.1 source code). If n is about 1.15, two samples are drawn per column, so 1,280 columns contain 2,560 samples, about 0.058 seconds.

You can also specify n yourself, but then the frame rate changes instead. With s=1280x360:n=7, the frame rate became about 4.92 fps, calculated as 44,100 ÷ 1,280 ÷ 7. The official documentation says that a larger n lowers the frame rate, and that n can be set only when rate is not specified explicitly. In this environment with FFmpeg 6.1.1, writing both n=7:rate=30 did not cause an error, and the result was about 4.92 fps.

When the waveform appears later than the sound, use setpts to start the timestamps at 0

I created an audio file where the sound starts exactly at 1.000 seconds, and checked the time of the first frame where the waveform appears. I compared a mono audio file and a stereo audio file with the same sound in both channels, changing rate each time.

rate Mono Stereo
25 1.040 seconds (1 frame late) 1.120 seconds (3 frames late)
30 1.000 seconds (no delay) 1.067 seconds (2 frames late)
60 1.000 seconds (no delay) 1.033 seconds (2 frames late)

With stereo, the waveform was late compared with the sound at every rate. Two frames at 30 fps is about 67 milliseconds.

You can see the frame times by adding showinfo after showwaves.

bash
ffmpeg -i onset.wav -filter_complex "showwaves=s=1280x360:rate=30,showinfo" -f null -

The pts_time on the first line is the time of the first frame output by showwaves. With rate=30, the mono audio showed pts_time:0, but the stereo audio showed pts_time:0.0666667. The video starts at the delayed time rather than at 0 seconds, and the waveform is shifted later than the sound by that amount. In the first example with no options, the video also started 0.12 seconds (3 frames at 25 fps) late and was 10.12 seconds long.

The fix is to add setpts with setpts=PTS-STARTPTS immediately after showwaves, so that the first frame's time is 0. With this added, the waveform appeared starting from the frame at 1.000 seconds in all six cases in the table above. When I also encoded the audio into the MP4, the waveform appeared from 1.000 seconds for both the stereo rate=25 and rate=30 audio, and the video matched the audio at 2.000 seconds.

This is the setpts=PTS-STARTPTS in the final command. The final command merges left and right into one waveform with aformat=channel_layouts=mono, and there is no delay with mono at rate=30. For that reason, the output video was identical frame for frame whether or not setpts was included. I included it for when you change rate, or when you split the channels as in the split_channels example below.

Also use setpts when drawing the channels separately

Adding split_channels=1 draws the left channel in the top half and the right channel in the bottom half. You can use different colors for each channel by separating them with |, as in colors=0x00c8e8|0xd85878. When the channels are split, the audio is not merged into mono, so the stereo delay appears as is. Add setpts=PTS-STARTPTS here as well.

bash
ffmpeg -i voice.wav -filter_complex "[0:a]showwaves=s=1280x360:mode=cline:rate=30:split_channels=1:colors=0x00c8e8|0xd85878:draw=full,setpts=PTS-STARTPTS[w];color=white:s=1280x360:r=30[bg];[bg][w]overlay=format=auto:shortest=1,format=yuv420p[v]" -map "[v]" -map 0:a -c:v libx264 -c:a aac -b:a 192k split.mp4
Two frames stacked vertically. The frame at 1.1 seconds has a light-blue waveform in the top half and a pink waveform in the bottom half. The frame at 8.5 seconds has nothing in the top half and only a pink waveform in the bottom half
A frame with split_channels=1, with the left channel in light blue and the right channel in pink

In the frame at 8.5 seconds, where only the right channel is playing, nothing was drawn in the top half (left), and only the pink waveform appeared in the bottom half (right). The video exported from the 10-second audio had both video and audio at 10.000 seconds.

For quiet parts to still show movement, use scale=sqrt

With the default scale=lin, a small sound with amplitude 0.05 became only a thin band 10 px tall on a 200 px high picture. With scale=sqrt, the height is computed from the square root of the amplitude (source code), so small sounds are drawn larger.

scale Small sound (5.0 seconds) Loud sound (1.1 seconds)
lin (default) 10 px 107 px
sqrt 44 px 146 px
Four waveform images stacked vertically. For a small sound, scale=lin gives a thin line-like band and scale=sqrt gives a thicker band. For a loud sound, scale=lin reaches about half the height, and scale=sqrt reaches close to the top and bottom edges
Comparison of scale=lin and scale=sqrt (cline, height 200 px)

The small sound went from 10 px to 44 px, more than four times larger, and the loud sound went from 107 px to 146 px, about 1.4 times larger. The difference between small and loud sounds became smaller, but even quiet sections show waveform movement that is visible. log and cbrt are also available. The differences in height between these are compared in the article about making a single-image waveform.

When adding a background, add shortest=1 to overlay

The showwaves video is transparent wherever there is no waveform. When I exported a silent frame as an RGBA PNG at 760×200, all 151,240 pixels were transparent except the 760 pixels of the center line. When I encoded that directly to H.264, the transparent parts became black (0, 0, 0).

If you want a white or other background, create the background with color, which generates a solid-color video, and overlay the waveform on it with overlay. For video output, there are two things to watch for here.

  • If you do not specify a duration, color keeps generating video endlessly. Add shortest=1 to overlay so that it stops when the waveform video ends.
  • Match the size and frame rate of color (s and r) to the s and rate of showwaves.

The part color=white:s=1280x360:r=30[bg];[bg][w]overlay=format=auto:shortest=1 in the final command is this part. The exported video had both video and audio at 10.000 seconds, with 300 frames of video at 1280×360, 30 fps, and yuv420p (High profile).

Processing time and file size

These are the processing time and file size when exporting with the final command.

Audio Processing time File size Video bitrate
10-second test audio (1 run) 1.8 seconds About 0.97 MB About 591 kbps
60-second pink noise (3 runs) 15.7 to 16.7 seconds About 18.7 MB About 2,295 kbps

For the 60-second pink noise, the audio (AAC 192 kbps) accounted for about 1.4 MB of the 18.7 MB, and most of the rest was video. For the 10-second test audio, which has silent and quiet sections, the video was about 591 kbps, about a quarter of the pink noise's bitrate.

​Posted by 86