Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
|
@@ -7,46 +7,49 @@ import uuid
|
|
| 7 |
import cloudinary
|
| 8 |
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
|
| 23 |
-
resolution: str = Query("
|
| 24 |
-
format: str = Query("
|
|
|
|
|
|
|
| 25 |
crf: int = Query(28, description="Constant Rate Factor (CRF) for compression (0-51, lower is better quality)")
|
| 26 |
):
|
| 27 |
try:
|
| 28 |
-
|
| 29 |
-
file_name = f"{uuid.uuid4()}.{format}"
|
| 30 |
file_path = os.path.join(VIDEO_DIR, file_name)
|
| 31 |
|
| 32 |
-
|
| 33 |
(
|
| 34 |
ffmpeg
|
| 35 |
.input(video_url)
|
| 36 |
-
.output(file_path, vf=f"scale={resolution}", crf=crf, vcodec=
|
|
|
|
| 37 |
.overwrite_output()
|
| 38 |
.run()
|
|
|
|
| 39 |
)
|
| 40 |
|
| 41 |
-
|
| 42 |
file_url = f"/videos/{file_name}"
|
| 43 |
|
| 44 |
-
return JSONResponse(content={"compressed_video_url": file_url})
|
| 45 |
|
| 46 |
except Exception as e:
|
| 47 |
return JSONResponse(content={"error": str(e)}, status_code=500)
|
| 48 |
|
| 49 |
-
|
| 50 |
cloudinary.config(
|
| 51 |
cloud_name=os.getenv("cloudname"),
|
| 52 |
api_key=os.getenv("key"),
|
|
@@ -54,7 +57,7 @@ cloudinary.config(
|
|
| 54 |
secure=True
|
| 55 |
)
|
| 56 |
|
| 57 |
-
|
| 58 |
@app.get("/compress_image")
|
| 59 |
async def compress_image(image_url: str = Query(..., description="URL of the image to compress")):
|
| 60 |
try:
|
|
@@ -71,4 +74,4 @@ async def compress_image(image_url: str = Query(..., description="URL of the ima
|
|
| 71 |
|
| 72 |
@app.get("/")
|
| 73 |
def root():
|
| 74 |
-
return "بتعمل شنو هنا يا زول"
|
|
|
|
| 7 |
import cloudinary
|
| 8 |
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"),
|
| 23 |
+
resolution: str = Query("1280x720", description="Resolution (e.g., 1280x720, 640x360)"),
|
| 24 |
+
format: str = Query("matroska", description="Output video format (e.g., mp4, mkv)"),
|
| 25 |
+
codec: str = Query("libx264", description="264 faster but bigger size,265 slower but smaller size"),
|
| 26 |
+
preset: str = Query("fast", description="ultrafast, superfast, fast, medium, slow"),
|
| 27 |
crf: int = Query(28, description="Constant Rate Factor (CRF) for compression (0-51, lower is better quality)")
|
| 28 |
):
|
| 29 |
try:
|
| 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"),
|
| 55 |
api_key=os.getenv("key"),
|
|
|
|
| 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:
|
|
|
|
| 74 |
|
| 75 |
@app.get("/")
|
| 76 |
def root():
|
| 77 |
+
return "بتعمل شنو هنا يا زول"
|