FASHHaha commited on
Commit
f6030b8
·
verified ·
1 Parent(s): a0fb1e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -14
app.py CHANGED
@@ -1,33 +1,33 @@
1
  import streamlit as st
2
- import yt_dlp
 
3
 
4
  # Function to download video or audio
5
  def download_media(url, media_type, resolution=None):
6
- ydl_opts = {
7
- 'format': 'bestvideo+bestaudio/best' if media_type == 'video' else 'bestaudio',
8
- 'postprocessors': [{'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3', 'preferredquality': '192'}] if media_type == 'audio' else [],
9
- }
10
-
11
- if resolution and media_type == 'video':
12
- ydl_opts['format'] = f'bestvideo[height<={resolution}]+bestaudio/best[height<={resolution}]'
13
-
14
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
15
- result = ydl.download([url])
16
 
17
- return result
 
 
 
 
 
 
 
18
 
19
  # Streamlit UI
20
  st.title("All in One YouTube Downloader")
21
  url = st.text_input("Enter YouTube URL:")
22
  media_type = st.selectbox("Select type:", ("video", "audio"))
23
- resolution = st.selectbox("Select resolution (for video):", ("144", "240", "360", "480", "720", "1080"), index=4)
24
 
25
  if st.button("Download"):
26
  if url:
27
  with st.spinner('Downloading...'):
28
  try:
29
- download_media(url, media_type, resolution if media_type == 'video' else None)
30
  st.success("Download completed!")
 
31
  except Exception as e:
32
  st.error(f"Error: {e}")
33
  else:
 
1
  import streamlit as st
2
+ import pafy
3
+ import os
4
 
5
  # Function to download video or audio
6
  def download_media(url, media_type, resolution=None):
7
+ video = pafy.new(url)
 
 
 
 
 
 
 
 
 
8
 
9
+ if media_type == 'video':
10
+ stream = video.getbest(preftype="mp4") if resolution is None else video.getbest(preftype="mp4", ftypestrict=False, ftype='mp4', res=resolution)
11
+ elif media_type == 'audio':
12
+ stream = video.getbestaudio(preftype="m4a")
13
+
14
+ download_path = os.path.join("downloads", stream.filename)
15
+ stream.download(filepath=download_path)
16
+ return download_path
17
 
18
  # Streamlit UI
19
  st.title("All in One YouTube Downloader")
20
  url = st.text_input("Enter YouTube URL:")
21
  media_type = st.selectbox("Select type:", ("video", "audio"))
22
+ resolution = st.selectbox("Select resolution (for video):", ("360p", "480p", "720p", "1080p"), index=2)
23
 
24
  if st.button("Download"):
25
  if url:
26
  with st.spinner('Downloading...'):
27
  try:
28
+ file_path = download_media(url, media_type, resolution if media_type == 'video' else None)
29
  st.success("Download completed!")
30
+ st.write(f"File saved as: {file_path}")
31
  except Exception as e:
32
  st.error(f"Error: {e}")
33
  else: