8
8
from fastapi .responses import FileResponse , StreamingResponse
9
9
import os
10
10
from starlette .status import HTTP_404_NOT_FOUND
11
-
11
+ import re
12
+ from typing import Dict
12
13
13
14
router = APIRouter ()
14
15
16
+ # Global dictionary to track the status of tasks
17
+ task_status : Dict [str , str ] = {}
18
+
15
19
16
20
# Retrieve the DATA_DIR from environment variable or use default
17
21
DATA_DIR = os .getenv ("DATA_DIR" , "/home/sam/github/youtube-auto-dub/backend/data" )
18
22
23
+ def extract_video_id (url : str ) -> str :
24
+ """
25
+ Extracts the video ID from a YouTube URL.
26
+ """
27
+ pattern = r"(?:v=|\/)([0-9A-Za-z_-]{11}).*"
28
+ match = re .search (pattern , str (url ))
29
+ return match .group (1 ) if match else None
30
+
19
31
20
32
@router .post ("/download/" )
21
- async def download_video (
22
- request : VideoDownloadRequest , background_tasks : BackgroundTasks
23
- ):
33
+ async def download_video (request : VideoDownloadRequest , background_tasks : BackgroundTasks ):
34
+ # Correctly access youtube_url from the request object
35
+ video_id = extract_video_id (request .url )
36
+ if not video_id :
37
+ logger .error ("Invalid YouTube URL provided." )
38
+ raise HTTPException (status_code = 400 , detail = "Invalid YouTube URL" )
39
+
40
+ if not video_id :
41
+ raise HTTPException (status_code = 400 , detail = "Invalid YouTube URL" )
24
42
try :
25
43
url = str (request .url )
26
44
logger .info (f"Received download request for URL: { url } " )
@@ -44,19 +62,26 @@ async def download_video(
44
62
output_path = os .path .join (
45
63
DATA_DIR , "final_videos" , os .path .basename (video_path )
46
64
)
65
+ task_status [video_id ] = "Processing"
47
66
48
67
background_tasks .add_task (
49
68
process_audio_and_assemble_video ,
50
69
video_path ,
51
70
audio_path ,
52
71
output_path ,
53
72
translated_text ,
73
+ video_id
54
74
)
55
- return {"message" : "Video processing started." , "caption_path" : caption_path }
75
+
76
+ return {"video_id" : video_id , "status" : "Processing started" }
56
77
except Exception as e :
57
78
logger .error (f"Error in download_video endpoint: { e } " )
58
79
raise HTTPException (status_code = 500 , detail = str (e ))
59
80
81
+ @router .get ("/status/{video_id}" )
82
+ def get_task_status (video_id : str ):
83
+ status = task_status .get (video_id , "Not Found" )
84
+ return {"video_id" : video_id , "status" : status }
60
85
61
86
@router .get ("/download-video/{video_id}" )
62
87
async def download_video (video_id : str ):
@@ -67,7 +92,7 @@ async def download_video(video_id: str):
67
92
68
93
69
94
def process_audio_and_assemble_video (
70
- video_path : str , audio_path : str , output_path : str , text_to_speak : str
95
+ video_path : str , audio_path : str , output_path : str , text_to_speak : str , video_id : str
71
96
):
72
97
try :
73
98
style = "default"
@@ -94,5 +119,8 @@ def process_audio_and_assemble_video(
94
119
video_path , processed_audio_path , output_path
95
120
)
96
121
122
+ task_status [video_id ] = "Completed"
123
+
97
124
except Exception as e :
125
+ task_status [video_id ] = "Failed"
98
126
logger .error (f"Error in process_audio_and_assemble_video: { e } " )
0 commit comments