slimshadow commited on
Commit
179b3c0
·
verified ·
1 Parent(s): f36079b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -53
app.py CHANGED
@@ -1,61 +1,42 @@
1
  import streamlit as st
2
- import yt_dlp
3
  import os
 
4
 
5
- def get_youtube_formats(url):
6
- ydl_opts = {
7
- 'listformats': True,
8
- }
9
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
10
- info_dict = ydl.extract_info(url, download=False)
11
- formats = info_dict.get('formats', [])
12
- return formats
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
 
25
- url = st.text_input("Enter the video URL:")
 
 
 
 
 
 
26
 
27
- if url:
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
- )
 
1
  import streamlit as st
 
2
  import os
3
+ import yt_dlp
4
 
5
+ # Streamlit UI setup
6
+ st.title("YouTube Video Downloader 8K supported")
 
 
 
 
 
 
7
 
8
+ # Streamlit input fields
9
+ url = st.text_input("Enter the YouTube URL:")
10
+ quality = st.selectbox("Select the desired quality:", ['4320', '2160', '1440', '1080', '720', '480'])
11
+ save_file_name = st.text_input("Enter the save file name (without extension):")
12
+
13
+ # Convert quality to the appropriate format string for yt-dlp
14
+ quality_format = f"bestvideo[height<={quality}]+bestaudio/best[height<={quality}]"
 
15
 
16
+ # Define download options based on user input
17
+ ydl_opts = {
18
+ 'format': quality_format, # Download the specified quality
19
+ 'outtmpl': f'{save_file_name}.%(ext)s', # Save the video with the specified name
20
+ }
21
 
22
+ if st.button("Download"):
23
+ # Download the video
24
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
25
+ ydl.download([url])
26
+
27
+ # Path to the downloaded video file
28
+ file_path = f'{save_file_name}.webm'
29
 
30
+ # Check if the file exists
31
+ if os.path.exists(file_path):
32
+ # Provide a download link for the file
33
+ with open(file_path, "rb") as file:
34
+ btn = st.download_button(
35
+ label="Download video",
36
+ data=file,
37
+ file_name=f"{save_file_name}.webm",
38
+ mime="video/webm"
39
+ )
40
+ st.success('Your video is ready for download.')
 
 
 
 
 
 
 
 
 
 
41
  else:
42
+ st.error("File not found. Please make sure the download was successful.")