JustusI commited on
Commit
f204ff2
·
verified ·
1 Parent(s): 750a9f5

fix yt errors

Browse files
Files changed (1) hide show
  1. app.py +51 -11
app.py CHANGED
@@ -3,6 +3,7 @@ import tempfile
3
  import os
4
  import yt_dlp
5
  from moviepy.editor import VideoFileClip
 
6
 
7
  from openai import OpenAI
8
  client = OpenAI()
@@ -15,6 +16,23 @@ client = OpenAI()
15
  # ---------------------------
16
  # Helper Functions
17
  # ---------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  def download_video(youtube_url: str, output_path: str) -> str:
19
  """Download a YouTube video using yt-dlp and save it to the given output path."""
20
  try:
@@ -25,6 +43,13 @@ def download_video(youtube_url: str, output_path: str) -> str:
25
  'quiet': False,
26
  'no_warnings': False,
27
  'merge_output_format': 'mp4',
 
 
 
 
 
 
 
28
  }
29
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
30
  info = ydl.extract_info(youtube_url, download=True)
@@ -32,7 +57,15 @@ def download_video(youtube_url: str, output_path: str) -> str:
32
  video_path = os.path.join(output_path, f"video.{ext}")
33
  return video_path
34
  except Exception as e:
35
- st.error(f"Error downloading video: {e}")
 
 
 
 
 
 
 
 
36
  return None
37
 
38
  def extract_audio(video_path: str) -> str:
@@ -118,16 +151,23 @@ elif input_mode == "YouTube URL":
118
  youtube_url = st.sidebar.text_input("Enter YouTube video URL:")
119
  if youtube_url:
120
  if st.sidebar.button("Download Video"):
121
- with st.spinner("Downloading video from YouTube..."):
122
- with tempfile.TemporaryDirectory() as temp_dir:
123
- video_path = download_video(youtube_url, temp_dir)
124
- if video_path:
125
- # Copy to a persistent temp file
126
- with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp:
127
- with open(video_path, 'rb') as src:
128
- tmp.write(src.read())
129
- video_path = tmp.name
130
- st.sidebar.success("Video downloaded successfully!")
 
 
 
 
 
 
 
131
 
132
  # ---------------------------
133
  # Process Video to Extract Audio and Transcript
 
3
  import os
4
  import yt_dlp
5
  from moviepy.editor import VideoFileClip
6
+ import socket
7
 
8
  from openai import OpenAI
9
  client = OpenAI()
 
16
  # ---------------------------
17
  # Helper Functions
18
  # ---------------------------
19
+ def check_internet_connection(host="8.8.8.8", port=53, timeout=3):
20
+ """Check if there's an active internet connection."""
21
+ try:
22
+ socket.setdefaulttimeout(timeout)
23
+ socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
24
+ return True
25
+ except socket.error:
26
+ return False
27
+
28
+ def check_youtube_access():
29
+ """Check if YouTube is accessible."""
30
+ try:
31
+ socket.setdefaulttimeout(5)
32
+ socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect(("www.youtube.com", 443))
33
+ return True
34
+ except socket.error:
35
+ return False
36
  def download_video(youtube_url: str, output_path: str) -> str:
37
  """Download a YouTube video using yt-dlp and save it to the given output path."""
38
  try:
 
43
  'quiet': False,
44
  'no_warnings': False,
45
  'merge_output_format': 'mp4',
46
+ # Add network-related options to handle connectivity issues
47
+ 'socket_timeout': 30,
48
+ 'retries': 10,
49
+ 'fragment_retries': 10,
50
+ 'extractor_retries': 5,
51
+ # Use IPv4 to avoid DNS issues
52
+ 'source_address': '0.0.0.0',
53
  }
54
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
55
  info = ydl.extract_info(youtube_url, download=True)
 
57
  video_path = os.path.join(output_path, f"video.{ext}")
58
  return video_path
59
  except Exception as e:
60
+ error_msg = str(e)
61
+ if "Failed to resolve" in error_msg or "No address associated with hostname" in error_msg:
62
+ st.error("⚠️ Network Error: Cannot connect to YouTube. Please check:")
63
+ st.error("1. Your internet connection is active")
64
+ st.error("2. YouTube is accessible in your network/region")
65
+ st.error("3. No firewall/proxy is blocking YouTube")
66
+ st.info("💡 Alternative: Try uploading the video directly if you have it saved locally.")
67
+ else:
68
+ st.error(f"Error downloading video: {e}")
69
  return None
70
 
71
  def extract_audio(video_path: str) -> str:
 
151
  youtube_url = st.sidebar.text_input("Enter YouTube video URL:")
152
  if youtube_url:
153
  if st.sidebar.button("Download Video"):
154
+ # Check internet connection first
155
+ if not check_internet_connection():
156
+ st.sidebar.error("❌ No internet connection detected. Please check your network.")
157
+ elif not check_youtube_access():
158
+ st.sidebar.error("❌ Cannot reach YouTube. It may be blocked by your network/firewall.")
159
+ st.sidebar.info("💡 Try using a VPN or upload the video directly instead.")
160
+ else:
161
+ with st.spinner("Downloading video from YouTube..."):
162
+ with tempfile.TemporaryDirectory() as temp_dir:
163
+ video_path = download_video(youtube_url, temp_dir)
164
+ if video_path:
165
+ # Copy to a persistent temp file
166
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp:
167
+ with open(video_path, 'rb') as src:
168
+ tmp.write(src.read())
169
+ video_path = tmp.name
170
+ st.sidebar.success("Video downloaded successfully!")
171
 
172
  # ---------------------------
173
  # Process Video to Extract Audio and Transcript