Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
|
@@ -25,13 +25,13 @@ async def compress_video(
|
|
| 25 |
crf: int = Query(28, description="Constant Rate Factor (CRF) for compression (0-51, lower is better quality)")
|
| 26 |
):
|
| 27 |
try:
|
| 28 |
-
file_name = f"{uuid.uuid4()}.
|
| 29 |
file_path = os.path.join(VIDEO_DIR, file_name)
|
| 30 |
|
| 31 |
(
|
| 32 |
ffmpeg
|
| 33 |
.input(video_url)
|
| 34 |
-
.output(file_path,
|
| 35 |
.global_args("-threads", "0")
|
| 36 |
.overwrite_output()
|
| 37 |
.run()
|
|
@@ -52,15 +52,30 @@ async def upload_video(
|
|
| 52 |
preset: str = Query("fast", description="ultrafast, superfast, fast, medium, slow"),
|
| 53 |
crf: int = Query(28, description="Constant Rate Factor (CRF) for compression (0-51, lower is better quality)")):
|
| 54 |
try:
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
| 60 |
buffer.write(file.file.read())
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
except Exception as e:
|
| 66 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 25 |
crf: int = Query(28, description="Constant Rate Factor (CRF) for compression (0-51, lower is better quality)")
|
| 26 |
):
|
| 27 |
try:
|
| 28 |
+
file_name = f"{uuid.uuid4()}.{format}"
|
| 29 |
file_path = os.path.join(VIDEO_DIR, file_name)
|
| 30 |
|
| 31 |
(
|
| 32 |
ffmpeg
|
| 33 |
.input(video_url)
|
| 34 |
+
.output(file_path, crf=crf, vcodec=codec, preset=preset, s=resolution, f=format)
|
| 35 |
.global_args("-threads", "0")
|
| 36 |
.overwrite_output()
|
| 37 |
.run()
|
|
|
|
| 52 |
preset: str = Query("fast", description="ultrafast, superfast, fast, medium, slow"),
|
| 53 |
crf: int = Query(28, description="Constant Rate Factor (CRF) for compression (0-51, lower is better quality)")):
|
| 54 |
try:
|
| 55 |
+
original_filename = file.filename
|
| 56 |
+
file_ext = original_filename.split(".")[-1]
|
| 57 |
+
temp_filename = f"{uuid.uuid4()}.{file_ext}"
|
| 58 |
+
temp_filepath = os.path.join(VIDEO_DIR, temp_filename)
|
| 59 |
+
|
| 60 |
+
with open(temp_filepath, "wb") as buffer:
|
| 61 |
buffer.write(file.file.read())
|
| 62 |
+
|
| 63 |
+
compressed_filename = f"{uuid.uuid4()}.{format}"
|
| 64 |
+
compressed_filepath = os.path.join(VIDEO_DIR, compressed_filename)
|
| 65 |
+
|
| 66 |
+
(
|
| 67 |
+
ffmpeg
|
| 68 |
+
.input(temp_filepath)
|
| 69 |
+
.output(compressed_filepath, crf=crf, vcodec=codec, preset=preset, s=resolution, f=format)
|
| 70 |
+
.global_args("-threads", "0")
|
| 71 |
+
.overwrite_output()
|
| 72 |
+
.run()
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
os.remove(temp_filepath) # Clean up the original uploaded file
|
| 76 |
+
|
| 77 |
+
file_url = f"/videos/{compressed_filename}"
|
| 78 |
+
return JSONResponse(content={"compressed_video_url": "https://akane710-imagecompress.hf.space" + file_url})
|
| 79 |
|
| 80 |
except Exception as e:
|
| 81 |
raise HTTPException(status_code=500, detail=str(e))
|