简报

简报

我正在尝试将 2 个 mp4 视频合并在一起ffmpeg.但最终得到的视频并不完美。

我的files.txt,指向要连接的视频:

file 'cutted1.mp4'
file 'cutted2.mp4'

使用的ffmpeg命令:

$ ffmpeg -f concat -i files.txt -c copy result_ffmpeg.mp4

连接之后,得到的视频有很多混乱:

  • 丢失了第二个视频的前几秒;
  • 冻结第二个视频的最后几秒;
  • 输出很多警告,类似这样的:Non-monotonous DTS in output stream 0:0; previous: 479509, current: 361106; changing to 479510. This may result in incorrect timestamps in the output file.

最后,我的完整终端输出:

$ ffmpeg -f concat -i files.txt -c copy result_ffmpeg.mp4
ffmpeg version 3.4.6-0ubuntu0.18.04.1 Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 7 (Ubuntu 7.3.0-16ubuntu3)
  configuration: --prefix=/usr --extra-version=0ubuntu0.18.04.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared
  WARNING: library configuration mismatch
  avcodec     configuration: --prefix=/usr --extra-version=0ubuntu0.18.04.1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --enable-gpl --disable-stripping --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librubberband --enable-librsvg --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-omx --enable-openal --enable-opengl --enable-sdl2 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libopencv --enable-libx264 --enable-shared --enable-version3 --disable-doc --disable-programs --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libtesseract --enable-libvo_amrwbenc
  libavutil      55. 78.100 / 55. 78.100
  libavcodec     57.107.100 / 57.107.100
  libavformat    57. 83.100 / 57. 83.100
  libavdevice    57. 10.100 / 57. 10.100
  libavfilter     6.107.100 /  6.107.100
  libavresample   3.  7.  0 /  3.  7.  0
  libswscale      4.  8.100 /  4.  8.100
  libswresample   2.  9.100 /  2.  9.100
  libpostproc    54.  7.100 / 54.  7.100
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x55a7392081c0] Auto-inserting h264_mp4toannexb bitstream filter
Input #0, concat, from 'files.txt':
  Duration: N/A, start: -0.023220, bitrate: 2167 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 2041 kb/s, 23.81 fps, 23.81 tbr, 16k tbn, 47.62 tbc
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(jpn): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 126 kb/s
    Metadata:
      handler_name    : SoundHandler
Output #0, mp4, to 'result_ffmpeg.mp4':
  Metadata:
    encoder         : Lavf57.83.100
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 2041 kb/s, 23.81 fps, 23.81 tbr, 16k tbn, 16k tbc
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(jpn): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 126 kb/s
    Metadata:
      handler_name    : SoundHandler
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x55a7392081c0] Auto-inserting h264_mp4toannexb bitstream filter
[mp4 @ 0x55a73920d420] Non-monotonous DTS in output stream 0:0; previous: 479508, current: 360606; changing to 479509. This may result in incorrect timestamps in the output file.
[mp4 @ 0x55a73920d420] Non-monotonous DTS in output stream 0:0; previous: 479509, current: 361106; changing to 479510. This may result in incorrect timestamps in the output file.

...(more of the same warnings removed for better readability)

[mp4 @ 0x55a73920d420] Non-monotonous DTS in output stream 0:0; previous: 479744, current: 478606; changing to 479745. This may result in incorrect timestamps in the output file.
[mp4 @ 0x55a73920d420] Non-monotonous DTS in output stream 0:0; previous: 479745, current: 479106; changing to 479746. This may result in incorrect timestamps in the output file.
[mp4 @ 0x55a73920d420] Non-monotonous DTS in output stream 0:0; previous: 479746, current: 479606; changing to 479747. This may result in incorrect timestamps in the output file.
frame= 1437 fps=0.0 q=-1.0 Lsize=   15403kB time=00:01:00.24 bitrate=2094.3kbits/s speed= 670x    
video:14426kB audio:930kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.306242%

那么,如何正确地连接视频呢?

答案1

简报

使用 ffmpeg,我可以通过为每个视频文件生成一个文件来连接这两个视频*.ts。它是视频传输流文件类型。此格式的工作方式类似于中间文件格式:此文件将两个不同的视频标准化,然后软化它们,以便之后进行连接。

编码

要连接的命令是:

  • 步骤 0:查看我们的文件夹,我们有。
$ ls ./
cutted1.mp4  cutted2.mp4
  • 步骤 1:*.ts从两个视频文件分别生成文件。
$ ffmpeg -i cutted1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate1.ts
$ ffmpeg -i cutted2.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate2.ts
  • 第 2 步:查看我们的文件夹,我们有两个*.ts文件:
$ ls ./
cutted1.mp4  cutted2.mp4  intermediate1.ts  intermediate2.ts
  • 步骤 3:连接两个 *.ts 文件:
$ ffmpeg -i "concat:intermediate1.ts|intermediate2.ts" -c copy -bsf:a aac_adtstoasc output.mp4
  • 步骤 4:最后查看我们的文件夹,我们有:
$ ls ./
cutted1.mp4  cutted2.mp4  intermediate1.ts  intermediate2.ts  output.mp4

参考:

这些链接帮助我解决了我的问题:

没有起作用:

就我的情况而言,下面的列表没有解决我的问题。但也许它可以解决你的问题。

(注意:Stackoverflow 避免我发布包含超过 8 个链接的答案。因此,我以非格式化的方式输入了下面的链接。如果任何版主认为它们有用,请将下面的 URL 编辑为可点击链接。谢谢。)

因此,我将其列出以供参考:

答案2

差不多五年后,让我说:哇,这太有帮助了。感谢您花时间发布您自己的问题的答案。ffmpeg 不是为有丢失等缺陷的视频源(行车记录仪、安全摄像头)设计的,因此通常的 concat 命令要么会阻塞,要么会生成不可行的输出(视频冻结)。

这些命令看起来很简单,但显然您花费了大量的研究和努力才得出这个答案!

相关内容