shiue20 commited on
Commit
e59cd7f
Β·
verified Β·
1 Parent(s): ed74b4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -15
app.py CHANGED
@@ -6,35 +6,56 @@ import gradio as gr
6
 
7
  def is_internet_available():
8
  try:
9
- # Try resolving YouTube hostname
10
  socket.gethostbyname('www.youtube.com')
11
  return True
12
  except socket.error:
13
  return False
14
 
15
- def download_video(url):
16
  if not url.strip():
17
  raise gr.Error("⚠️ Please enter a valid YouTube or TikTok URL.")
18
  if not is_internet_available():
19
  raise gr.Error("❌ No internet access detected. Please enable internet in your deployment environment.")
20
 
21
  temp_dir = tempfile.mkdtemp()
 
22
  output_path = os.path.join(temp_dir, "%(title)s.%(ext)s")
23
 
24
- ydl_opts = {
25
- 'format': 'best',
26
- 'outtmpl': output_path,
27
- 'quiet': True,
28
- 'no_warnings': True,
29
- 'noplaylist': True,
30
- 'socket_timeout': 15,
31
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  try:
34
  with YoutubeDL(ydl_opts) as ydl:
35
  info = ydl.extract_info(url, download=True)
36
  downloaded_file = ydl.prepare_filename(info)
37
 
 
 
 
 
38
  if not os.path.exists(downloaded_file):
39
  raise gr.Error("❌ Download failed. File not found.")
40
 
@@ -51,11 +72,14 @@ def download_video(url):
51
  raise gr.Error(f"❌ Error: {err}")
52
 
53
  demo = gr.Interface(
54
- fn=download_video,
55
- inputs=gr.Textbox(label="πŸŽ₯ Enter YouTube or TikTok URL"),
56
- outputs=gr.File(label="πŸ“ Downloaded Video File"),
57
- title="🎬 Video Downloader",
58
- description="Download videos from YouTube or TikTok. Paste a valid video link and get the file.",
 
 
 
59
  theme="default"
60
  )
61
 
 
6
 
7
  def is_internet_available():
8
  try:
 
9
  socket.gethostbyname('www.youtube.com')
10
  return True
11
  except socket.error:
12
  return False
13
 
14
+ def download_media(url, format_choice):
15
  if not url.strip():
16
  raise gr.Error("⚠️ Please enter a valid YouTube or TikTok URL.")
17
  if not is_internet_available():
18
  raise gr.Error("❌ No internet access detected. Please enable internet in your deployment environment.")
19
 
20
  temp_dir = tempfile.mkdtemp()
21
+ # File template
22
  output_path = os.path.join(temp_dir, "%(title)s.%(ext)s")
23
 
24
+ # yt-dlp options differ based on format
25
+ if format_choice == "Audio (MP3)":
26
+ ydl_opts = {
27
+ 'format': 'bestaudio/best',
28
+ 'outtmpl': output_path,
29
+ 'quiet': True,
30
+ 'no_warnings': True,
31
+ 'noplaylist': True,
32
+ 'postprocessors': [{
33
+ 'key': 'FFmpegExtractAudio',
34
+ 'preferredcodec': 'mp3',
35
+ 'preferredquality': '192',
36
+ }],
37
+ 'socket_timeout': 15,
38
+ }
39
+ else: # Video
40
+ ydl_opts = {
41
+ 'format': 'bestvideo+bestaudio/best',
42
+ 'outtmpl': output_path,
43
+ 'quiet': True,
44
+ 'no_warnings': True,
45
+ 'noplaylist': True,
46
+ 'merge_output_format': 'mp4',
47
+ 'socket_timeout': 15,
48
+ }
49
 
50
  try:
51
  with YoutubeDL(ydl_opts) as ydl:
52
  info = ydl.extract_info(url, download=True)
53
  downloaded_file = ydl.prepare_filename(info)
54
 
55
+ # If audio, extension is mp3 due to postprocessor
56
+ if format_choice == "Audio (MP3)":
57
+ downloaded_file = os.path.splitext(downloaded_file)[0] + ".mp3"
58
+
59
  if not os.path.exists(downloaded_file):
60
  raise gr.Error("❌ Download failed. File not found.")
61
 
 
72
  raise gr.Error(f"❌ Error: {err}")
73
 
74
  demo = gr.Interface(
75
+ fn=download_media,
76
+ inputs=[
77
+ gr.Textbox(label="πŸŽ₯ Enter YouTube or TikTok URL"),
78
+ gr.Radio(["Audio (MP3)", "Video (MP4)"], label="Select format", value="Audio (MP3)")
79
+ ],
80
+ outputs=gr.File(label="πŸ“ Downloaded File"),
81
+ title="🎬 Audio/Video Downloader",
82
+ description="Download audio (MP3) or video (MP4) from YouTube or TikTok by pasting a valid URL.",
83
  theme="default"
84
  )
85