Linux中FFmpeg无损整合两条视频

请注意,本文编写于 103 天前,最后修改于 103 天前,其中某些信息可能已经过时。

前言

最近在追B站的《说唱新世代》,这部综艺可以算得上是说唱届的一股清流,由于最近在海外,无法通过B站直接观看,YouTube上一些音乐由于无海外版权原因,进行了消音处理。这对一档音乐类节目来说是很致命的了,有时候手里干着活,边上放着节目,突然就没声音了。于是通过下载渠道下载进emby进行观看,前4期倒也相安无事,从第五期开始有些地方就没有更新,或者是跟B站一样,每一期分成了上下两部分,对于emby管理来说有点麻烦。求人不如靠自己,先把分集下载回来,再用FFmpeg进行视频合并这样就符合我食用的要求。

模板

首先安装FFmpeg,这部分就不赘述了
在视频目录下新建一个“filelist.txt”文件,文件内容如下

file '1.mp4'
file '2.mp4'
file '3.mp4'
file '4.mp4'
file '5.mp4'
file '6.mp4'
file '7.mp4'
file '8.mp4'
file '9.mp4'
file '10.mp4'
file '11.mp4'
file '12.mp4'
file '13.mp4'
file '14.mp4'
file '15.mp4'
file '16.mp4'

编辑完成后,进入视频所在目录,输入如下指令

ffmpeg -f concat -i filelist.txt -c copy output.mp4

此时有可能会报错

[concat @ 0x65100c0] Unsafe file name 'output.mp4' filelist.txt: Operation not permitted

根据ffmpeg的文档提示,需要在 -i 前加入 -safe 0 这个flag

ffmpeg -f concat -safe 0 filelist.txt -c copy output.mp4

此时就应该有正常输出了。

实际操作

以上是都是模板示例,以下是我操作《说唱新世代》第6集,将上下两部分合并的实际操作过程,对上面有疑问的,可以参考下面的实操,自行进行修改命令。

cd /home/PT/说唱新世代.Rap.for.Youth.2020.WEB-DL.1080p.H264.AAC-HDSWEB #进入视频所在目录
nano filelist.txt #编辑filelist.txt文件

#以下为文件内容
file '说唱新世代.Rap.for.Youth.2020.E06-1.Extended.WEB-DL.1080p.H264.AAC-HDSWEB.mp4'
file '说唱新世代.Rap.for.Youth.2020.E06-2.Extended.WEB-DL.1080p.H264.AAC-HDSWEB.mp4'
#以上为文件内容

ffmpeg -f concat -safe 0 -i filelist.txt -c copy '说唱新世代.Rap.for.Youth.2020.E06.Extended.WEB-DL.1080p.H264.AAC-HDSWEB.mp4' #使用ffmpeg将filelist.txt内的两条视频进行合并

#以下是最后的输出
ffmpeg version 4.2.2-static https://johnvansickle.com/ffmpeg/  Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 8 (Debian 8.3.0-6)
  configuration: --enable-gpl --enable-version3 --enable-static --disable-debug --disable-ffplay --disable-indev=sndio --disable-outdev=sndio --cc=gcc --enable-fontconfig --enable-frei0r --enable-gnutls --enable-gmp --enable-libgme --enable-gray --enable-libaom --enable-libfribidi --enable-libass --enable-libvmaf --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librubberband --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libvorbis --enable-libopus --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libdav1d --enable-libxvid --enable-libzvbi --enable-libzimg
  libavutil      56. 31.100 / 56. 31.100
  libavcodec     58. 54.100 / 58. 54.100
  libavformat    58. 29.100 / 58. 29.100
  libavdevice    58.  8.100 / 58.  8.100
  libavfilter     7. 57.100 /  7. 57.100
  libswscale      5.  5.100 /  5.  5.100
  libswresample   3.  5.100 /  3.  5.100
  libpostproc    55.  5.100 / 55.  5.100
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x63e0000] stream 0, timescale not set
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x63e0000] Auto-inserting h264_mp4toannexb bitstream filter
Input #0, concat, from 'filelist.txt':
  Duration: N/A, start: 0.000000, bitrate: 6148 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709/unknown/bt709), 1920x1080 [SAR 1:1 DAR 16:9], 5831 kb/s, 25 fps, 25 tbr, 16k tbn, 50 tbc
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 317 kb/s
    Metadata:
      handler_name    : SoundHandler
    Stream #0:2: Video: mjpeg (Baseline), yuvj444p(pc, bt470bg/unknown/unknown), 1000x625, 90k tbr, 90k tbn, 90k tbc
Output #0, mp4, to '说唱新世代.Rap.for.Youth.2020.E06.Extended.WEB-DL.1080p.H264.AAC-HDSWEB.mp4':
  Metadata:
    encoder         : Lavf58.29.100
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709/unknown/bt709), 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 5831 kb/s, 25 fps, 25 tbr, 16k tbn, 16k tbc
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 317 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
frame=40444 fps=282 q=-1.0 size= 1248512kB time=00:26:57.66 bitrate=6322.6kbits/s speed=11.2x    frame=40764 fps=282 q=-1.0 size= 1259008kB time=00:27:10.44 bitrate=6325.8kbits/frame=41115 fps=284 q=-1.0 size= 1269760kB time=00:27:24.50 bitrate=6325.2kbits/frame=41240 fps=284 q=-1.0 size= 1274368kB time=00:27:29.51 bitrate=6328.9kbits/frame=41327 fps=283 q=-1.0 size= 1278464kB time=00:27:32.97 bitrate=6336.0kbits/frame=41437 fps=283 q=-1.0 size= 1282816kB time=00:27:37.38 bitrate=6340.6kbits/frame=41585 fps=283 q=-1.0 size= 1287168kB time=00:27:43.31 bitrate=6339.4kbits/frame=41799 fps=283 q=-1.0 size= 1291520kB time=00:27:51.87 bitrate=6328.3kbits/frame=42120 fps=285 q=-1.0 size= 1295360kB time=00:28:04.71 bitrate=6298.7kbits/frame=42316 fps=285 q=-1.0 size= 1298944kB time=00:28:12.54 bitrate=6287.0kbits/frame=42518 fps=285 q=-1.0 size= 1302528kB time=00:28:20.62 bitrate=6274.3kbits/frame=42708 fps=285 q=-1.0 size= 1306112kB time=00:28:28.22 bitrate=6263.6kbits/frame=43048 fps=286 q=-1.0 size= 1313536kB time=00:28:41.83 bitrate=6249.4kbits/frame=43172 fps=286 q=-1.0 size= 1317120kB time=00:28:46.76 bitrate=6248.6kbits/frame=43517 fps=287 q=-1.0 size= 1323264kB time=00:29:00.56 bitrate=6228.0kbits/frame=44149 fps=290 q=-1.0 size= 1335552kB time=00:29:25.84 bitrate=6195.8kbits/frame=44601 fps=292 q=-1.0 size= 1343744kB time=00:29:43.93 bitrate=6170.6kbits/frame=45685 fps=298 q=-1.0 size= 1366016kB time=00:30:27.30 bitrate=6124.0kbits/frame=46145 fps=300 q=-1.0 size= 1376512kB time=00:30:45.71 bitrate=6109.5kbits/frame=47208 fps=306 q=-1.0 size= 1399040kB time=00:31:28.23 bitrate=6069.7kbits/frame=47619 fps=307 q=-1.0 size= 1413376kB time=00:31:44.66 bitrate=6079.0kbits/frame=48061 fps=309 q=-1.0 size= 1425920kB time=00:32:02.34 bitrate=6076.5kbits/frame=48380 fps=310 q=-1.0 size= 1437952kB time=00:32:15.08 bitrate=6087.4kbits/frame=48775 fps=312 q=-1.0 size= 1450240kB time=00:32:30.89 bitrate=6089.7kbits/frame=48846 fps=311 q=-1.0 size= 1454592kB time=00:32:33.74 bitrate=6099.1kbits/frame=49510 fps=314 q=-1.0 size= 1478144kB time=00:33:00.30 bitrate=6114.7kbits/frame=49927 fps=316 q=-1.0 size= 1494016kB time=00:33:16.99 bitrate=6128.7kbits/frame=50217 fps=316 q=-1.0 size= 1503488kB time=00:33:28.59 bitrate=6131.9kbits/frame=50717 fps=318 q=-1.0 size= 1519360kB time=00:33:48.58 bitrate=6135.6kbits/frame=50952 fps=319 q=-1.0 size= 1528320kB time=00:33:57.97 bitrate=6143.4kbits/frame=51438 fps=321 q=-1.0 size= 1542656kB time=00:34:17.42 bitrate=6142.3kbits/frame=51735 fps=321 q=-1.0 size= 1552640kB time=00:34:29.29 bitrate=6146.7kbits/frame=52158 fps=323 q=-1.0 size= 1568256kB time=00:34:46.22 bitr[mov,mp4,m4a,3gp,3g2,mj2 @ 0x63e7f00] stream 0, timescale not setate=6146.2kbits/s speed=12.6x    
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x63e7f00] Auto-inserting h264_mp4toannexb bitstream filter
frame=353605 fps=287 q=-1.0 Lsize=10630596kB time=03:55:44.36 bitrate=6156.9kbits/s speed=11.5x    
video:10069923kB audio:547984kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.119510%

完成后,检查以下最后生成的视频尺寸(Lsize)和视频长度(time)是否和所拼接的多条视频相加后相符,基本上是不会出错的。

添加新评论

评论列表