EdgarDataScientist commited on
Commit
ba6451b
·
verified ·
1 Parent(s): 5cb7e51

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -16
app.py CHANGED
@@ -8,22 +8,27 @@ import os
8
  CLASSIFIER = "Jzuluaga/accent-id-commonaccent_xlsr-en-english"
9
 
10
  def download_video(url):
11
- """Handles YouTube and direct video links"""
12
- if "youtube.com" in url:
13
- yt = YouTube(url)
14
- stream = yt.streams.filter(progressive=True, file_extension='mp4').first()
15
- video_path = stream.download()
16
- return video_path
17
- else: # Direct download (assumes URL is direct mp4 link)
18
- # Download file locally
19
- import requests
20
- local_filename = "temp_video.mp4"
21
- with requests.get(url, stream=True) as r:
22
- r.raise_for_status()
23
- with open(local_filename, 'wb') as f:
24
- for chunk in r.iter_content(chunk_size=8192):
25
- f.write(chunk)
26
- return local_filename
 
 
 
 
 
27
 
28
  def extract_audio(video_path):
29
  clip = VideoFileClip(video_path)
 
8
  CLASSIFIER = "Jzuluaga/accent-id-commonaccent_xlsr-en-english"
9
 
10
  def download_video(url):
11
+ """Handles YouTube and direct video links with error handling"""
12
+ try:
13
+ if "youtube.com" in url or "youtu.be" in url:
14
+ yt = YouTube(url)
15
+ stream = yt.streams.filter(progressive=True, file_extension='mp4').first()
16
+ if not stream:
17
+ raise ValueError("No suitable video stream found.")
18
+ return stream.download()
19
+ else:
20
+ # For direct MP4 links, download file
21
+ import requests
22
+ local_filename = "temp_video.mp4"
23
+ with requests.get(url, stream=True) as r:
24
+ r.raise_for_status()
25
+ with open(local_filename, 'wb') as f:
26
+ for chunk in r.iter_content(chunk_size=8192):
27
+ f.write(chunk)
28
+ return local_filename
29
+ except Exception as e:
30
+ raise RuntimeError(f"Failed to download video: {e}")
31
+
32
 
33
  def extract_audio(video_path):
34
  clip = VideoFileClip(video_path)