【发布时间】:2021-02-17 16:57:21
【问题描述】:
我想为用户上传并从前端发回的视频生成缩略图(例如,在 1 秒处提取帧)。
user_upload = request.files['file']
这样做的正确方法是什么?是否可以不先将文件保存在本地并先读取框架来提取帧?
subprocess.call(['ffmpeg', '-i', user_upload, '-ss',
'00:00:00.000', '-vframes', '1', output_dir])
或
vidcap = cv2.VideoCapture(user_upload)
success, image = vidcap.read()
count = 0
while success and count < 1:
cv2.imwrite("frame%d.jpg" % count, image) # save frame as JPEG file
success, image = vidcap.read()
print('Read a new frame: ', success)
count += 1
FFmpeg 和 cv2 都需要读取 Path,而不是 FileStorage。
File "/Users/glwang/bikan/yc2020/eb-flask/siyu/utils.py", line 8, in capture_thumnail
'00:00:00.000', '-vframes', '1', output_dir])
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 267, in call
with Popen(*popenargs, **kwargs) as p:
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 1275, in _execute_child
restore_signals, start_new_session, preexec_fn)
TypeError: expected str, bytes or os.PathLike object, not FileStorage
另外,当我尝试先在本地保存文件并使用FFmpeg提取帧时,
user_upload.save('path/to/somewhere')
它显示
File "/siyu/utils.py", line 7, in capture_thumnail
video_input.save(video_input.filename)
File "/Users/lib/python3.6/site-packages/werkzeug/datastructures.py", line 3070, in save
copyfileobj(self.stream, dst, buffer_size)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/shutil.py", line 79, in copyfileobj
buf = fsrc.read(length)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tempfile.py", line 738, in read
return self._file.read(*args)
ValueError: read of closed file
【问题讨论】:
标签: python opencv flask video ffmpeg