Spaces:
Paused
Paused
| from fastapi import FastAPI, Query, File, UploadFile, HTTPException | |
| from fastapi.responses import JSONResponse | |
| from fastapi.staticfiles import StaticFiles | |
| import os | |
| import ffmpeg | |
| import uuid | |
| import cloudinary | |
| import cloudinary.uploader | |
| from cloudinary.utils import cloudinary_url | |
| VIDEO_DIR = "compressed_videos" | |
| os.makedirs(VIDEO_DIR, exist_ok=True) | |
| app = FastAPI() | |
| app.mount("/videos", StaticFiles(directory=VIDEO_DIR), name="videos") | |
| async def compress_video( | |
| video_url: str = Query(..., description="URL of the video"), | |
| resolution: str = Query("1280x720", description="Resolution (e.g., 1280x720, 640x360)"), | |
| format: str = Query("matroska", description="Output video format (e.g., mp4, mkv)"), | |
| codec: str = Query("libx264", description="264 faster but bigger size,265 slower but smaller size"), | |
| preset: str = Query("fast", description="ultrafast, superfast, fast, medium, slow"), | |
| crf: int = Query(28, description="Constant Rate Factor (CRF) for compression (0-51, lower is better quality)") | |
| ): | |
| try: | |
| file_name = f"{uuid.uuid4()}.{format}" | |
| file_path = os.path.join(VIDEO_DIR, file_name) | |
| ( | |
| ffmpeg | |
| .input(video_url) | |
| .output(file_path, crf=crf, vcodec=codec, preset=preset, s=resolution, f=format) | |
| .global_args("-threads", "0") | |
| .overwrite_output() | |
| .run() | |
| ) | |
| file_url = f"/videos/{file_name}" | |
| return JSONResponse(content={"compressed_video_url": "https://akane710-imagecompress.hf.space" + file_url}) | |
| except Exception as e: | |
| return JSONResponse(content={"error": str(e)}, status_code=500) | |
| async def upload_video( | |
| file: UploadFile = File(...), | |
| resolution: str = Query("1280x720", description="Resolution (e.g., 1280x720, 640x360)"), | |
| format: str = Query("matroska", description="Output video format (e.g., mp4, mkv)"), | |
| codec: str = Query("libx264", description="264 faster but bigger size,265 slower but smaller size"), | |
| preset: str = Query("fast", description="ultrafast, superfast, fast, medium, slow"), | |
| crf: int = Query(28, description="Constant Rate Factor (CRF) for compression (0-51, lower is better quality)")): | |
| try: | |
| original_filename = file.filename | |
| file_ext = original_filename.split(".")[-1] | |
| temp_filename = f"{uuid.uuid4()}.{file_ext}" | |
| temp_filepath = os.path.join(VIDEO_DIR, temp_filename) | |
| with open(temp_filepath, "wb") as buffer: | |
| buffer.write(file.file.read()) | |
| compressed_filename = f"{uuid.uuid4()}.{format}" | |
| compressed_filepath = os.path.join(VIDEO_DIR, compressed_filename) | |
| ( | |
| ffmpeg | |
| .input(temp_filepath) | |
| .output(compressed_filepath, crf=crf, vcodec=codec, preset=preset, s=resolution, f=format) | |
| .global_args("-threads", "0") | |
| .overwrite_output() | |
| .run() | |
| ) | |
| os.remove(temp_filepath) # Clean up the original uploaded file | |
| file_url = f"/videos/{compressed_filename}" | |
| return JSONResponse(content={"compressed_video_url": "https://akane710-imagecompress.hf.space" + file_url}) | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| cloudinary.config( | |
| cloud_name=os.getenv("cloudname"), | |
| api_key=os.getenv("key"), | |
| api_secret=os.getenv("secret"), | |
| secure=True | |
| ) | |
| async def compress_image(image_url: str = Query(..., description="URL of the image to compress")): | |
| try: | |
| upload_result = cloudinary.uploader.upload(image_url) | |
| compressed_url, options = cloudinary_url( | |
| upload_result["public_id"], | |
| fetch_format="auto", | |
| quality="auto:low" | |
| ) | |
| return JSONResponse(content={"compressed_image_url": compressed_url}) | |
| except Exception as e: | |
| return JSONResponse(content={"error": str(e)}, status_code=500) | |
| def root(): | |
| return "بتعمل شنو هنا يا زول" | |