我想为从前端上传并发回的视频用户生成缩略图(例如,提取帧为1s)。
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