簡體   English   中英

問題使用 moviepy 包 (Python)

[英]Issue using moviepy package (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, 0, 10, targetname=cutclip)

我得到一個長度為 10,03 或類似的視頻(在幀數方面,我得到 602 幀而不是 600)。 有沒有辦法獲得更准確的輸出?

嗯...我會合並這個答案和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)
    

順便說一下,我相信您應該盡可能以毫秒的精度指定時間。

請注意,上面的代碼未經測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM