解决Python3使用moviePy编辑视频时,clip.show()卡顿

按照官方的说明,clip.show()是利用pygame模块来展示的,pygame在显示图像时需要加入以下代码防止卡顿

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()

于是为方便后续的调试,封装了一个对象

import threading
from moviepy.editor import *
import pygame

class video(threading.Thread):
    def __init__(self,  clip):
        threading.Thread.__init__(self)
        self.clip = clip
    def show(self, timing:float=0, interactive:bool=False):
        show_clip(self.clip, timing, interactive)
    def preview(self,  fps:int = 0, audio:bool = True):
        preview(self.clip, fps=fps, audio=audio)

def show_clip(clip, timing:float=0, interactive:bool = False):
    if timing > 0:
        clip.show(timing, interactive=interactive)
    else:
        clip.show(interactive=interactive)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()

def preview(clip, fps:int = 0, audio:bool = True):
    if fps > 0:
        clip.preview(fps=fps, audio=audio)
    else:
        clip.preview(audio=audio)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()

clip1 = VideoFileClip('abc.mp4')
video(clip1).show()

这里顺便把preview方法也封装了一下(实测preview好像是不会造成卡顿)

这样以后直接用video(clip).show()方法即可预览首帧,

video(clip).show(5, interactive=True) 预览5秒位置的首帧,并开启交互模式

上一篇:2021-07-20


下一篇:MySQL数据库设置连接超时断开