使用ffmpeg剪辑视频时丢失帧

2024-05-23 20:32:19 发布

您现在位置:Python中文网/ 问答频道 /正文

当我用ffmpeg剪辑视频时,我好像丢了帧。你知道吗

以下是我采取的步骤:

[获取要剪切的帧编号]->;[将帧编号转换为hh:mm:不锈钢格式]->;[运行ffmpeg进程]

代码如下:

import subprocess

def frames_to_timecode(frame,frameRate):
    '''
    Convert frame into a timecode HH:MM:SS.MS
    frame = The frame to convert into a time code
    frameRate = the frame rate of the video
    '''
    #convert frames into seconds
    seconds = frame / frameRate

    #generate the time code
    timeCode = '{h:02d}:{m:02d}:{s:02f}'.format(
    h=int(seconds/3600),
    m=int(seconds/60%60),
    s=seconds%60)

    return timeCode

frameRate = 24.0

inputVideo = r"C:\Users\aquamen\Videos\vlc-record-2018-10-23-17h11m11s-SEQ-0200_animatic_v4_20180827_short.mp4"
outputVideo = r"C:\Users\aquamen\Videos\ffmpeg_test_clip001.mp4"
ffmpeg = r"C:\ffmpeg\ffmpeg-20181028-e95987f-win64-static\bin\ffmpeg.exe" 

endFrame = frames_to_timecode(29,frameRate)
startFrame = frames_to_timecode(10,frameRate)

subprocess.call([ffmpeg,'-i',inputVideo,'-ss',startFrame,'-to',endFrame,outputVideo])

这是原始视频和剪辑视频的图像,时间码显示在处理过程中丢失了一帧。时间代码应该显示00:01:18:10,而不是00:01:18:11。你知道吗

Original Video that clip was taken from

Clipped Video that's missing the frame


Tags: theto代码gtframes视频剪辑frame
1条回答
网友
1楼 · 发布于 2024-05-23 20:32:19

所以我的一个朋友发现了这个。因此,如果将帧除以fps(frame/fps),就得到了需要使用-ss剪切该帧的时间点,但问题是默认情况下python会将小数点后12位舍入。所以你不需要对数字进行四舍五入,只需要给ffmpeg最多3个小数位。你知道吗

所以这里是我为任何遇到这个问题的人修改的代码。如果要在帧号上剪切视频,请使用以下命令:

import subprocess


def frame_to_seconds(frame,frameRate):
    '''
    This will turn the frame into seconds.miliseconds
    so you can cut on frames in ffmpeg
    frame = The frame to convert into seconds
    frameRate = the frame rate of the video
    '''
    frameRate = float(frameRate)
    seconds = frame / frameRate
    result = str(seconds - seconds % 0.001)

    return result

inputVideo = "yourVideo.mp4"

outputVideo = "clipedVideo.mp4"
ffmpeg = r"C:\ffmpeg\bin\ffmpeg.exe" 

frameRate = 24

subprocess.call([ffmpeg,
                 '-i',inputVideo,
                 '-ss',frame_to_seconds(10,frameRate),
                 '-to',frame_to_seconds(20,frameRate),
                 outputVideo])

相关问题 更多 >