Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from fastapi import FastAPI, Query
|
| 2 |
from fastapi.responses import JSONResponse
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
import os
|
|
@@ -9,14 +9,12 @@ import cloudinary.uploader
|
|
| 9 |
from cloudinary.utils import cloudinary_url
|
| 10 |
|
| 11 |
VIDEO_DIR = "compressed_videos"
|
| 12 |
-
os.makedirs(VIDEO_DIR, exist_ok=True)
|
| 13 |
|
| 14 |
app = FastAPI()
|
| 15 |
|
| 16 |
-
|
| 17 |
app.mount("/videos", StaticFiles(directory=VIDEO_DIR), name="videos")
|
| 18 |
|
| 19 |
-
|
| 20 |
@app.get("/compress_video")
|
| 21 |
async def compress_video(
|
| 22 |
video_url: str = Query(..., description="URL of the video"),
|
|
@@ -30,25 +28,36 @@ async def compress_video(
|
|
| 30 |
file_name = f"{uuid.uuid4()}.mkv"
|
| 31 |
file_path = os.path.join(VIDEO_DIR, file_name)
|
| 32 |
|
| 33 |
-
|
| 34 |
(
|
| 35 |
ffmpeg
|
| 36 |
.input(video_url)
|
| 37 |
.output(file_path, vf=f"scale={resolution}", crf=crf, vcodec=codec, preset=preset, f=format)
|
| 38 |
-
.global_args("-threads", "0")
|
| 39 |
.overwrite_output()
|
| 40 |
.run()
|
| 41 |
-
|
| 42 |
)
|
| 43 |
|
| 44 |
-
|
| 45 |
file_url = f"/videos/{file_name}"
|
| 46 |
-
|
| 47 |
-
return JSONResponse(content={"compressed_video_url": "https://akane710-imagecompress.hf.space"+file_url})
|
| 48 |
|
| 49 |
except Exception as e:
|
| 50 |
return JSONResponse(content={"error": str(e)}, status_code=500)
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
cloudinary.config(
|
| 54 |
cloud_name=os.getenv("cloudname"),
|
|
@@ -57,7 +66,6 @@ cloudinary.config(
|
|
| 57 |
secure=True
|
| 58 |
)
|
| 59 |
|
| 60 |
-
|
| 61 |
@app.get("/compress_image")
|
| 62 |
async def compress_image(image_url: str = Query(..., description="URL of the image to compress")):
|
| 63 |
try:
|
|
@@ -71,7 +79,7 @@ async def compress_image(image_url: str = Query(..., description="URL of the ima
|
|
| 71 |
|
| 72 |
except Exception as e:
|
| 73 |
return JSONResponse(content={"error": str(e)}, status_code=500)
|
| 74 |
-
|
| 75 |
@app.get("/")
|
| 76 |
def root():
|
| 77 |
-
return "بتعمل شنو هنا يا زول"
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Query, File, UploadFile, HTTPException
|
| 2 |
from fastapi.responses import JSONResponse
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
import os
|
|
|
|
| 9 |
from cloudinary.utils import cloudinary_url
|
| 10 |
|
| 11 |
VIDEO_DIR = "compressed_videos"
|
| 12 |
+
os.makedirs(VIDEO_DIR, exist_ok=True)
|
| 13 |
|
| 14 |
app = FastAPI()
|
| 15 |
|
|
|
|
| 16 |
app.mount("/videos", StaticFiles(directory=VIDEO_DIR), name="videos")
|
| 17 |
|
|
|
|
| 18 |
@app.get("/compress_video")
|
| 19 |
async def compress_video(
|
| 20 |
video_url: str = Query(..., description="URL of the video"),
|
|
|
|
| 28 |
file_name = f"{uuid.uuid4()}.mkv"
|
| 29 |
file_path = os.path.join(VIDEO_DIR, file_name)
|
| 30 |
|
|
|
|
| 31 |
(
|
| 32 |
ffmpeg
|
| 33 |
.input(video_url)
|
| 34 |
.output(file_path, vf=f"scale={resolution}", crf=crf, vcodec=codec, preset=preset, f=format)
|
| 35 |
+
.global_args("-threads", "0")
|
| 36 |
.overwrite_output()
|
| 37 |
.run()
|
|
|
|
| 38 |
)
|
| 39 |
|
|
|
|
| 40 |
file_url = f"/videos/{file_name}"
|
| 41 |
+
return JSONResponse(content={"compressed_video_url": "https://akane710-imagecompress.hf.space" + file_url})
|
|
|
|
| 42 |
|
| 43 |
except Exception as e:
|
| 44 |
return JSONResponse(content={"error": str(e)}, status_code=500)
|
| 45 |
|
| 46 |
+
@app.post("/upload_video")
|
| 47 |
+
async def upload_video(file: UploadFile = File(...)):
|
| 48 |
+
try:
|
| 49 |
+
file_ext = file.filename.split(".")[-1]
|
| 50 |
+
new_filename = f"{uuid.uuid4()}.{file_ext}"
|
| 51 |
+
file_path = os.path.join(VIDEO_DIR, new_filename)
|
| 52 |
+
|
| 53 |
+
with open(file_path, "wb") as buffer:
|
| 54 |
+
buffer.write(file.file.read())
|
| 55 |
+
|
| 56 |
+
file_url = f"/videos/{new_filename}"
|
| 57 |
+
return JSONResponse(content={"uploaded_video_url": "https://akane710-imagecompress.hf.space" + file_url})
|
| 58 |
+
|
| 59 |
+
except Exception as e:
|
| 60 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 61 |
|
| 62 |
cloudinary.config(
|
| 63 |
cloud_name=os.getenv("cloudname"),
|
|
|
|
| 66 |
secure=True
|
| 67 |
)
|
| 68 |
|
|
|
|
| 69 |
@app.get("/compress_image")
|
| 70 |
async def compress_image(image_url: str = Query(..., description="URL of the image to compress")):
|
| 71 |
try:
|
|
|
|
| 79 |
|
| 80 |
except Exception as e:
|
| 81 |
return JSONResponse(content={"error": str(e)}, status_code=500)
|
| 82 |
+
|
| 83 |
@app.get("/")
|
| 84 |
def root():
|
| 85 |
+
return "بتعمل شنو هنا يا زول"
|