Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -16,8 +16,11 @@ async def process_video(file: UploadFile = File(...)):
|
|
16 |
input_filename = f"/tmp/input_{uuid.uuid4()}.mp4"
|
17 |
output_filename = f"/tmp/output_{uuid.uuid4()}.mp4"
|
18 |
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
21 |
|
22 |
cap = cv2.VideoCapture(input_filename)
|
23 |
success, frame = cap.read()
|
@@ -37,12 +40,20 @@ async def process_video(file: UploadFile = File(...)):
|
|
37 |
'-c:a', 'copy',
|
38 |
output_filename
|
39 |
]
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
41 |
os.remove(input_filename)
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
46 |
else:
|
47 |
os.remove(input_filename)
|
48 |
return {"error": "Não foi possível detectar o produto para realizar o crop."}
|
|
|
16 |
input_filename = f"/tmp/input_{uuid.uuid4()}.mp4"
|
17 |
output_filename = f"/tmp/output_{uuid.uuid4()}.mp4"
|
18 |
|
19 |
+
try:
|
20 |
+
with open(input_filename, "wb") as f:
|
21 |
+
f.write(await file.read())
|
22 |
+
except Exception as e:
|
23 |
+
return {"error": f"Erro ao salvar o vídeo de entrada: {str(e)}"}
|
24 |
|
25 |
cap = cv2.VideoCapture(input_filename)
|
26 |
success, frame = cap.read()
|
|
|
40 |
'-c:a', 'copy',
|
41 |
output_filename
|
42 |
]
|
43 |
+
try:
|
44 |
+
subprocess.run(crop_cmd, check=True)
|
45 |
+
except subprocess.CalledProcessError as e:
|
46 |
+
os.remove(input_filename)
|
47 |
+
return {"error": f"Erro ao processar o vídeo com FFmpeg: {str(e)}"}
|
48 |
+
|
49 |
os.remove(input_filename)
|
50 |
|
51 |
+
if os.path.exists(output_filename):
|
52 |
+
response = FileResponse(output_filename, media_type="video/mp4", filename="cropped_video.mp4")
|
53 |
+
response.headers["X-Delete-File"] = output_filename
|
54 |
+
return response
|
55 |
+
else:
|
56 |
+
return {"error": "O arquivo de saída não foi gerado."}
|
57 |
else:
|
58 |
os.remove(input_filename)
|
59 |
return {"error": "Não foi possível detectar o produto para realizar o crop."}
|