【问题标题】:Issue using moviepy package (Python)使用moviepy包(Python)的问题
【发布时间】:2021-01-03 16:14:46
【问题描述】:

我正在使用来自 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, 0, 10, targetname=cutclip)

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

【问题讨论】:

    标签: python python-3.x ffmpeg video-processing video-editing


    【解决方案1】:

    嗯...我会合并this 答案和ffmpeg_extract_subclip(https://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", "copy", targetname]
        
        subprocess_call(cmd)
    

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

    def ffmpeg_extract_subclip_precisely(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"), "-i", filename,
               "-force_key_frames", "{}:{}".format(t1,t2), "temp_out.mp4"]
    
        subprocess_call(cmd)
        
        cmd = [get_setting("FFMPEG_BINARY"),"-y",
               "-ss", "%0.2f"%t1,
               "-i", "temp_out.mp4",
               "-t", "%0.2f"%(t2-t1),
               "-map", "0", "-vcodec", "copy", "-acodec", "copy", targetname]
        
        subprocess_call(cmd)
        
    

    顺便说一下,我认为您应该尽可能以毫秒精度指定时间。

    请注意以上代码未经测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-13
      • 2022-07-27
      • 1970-01-01
      • 2021-07-19
      • 2018-03-11
      • 2015-06-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多