【发布时间】:2023-02-01 15:22:57
【问题描述】:
我正在尝试从上传单个视频切换到上传和处理多个视频;但是,我的代码似乎只保存/读取第一个视频。我似乎无法弄清楚为什么,因为当我打印上传的文件列表时,它确实包括所有被忽略的后续视频。
后端: 快速API
这是代码在后端的样子:
@app.post("/upload")
def upload_video(fileList: Optional[List[UploadFile]] = File(None)):
videofiles = []
for file in fileList:
print("Uploading:", file.filename)
print(".................................")
extension = file.filename.split(".")[-1] in ("mp4", "avi")
if not extension:
return "Video must be in mp4 or avi format!"
try:
try:
contents = file.file.read()
with temp as f:
print("what's happening: ")
f.write(contents)
videofiles.append(cv2.VideoCapture(temp.name))
print('list of videos uploaded :')
for vidcap in videofiles:
print("video:", vidcap)
# Check if camera opened successfully
if (vidcap.isOpened() == False):
print("Error opening video file")
# get height, width and frame count of the video
width, height = (
int(vidcap.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(vidcap.get(cv2.CAP_PROP_FRAME_HEIGHT))
)
print(f"width: {width}")
print(f"height: {height}")
# count the number of frames
frames = vidcap.get(cv2.CAP_PROP_FRAME_COUNT)
fps = vidcap.get(cv2.CAP_PROP_FPS)
# calculate duration of the video
seconds = round(frames / fps)
video_time = datetime.timedelta(seconds=seconds)
print(f"duration in seconds: {seconds}")
print(f"video time: {video_time}")
except Exception:
return {"message": "There was an error uploading the file"}
finally:
file.file.close()
except Exception:
return {"message": "There was an error processing the file"}
finally:
os.remove(temp.name)
count = 0;
for vid in videofiles:
count += 1
print("number of video capture objects uploaded:", count)
return {"uploadStatus": "Complete", "filenames": [f.filename for f in fileList]}
我觉得这与视频捕获有关,但我认为当我从循环播放单个视频捕获的视频列表切换到每个上传视频的视频捕获列表时,这个问题得到了解决。但正如您从屏幕截图中看到的那样,视频捕获列表只有第一个视频的一个对象。
知道是什么原因造成的吗?
编辑:我将这个question 用于单个视频上传,并基于相同的逻辑构建以遍历视频列表,但这也不起作用。
【问题讨论】:
-
@Chris 嗨,不,不幸的是。我使用相同的逻辑遍历我正在传递的视频列表,但它似乎不是那样工作的。我将 try-except 放在 for 循环中,但视频捕获列表仍然只包含第一个视频的捕获。顺便说一句,谢谢你指出来! :)
标签: reactjs opencv fastapi video-capture multiple-file-upload