KeyError: 'video_fps' with moviepy ffmpeg



我正在写一个Python脚本,在Django服务器上将视频(.MP4(转换为音频文件(.MP3(。为了实现这一点,我使用Moviepy库,但当我运行脚本时,我会收到以下错误:

Internal Server Error: /test/
Traceback (most recent call last):
File "C:UsersetshoAppDataLocalProgramsPythonPython38libsite-packagesdjangocorehandlersexception.py", line 34, in inner
response = get_response(request)
File "C:UsersetshoAppDataLocalProgramsPythonPython38libsite-packagesdjangocorehandlersbase.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:UsersetshoAppDataLocalProgramsPythonPython38libsite-packagesdjangocorehandlersbase.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:UsersetshoAppDataLocalProgramsPythonPython38libsite-packagesdjangoviewsdecoratorscsrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:shoemusicsitemainviews.py", line 29, in test
video = VideoFileClip(os.path.join(basePath + ".mp4"))
File "C:UsersetshoAppDataLocalProgramsPythonPython38libsite-packagesmoviepyvideoioVideoFileClip.py", line 88, in __init__
self.reader = FFMPEG_VideoReader(filename, pix_fmt=pix_fmt,
File "C:UsersetshoAppDataLocalProgramsPythonPython38libsite-packagesmoviepyvideoioffmpeg_reader.py", line 34, in __init__
self.fps = infos['video_fps']
KeyError: 'video_fps'
[15/Nov/2019 23:49:43] "POST /test/ HTTP/1.1" 500 80909

我几乎找不到关于这个错误或如何解决它的信息,所以任何帮助或见解都将不胜感激。

这是我的Python脚本(views.py(:

import pyodbc, json, pytube
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework import parsers
import os
from moviepy.editor import *
@csrf_exempt
def test(request):
if request.method == 'POST':
filePath = 'C:\Users\etsho\Music\'
#retrieve url from app
body_unicode = request.body.decode('utf-8')
body = json.loads(body_unicode)
videoURL = body['url']
print("before download")
#download youtube video
youtube = pytube.YouTube(videoURL)
videoTitle = youtube.title
video = youtube.streams.filter(only_audio=True).first()
freshDownload = video.download(filePath)
print("after download")
basePath, extension = os.path.splitext(freshDownload)
video = VideoFileClip(os.path.join(basePath + ".mp4"))
video.audio.write_audiofile(os.path.join(basePath + ".mp3"))
print("video converted")

return HttpResponse("")

所以@furas发现了一些东西。最终出现问题的是线路的only_audio=True部分。视频仍然下载,文件仍然是.MP4,但视频没有图片(根据only_audio(,但对我来说,它只是一个黑色视频。实际发生的情况是,视频中没有实际的帧,因此video_fps是错误名称。在删除only_audio=True输入后,下载了一个正常的视频,然后进行了应有的转换。

当视频不包含任何视觉内容时会发生错误,请确保上传的视频不是空白的。

最新更新