python - KeyError:带有moviepy ffmpeg的'video_fps'
问题描述
我正在编写一个 Python 脚本来将视频(.MP4)转换为 Django 服务器上的音频文件(.MP3)。为此,我使用了 Moviepy 库,但是当我运行脚本时,出现以下错误:
Internal Server Error: /test/
Traceback (most recent call last):
File "C:\Users\etsho\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\etsho\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\etsho\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\etsho\AppData\Local\Programs\Python\Python38\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\shoe\musicsite\main\views.py", line 29, in test
video = VideoFileClip(os.path.join(basePath + ".mp4"))
File "C:\Users\etsho\AppData\Local\Programs\Python\Python38\lib\site-packages\moviepy\video\io\VideoFileClip.py", line 88, in __init__
self.reader = FFMPEG_VideoReader(filename, pix_fmt=pix_fmt,
File "C:\Users\etsho\AppData\Local\Programs\Python\Python38\lib\site-packages\moviepy\video\io\ffmpeg_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
输入后,下载了一个普通视频,然后进行了应有的转换。
推荐阅读
- python - 如何在二维张量中获得相同值的张量索引?
- javascript - 如何将上下文方法传递给useEffect中的函数?
- javascript - Chrome:如果页面可滚动,则不会触发带有鼠标中键的辅助单击事件
- jmeter - 在 JMeter 中记录任何应用程序行为或脚本
- c++ - 带有 cmake-conan 的柯南缺少 conanbuild.conf
- mongodb - PUT 请求的 Mongoose 错误,但 POST 或 GET 没有
- python - 使用 sqlalchemy 更改在 SQL 表中动态创建的列的 VARCHAR 长度
- .net - 如何在经典的 asp 页面上从 webconfig 文件中获取邮件设置
- flutter - GestureDedector 的 onTap 事件不适用于 Sizedbox
- php - Laravel 5.7 验证 required_if null