You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

FFmpeg从帧序列生成子视频时非首段视频黑屏问题

解决FFmpeg分割帧序列生成子视频时开头黑屏的问题

问题原因

你当前的命令只用select滤镜筛选了目标区间的帧,但FFmpeg会保留原始帧序列的时间戳信息。比如处理[64,110]这类非起始区间时,FFmpeg会默认前面空缺帧的时间是存在的,播放时就会先显示对应时长的黑屏,直到目标起始帧的时间戳到来。

解决方案

方法1:重置时间戳(修改滤镜)

在select滤镜后追加setpts=PTS-STARTPTS,把筛选后的帧时间戳从0开始重新计算,彻底消除黑屏问题。

修改后的Python代码:

for i, (start_idx, end_idx) in enumerate(self.video_frames_indexes):
    frame_count = end_idx - start_idx
    if frame_count > 10:
        shape = cv2.imread(f'output/video_reconstitution/{video_name}/final/frame_{start_idx}.jpg').shape
        os.system(f'ffmpeg -r 30 -s {shape[0]}x{shape[1]} -i output/video_reconstitution/{video_name}/final/frame_%d.JPG'
              f' -vf "select=between(n\,{start_idx}\,{end_idx}),setpts=PTS-STARTPTS" -vcodec libx264 -crf 25'
              f' output/video_reconstitution/IMG_7303/sub_videos/serrage_{i}.mp4')

对应的FFmpeg命令:

ffmpeg -r 30 -s {shape[0]}x{shape[1]} -i output/video_reconstitution/{video_name}/final/frame_%d.JPG -vf "select=between(n\,{start_idx}\,{end_idx}),setpts=PTS-STARTPTS" -vcodec libx264 -crf 25 output/video_reconstitution/IMG_7303/sub_videos/serrage_{i}.mp4

方法2:直接指定起始帧与帧数量(更高效)

跳过前面不需要的帧,直接从目标起始帧开始读取,并用-frames:v指定要读取的总帧数,完全避免滤镜处理带来的时间戳问题,生成速度更快。

修改后的Python代码:

for i, (start_idx, end_idx) in enumerate(self.video_frames_indexes):
    frame_count = end_idx - start_idx
    if frame_count > 10:
        shape = cv2.imread(f'output/video_reconstitution/{video_name}/final/frame_{start_idx}.jpg').shape
        os.system(f'ffmpeg -r 30 -s {shape[0]}x{shape[1]} -start_number {start_idx} -i output/video_reconstitution/{video_name}/final/frame_%d.JPG'
              f' -frames:v {frame_count} -vcodec libx264 -crf 25'
              f' output/video_reconstitution/IMG_7303/sub_videos/serrage_{i}.mp4')

对应的FFmpeg命令:

ffmpeg -r 30 -s {shape[0]}x{shape[1]} -start_number {start_idx} -i output/video_reconstitution/{video_name}/final/frame_%d.JPG -frames:v {end_idx - start_idx} -vcodec libx264 -crf 25 output/video_reconstitution/IMG_7303/sub_videos/serrage_{i}.mp4

说明

方法2更推荐,它直接读取目标帧范围,不需要对所有帧进行筛选处理,生成视频的速度更快,也不会出现时间戳相关的异常。

内容的提问来源于stack exchange,提问作者arlaine

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 16:05:20