如何使用 ffmpeg 在频谱音频效果上插入背景图像?

如何使用 ffmpeg 在频谱音频效果上插入背景图像?

基于漂亮的答案,使用ffmpeg我生成一个直方图使用以下命令行从音频文件中获取:

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

直方图

我在网上搜索了很多但没有找到,是否可以将图像设置为此光谱效果的背景?

更新

我尝试了这个命令:

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

但转换速度却慢得可笑。

在这里查看完整日志:

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

直方图输出包含一个 alpha 通道,但它只影响顶部,所以如果你想从声波图中删除黑色,你需要使用键控过滤器,例如彩色键

在此处输入图片描述

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

您的输入是 700x457,但每个尺寸都需要是偶数,因此添加了比例过滤器使其变为 700x458。直方图也设置为匹配此尺寸,但它可以是任意尺寸。

相关内容