我正在使用 ffmpeg 创建视频。源是一组 png 图像(每帧一个)和一个 mp3 文件。我想创建一个带声音的 mp4。
我目前分两个阶段进行。第一步将 png 转换为 mp4:
%d%\ffmpeg -r 25 -i png\%1-%%06d.png -s 1280x720 -c:v libx264 -pix_fmt yuv420p -b:v 500k -profile:v baseline -movflags faststart temp.mp4
第二步合并声音和临时mp4:
%d%\ffmpeg -i temp.mp4 -i %1\%1.mp3 %1.mp4
这可行,但声音很差。我认为它正在将 mp3 转换为 aac。
所以我尝试了这个:
%d%\ffmpeg -i temp.mp4 -i %1\%1.mp3 -c copy -map 0:0 -map 1:0 %1.mp4
我没有声音。我尝试了许多其他选项、不同的编解码器,更改了其他音频参数,但还是没有声音。
获取声音的唯一方法似乎是保留音频的默认设置,但质量太差了。
编辑这里是输出
C:\ffmpeg\bin\ffmpeg -r 25 -i png\scene2-%06d.png -s 1280x720 -c:v libx264 -pix_fmt yuv420p
-b:v 500k -profile:v baseline -movflags faststart temp.mp4
ffmpeg version N-57057-g024bf3a Copyright (c) 2000-2013 the FFmpeg developers
built on Oct 11 2013 18:01:59 with gcc 4.8.1 (GCC)
configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-av
isynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enab
le-iconv --enable-libass --enable-libbluray --enable-libcaca --enable-libfreetyp
e --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --ena
ble-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-l
ibopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libsp
eex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aa
cenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavp
ack --enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib
libavutil 52. 46.101 / 52. 46.101
libavcodec 55. 35.100 / 55. 35.100
libavformat 55. 19.100 / 55. 19.100
libavdevice 55. 4.100 / 55. 4.100
libavfilter 3. 88.101 / 3. 88.101
libswscale 2. 5.101 / 2. 5.101
libswresample 0. 17.103 / 0. 17.103
libpostproc 52. 3.100 / 52. 3.100
Input #0, image2, from 'png\scene2-%06d.png':
Duration: 00:00:13.00, start: 0.000000, bitrate: N/A
Stream #0:0: Video: png, rgba, 1280x720 [SAR 3779:3779 DAR 16:9], 25 fps, 25
tbr, 25 tbn, 25 tbc
[libx264 @ 003f36a0] using SAR=1/1
[libx264 @ 003f36a0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.1 Cache64
[libx264 @ 003f36a0] profile Constrained Baseline, level 3.1
[libx264 @ 003f36a0] 264 - core 138 r2358 9e941d1 - H.264/MPEG-4 AVC codec - Cop
yleft 2003-2013 - http://www.videolan.org/x264.html - options: cabac=0 ref=3 deb
lock=1:0:0 analyse=0x1:0x111 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 m
e_range=16 chroma_me=1 trellis=1 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chro
ma_qp_offset=-2 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 i
nterlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=250 k
eyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=abr mbtree=1 bitrate
=500 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'temp.mp4':
Metadata:
encoder : Lavf55.19.100
Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 1280x7
20 [SAR 1:1 DAR 16:9], q=-1--1, 500 kb/s, 12800 tbn, 25 tbc
Stream mapping:
Stream #0:0 -> #0:0 (png -> libx264)
Press [q] to stop, [?] for help
frame= 57 fps= 56 q=35.0 size= 16kB time=00:00:00.40 bitrate= 320.3kbits/
frame= 84 fps= 55 q=26.0 size= 39kB time=00:00:01.48 bitrate= 214.0kbits/
frame= 113 fps= 56 q=19.0 size= 60kB time=00:00:02.64 bitrate= 186.0kbits/
frame= 141 fps= 55 q=21.0 size= 152kB time=00:00:03.76 bitrate= 332.0kbits/
frame= 167 fps= 55 q=24.0 size= 268kB time=00:00:04.80 bitrate= 457.8kbits/
frame= 190 fps= 53 q=25.0 size= 353kB time=00:00:05.72 bitrate= 506.2kbits/
frame= 213 fps= 52 q=26.0 size= 412kB time=00:00:06.64 bitrate= 508.0kbits/
frame= 238 fps= 52 q=23.0 size= 419kB time=00:00:07.64 bitrate= 449.1kbits/
frame= 262 fps= 51 q=22.0 size= 454kB time=00:00:08.60 bitrate= 432.5kbits/
frame= 286 fps= 51 q=22.0 size= 523kB time=00:00:09.56 bitrate= 448.1kbits/
frame= 309 fps= 50 q=25.0 size= 660kB time=00:00:10.48 bitrate= 515.8kbits/
[mp4 @ 003f3160] Starting second pass: moving the moov atom to the beginning of
the file
frame= 325 fps= 47 q=-1.0 Lsize= 794kB time=00:00:13.00 bitrate= 500.5kbits
/s
video:792kB audio:0kB subtitle:0 global headers:0kB muxing overhead 0.256995%
[libx264 @ 003f36a0] frame I:2 Avg QP:13.55 size: 24853
[libx264 @ 003f36a0] frame P:323 Avg QP:15.10 size: 2356
[libx264 @ 003f36a0] mb I I16..4: 91.3% 0.0% 8.8%
[libx264 @ 003f36a0] mb P I16..4: 1.5% 0.0% 0.2% P16..4: 2.4% 1.0% 0.4%
0.0% 0.0% skip:94.5%
[libx264 @ 003f36a0] final ratefactor: 19.04
[libx264 @ 003f36a0] coded y,uvDC,uvAC intra: 6.3% 15.8% 12.8% inter: 1.3% 2.5%
1.4%
[libx264 @ 003f36a0] i16 v,h,dc,p: 82% 17% 2% 0%
[libx264 @ 003f36a0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 39% 18% 25% 3% 3% 4% 3%
2% 3%
[libx264 @ 003f36a0] i8c dc,h,v,p: 69% 23% 8% 1%
[libx264 @ 003f36a0] ref P L0: 75.5% 14.7% 9.8%
[libx264 @ 003f36a0] kb/s:498.84
C:\ffmpeg\bin\ffmpeg -i temp.mp4 -i scene2\scene2.mp3 -c copy -map 0:0 -map 1:0 scene2.mp4
ffmpeg version N-57057-g024bf3a Copyright (c) 2000-2013 the FFmpeg developers
built on Oct 11 2013 18:01:59 with gcc 4.8.1 (GCC)
configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-av
isynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enab
le-iconv --enable-libass --enable-libbluray --enable-libcaca --enable-libfreetyp
e --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --ena
ble-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-l
ibopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libsp
eex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aa
cenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavp
ack --enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib
libavutil 52. 46.101 / 52. 46.101
libavcodec 55. 35.100 / 55. 35.100
libavformat 55. 19.100 / 55. 19.100
libavdevice 55. 4.100 / 55. 4.100
libavfilter 3. 88.101 / 3. 88.101
libswscale 2. 5.101 / 2. 5.101
libswresample 0. 17.103 / 0. 17.103
libpostproc 52. 3.100 / 52. 3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'temp.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf55.19.100
Duration: 00:00:13.00, start: 0.000000, bitrate: 500 kb/s
Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yu
v420p, 1280x720 [SAR 1:1 DAR 16:9], 499 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc
(default)
Metadata:
handler_name : VideoHandler
Input #1, mp3, from 'scene2\scene2.mp3':
Metadata:
genre : Blues
Duration: 00:00:09.86, start: 0.000000, bitrate: 128 kb/s
Stream #1:0: Audio: mp3, 16000 Hz, mono, s16p, 128 kb/s
Output #0, mp4, to 'scene2.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf55.19.100
Stream #0:0(und): Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 1280x720 [S
AR 1:1 DAR 16:9], q=2-31, 499 kb/s, 25 fps, 12800 tbn, 12800 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1: Audio: mp3 (i[0][0][0] / 0x0069), 16000 Hz, mono, 128 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Stream #1:0 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame= 325 fps=0.0 q=-1.0 Lsize= 951kB time=00:00:12.96 bitrate= 601.4kbits
/s
video:792kB audio:154kB subtitle:0 global headers:0kB muxing overhead 0.540095%
编辑添加 -i scene2.mp4 的输出
C:\ffmpeg\bin\ffmpeg -i scene2.mp4
ffmpeg version N-57057-g024bf3a Copyright (c) 2000-2013 the FFmpeg developers
built on Oct 11 2013 18:01:59 with gcc 4.8.1 (GCC)
configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-av
isynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enab
le-iconv --enable-libass --enable-libbluray --enable-libcaca --enable-libfreetyp
e --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --ena
ble-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-l
ibopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libsp
eex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aa
cenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavp
ack --enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib
libavutil 52. 46.101 / 52. 46.101
libavcodec 55. 35.100 / 55. 35.100
libavformat 55. 19.100 / 55. 19.100
libavdevice 55. 4.100 / 55. 4.100
libavfilter 3. 88.101 / 3. 88.101
libswscale 2. 5.101 / 2. 5.101
libswresample 0. 17.103 / 0. 17.103
libpostproc 52. 3.100 / 52. 3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'scene2.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf55.19.100
Duration: 00:00:13.00, start: 0.000000, bitrate: 599 kb/s
Stream #0:0(und): Video: h264 (Constrained Baseline) (avc1 / 0x31637661), yu
v420p, 1280x720 [SAR 1:1 DAR 16:9], 499 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc
(default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: mp3 (mp4a / 0x6134706D), 16000 Hz, mono, s16p, 128
kb/s (default)
Metadata:
handler_name : SoundHandler
At least one output file must be specified
答案1
Windows Media Player 对 MP4 容器格式的 MP3 非常挑剔。它不喜欢低于 32 kHz 的音频采样率,也不喜欢 96 kHz 或更高的采样率(我没有测试确切的上限)。
QuickTime 未播放任何包含 MP4 容器中的 MP3 的示例文件。
您有多种选择来解决此限制:
1. 使用不同的播放器
甚高频液晶效果很好,是一个很棒的播放器。此选项的优点是您可以流复制音频(正如您正在做的那样)而不是重新编码它;从而保持质量。
2. 使用不同的容器格式
例如.mov
。与上一个选项一样,这也将允许您流式复制原始音频。
3. 使用不同的音频格式
如果您必须使用 WMP,则必须重新编码音频。由于 AAC 音频是 MP4 容器中的标准,因此您可以使用:-c:a aac
。请参阅FFmpeg Wiki:AAC 音频编码指南了解更多信息。
4.更改音频采样率
如果您想要输出 MP3,请尝试以下-ar
选项,例如-ar 48000
。这应该是最后一个要尝试的选项。
一步完成所有操作
现在您有了可行的方法,您可以一次性完成所有操作,而不必通过额外的、不必要的步骤重新编码您的视频。
流复制音频
ffmpeg -framerate 25 -i %06d.png -i input.mp3 -c:v libx264 \
-vf scale=1280:-2,format=yuv420p -c:a copy -movflags +faststart output.mp4
重新编码音频
ffmpeg -framerate 25 -i %06d.png -i input.mp3 -c:v libx264 \
-vf scale=1280:-2,format=yuv420p -c:a aac -movflags +faststart output.mp4
笔记
使用
-framerate
而不是-r
作为输入选项图像文件解复用器。如果我没记错的话,与相比它避免了几个问题-r
。