Skip to content

Instantly share code, notes, and snippets.

@rgeraldporter
Last active September 18, 2023 08:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgeraldporter/c79f5b30303dc605bf920208e7736c26 to your computer and use it in GitHub Desktop.
Save rgeraldporter/c79f5b30303dc605bf920208e7736c26 to your computer and use it in GitHub Desktop.
Spectrogram Video Generation
#!/usr/bin/env bash
# Note that some tweaks may be required to make the overlay "line" to appear correctly where the audio is actually playing
# The overlay.png needs to be 1920 x 784 for this, if you change scale you'll need to change that file size
# FFMPEG makes very stuttery videos in spectrograms in some ratios/resolutions; I've tried to do high res but with little success
# So far these settings are the smoothest I can get, but it can still look a bit stuttery in YouTube / full screen on a big screen
# The various steps below involve generating the spectrogram, adding the overlay visual, adding the audio,
# moving the audio over a bit to line up with the play line, framing the video in a more standard resolution/ratio
# On MacOS I ran this using: (where mp3 is name-of-file.mp3)
# sh spectrogram.sh name-of-file
if [ "$1" != "" ]; then
echo "Converting to spectrogram video..."
else
echo "You must provide an MP3 file name."
fi
ffmpeg -i "$1.mp3" \
-filter_complex "aevalsrc=0|0:d=18[silence];[0:a][silence]concat=n=2:v=0:a=1[out]" \
-map [out] -c:a libmp3lame -q:a 2 stage-0.mp3 -y && \
ffmpeg -i stage-0.mp3 -filter_complex \
"[0:a]showspectrum=s=1250x500:fps=60:mode=combined:color=intensity:stop=12000:slide=scroll,format=yuv420p[v]" \
-map "[v]" -map 0:a \
-b:v 700k -b:a 360k stage-1.mp4 -y && \
ffmpeg -i stage-1.mp4 -vf scale=1920x784 stage-2.mp4 -y && \
ffmpeg -i stage-2.mp4 -i overlay.png -filter_complex "overlay=0:0" stage-3.mp4 -y && \
ffmpeg -i stage-3.mp4 -ss 12 -vcodec copy -acodec copy stage-4.mp4 -y && \
ffmpeg -i stage-4.mp4 -i "$1.mp3" -c:v copy -map 0:v:0 -map 1:a:0 stage-5.mp4 -y && \
ffmpeg -i stage-5.mp4 -itsoffset 3.1 -i stage-5.mp4 -strict -2 -map 0:v -map 1:a -c copy "$1.mp4" -y
# remove this to clean up after itself; can leave it in case things don't work right and you can figure out at what stage things went wrong
#rm -rf stage-*.mp4
@aviceda
Copy link

aviceda commented Mar 11, 2020

Just tried to run the program in Linuxmint19, but get this output from my bash shell:

@-:~/spectrogram$ sh spectrogram.sh nosb_v4
Converting to spectrogram video...
ffmpeg version 3.4.6-0ubuntu0.18.04.1 Copyright (c) 2000-2019 the FFmpeg developers
built with gcc 7 (Ubuntu 7.3.0-16ubuntu3)
configuration: --prefix=/usr --extra-version=0ubuntu0.18.04.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
WARNING: library configuration mismatch
avcodec configuration: --prefix=/usr --extra-version=0ubuntu0.18.04.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared --enable-version3 --disable-doc --disable-programs --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libtesseract --enable-libvo_amrwbenc
libavutil 55. 78.100 / 55. 78.100
libavcodec 57.107.100 / 57.107.100
libavformat 57. 83.100 / 57. 83.100
libavdevice 57. 10.100 / 57. 10.100
libavfilter 6.107.100 / 6.107.100
libavresample 3. 7. 0 / 3. 7. 0
libswscale 4. 8.100 / 4. 8.100
libswresample 2. 9.100 / 2. 9.100
libpostproc 54. 7.100 / 54. 7.100
Input #0, mp3, from 'nosb_v4.mp3':
Metadata:
artist : Noisy Scrub-bird, nosb,
Duration: 00:01:54.99, start: 0.025057, bitrate: 167 kb/s
Stream #0:0: Audio: mp3, 44100 Hz, stereo, s16p, 167 kb/s
Metadata:
encoder : LAME3.100
Stream mapping:
Stream #0:0 (mp3) -> concat:in0:a0
concat -> Stream #0:0 (libmp3lame)
Press [q] to stop, [?] for help
Output #0, mp3, to 'stage-0.mp3':
Metadata:
TPE1 : Noisy Scrub-bird, nosb,
TSSE : Lavf57.83.100
Stream #0:0: Audio: mp3 (libmp3lame), 44100 Hz, stereo, s16p
Metadata:
encoder : Lavc57.107.100 libmp3lame
[Parsed_aevalsrc_0 @ 0x563eedad3600] EOF timestamp not reliable.1x
size= 2435kB time=00:02:12.96 bitrate= 150.0kbits/s speed=71.7x
video:0kB audio:2435kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.011552%
ffmpeg version 3.4.6-0ubuntu0.18.04.1 Copyright (c) 2000-2019 the FFmpeg developers
built with gcc 7 (Ubuntu 7.3.0-16ubuntu3)
configuration: --prefix=/usr --extra-version=0ubuntu0.18.04.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
WARNING: library configuration mismatch
avcodec configuration: --prefix=/usr --extra-version=0ubuntu0.18.04.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared --enable-version3 --disable-doc --disable-programs --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libtesseract --enable-libvo_amrwbenc
libavutil 55. 78.100 / 55. 78.100
libavcodec 57.107.100 / 57.107.100
libavformat 57. 83.100 / 57. 83.100
libavdevice 57. 10.100 / 57. 10.100
libavfilter 6.107.100 / 6.107.100
libavresample 3. 7. 0 / 3. 7. 0
libswscale 4. 8.100 / 4. 8.100
libswresample 2. 9.100 / 2. 9.100
libpostproc 54. 7.100 / 54. 7.100
Input #0, mp3, from 'stage-0.mp3':
Metadata:
artist : Noisy Scrub-bird, nosb,
encoder : Lavf57.83.100
Duration: 00:02:12.99, start: 0.025057, bitrate: 149 kb/s
Stream #0:0: Audio: mp3, 44100 Hz, stereo, s16p, 149 kb/s
Metadata:
encoder : Lavc57.10
[Parsed_showspectrum_0 @ 0x55d0f88ebd40] Option 'fps' not found
[AVFilterGraph @ 0x55d0f88e8700] Error initializing filter 'showspectrum' with args 's=1250x500:fps=60:mode=combined:color=intensity:stop=12000:slide=scroll'
Error initializing complex filters.
Option not found

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment