gpt4 book ai didi

python - OSError : MoviePy error: the file guitar. mp4 找不到

转载 作者:行者123 更新时间:2023-12-04 22:54:27 24 4
gpt4 key购买 nike

我正在使用 react 和 flask/python 开发视频到音频转换器。
我收到了 500 条错误:

raise IOError(("MoviePy error: the file %s could not be found!\n"
OSError: MoviePy error: the file guitar.mp4 could not be found!
Please check that you entered the correct path.
编辑:如评论中所述,moviepy VideoFileClip 正在寻找路径。根据建议,我现在正尝试将传入的视频文件写入应用程序后端的临时目录。更新的堆栈跟踪显示文件路径打印,但是当呈现给 VideoFileClip 时,它仍然不满意。
以下片段是视频文件上传的 onSubmit:
const onSubmit = async (e) => {
e.preventDefault()
const data = new FormData()
console.log('hopefully the mp4', videoData)
data.append('mp3', videoData)
console.log('hopefully a form object with mp4', data)
const response = await fetch('/api/convert', {
method: "POST",
body: data
})
if (response.ok) {
const converted = await response.json()
setMp3(converted)
console.log(mp3)
} else {
window.alert("something went wrong :(");
}
}
Here is a link to an image depicting the console output of my file upload
来自 初始化 .py
app = Flask(__name__)

app.config.from_object(Config)
app.register_blueprint(convert, url_prefix='/api/convert')

CORS(app)

来自converter.py
import os
from flask import Blueprint, jsonify, request
import imageio
from moviepy.editor import *


convert = Blueprint('convert', __name__)

@convert.route('', methods=['POST'])
def convert_mp4():
if request.files['mp3'].filename:
os.getcwd()
filename = request.files['mp3'].filename
print('hey its a file again', filename)
safe_filename = secure_filename(filename)
video_file = os.path.join("/temp/", safe_filename)
print('hey its the file path', video_file)
video_clip = VideoFileClip(video_file)
print('hey its the VideoFileClip', video_clip)
audio_clip = video_clip.audio
audio_clip.write_audiofile(os.path.join("/temp/", f"{safe_filename}-converted.mp3"))

video_clip.close()
audio_clip.close()

return jsonify(send_from_directory(os.path.join("/temp/", f"{safe_filename}-converted.mp3")))
else:
return {'error': 'something went wrong :('}


在下面的堆栈跟踪中,您可以看到打印视频名称的文件,我对为什么这可能不起作用的唯一其他想法是因为它在发布请求中丢失了,但事实上它是在我的 if file: 之后打印的检查让我很困惑。
hey its a file again guitar.mp4
hey its the file path /temp/guitar.mp4
127.0.0.1 - - [22/Apr/2021 12:12:15] "POST /api/convert HTTP/1.1" 500 -
Traceback (most recent call last):
File "/home/jasondunn/projects/audioconverter/.venv/lib/python3.8/site-packages/flask/app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "/home/jasondunn/projects/audioconverter/.venv/lib/python3.8/site-packages/flask/app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "/home/jasondunn/projects/audioconverter/.venv/lib/python3.8/site-packages/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/home/jasondunn/projects/audioconverter/.venv/lib/python3.8/site-packages/flask/app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/jasondunn/projects/audioconverter/.venv/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/home/jasondunn/projects/audioconverter/.venv/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/home/jasondunn/projects/audioconverter/.venv/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/jasondunn/projects/audioconverter/.venv/lib/python3.8/site-packages/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/home/jasondunn/projects/audioconverter/.venv/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/jasondunn/projects/audioconverter/.venv/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/home/jasondunn/projects/audioconverter/.venv/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/home/jasondunn/projects/audioconverter/.venv/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/jasondunn/projects/audioconverter/back/api/converter.py", line 20, in convert_mp4
video_clip = VideoFileClip(video_file)
File "/home/jasondunn/projects/audioconverter/.venv/lib/python3.8/site-packages/moviepy/video/io/VideoFileClip.py", line 88, in __init__
self.reader = FFMPEG_VideoReader(filename, pix_fmt=pix_fmt,
File "/home/jasondunn/projects/audioconverter/.venv/lib/python3.8/site-packages/moviepy/video/io/ffmpeg_reader.py", line 35, in __init__
infos = ffmpeg_parse_infos(filename, print_infos, check_duration,
File "/home/jasondunn/projects/audioconverter/.venv/lib/python3.8/site-packages/moviepy/video/io/ffmpeg_reader.py", line 270, in ffmpeg_parse_infos
raise IOError(("MoviePy error: the file %s could not be found!\n"
OSError: MoviePy error: the file /temp/guitar.mp4 could not be found!
Please check that you entered the correct path.
提前感谢您查看/ future 的建议。 Stack Overflow 上的第一个官方帖子 :)

最佳答案

貌似python找不到guitar.mp4 :(
我似乎需要在处理之前将文件内容保存在磁盘上。看着docs对于 MoviePy您需要将文件名或绝对路径传入 VideoFileClip构造函数,该对象将打开磁盘上的文件并处理实例化后的处理。
在请求中保存文件应该很简单。下面的代码应该能够处理这个file.save(os.path.join("/path/to/some/dir", filename))现在你可以给VideoFileClip文件的正确 URI。video_clip = VideoFileClip(os.path.join("/path/to/some/dir", filename))这就是我要写的 convert_mp4虽然它没有经过测试。

@convert.route('', methods=["POST"])
def convert_mp4():
if request.files.get("mp3"):

# clean the filename
safe_filename = secure_filename(request.files["mp3"].filename)

# save file to some directory on disk
request.files["mp3"].save(os.path.join("/path/to/some/dir", safe_filename))
video_clip = VideoFileClip(os.path.join("/path/to/some/dir", safe_filename))
audio_clip = video_clip.audio # convert to audio

# you may need to change the name or save to a different directory
audio_clip.write_audiofile(os.path.join("/path/to/some/dir", f"{safe_filename}.mp3"))

# close resources, maybe use a context manager for better readability
video_clip.close()
audio_clip.close()

# responds with data from a specific file
send_from_directory("/path/to/some/dir", f"{safe_filename}.mp3")
else:
return jsonify(error="File 'mp3' does not exist!")
每当您通过 flask 将数据保存到磁盘时,您都应该使用 secure_filename它内置于 werkzeug 项目的 flask 中。此函数将清除输入名称,因此攻击者无法创建恶意文件名。
我建议甚至更进一步,也许创建 2 个端点。一个用于提交数据以进行处理,第二个用于检索它。这使您的请求保持快速并允许 flask 同时处理其他请求(但是您将需要一些后台进程来处理转换)。
2021 年 4 月 30 日更新
我知道我们在 Discord 上解决了这个问题,但我想记录解决方案。
您的 MP4 数据未使用 save 保存到磁盘。方法(见 this)。您可以检查实现此功能的上述代码。
一旦完成,我们现在知道这些数据在哪里,并且可以实例化 VideoFileClip使用已知文件路径的对象,这将允许转换发生,然后您需要将转换后的 MP3 文件保存在文件系统中的某个位置。
将 MP3 保存到磁盘后,您可以使用 flask send_from_directory函数在您的响应中发回数据。此响应不能包含 JSON 内容,因为内容类型已设置为 audio/mpeg根据 MP3 文件的内容。

关于python - OSError : MoviePy error: the file guitar. mp4 找不到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67205230/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com