Hermit11 commited on
Commit
83b2f6c
·
verified ·
1 Parent(s): 6fab055

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +80 -80
main.py CHANGED
@@ -1,80 +1,80 @@
1
- from fastapi import FastAPI, HTTPException
2
- from pydantic import BaseModel
3
- import cv2
4
- import numpy as np
5
- import requests
6
- import tempfile
7
- import os
8
-
9
- app = FastAPI()
10
-
11
- class VideoURLs(BaseModel):
12
- urls: list[str]
13
-
14
- def download_video(url):
15
- try:
16
- response = requests.get(url, verify=False) # Added verify=False
17
- response.raise_for_status()
18
- with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmp_file:
19
- tmp_file.write(response.content)
20
- return tmp_file.name
21
- except requests.RequestException as e:
22
- raise Exception(f"Failed to download video from {url}: {str(e)}")
23
-
24
- def combine_videos(urls):
25
- if not urls:
26
- return None
27
-
28
- temp_files = []
29
- for url in urls:
30
- if url.strip():
31
- try:
32
- temp_files.append(download_video(url.strip()))
33
- except Exception as e:
34
- raise HTTPException(status_code=400, detail=str(e))
35
-
36
- if not temp_files:
37
- raise HTTPException(status_code=400, detail="No valid videos to combine")
38
-
39
- captures = [cv2.VideoCapture(file) for file in temp_files]
40
-
41
- fps = captures[0].get(cv2.CAP_PROP_FPS)
42
- frame_size = (int(captures[0].get(cv2.CAP_PROP_FRAME_WIDTH)),
43
- int(captures[0].get(cv2.CAP_PROP_FRAME_HEIGHT)))
44
-
45
- output_path = 'combined_video.mp4'
46
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
47
- out = cv2.VideoWriter(output_path, fourcc, fps, frame_size)
48
-
49
- for cap in captures:
50
- while True:
51
- ret, frame = cap.read()
52
- if not ret:
53
- break
54
- out.write(frame)
55
-
56
- for cap in captures:
57
- cap.release()
58
- out.release()
59
-
60
- for file in temp_files:
61
- os.remove(file)
62
-
63
- return output_path
64
-
65
- @app.post("/combine_videos")
66
- async def process_urls(video_urls: VideoURLs):
67
- try:
68
- combined_video = combine_videos(video_urls.urls)
69
- if combined_video:
70
- return {"video_path": combined_video}
71
- else:
72
- raise HTTPException(status_code=400, detail="Failed to combine videos")
73
- except HTTPException as he:
74
- raise he
75
- except Exception as e:
76
- raise HTTPException(status_code=500, detail=str(e))
77
-
78
- if __name__ == "__main__":
79
- import uvicorn
80
- uvicorn.run(app, host="0.0.0.0", port=8000)
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
+ import cv2
4
+ import numpy as np
5
+ import requests
6
+ import tempfile
7
+ import os
8
+
9
+ app = FastAPI()
10
+
11
+ class VideoURLs(BaseModel):
12
+ urls: list[str]
13
+
14
+ def download_video(url):
15
+ try:
16
+ response = requests.get(url, verify=False) # Added verify=False
17
+ response.raise_for_status()
18
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmp_file:
19
+ tmp_file.write(response.content)
20
+ return tmp_file.name
21
+ except requests.RequestException as e:
22
+ raise Exception(f"Failed to download video from {url}: {str(e)}")
23
+
24
+ def combine_videos(urls):
25
+ if not urls:
26
+ return None
27
+
28
+ temp_files = []
29
+ for url in urls:
30
+ if url.strip():
31
+ try:
32
+ temp_files.append(download_video(url.strip()))
33
+ except Exception as e:
34
+ raise HTTPException(status_code=400, detail=str(e))
35
+
36
+ if not temp_files:
37
+ raise HTTPException(status_code=400, detail="No valid videos to combine")
38
+
39
+ captures = [cv2.VideoCapture(file) for file in temp_files]
40
+
41
+ fps = captures[0].get(cv2.CAP_PROP_FPS)
42
+ frame_size = (int(captures[0].get(cv2.CAP_PROP_FRAME_WIDTH)),
43
+ int(captures[0].get(cv2.CAP_PROP_FRAME_HEIGHT)))
44
+
45
+ output_path = 'combined_video.mp4'
46
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
47
+ out = cv2.VideoWriter(output_path, fourcc, fps, frame_size)
48
+
49
+ for cap in captures:
50
+ while True:
51
+ ret, frame = cap.read()
52
+ if not ret:
53
+ break
54
+ out.write(frame)
55
+
56
+ for cap in captures:
57
+ cap.release()
58
+ out.release()
59
+
60
+ for file in temp_files:
61
+ os.remove(file)
62
+
63
+ return output_path
64
+
65
+ @app.post("/combine_videos")
66
+ async def process_urls(video_urls: VideoURLs):
67
+ try:
68
+ combined_video = combine_videos(video_urls.urls)
69
+ if combined_video:
70
+ return {"video_path": combined_video}
71
+ else:
72
+ raise HTTPException(status_code=400, detail="Failed to combine videos")
73
+ except HTTPException as he:
74
+ raise he
75
+ except Exception as e:
76
+ raise HTTPException(status_code=500, detail=str(e))
77
+
78
+ if __name__ == "__main__":
79
+ import uvicorn
80
+ uvicorn.run(app, host="0.0.0.0", port=8080) # Changed port to 8080