Akane710 commited on
Commit
01b162f
·
verified ·
1 Parent(s): 42951be

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +26 -37
main.py CHANGED
@@ -4,6 +4,8 @@ import cloudinary
4
  import cloudinary.uploader
5
  from cloudinary.utils import cloudinary_url
6
  import os
 
 
7
 
8
  app = FastAPI()
9
 
@@ -15,55 +17,42 @@ cloudinary.config(
15
  secure=True
16
  )
17
 
18
- # Image compression endpoint
19
- @app.get("/compress_image")
20
- async def compress_image(image_url: str = Query(..., description="URL of the image to compress")):
21
- try:
22
- upload_result = cloudinary.uploader.upload(image_url)
23
- compressed_url, options = cloudinary_url(
24
- upload_result["public_id"],
25
- fetch_format="auto",
26
- quality="auto:low"
27
- )
28
- return JSONResponse(content={"compressed_image_url": compressed_url})
29
-
30
- except Exception as e:
31
- return JSONResponse(content={"error": str(e)}, status_code=500)
32
-
33
- # Video compression and format conversion endpoint
34
  @app.get("/compress_video")
35
  async def compress_video(
36
  video_url: str = Query(..., description="URL of the video to compress and convert"),
37
- quality: str = Query("auto:low", description="Compression quality (e.g., auto:low, auto:good)"),
38
- resolution: str = Query(None, description="Resolution (e.g., 720p, 480p)"),
39
  format: str = Query("mkv", description="Output video format (e.g., mp4, mkv)"),
40
- codec: str = Query("h265", description="Video codec (e.g., h264, h265)")
41
  ):
42
  try:
43
- # Upload the video to Cloudinary
44
- upload_result = cloudinary.uploader.upload_large(
45
- video_url, resource_type="video"
46
- )
47
 
48
- # Build the transformation options
49
- transformation = {
50
- "fetch_format": format, # Set the output format
51
- "quality": quality, # Set compression quality
52
- "video_codec": codec # Specify the video codec
53
- }
 
 
54
 
55
- # Add resolution only if specified
56
- if resolution:
57
- transformation["width"] = resolution.split("p")[0]
58
- transformation["crop"] = "scale" # Ensures scaling to specified resolution
59
 
60
- # Generate the compressed and converted video URL
61
  compressed_video_url, options = cloudinary_url(
62
  upload_result["public_id"],
63
- resource_type="video",
64
- **transformation
65
  )
66
 
 
 
 
67
  return JSONResponse(content={"compressed_video_url": compressed_video_url})
68
 
69
  except Exception as e:
@@ -72,4 +61,4 @@ async def compress_video(
72
  # Root endpoint
73
  @app.get("/")
74
  def root():
75
- return "بتعمل شنو هنا يا زول"
 
4
  import cloudinary.uploader
5
  from cloudinary.utils import cloudinary_url
6
  import os
7
+ import ffmpeg
8
+ import tempfile
9
 
10
  app = FastAPI()
11
 
 
17
  secure=True
18
  )
19
 
20
+ # Video compression and format conversion endpoint using FFmpeg
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  @app.get("/compress_video")
22
  async def compress_video(
23
  video_url: str = Query(..., description="URL of the video to compress and convert"),
24
+ resolution: str = Query("265x", description="Resolution (e.g., 720x480, 265x265)"),
 
25
  format: str = Query("mkv", description="Output video format (e.g., mp4, mkv)"),
26
+ crf: int = Query(28, description="Constant Rate Factor (CRF) for compression (0-51, lower is better quality)")
27
  ):
28
  try:
29
+ # Create a temporary file to store the downloaded video
30
+ with tempfile.NamedTemporaryFile(suffix=f".{format}", delete=False) as temp_file:
31
+ temp_file_path = temp_file.name
 
32
 
33
+ # Download the video from the URL using FFmpeg
34
+ (
35
+ ffmpeg
36
+ .input(video_url)
37
+ .output(temp_file_path, vf=f"scale={resolution}", crf=crf, vcodec="libx265", format=format)
38
+ .overwrite_output()
39
+ .run()
40
+ )
41
 
42
+ # Upload the compressed video to Cloudinary
43
+ upload_result = cloudinary.uploader.upload_large(
44
+ temp_file_path, resource_type="video"
45
+ )
46
 
47
+ # Generate the URL for the compressed video
48
  compressed_video_url, options = cloudinary_url(
49
  upload_result["public_id"],
50
+ resource_type="video"
 
51
  )
52
 
53
+ # Clean up the temporary file
54
+ os.remove(temp_file_path)
55
+
56
  return JSONResponse(content={"compressed_video_url": compressed_video_url})
57
 
58
  except Exception as e:
 
61
  # Root endpoint
62
  @app.get("/")
63
  def root():
64
+ return "بتعمل شنو هنا يا زول"