使用 ffmpeg 合并两个 mp4 视频文件时出错

使用 ffmpeg 合并两个 mp4 视频文件时出错

我有几个 mp4 视频文件,想将它们合并。

我正在使用这些命令将 .mp4 转换为 .ts,然后将它们合并在一起。參考

ffmpeg -i part1.mp4  -bsf:v h264_mp4toannexb -f mpegts -y part1.ts # executes successfully

ffmpeg -i part2.mp4  -bsf:v h264_mp4toannexb -f mpegts -y part2.ts # executes successfully

ffmpeg -i "concat:part1.ts|part2.ts"  -bsf:a aac_adtstoasc -y full.mp4 # Error

错误:(但执行仍然继续)输入数据包太小,将比特流过滤器应用于流#0:1的输出数据包时出错,持续时间为0.999992,太大

合并两个文件要花上几个小时,而合并其他视频文件则不需要这么长时间(我认为 part2.mp4 有问题,但不知道如何解决)

这是视频信息

第一部分.mp4

 C:\Users\JAGZ\Desktop\vid>ffmpeg -i part1.mp4
ffmpeg version 3.2.4 Copyright (c) 2000-2017 the FFmpeg developers
  built with gcc 6.3.0 (GCC)
  configuration: --enable-gpl --enable-version3 --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-nvenc --enable-avisynth --enable-b
zlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcac
a --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb
--enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libsnappy --enable-libsoxr
--enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --e
nable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzimg --enable-lzma --enab
le-zlib
  libavutil      55. 34.101 / 55. 34.101
  libavcodec     57. 64.101 / 57. 64.101
  libavformat    57. 56.101 / 57. 56.101
  libavdevice    57.  1.100 / 57.  1.100
  libavfilter     6. 65.100 /  6. 65.100
  libswscale      4.  2.100 /  4.  2.100
  libswresample   2.  3.100 /  2.  3.100
  libpostproc    54.  1.100 / 54.  1.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'part1.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf57.66.104
  Duration: 00:00:07.21, start: 0.000000, bitrate: 2355 kb/s
    Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt470bg), 720x576 [SAR 128:117 DAR 160:117], 2034 kb/s, 25 fps, 2
5 tbr, 90k tbn, 50 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 317 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
At least one output file must be specified

第二部分.mp4

C:\Users\JAGZ\Desktop\vid>ffmpeg -i part2.mp4
ffmpeg version 3.2.4 Copyright (c) 2000-2017 the FFmpeg developers
  built with gcc 6.3.0 (GCC)
  configuration: --enable-gpl --enable-version3 --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-nvenc --enable-avisynth --enable-b
zlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcac
a --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb
--enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libsnappy --enable-libsoxr
--enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --e
nable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzimg --enable-lzma --enab
le-zlib
  libavutil      55. 34.101 / 55. 34.101
  libavcodec     57. 64.101 / 57. 64.101
  libavformat    57. 56.101 / 57. 56.101
  libavdevice    57.  1.100 / 57.  1.100
  libavfilter     6. 65.100 /  6. 65.100
  libswscale      4.  2.100 /  4.  2.100
  libswresample   2.  3.100 /  2.  3.100
  libpostproc    54.  1.100 / 54.  1.100
[mov,mp4,m4a,3gp,3g2,mj2 @ 003075c0] stream 0, timescale not set
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'part2.mp4':
  Metadata:
    major_brand     : M4V
    minor_version   : 1
    compatible_brands: M4V M4A mp42isom
    creation_time   : 2016-03-07T16:01:20.000000Z
    title           : 2006/14 Coke Crisis
    media_type      : 9
    network         : A/V Input
    date            : 2016-03-07
    iTunMOVI        : <?xml version="1.0" encoding="UTF-8"?>
                    : <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
                    : <plist version="1.0">
                    : <dict>
                    :   <key>asset-info</key>
                    :   <dict>
                    :           <key>file-size</key>
                    :           <integer>837215671</integer>
                    :           <key>flavor</key>
                    :           <string>4:640x480</string>
                    :           <key>high-definition</key>
                    :           <false/>
                    :           <key>screen-format</key>
                    :           <string>standard</string>
                    :           <key>soundtrack</key>
                    :           <string>LC</string>
                    :   </dict>
                    :   <key>cast</key>
                    :   <array/>
                    :   <key>codirectors</key>
                    :   <array/>
                    :   <key>copy-warning</key>
                    :   <string></string>
                    :   <key>directors</key>
                    :   <array/>
                    :   <key>producers</key>
                    :   <array/>
                    :   <key>screenwriters</key>
                    :   <array/>
                    :   <key>studio</key>
                    :   <string></string>
                    : </dict>
                    : </plist>
                    :
  Duration: 01:09:03.08, start: 0.000000, bitrate: 1616 kb/s
    Stream #0:0(eng): Video: h264 (Baseline) (avc1 / 0x31637661), yuv420p(tv), 640x480 [SAR 1:1 DAR 4:3], 1485 kb/s, 24.99 fps, 25 tbr, 25k
 tbn, 50 tbc (default)
    Metadata:
      creation_time   : 2016-03-07T16:01:20.000000Z
      handler_name    : ?Apple Video Media Handler
      encoder         : H.264/AVC
    Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 127 kb/s (default)
    Metadata:
      creation_time   : 2016-03-07T16:01:20.000000Z
      handler_name    : ?Apple Sound Media Handler
    Stream #0:2: Video: mjpeg, yuvj420p(pc, bt470bg/unknown/unknown), 512x748 [SAR 72:72 DAR 128:187], 90k tbr, 90k tbn, 90k tbc
At least one output file must be specified

有人可以建议我做什么更改来合并这些视频吗?

答案1

您的输入存在问题:输入视频的帧尺寸不同(720×576 vs. 640×480)。在这种情况下,您无法直接连接原始比特流。好吧,您可以,但这样播放将不支持切换分辨率 - 它只会以第一个输入视频的分辨率显示,或者可能会使某些播放器崩溃。

相反,您可能应该对视频进行解码并将它们转换为通用格式(帧尺寸、帧速率、色彩空间、像素纵横比等),然后重新对其进行编码。

上述操作可以通过concat过滤器实现,但首先,您必须缩放/填充其中一个输入,例如通过升级part2.mp4到 720×576(带填充):

ffmpeg -i part1.mp4 -i part2.mp4 -filter_complex "\
[0:v]setsar=1[0v]; \
[1:v]scale=720:576:force_original_aspect_ratio=decrease,setsar=1,\
     pad=720:576:(ow-iw)/2:(oh-ih)/2[1v]; \
[0v][0:a][1v][1:a]concat=n=2:v=1:a=1[outv][outa] \
" -map "[outv]" -map "[outa]" \
-c:v libx264 -crf 23 \
output.mp4

这个帖子FFmpeg 维基了解有关缩放和连接的更多信息。并阅读H.264 编码指南如何设置输出质量。

相关内容