![剪切和合并相机的 H264 视频会导致文件损坏](https://linux22.com/image/1489040/%E5%89%AA%E5%88%87%E5%92%8C%E5%90%88%E5%B9%B6%E7%9B%B8%E6%9C%BA%E7%9A%84%20H264%20%E8%A7%86%E9%A2%91%E4%BC%9A%E5%AF%BC%E8%87%B4%E6%96%87%E4%BB%B6%E6%8D%9F%E5%9D%8F.png)
我不确定是特定视频的问题还是命令不正确。我已经使用最新的 ffmpeg 4/4/2016 版本和旧版本 v2.8.6 进行了测试。Sample.avi 由 PTZ 摄像机录制。
为了剪切和合并视频文件,我使用了此处的命令邮政。
# skip 30 seconds and re-mux a 60 seconds segment
ffmpeg -ss 30 -t 60 -i D:\VideoCutting\Sample.avi -c:v copy -c:a copy -bsf:v h264_mp4toannexb D:\VideoCutting\Segment1.ts
# skip 3600 seconds and re-mux a 60 seconds segment
ffmpeg -ss 3600 -t 60 -i D:\VideoCutting\Sample.avi -c:v copy -c:a copy -bsf:v h264_mp4toannexb D:\VideoCutting\Segment2.ts
输出:
Input #0, avi, from 'D:\VideoCutting\Sample.avi':
Duration: 06:05:07.14, start: 0.000000, bitrate: 2479 kb/s
Stream #0:0: Video: h264 (High) (H264 / 0x34363248), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 2476 kb/s, 14.98 fps, 14.99 tbr, 14.98 tbn, 59.94 tbc
Output #0, mpegts, to 'D:\VideoCutting\Segment1.ts':
Metadata:
encoder : Lavf57.29.101
Stream #0:0: Video: h264 (H264 / 0x34363248), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 2476 kb/s, 14.98 fps, 14.99 tbr, 90k tbn, 14.98 tbc
Stream mapping:
Stream #0:0 -> #0:0 (copy)
错误:
[NULL @ 05149480] Packet header is not contained in global extradata, corrupted stream or invalid MP4/AVCC bitstream
[NULL @ 05149480] Failed to open bitstream filter h264_mp4toannexb for stream 0 with codec copy: Invalid argument
[mpegts @ 051c8420] Timestamps are unset in a packet for stream 0. This is depre cated and will stop working in the future. Fix your code to set the timestamps properly
视频片段已创建并可播放。
但是,当我加入片段时,合并的视频不再可播放:
# merge the segments and re-mux them as mp4
ffmpeg -i "concat:D:\VideoCutting\Segment1.ts|D:\VideoCutting\Segment2.ts" -c:v copy -c:a copy -movflags empty_moov -flags global_header -bsf:v dump_extra Merged.mp4
结果:
Input #0, mpegts, from 'concat:D:\VideoCutting\Segment1.ts|D:\VideoCutting\Segme
nt2.ts':
Duration: 00:01:00.15, start: 1.400000, bitrate: 7610 kb/s
Program 1
Metadata:
service_name : Service01
service_provider: FFmpeg
Stream #0:0[0x100]: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p, 19
20x1080 [SAR 1:1 DAR 16:9], 29.97 fps, 14.99 tbr, 90k tbn, 59.94 tbc
Output #0, mp4, to 'Merged.mp4':
Metadata:
encoder : Lavf57.29.101
Stream #0:0: Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 1920x1080 [SAR 1
:1 DAR 16:9], q=2-31, 29.97 fps, 14.99 tbr, 90k tbn, 90k tbc
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
[mpegts @ 0003ca60] DTS 126000 < 5539218 out of order
frame= 1803 fps=0.0 q=-1.0 Lsize= 51655kB time=00:02:00.26 bitrate=3518.7kbits
/s speed= 601x
video:51633kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing
overhead: 0.042451%
错误: [mpegts @ 0003ca60] DTS 126000 < 5539218 故障
从其他帖子中我猜问题出在格式不正确的视频文件上。我可以不重新编码就剪切和合并它吗?还是我必须先以某种方式修复视频文件?
答案1
因此,您在 AVI 中拥有 H.264,并且看起来 H.264 比特流过滤器并不适用于此。
使用它来生成片段:
# skip 30 seconds and re-mux a 60 seconds segment
ffmpeg -ss 30 -t 60 -i D:\VideoCutting\Sample.avi -c copy -avoid_negative_ts make_zero -fflags +genpts D:\VideoCutting\Segment1.ts
然后合并段
ffmpeg -i "concat:D:\VideoCutting\Segment1.ts|D:\VideoCutting\Segment2.ts" -c copy -flags +global_header -fflags +genpts Merged.mp4