1

Based on this beautiful answer, using ffmpeg I generate an ahistogram from an audio file with this command line:

ffmpeg -i input.flac -filter_complex \
"[0:a]ahistogram,format=yuv420p[v]" \
-map "[v]" -map 0:a output.mp4

ahistogram

I searched a lot on the internet but I can't find it, is it possible to set an image as background to this spectrum effect?

UPDATE

I tried this command:

ffmpeg -i /var/www/html/bg.jpg -i input.mp3 -filter_complex \
"[1:a]ahistogram=s=640x360:slide=scroll,colorkey=color=black:similarity=0.2[fg]; \
 [0:v][fg]overlay=shortest=1, format=yuv420p,scale=1280:-2[v]" \
-map "[v]" -map 1:a -c:a copy -shortest output.mp4

But the conversion speed is ridiculous slow.

Check the full log here:

ffmpeg version git-2017-01-22-f1214ad Copyright (c) 2000-2017 the FFmpeg developers
  built with gcc 4.8 (Ubuntu 4.8.4-2ubuntu1~14.04.3)
  configuration: --extra-libs=-ldl --prefix=/opt/ffmpeg --mandir=/usr/share/man --enable-avresample --disable-debug --enable-nonfree --enable-gpl --enable-version3 --enable-libopencore-amrnb --enable-libopencore-amrwb --disable-decoder=amrnb --disable-decoder=amrwb --enable-libpulse --enable-libfreetype --enable-gnutls --enable-libx264 --enable-libx265 --enable-libfdk-aac --enable-libvorbis --enable-libmp3lame --enable-libopus --enable-libvpx --enable-libspeex --enable-libass --enable-avisynth --enable-libsoxr --enable-libxvid --enable-libvidstab --enable-libwavpack --enable-nvenc
  libavutil      55. 44.100 / 55. 44.100
  libavcodec     57. 75.100 / 57. 75.100
  libavformat    57. 63.100 / 57. 63.100
  libavdevice    57.  2.100 / 57.  2.100
  libavfilter     6. 69.100 /  6. 69.100
  libavresample   3.  2.  0 /  3.  2.  0
  libswscale      4.  3.101 /  4.  3.101
  libswresample   2.  4.100 /  2.  4.100
  libpostproc    54.  2.100 / 54.  2.100
Input #0, image2, from 'bg.jpg':
  Duration: 00:00:00.04, start: 0.000000, bitrate: 8257 kb/s
    Stream #0:0: Video: mjpeg, yuvj420p(pc, bt470bg/unknown/unknown), 700x457 [SAR 1:1 DAR 700:457], 25 tbr, 25 tbn, 25 tbc
Input #1, mp3, from 'output2.mp3':
  Metadata:
    encoder         : Lavf57.46.100
  Duration: 00:00:21.84, start: 0.050113, bitrate: 32 kb/s
    Stream #1:0: Audio: mp3, 22050 Hz, mono, s16p, 32 kb/s
File 'output.mp4' already exists. Overwrite ? [y/N] y
[swscaler @ 0x3ae8e60] deprecated pixel format used, make sure you did set range correctly
[libx264 @ 0x3a5f7a0] using SAR=2438/2437
[libx264 @ 0x3a5f7a0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 AVX2 LZCNT BMI2
[libx264 @ 0x3a5f7a0] profile High, level 3.2
[libx264 @ 0x3a5f7a0] 264 - core 148 r2762 90a61ec - H.264/MPEG-4 AVC codec - Copyleft 2003-2017 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=1 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'output.mp4':
  Metadata:
    encoder         : Lavf57.63.100
    Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 1280x836 [SAR 7315:7312 DAR 700:457], q=-1--1, 25 fps, 12800 tbn, 25 tbc (default)
    Metadata:
      encoder         : Lavc57.75.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
    Stream #0:1: Audio: mp3 (i[0][0][0] / 0x0069), 22050 Hz, mono, s16p, 32 kb/s
Stream mapping:
  Stream #0:0 (mjpeg) -> overlay:main
  Stream #1:0 (mp3) -> ahistogram
  scale -> Stream #0:0 (libx264)
  Stream #1:0 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame=    1 fps=0.1 q=28.0 Lsize=      18kB time=00:00:00.08 bitrate=1841.1kbits/s speed=0.0112x
  • 1
    Use this answer with your filter. – Gyan Mar 3 '17 at 18:36
  • @Mulvya I already tried it...but how to? This doesn't work sudo ffmpeg -r 1 -loop 1 -i bg.jpg -i input.mp3 -filter_complex "[0:a]showwaves=s=1920x1080:mode=line,format=yuv420p[v]" -map "[v]" -map 0:a -c:v libx264 -c:a copy output.mp4 – NineCattoRules Mar 3 '17 at 18:55
  • @Mulvya question updated – NineCattoRules Mar 3 '17 at 23:15
2

ahistogram output contains an alpha channel, but it only affects the top part, so if you want to remove the black from the sonogram as well you'll need to use a keying filter such as colorkey:

enter image description here

ffmpeg -loop 1 -i background.jpg -i audio.mp3 -filter_complex \
"[1:a]ahistogram=s=700x458:slide=scroll,colorkey=color=black:similarity=0.2[fg]; \
 [0:v]scale=iw:-2[bg]; \
 [bg][fg]overlay,format=yuv420p[v]" \
-map "[v]" -map 1:a -shortest output.mp4

Your input is 700x457, but each dimension needs to be even, so scale filter is added to make it 700x458. ahistogram is also set to match this size, but it could be any arbitrary size.

|improve this answer|||||
  • I have a problem, height not divisible by 2 (700x457) ...I tried edit this at the end -shortest scale=1280:-2 output.mp4 without success – NineCattoRules Mar 3 '17 at 18:45
  • 1
    End the filter_complex like this: format=yuv420p,scale=1280:-2[v] – Gyan Mar 3 '17 at 19:09
  • @Mulvya I tried but ...it starts and does nothing, the speed is below speed=0.0197x and I get this deprecated pixel format used, make sure you did set range correctly – NineCattoRules Mar 3 '17 at 19:16
  • 1
    Show the full command and console output. – Gyan Mar 3 '17 at 19:24
  • 1
    @NineCattoRules I adjusted the answer to fit your particular inputs. – llogan Mar 4 '17 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.