假设我创建了一个用于将 youtube 视频转换为 gif 的基本网络
这是我对 django 代码的看法
from django.shortcuts import render
from django.http import HttpResponse
from django.template import RequestContext
from django.shortcuts import render_to_response
import pafy
from PIL import Image
from moviepy.editor import *
import os
context = RequestContext(request)
if request.GET:
link = request.GET.get('link','')
url = link
try:
video = pafy.new(url)
except:
return render_to_response("generated/invalid.html", context)
video = video.getbest()
if os.path.isfile(os.getcwd()+'\\static\\'+video.title+'.gif')==True:
gifpath=video.title+'.gif'
context_dict = {'staticpath':gifpath}
return render_to_response("generated/generated.html", context_dict, context)
video.download(quiet=True)
clip = (VideoFileClip(os.getcwd()+"\\"+video.title+'.'+video.extension).resize(0.4))
clip.write_gif(os.getcwd()+'\\static\\'+video.title+'.gif', fps=9, opt='optimizeplus', loop=0)
gifpath=video.title+'.gif'
context_dict = {'staticpath':gifpath}
return render_to_response("generated/generated.html", context_dict, context)
它工作正常,但是在我转换 2-5 个视频(它是随机的)之后,它给了我这个错误, 从它的外观来看,原因在于 VideoFileClip 方法 AttributeError 如果我重新启动 django 服务器它将再次工作,奇怪! !
在django调试器中,异常是[WinError 6] The handle is invalid
用 windows 8.1 64 位和 windows 7 32 位试了一下
更新,我认为这是导致此问题的 gif 转换器,在我打开下载状态 pafy 确认我下载了视频后(查看最高和最低请求)
但奇怪的是,随机运行后会弹出错误。你认为这是我的代码的错还是lib?