Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -5,62 +5,83 @@ import numpy as np
|
|
| 5 |
import requests
|
| 6 |
import tempfile
|
| 7 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
class VideoURLs(BaseModel):
|
| 12 |
-
urls:
|
|
|
|
|
|
|
| 13 |
|
| 14 |
def download_video(url):
|
| 15 |
try:
|
| 16 |
-
response = requests.get(url, 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 |
-
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
@app.post("/combine_videos")
|
| 66 |
async def process_urls(video_urls: VideoURLs):
|
|
@@ -71,10 +92,12 @@ async def process_urls(video_urls: VideoURLs):
|
|
| 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)
|
|
|
|
| 5 |
import requests
|
| 6 |
import tempfile
|
| 7 |
import os
|
| 8 |
+
from typing import List
|
| 9 |
+
import logging
|
| 10 |
+
import urllib3
|
| 11 |
+
|
| 12 |
+
# Suppress only the single InsecureRequestWarning
|
| 13 |
+
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
| 14 |
|
| 15 |
app = FastAPI()
|
| 16 |
|
| 17 |
+
# Set up logging
|
| 18 |
+
logging.basicConfig(level=logging.INFO)
|
| 19 |
+
logger = logging.getLogger(__name__)
|
| 20 |
+
|
| 21 |
class VideoURLs(BaseModel):
|
| 22 |
+
urls: List[str]
|
| 23 |
+
|
| 24 |
+
MAX_VIDEOS = 10
|
| 25 |
|
| 26 |
def download_video(url):
|
| 27 |
try:
|
| 28 |
+
response = requests.get(url, verify=False, timeout=30)
|
| 29 |
response.raise_for_status()
|
| 30 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmp_file:
|
| 31 |
tmp_file.write(response.content)
|
| 32 |
return tmp_file.name
|
| 33 |
except requests.RequestException as e:
|
| 34 |
+
logger.error(f"Failed to download video from {url}: {str(e)}")
|
| 35 |
raise Exception(f"Failed to download video from {url}: {str(e)}")
|
| 36 |
|
| 37 |
def combine_videos(urls):
|
| 38 |
if not urls:
|
| 39 |
return None
|
| 40 |
|
| 41 |
+
if len(urls) > MAX_VIDEOS:
|
| 42 |
+
raise HTTPException(status_code=400, detail=f"Maximum of {MAX_VIDEOS} videos allowed")
|
| 43 |
+
|
| 44 |
temp_files = []
|
| 45 |
for url in urls:
|
| 46 |
if url.strip():
|
| 47 |
try:
|
| 48 |
temp_files.append(download_video(url.strip()))
|
| 49 |
except Exception as e:
|
| 50 |
+
logger.error(f"Error downloading video: {str(e)}")
|
| 51 |
raise HTTPException(status_code=400, detail=str(e))
|
| 52 |
|
| 53 |
if not temp_files:
|
| 54 |
raise HTTPException(status_code=400, detail="No valid videos to combine")
|
| 55 |
|
| 56 |
+
try:
|
| 57 |
+
captures = [cv2.VideoCapture(file) for file in temp_files]
|
| 58 |
|
| 59 |
+
fps = captures[0].get(cv2.CAP_PROP_FPS)
|
| 60 |
+
frame_size = (int(captures[0].get(cv2.CAP_PROP_FRAME_WIDTH)),
|
| 61 |
+
int(captures[0].get(cv2.CAP_PROP_FRAME_HEIGHT)))
|
| 62 |
|
| 63 |
+
output_path = 'combined_video.mp4'
|
| 64 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 65 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, frame_size)
|
| 66 |
|
| 67 |
+
for cap in captures:
|
| 68 |
+
while True:
|
| 69 |
+
ret, frame = cap.read()
|
| 70 |
+
if not ret:
|
| 71 |
+
break
|
| 72 |
+
out.write(frame)
|
| 73 |
|
| 74 |
+
for cap in captures:
|
| 75 |
+
cap.release()
|
| 76 |
+
out.release()
|
| 77 |
|
| 78 |
+
for file in temp_files:
|
| 79 |
+
os.remove(file)
|
| 80 |
|
| 81 |
+
return output_path
|
| 82 |
+
except Exception as e:
|
| 83 |
+
logger.error(f"Error combining videos: {str(e)}")
|
| 84 |
+
raise HTTPException(status_code=500, detail=f"Error combining videos: {str(e)}")
|
| 85 |
|
| 86 |
@app.post("/combine_videos")
|
| 87 |
async def process_urls(video_urls: VideoURLs):
|
|
|
|
| 92 |
else:
|
| 93 |
raise HTTPException(status_code=400, detail="Failed to combine videos")
|
| 94 |
except HTTPException as he:
|
| 95 |
+
logger.error(f"HTTP Exception: {str(he)}")
|
| 96 |
raise he
|
| 97 |
except Exception as e:
|
| 98 |
+
logger.error(f"Unexpected error: {str(e)}")
|
| 99 |
raise HTTPException(status_code=500, detail=str(e))
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|
| 102 |
import uvicorn
|
| 103 |
+
uvicorn.run(app, host="0.0.0.0", port=8080)
|