微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用moviepy软件包的问题Python

如何解决使用moviepy软件包的问题Python

我正在使用来自moviepy的ffmpeg_extract_subclip函数来处理视频文件。但是,我得到的视频剪辑在我设置的开始时间和结束时间之间是不同的长度。例如,写:

from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip

clip=clip_filename
cutclip="cutvideo.avi"
ffmpeg_extract_subclip(clip_filename,10,targetname=cutclip)

我得到的视频长度为10,03或类似的长度(就帧数而言,我得到602帧而不是600帧)。 有没有办法获得更准确的输出

解决方法

嗯...我要合并this的答案和ffmpeg_extract_subcliphttps://zulko.github.io/moviepy/_modules/moviepy/video/io/ffmpeg_tools.html#ffmpeg_extract_subclip)的实际实现:

def ffmpeg_extract_subclip(filename,t1,t2,targetname=None):
        """ Makes a new video file playing video file ``filename`` between
        the times ``t1`` and ``t2``. """
    name,ext = os.path.splitext(filename)
    if not targetname:
        T1,T2 = [int(1000*t) for t in [t1,t2]]
        targetname = "%sSUB%d_%d.%s" % (name,T1,T2,ext)
    
    cmd = [get_setting("FFMPEG_BINARY"),"-y","-ss","%0.2f"%t1,"-i",filename,"-t","%0.2f"%(t2-t1),"-map","0","-vcodec","copy","-acodec",targetname]
    
    subprocess_call(cmd)

因此,如您所见,该库非常无状态,因此可以轻松扩展:

def ffmpeg_extract_subclip_precisely(filename,targetname=None):
    """ Makes a new video file playing video file ``filename`` between
        the times ``t1`` and ``t2``. """
    name,ext)

    cmd = [get_setting("FFMPEG_BINARY"),"-force_key_frames","{}:{}".format(t1,t2),"temp_out.mp4"]

    subprocess_call(cmd)
    
    cmd = [get_setting("FFMPEG_BINARY"),"temp_out.mp4",targetname]
    
    subprocess_call(cmd)
    

我相信您应该尽可能以毫秒为单位指定时间。

请注意,上面的代码未经测试。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。