FFmpeg - 将视频剪切成片段,分别转码,然后连接并输出为 .ts

FFmpeg - 将视频剪切成片段,分别转码,然后连接并输出为 .ts

这是使用 ffmpeg 连接剪辑时出现音频错误

我想要实现的目标如下:

  1. 将输入视频切成块
  2. 使用 x.264 和 libfdk_aac 将每个块(视频和音频)单独转码到 .ts 容器中
  3. 将转码后的块连接起来并输出为 1 个 .ts 文件

我可以剪切片段并使用 trim/concat 解决方案将它们连接起来正如这里建议的那样

ffmpeg -i input.mp4 -filter_complex "[0:v]trim=duration=5[av];[0:a]atrim=duration=5[aa];\
[0:v]trim=start=5:end=10,setpts=PTS-STARTPTS[bv]; [0:a]atrim=start=05:end=10,asetpts=PTS-STARTPTS[ba];\
[av][bv]concat[cv];[aa][ba]concat=v=0:a=1[ca];\
[0:v]trim=start=10:end=15,setpts=PTS-STARTPTS[dv];\
[0:a]atrim=start=10:end=15,asetpts=PTS-STARTPTS[da];\
[cv][dv]concat[outv];[ca][da]concat=v=0:a=1[outa]" -map [outv] -map [outa] output.mp4

然而,该解决方案缺少“单独转码每个片段”的步骤。

我尝试了另一种方法 - 剪切每个片段并使用相同的命令进行转码

ffmpeg -i input.mp4 -ss 00 -t 10 -vcodec libx264 -acodec libfdk_aac -f mpegts segment0.ts

然后连接

printf "file '%s'\n" ./*.ts > mylist.txt

ffmpeg -f concat -i mylist.txt -vcodec copy -bsf:a aac_adtstoasc output.mp4

然而,在分段/转码步骤之后,每个分段在其音频流的开头都会有短暂的静音,然后在连接视频的每个粘合点都可以听到。我尝试了一些测试视频,只有 1 个我听不到这个引入的静音。

现在我想知道是否有办法使用上面的修剪/连接解决方​​案,但包括“在连接之前分别对每个片段进行转码”步骤。也许这必须在 3 个命令中实现,即修剪成片段、对每个片段进行转码、连接转码后的片段,而不能在同一个命令中完成所有操作。

在我尝试使用搜索和转码的方法中,发生了这些音频问题,我怀疑它们是在每个片段的转码过程中引入的。不确定这是否是由于我尝试的方法造成的,可以使用例如 Trim 过滤器(或另一个?)和后续转码来避免,但我不知道可以实现这一点的命令或命令序列。


尝试这个命令

ffmpeg -i mtb.mp4 -ss 05 -t 5 -c:v libx264 -c:a aac -strict -2 transcoded3.mp4

产生以下控制台输出

ffmpeg -i mtb.mp4 -ss 05 -t 5 -c:v libx264 -c:a aac -strict -2 transcoded3.mp4
ffmpeg version 2.4.git Copyright (c) 2000-2014 the FFmpeg developers
built on Nov 20 2014 12:45:24 with gcc 4.8 (Ubuntu 4.8.2-19ubuntu1)
configuration: --prefix=/home/tobi/ffmpeg_build --extra-cflags=-I/home/tobi/ffmpeg_build/include     --extra-ldflags=-L/home/tobi/ffmpeg_build/lib --bindir=/home/tobi/bin --enable-gpl --enable-libass -    -enable-libfdk-aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libtheora --    enable-libvorbis --enable-libvpx --enable-libx264 --enable-nonfree --enable-x11grab
libavutil      54. 14.100 / 54. 14.100
libavcodec     56. 12.101 / 56. 12.101
libavformat    56. 14.100 / 56. 14.100
libavdevice    56.  3.100 / 56.  3.100
libavfilter     5.  2.103 /  5.  2.103
libswscale      3.  1.101 /  3.  1.101
libswresample   1.  1.100 /  1.  1.100
libpostproc    53.  3.100 / 53.  3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'mtb.mp4':
Metadata:
major_brand     : mp42
minor_version   : 1
compatible_brands: mp41mp42isom
creation_time   : 2014-06-07 13:05:13
Duration: 00:01:40.35, start: 0.263317, bitrate: 3231 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720, 3027 kb/s, 29.97     fps, 29.97 tbr, 60k tbn, 59.94 tbc (default)
Metadata:
  creation_time   : 2014-06-07 13:05:13
  handler_name    : Core Media Video
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 191 kb/s  (default)
Metadata:
  creation_time   : 2014-06-07 13:05:13
  handler_name    : Core Media Audio
[libx264 @ 0x25ec3a0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2
[libx264 @ 0x25ec3a0] profile High, level 3.1
[libx264 @ 0x25ec3a0] 264 - core 142 - H.264/MPEG-4 AVC codec - Copyleft 2003-2014 -     http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex     subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0     deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=1 lookahead_threads=1 sliced_threads=0 nr=0     decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0     direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0     rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40     aq=1:1.00
Output #0, mp4, to 'transcoded3.mp4':
Metadata:
major_brand     : mp42
minor_version   : 1
compatible_brands: mp41mp42isom
encoder         : Lavf56.14.100
Stream #0:0(und): Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 1280x720, q=-1--1,     29.97 fps, 30k tbn, 29.97 tbc (default)
Metadata:
  creation_time   : 2014-06-07 13:05:13
  handler_name    : Core Media Video
  encoder         : Lavc56.12.101 libx264
Stream #0:1(und): Audio: aac ([64][0][0][0] / 0x0040), 44100 Hz, stereo, fltp, 128 kb/s     (default)
Metadata:
  creation_time   : 2014-06-07 13:05:13
  handler_name    : Core Media Audio
  encoder         : Lavc56.12.101 aac
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
frame=    1 fps=1.0 q=0.0 size=       0kB time=00:00:00.99 bitrate=   0.4kbits/sframe=   24 fps=     16 q=0.0 size=       0kB time=00:00:01.76 bitrate=   0.2kbits/sframe=   42 fps= 17 q=29.0 size=          53kB time=00:00:02.34 bitrate= 186.5kbits/frame=   45 fps= 15 q=29.0 size=      95kB     time=00:00:02.46 bitrate= 317.7kbits/frame=   49 fps= 13 q=29.0 size=     131kB time=00:00:02.60     bitrate= 411.1kbits/frame=   51 fps= 12 q=29.0 size=     169kB time=00:00:02.64 bitrate=     523.9kbits/frame=   54 fps= 11 q=29.0 size=     215kB time=00:00:02.76 bitrate= 636.9kbits/frame=       58 fps= 11 q=29.0 size=     247kB time=00:00:02.87 bitrate= 703.6kbits/frame=   61 fps= 10 q=29.0     size=     284kB time=00:00:02.99 bitrate= 776.5kbits/frame=   65 fps=9.8 q=29.0 size=     323kB     time=00:00:03.13 bitrate= 844.6kbits/frame=   69 fps=9.5 q=29.0 size=     363kB time=00:00:03.25     bitrate= 913.8kbits/frame=   72 fps=9.2 q=29.0 size=     408kB time=00:00:03.36 bitrate=     993.7kbits/frame=   76 fps=9.1 q=29.0 size=     437kB time=00:00:03.48 bitrate=1028.8kbits/frame=       80 fps=8.9 q=29.0 size=     483kB time=00:00:03.62 bitrate=1092.0kbits/frame=   83 fps=8.7 q=29.0     size=     516kB time=00:00:03.71 bitrate=1137.6kbits/frame=   86 fps=8.5 q=29.0 size=     557kB     time=00:00:03.83 bitrate=1190.5kbits/frame=   89 fps=8.2 q=29.0 size=     601kB time=00:00:03.92     bitrate=1254.7kbits/frame=   93 fps=8.2 q=29.0 size=     635kB time=00:00:04.06     bitrate=1279.6kbits/frame=   97 fps=8.0 q=29.0 size=     683kB time=00:00:04.20     bitrate=1331.7kbits/frame=  101 fps=7.9 q=29.0 size=     737kB time=00:00:04.31 bitrate=1397.1kbits/frame=  105 fps=7.8 q=29.0 size=     776kB time=00:00:04.45 bitrate=1426.4kbits/frame=  109 fps=7.7 q=29.0 size=     827kB time=00:00:04.59 bitrate=1473.2kbits/frame=  112 fps=7.6 q=29.0 size=     855kB time=00:00:04.69 bitrate=1492.8kbits/frame=  115 fps=7.5 q=29.0 size=     898kB time=00:00:04.78 bitrate=1538.3kbits/frame=  119 fps=7.5 q=29.0 size=     944kB time=00:00:04.92 bitrate=1570.5kbits/frame=  123 fps=7.5 q=29.0 size=     986kB time=00:00:04.94 bitrate=1633.0kbits/frame=  127 fps=7.4 q=29.0 size=    1040kB time=00:00:04.94 bitrate=1722.5kbits/frame=  131 fps=7.4 q=29.0 size=    1089kB time=00:00:04.94 bitrate=1803.1kbits/frame=  134 fps=7.3 q=29.0 size=    1146kB time=00:00:04.94 bitrate=1898.5kbits/frame=  137 fps=7.2 q=29.0 size=    1189kB time=00:00:04.94 bitrate=1970.1kbits/frame=  140 fps=7.2 q=29.0 size=    1231kB time=00:00:04.94 bitrate=2039.6kbits/frame=  143 fps=7.1 q=29.0 size=    1275kB time=00:00:04.94 bitrate=2112.6kbits/frame=  147 fps=7.1 q=29.0 size=    1326kB time=00:00:04.94 bitrate=2196.8kbits/frame=  150 fps=5.3 q=29.0 Lsize=    2092kB time=00:00:05.01 bitrate=3416.7kbits/s    
video:2007kB audio:79kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead:     0.283190%
[libx264 @ 0x25ec3a0] frame I:1     Avg QP:24.42  size: 53959
[libx264 @ 0x25ec3a0] frame P:123   Avg QP:24.87  size: 15050
[libx264 @ 0x25ec3a0] frame B:26    Avg QP:28.35  size:  5760
[libx264 @ 0x25ec3a0] consecutive B-frames: 70.7% 13.3% 16.0%  0.0%
[libx264 @ 0x25ec3a0] mb I  I16..4:  3.2% 80.6% 16.1%
[libx264 @ 0x25ec3a0] mb P  I16..4:  1.7%  8.2%  1.4%  P16..4: 44.2% 20.7%  6.6%  0.0%  0.0%        skip:17.1%
[libx264 @ 0x25ec3a0] mb B  I16..4:  0.4%  1.6%  0.3%  B16..8: 48.8%  5.7%  0.6%  direct: 0.8%      skip:41.9%  L0:53.1% L1:45.4% BI: 1.5%
[libx264 @ 0x25ec3a0] 8x8 transform intra:72.8% inter:76.3%
[libx264 @ 0x25ec3a0] coded y,uvDC,uvAC intra: 64.3% 36.4% 1.5% inter: 23.5% 15.1% 0.0%
[libx264 @ 0x25ec3a0] i16 v,h,dc,p: 32% 21% 25% 22%
[libx264 @ 0x25ec3a0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 17% 18% 20%  6%  8%  6%  9%  6%  9%
[libx264 @ 0x25ec3a0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 15% 21% 14%  7% 10%  8% 11%  6%  8%
[libx264 @ 0x25ec3a0] i8c dc,h,v,p: 69% 15% 14%  2%
[libx264 @ 0x25ec3a0] Weighted P-Frames: Y:2.4% UV:0.0%
[libx264 @ 0x25ec3a0] ref P L0: 77.7% 20.1%  1.6%  0.5%  0.0%
[libx264 @ 0x25ec3a0] ref B L0: 99.0%  0.9%  0.1%
[libx264 @ 0x25ec3a0] ref B L1: 98.8%  1.2%
[libx264 @ 0x25ec3a0] kb/s:3284.53

答案1

您实际上是在谈论对不同视频块进行不同的编码,这些编码可以通过不同的运行来实现 - 就像您的命令一样ffmpeg -i mtb.mp4 -ss 05 -t 5 -c:v libx264 -c:a aac -strict -2 transcoded3.mp4

要使用Trim过滤器将一个时间段从 5 秒切到 10 秒,请使用以下命令(这确实使用了setptsasetpts):

ffmpeg -i mtb.mp4 -filter_complex \
“[0:v]trim=start=05:end=10,setpts=PTS-STARTPTS[tv];\
[0:a]atrim=start=05:end=10,asetpts=PTS-STARTPTS[ta]” \
-map [tv] -map [ta] fragment2.ts

请注意,我根本没有包含任何编码参数(质量)。

当您连接 ts 文件时您应该能够简单地说:

cat 段1.ts 段2.ts .... 段n.ts > 完整文件.ts

相关内容