视频切片(ffmpeg)

视频切片(ffmpeg)

Content #

segment_format指定切片格式 #

ffmpeg -re -i input.mp4 -c copy -f segment -segment_format mp4 output-%d.mp4

查看第一分片mp4的最后的时间戳 #

ffprobe -v quiet -show_packets -select_streams v output-0.mp4 \
   | grep pts_time | tail -n 3

生成ffconcat格式索引文件 #

ffmpeg -re -i input.mp4 -c copy -f segment -segment_format mp4 \
    -segment_list_type ffconcat -segment_list output.lst output-%d.mp4

生成FLAT格式索引文件 #

ffmpeg -re -i input.mp4 -c copy -f segment -segment_format mp4 \
    -segment_list_type flat -segment_list filelist.txt output-%d.mp4

生成M3U8格式索引文件 #

ffmpeg -re -i input.mp4 -c copy -f segment -segment_format mp4 \
    -segment_list_type m3u8 -segment_list output.m3u8 output-%d.mp4

生成CSV格式索引文件 #

ffmpeg -re -i input.mp4 -c copy -f segment -segment_format mp4 \
    -segment_list_type csv -segment_list filelist.csv output-%d.mp4

csv列表文件中内容分为三个字段:文件名,文件起始时间,文件结束时间

ss与t参数 #

从视频文件的第10秒钟开始截取 #

ffmpeg -ss 10 -i input.mp4 -c copy output.ts

截取视频文件的前10秒 #

t参数可以指定截取的视频长度。

ffmpeg -i input.mp4 -c copy -t 10 -copyts output.mp4

使用output_ts_offset指定输出文件的start_time #

使用ss或t参数,输出文件的start_time会归0,可用output_ts_offset来指定。

ffmpeg -i input.mp4 -c copy -t 10 -output_ts_offset 120 output.mp4

-copyts Do not process input timestamps, but keep their values without trying to sanitize them. In particular, do not remove the initial start time offset value.

Note that, depending on the vsync option or on specific muxer processing (e.g. in case the format option avoid_negative_ts is enabled) the output timestamps may mismatch with the input timestamps even when this option is selected.