Ujeshhh commited on
Commit
6e10c3a
·
verified ·
1 Parent(s): 1b0ff1c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -34
app.py CHANGED
@@ -1,47 +1,43 @@
1
  import gradio as gr
2
- from pytube import YouTube
3
  from moviepy.audio.io.AudioFileClip import AudioFileClip
 
4
  import os
5
 
6
  def download_youtube(url, output_format, resolution, audio_bitrate):
7
  try:
8
- yt = YouTube(url)
9
- title = yt.title.replace(" ", "_").replace("/", "_")
10
-
11
- # Set output filename and path
12
- output_filename = f"{title}.{output_format.lower()}"
13
- output_path = f"downloads/{output_filename}"
14
-
15
- # Get stream
16
- if output_format == "MP4":
17
- stream = yt.streams.filter(progressive=True, file_extension="mp4", res=resolution).first()
18
- else: # Audio
19
- stream = yt.streams.filter(only_audio=True).order_by("abr").desc().first()
20
-
21
- if not stream:
22
- return "Selected quality not available", None
23
-
24
- # Download
25
- downloaded_path = stream.download(output_path="downloads", filename=title + ".mp4")
26
-
27
- if output_format in ["MP3", "WAV"]:
28
- audio = AudioFileClip(downloaded_path)
29
- if output_format == "MP3":
30
- final_path = f"downloads/{title}.mp3"
31
- audio.write_audiofile(final_path, bitrate=audio_bitrate)
32
- else:
33
- final_path = f"downloads/{title}.wav"
34
- audio.write_audiofile(final_path)
35
- audio.close()
36
- os.remove(downloaded_path)
37
- else:
38
- final_path = downloaded_path
39
-
40
- return f"Downloaded: {output_format}", final_path
41
 
42
  except Exception as e:
43
  return f"Error: {str(e)}", None
44
 
 
45
  formats = ["MP3", "WAV", "MP4"]
46
  resolutions = ["360p", "480p", "720p", "1080p"]
47
  bitrates = ["64k", "128k", "192k", "256k", "320k"]
 
1
  import gradio as gr
 
2
  from moviepy.audio.io.AudioFileClip import AudioFileClip
3
+ import yt_dlp
4
  import os
5
 
6
  def download_youtube(url, output_format, resolution, audio_bitrate):
7
  try:
8
+ output_dir = "downloads"
9
+ os.makedirs(output_dir, exist_ok=True)
10
+ output_template = f"{output_dir}/%(title)s.%(ext)s"
11
+
12
+ ydl_opts = {
13
+ "format": "bestaudio/best" if output_format in ["MP3", "WAV"] else f"bestvideo[height<={resolution[:-1]}]+bestaudio/best",
14
+ "outtmpl": output_template,
15
+ "postprocessors": [],
16
+ }
17
+
18
+ if output_format == "MP3":
19
+ ydl_opts["postprocessors"].append({
20
+ 'key': 'FFmpegExtractAudio',
21
+ 'preferredcodec': 'mp3',
22
+ 'preferredquality': audio_bitrate.replace("k", ""),
23
+ })
24
+ elif output_format == "WAV":
25
+ ydl_opts["postprocessors"].append({
26
+ 'key': 'FFmpegExtractAudio',
27
+ 'preferredcodec': 'wav',
28
+ })
29
+
30
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
31
+ info_dict = ydl.extract_info(url, download=True)
32
+ filename = ydl.prepare_filename(info_dict)
33
+ if output_format in ["MP3", "WAV"]:
34
+ filename = os.path.splitext(filename)[0] + f".{output_format.lower()}"
35
+ return "Download complete", filename
 
 
 
 
 
36
 
37
  except Exception as e:
38
  return f"Error: {str(e)}", None
39
 
40
+
41
  formats = ["MP3", "WAV", "MP4"]
42
  resolutions = ["360p", "480p", "720p", "1080p"]
43
  bitrates = ["64k", "128k", "192k", "256k", "320k"]