slimshadow commited on
Commit
f36079b
·
verified ·
1 Parent(s): 7e362ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -4
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import streamlit as st
2
  import yt_dlp
 
3
 
4
  def get_youtube_formats(url):
5
  ydl_opts = {
@@ -12,10 +13,12 @@ def get_youtube_formats(url):
12
 
13
  def download_video(url, format_id=None):
14
  ydl_opts = {
15
- 'format': format_id if format_id else 'best'
 
16
  }
17
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
18
  ydl.download([url])
 
19
 
20
  st.title("Video Downloader")
21
 
@@ -25,18 +28,34 @@ if url:
25
  if "youtube.com" in url or "youtu.be" in url:
26
  st.write("Fetching available formats from YouTube...")
27
  formats = get_youtube_formats(url)
28
- format_options = [f"{f['format_id']} - {f.get('format_note', 'No note')} - {f['ext']}" for f in formats]
29
 
30
  format_choice = st.selectbox("Select video quality:", format_options)
31
  selected_format_id = format_choice.split(' ')[0]
32
 
33
  if st.button("Download"):
34
  st.write("Downloading...")
35
- download_video(url, format_id=selected_format_id)
36
  st.success("Download complete!")
 
 
 
 
 
 
 
 
37
  else:
38
  st.write("Downloading in best available quality...")
39
  if st.button("Download"):
40
  st.write("Downloading...")
41
- download_video(url)
42
  st.success("Download complete!")
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import yt_dlp
3
+ import os
4
 
5
  def get_youtube_formats(url):
6
  ydl_opts = {
 
13
 
14
  def download_video(url, format_id=None):
15
  ydl_opts = {
16
+ 'format': format_id if format_id else 'best',
17
+ 'outtmpl': 'downloaded_video.%(ext)s'
18
  }
19
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
20
  ydl.download([url])
21
+ return 'downloaded_video.mp4' # Assuming MP4 format for simplicity
22
 
23
  st.title("Video Downloader")
24
 
 
28
  if "youtube.com" in url or "youtu.be" in url:
29
  st.write("Fetching available formats from YouTube...")
30
  formats = get_youtube_formats(url)
31
+ format_options = [f"{f['format_id']} - {f.get('format_note', 'No note')} - {f['ext']} - {f['filesize'] or 'Unknown size'}" for f in formats]
32
 
33
  format_choice = st.selectbox("Select video quality:", format_options)
34
  selected_format_id = format_choice.split(' ')[0]
35
 
36
  if st.button("Download"):
37
  st.write("Downloading...")
38
+ downloaded_file = download_video(url, format_id=selected_format_id)
39
  st.success("Download complete!")
40
+
41
+ with open(downloaded_file, 'rb') as file:
42
+ btn = st.download_button(
43
+ label="Download Video",
44
+ data=file,
45
+ file_name=downloaded_file,
46
+ mime='video/mp4'
47
+ )
48
  else:
49
  st.write("Downloading in best available quality...")
50
  if st.button("Download"):
51
  st.write("Downloading...")
52
+ downloaded_file = download_video(url)
53
  st.success("Download complete!")
54
+
55
+ with open(downloaded_file, 'rb') as file:
56
+ btn = st.download_button(
57
+ label="Download Video",
58
+ data=file,
59
+ file_name=downloaded_file,
60
+ mime='video/mp4'
61
+ )