FFMPEG concat demuxer - 如何使 mp4 文件格式兼容

FFMPEG concat demuxer - 如何使 mp4 文件格式兼容

我需要在服务器端自动执行 mp4 连接,我正在使用 FFMPEG。我将上传 mp4 文件,并希望为每个文件附加 Title.mp4 和 End.mp4。我还叠加了音轨(输入视频没有声音)服务器负载可能很高,因此我想尽可能高效地使用 ffmpeg 的 concat demuxer 来执行此操作,以避免重新编码视频。

在收到每个文件的样本后,我并没有成功,我认为这是由于文件格式不匹配造成的。我的结果有良好的 Title.mp4 和音频,然后当上传的样本 mp4 应该播放时,视频的上半部分出现了乱码的绿色/粉红色/红色像素,然后 End.mp4 播放正常。这是我的 ffmpeg 命令和输出:

$ ffmpeg -f concat -i <(printf "file '%s'\n" Title.mp4 Sample.mp4 End.mp4) -i SoundTrack.wav -c:v copy -strict -2 -y Out.mp4

ffmpeg version 2.6.1 Copyright (c) 2000-2015 the FFmpeg developers
built with gcc 4.1.2 (GCC) 20080704 (Red Hat 4.1.2-55)
configuration: --prefix=/home/dpmsmobi/ffmpeg_build --extra-cflags=-I/home/dpmsmobi/ffmpeg_build/include --extra-ldflags=-L/home/dpmsmobi/ffmpeg_build/lib --bindir=/home/dpmsmobi/bin --enable-gpl --enable-nonfree --enable-libmp3lame --enable-libvorbis --enable-libvpx --enable-libx264
  libavutil      54. 20.100 / 54. 20.100
  libavcodec     56. 26.100 / 56. 26.100
  libavformat    56. 25.101 / 56. 25.101
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 11.102 /  5. 11.102
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  1.100 /  1.  1.100
  libpostproc    53.  3.100 / 53.  3.100
Input #0, concat, from '/dev/fd/63':
  Duration: N/A, start: 0.000000, bitrate: 1810 kb/s
    Stream #0:0: Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv), 768x512 [SAR 1:1 DAR 3:2], 1810 kb/s, 30 fps, 30 tbr, 30k tbn, 60 tbc
Guessed Channel Layout for  Input Stream #1.0 : stereo
Input #1, wav, from 'SoundTrack.wav':
  Metadata:
    encoded_by      : Adobe Premiere Pro CC 2014 (Maci
    encoder         : Adobe Premiere Pro CC 2014 (Macintosh)
    date            : 2015-04-07
    creation_time   : 11:12:10
    time_reference  : 0
  Duration: 00:00:15.06, bitrate: 1551 kb/s
    Stream #1:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 48000 Hz, 2 channels, s16, 1536 kb/s
Output #0, mp4, to 'Out.mp4':
  Metadata:
    encoder         : Lavf56.25.101
    Stream #0:0: Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 768x512 [SAR 1:1 DAR 3:2], q=2-31, 1810 kb/s, 30 fps, 30 tbr, 30k tbn, 30k tbc
    Stream #0:1: Audio: aac ([64][0][0][0] / 0x0040), 48000 Hz, stereo, fltp, 128 kb/s
    Metadata:
      encoder         : Lavc56.26.100 aac
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #1:0 -> #0:1 (pcm_s16le (native) -> aac (native))
Press [q] to stop, [?] for help
[concat @ 0x1dedc20] Thread message queue blocking; consider raising the thread_queue_size option (current value: 8)
[concat @ 0x1dedc20] DTS 69750 < 91000 out of order
[mp4 @ 0x1f75060] Non-monotonous DTS in output stream 0:0; previous: 91000, current: 69750; changing to 91001. This may result in incorrect timestamps in the output file.

<----- many more Non-monotonous DTS messages omitted here ---->

frame=  427 fps=0.0 q=-1.0 Lsize=    4123kB time=00:00:15.06 bitrate=2242.5kbits/s    
video:3873kB audio:236kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.344173%

我可以成功将 Title.mp4 连接到 End.mp4,并且可以成功连接两个 Sample.mp4 文件,所以我知道我正确使用了 ffmpeg 命令。我还可以使用以下 ffmpeg 命令和 filter_complex 而不是 concat demuxer 成功连接文件(由于重新编码,这需要更长的时间):

ffmpeg -i Title.mp4 -i Sample.mp4 -i End.mp4 -i SoundTrack.wav -filter_complex '[0:0] [1:0] [2:0] concat=n=3:v=1 [v]' -map '[v]' -map 3:0 -crf 20 -strict -2 -y Out2.mp4 

以下是每种类型的 mp4 文件的 MediaInfo 输出:

$ mediainfo Title.mp4
General
Complete name                            : Title.mp4
Format                                   : MPEG-4
Format profile                           : Base Media / Version 2
Codec ID                                 : mp42
File size                                : 693 KiB
Duration                                 : 3s 100ms
Overall bit rate mode                    : Variable
Overall bit rate                         : 1 831 Kbps
Encoded date                             : UTC 2015-04-07 19:15:03
Tagged date                              : UTC 2015-04-07 19:15:03
©TIM                                     : 00:00:00:00
©TSC                                     : 30
©TSZ                                     : 1

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : [email protected]
Format settings, CABAC                   : Yes
Format settings, ReFrames                : 3 frames
Codec ID                                 : avc1
Codec ID/Info                            : Advanced Video Coding
Duration                                 : 3s 100ms
Bit rate mode                            : Variable
Bit rate                                 : 1 811 Kbps
Maximum bit rate                         : 3 000 Kbps
Width                                    : 768 pixels
Height                                   : 512 pixels
Display aspect ratio                     : 3:2
Frame rate mode                          : Constant
Frame rate                               : 30.000 fps
Standard                                 : NTSC
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.154
Stream size                              : 685 KiB (99%)
Language                                 : English
Encoded date                             : UTC 2015-04-07 19:15:03
Tagged date                              : UTC 2015-04-07 19:15:03
Color range                              : Limited

$ mediainfo Sample.mp4
General
Complete name                            : Sample.mp4
Format                                   : MPEG-4
Format profile                           : Base Media
Codec ID                                 : isom
File size                                : 2.93 MiB
Duration                                 : 7s 9ms
Overall bit rate                         : 3 505 Kbps
Encoded date                             : UTC 1970-01-01 00:00:00
Tagged date                              : UTC 1970-01-01 00:00:00
Writing application                      : Lavf52.64.2

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : [email protected]
Format settings, CABAC                   : No
Format settings, ReFrames                : 1 frame
Format settings, GOP                     : M=1, N=30
Codec ID                                 : avc1
Codec ID/Info                            : Advanced Video Coding
Duration                                 : 7s 9ms
Bit rate                                 : 3 500 Kbps
Width                                    : 768 pixels
Height                                   : 512 pixels
Display aspect ratio                     : 3:2
Frame rate mode                          : Variable
Frame rate                               : 30.250 fps
Minimum frame rate                       : 23.462 fps
Maximum frame rate                       : 296.053 fps
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.294
Stream size                              : 2.92 MiB (100%)
Language                                 : English
Encoded date                             : UTC 1970-01-01 00:00:00
Tagged date                              : UTC 1970-01-01 00:00:00

我很确定这是 mp42 与 isom 编解码器 ID 的问题,也可能是恒定帧速率与可变帧速率的问题。我无法更改输入的 mp4,但我知道它们的格式将保持不变。我该如何重新格式化标题和结束 mp4 以匹配输入的 mp4 文件,以便我可以使用 ffmpeg concat demux?

相关内容