使用静态图像将mp3转换为视频(ffmpeg/libav & BASH)

47

几年前,我使用了以下Bash脚本,从多个MP3文件和一张图片中提取视频(使视频中的图像在整个MP3长度上保持不变),这个脚本运行良好。

i=0;
for file in *.mp3; 
do 
i=$((i+1));
ffmpeg -loop 1 -shortest -y -i image.jpg -i $file -acodec copy -vcodec libx264 $file.flv;
done

现在我想再次使用它来做同样的事情。

问题:

它不会在mp3结尾处停止转换。这意味着当前的mp3文件长度为3分钟,但脚本会一直转换,除非我手动停止它,因此生成的flv文件长度比3分钟要大得多。

(已安装ffmpeg,但使用libav时情况相同)

输出:

ffmpeg version 1.2.6-7:1.2.6-1~trusty1 Copyright (c) 2000-2014 the FFmpeg developers
  built on Apr 26 2014 18:52:58 with gcc 4.8 (Ubuntu 4.8.2-19ubuntu1)
  configuration: --arch=amd64 --disable-stripping --enable-avresample --enable-pthreads --enable-runtime-cpudetect --extra-version='7:1.2.6-1~trusty1' --libdir=/usr/lib/x86_64-linux-gnu --prefix=/usr --enable-bzlib --enable-libdc1394 --enable-libfreetype --enable-frei0r --enable-gnutls --enable-libgsm --enable-libmp3lame --enable-librtmp --enable-libopencv --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libschroedinger --enable-libspeex --enable-libtheora --enable-vaapi --enable-vdpau --enable-libvorbis --enable-libvpx --enable-zlib --enable-gpl --enable-postproc --enable-libcdio --enable-x11grab --enable-libx264 --shlibdir=/usr/lib/x86_64-linux-gnu --enable-shared --disable-static
  libavutil      52. 18.100 / 52. 18.100
  libavcodec     54. 92.100 / 54. 92.100
  libavformat    54. 63.104 / 54. 63.104
  libavdevice    53.  5.103 / 53.  5.103
  libavfilter     3. 42.103 /  3. 42.103
  libswscale      2.  2.100 /  2.  2.100
  libswresample   0. 17.102 /  0. 17.102
  libpostproc    52.  2.100 / 52.  2.100
[image2 @ 0x6d0740] max_analyze_duration 5000000 reached at 5000000 microseconds
Input #0, image2, from 'Bild2.png':
  Duration: 00:00:00.04, start: 0.000000, bitrate: N/A
    Stream #0:0: Video: png, rgba, 382x417, 25 fps, 25 tbr, 25 tbn, 25 tbc
[mp3 @ 0x6c4ac0] max_analyze_duration 5000000 reached at 5015510 microseconds
[mp3 @ 0x6c4ac0] Estimating duration from bitrate, this may be inaccurate
Input #1, mp3, from 'sons.mp3':
  Duration: 00:00:35.11, start: 0.000000, bitrate: 127 kb/s
    Stream #1:0: Audio: mp3, 44100 Hz, stereo, s16p, 128 kb/s
[libx264 @ 0x6da700] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
[libx264 @ 0x6da700] profile High 4:4:4 Predictive, level 2.1, 4:4:4 8-bit
[libx264 @ 0x6da700] 264 - core 142 r2389 956c8d8 - H.264/MPEG-4 AVC codec - Copyleft 2003-2014 - 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=4 threads=6 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, flv, to 'sons.mp3.flv':
  Metadata:
    encoder         : Lavf54.63.104
    Stream #0:0: Video: h264 ([7][0][0][0] / 0x0007), yuv444p, 382x417, q=-1--1, 1k tbn, 25 tbc
    Stream #0:1: Audio: mp3 ([2][0][0][0] / 0x0002), 44100 Hz, stereo, 128 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (png -> libx264)
  Stream #1:0 -> #0:1 (copy)
Press [q] to stop, [?] for help

使用FFmpeg将一个图像和一个音频文件合并成一个视频。相关链接:https://superuser.com/questions/1041816/combine-one-image-one-audio-file-to-make-one-video-using-ffmpeg - Ciro Santilli OurBigBook.com
3个回答

106

首先,我强烈建议您更新ffmpeg到新版本。您当前正在使用v1.2.6-7,但现在的版本是2.3.3。

这里是一个有效的命令(参见ffmpeg wiki):

ffmpeg -loop 1 -i image.jpg -i audio.mp3 -c:a copy -c:v libx264 -shortest out.mp4

此外,如果您使用原始声音(aiff或wave)作为输入,可以通过重新编码音频来优化总体重量:

ffmpeg -loop 1 -i image.jpg -i audio.wav -c:a aac -ab 112k -c:v libx264 -shortest -strict -2 out.mp4

如需进一步阅读,请参见此相关的stackoverflow帖子


@gnou mp4 在 iPhone 上无法播放... 请帮忙解决。 - neoDev
@neoDev 在这种情况下,您需要使用aac音频编解码器或libfdk_aac进行音频通道编码,以获得更好的质量(如果您的ffmpeg已经编译了它)。 - gnou
4
工作效果不错,但速度非常缓慢(大约需要实时处理一张静止图像)。为什么呢? - ESP32
4
最终,我从一段30分钟的mp3音频(20 MB)和一张静态图像获得了一个大小为400 MB的文件。 - ESP32
专业提示。您用作“图像”文件的图像越小,转换速度就越快。我使用了一个1像素乘1像素的白色图像,获得了700倍的加速。此外,文件大小非常相似。例如,一个37MB的m4a文件变成了38.5MB的mp4文件。但是,如果您是为YouTube做这个,他们不喜欢1像素的白色图像哈哈。所以我选择了一个640x400的极简壁纸(10KB),获得了60倍的加速。现在已经足够好了。 - SuperMar1o
显示剩余3条评论

4
ffmpeg -loop 1 -i image.jpg -i audio.mp3 -shortest -c:v libx264 -c:a copy result.mkv 

适用于3.4.2版本


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接