栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

解决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秒位置的首帧,并开启交互模式

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/488609.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号