MJPansa commited on
Commit
25ead2e
1 Parent(s): 187461d

Update app.py

Browse files

add download button

Files changed (1) hide show
  1. app.py +19 -5
app.py CHANGED
@@ -1,30 +1,44 @@
1
  import streamlit as st
2
  from pytube import YouTube
3
  import os
 
4
 
5
- # Function to download YouTube video
6
  def download_video(url, selected_quality):
7
  try:
8
  yt = YouTube(url)
9
  st.write("Video Title:", yt.title)
10
  st.write("Downloading...")
11
  stream = yt.streams.filter(progressive=True, file_extension='mp4').get_by_resolution(selected_quality)
12
- download_path = os.path.join(os.path.expanduser("~\Downloads"))
13
- stream.download(output_path=download_path)
14
  st.success("Download completed successfully!")
 
15
  except Exception as e:
16
  st.error(f"An error occurred: {e}")
 
 
 
 
 
 
 
 
 
 
17
 
18
  # Streamlit UI
19
  st.title("YouTube Video Downloader")
20
 
21
  url = st.text_input("Enter YouTube Video URL:", "")
22
  quality_options = ["1080p", "720p", "480p", "360p"]
23
-
24
  selected_quality = st.selectbox("Select Video Quality:", quality_options)
25
 
26
  if st.button("Download"):
27
  if url.strip() == "":
28
  st.warning("Please enter a YouTube video URL.")
29
  else:
30
- download_video(url, selected_quality)
 
 
 
 
1
  import streamlit as st
2
  from pytube import YouTube
3
  import os
4
+ import base64
5
 
6
+ # Function to download YouTube video and return the path of the downloaded file
7
  def download_video(url, selected_quality):
8
  try:
9
  yt = YouTube(url)
10
  st.write("Video Title:", yt.title)
11
  st.write("Downloading...")
12
  stream = yt.streams.filter(progressive=True, file_extension='mp4').get_by_resolution(selected_quality)
13
+ download_path = os.path.join(os.path.expanduser("~"), "Downloads")
14
+ downloaded_file_path = stream.download(output_path=download_path)
15
  st.success("Download completed successfully!")
16
+ return downloaded_file_path
17
  except Exception as e:
18
  st.error(f"An error occurred: {e}")
19
+ return None
20
+
21
+ # Function to create a download link
22
+ def create_download_link(file_path):
23
+ file_name = os.path.basename(file_path)
24
+ with open(file_path, "rb") as f:
25
+ bytes = f.read()
26
+ b64 = base64.b64encode(bytes).decode()
27
+ href = f'<a href="data:file/mp4;base64,{b64}" download="{file_name}">Download {file_name}</a>'
28
+ return href
29
 
30
  # Streamlit UI
31
  st.title("YouTube Video Downloader")
32
 
33
  url = st.text_input("Enter YouTube Video URL:", "")
34
  quality_options = ["1080p", "720p", "480p", "360p"]
 
35
  selected_quality = st.selectbox("Select Video Quality:", quality_options)
36
 
37
  if st.button("Download"):
38
  if url.strip() == "":
39
  st.warning("Please enter a YouTube video URL.")
40
  else:
41
+ downloaded_file_path = download_video(url, selected_quality)
42
+ if downloaded_file_path:
43
+ download_link = create_download_link(downloaded_file_path)
44
+ st.markdown(download_link, unsafe_allow_html=True)