MujtabaAli commited on
Commit
07e567b
1 Parent(s): 33a7c96

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -31
app.py CHANGED
@@ -1,43 +1,58 @@
1
  import streamlit as st
2
  import yt_dlp
3
- import os
4
 
5
- def download_video(url, download_path):
 
6
  ydl_opts = {
7
- 'format': 'best', # Download the best quality by default
8
- 'outtmpl': os.path.join(download_path, '%(title)s.%(ext)s'),
9
- 'noplaylist': True, # Don't download playlists
10
  }
 
 
 
11
 
 
 
 
 
 
 
 
12
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
13
- info_dict = ydl.extract_info(url, download=False)
14
- formats = info_dict.get('formats', [])
15
- best_quality = formats[0]
16
- for f in formats:
17
- if f['height'] > best_quality['height']:
18
- best_quality = f
19
- download_url = best_quality['url']
20
- file_extension = best_quality['ext']
21
-
22
- # Initiate the download
23
- ydl_opts['format'] = best_quality['format_id']
24
- ydl_opts['outtmpl'] = os.path.join(download_path, '%(title)s.' + file_extension)
25
  ydl.download([url])
26
 
27
- return f"Downloaded: {info_dict['title']}.{file_extension}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
- # Streamlit App
30
- st.title('Video Downloader')
31
- st.write("Enter the video link to download it from YouTube, Facebook, Instagram, X (formerly Twitter), or Telegram.")
32
 
33
- # User input
34
- video_url = st.text_input("Video URL")
35
- download_button = st.button("Download Video")
 
 
 
 
 
36
 
37
- if download_button:
38
- if video_url:
39
- with st.spinner("Downloading..."):
40
- result = download_video(video_url, os.path.expanduser("~") + '/Downloads')
41
- st.success(result)
42
- else:
43
- st.error("Please enter a valid video URL.")
 
1
  import streamlit as st
2
  import yt_dlp
 
3
 
4
+ # Function to get video info (formats, quality, etc.)
5
+ def get_video_info(url):
6
  ydl_opts = {
7
+ 'quiet': True,
8
+ 'noplaylist': True,
 
9
  }
10
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
11
+ info = ydl.extract_info(url, download=False)
12
+ return info
13
 
14
+ # Function to download video
15
+ def download_video(url, format):
16
+ ydl_opts = {
17
+ 'format': format,
18
+ 'outtmpl': '%(title)s.%(ext)s', # Name the file based on the video title
19
+ 'noplaylist': True,
20
+ }
21
  with yt_dlp.YoutubeDL(ydl_opts) as ydl:
 
 
 
 
 
 
 
 
 
 
 
 
22
  ydl.download([url])
23
 
24
+ # Streamlit layout
25
+ st.title("Video Downloader App")
26
+
27
+ # User input for video URL
28
+ url = st.text_input('Enter Video URL')
29
+
30
+ if url:
31
+ try:
32
+ # Get video information (formats)
33
+ video_info = get_video_info(url)
34
+ formats = video_info.get('formats', [])
35
+
36
+ # Prepare list of available formats (e.g., 'best', 'mp4', 'webm')
37
+ format_options = []
38
+ for f in formats:
39
+ # Ensure format has 'format_note' and 'ext' to avoid key errors
40
+ if 'format_note' in f and 'ext' in f and 'filesize' in f:
41
+ format_options.append(f"{f['format_note']} - {f['ext']} - {f['filesize']} bytes")
42
+
43
+ if format_options:
44
+ # User selects a format
45
+ selected_format = st.selectbox('Select Video Quality', format_options)
46
 
47
+ # Convert selected format to yt-dlp's format identifier
48
+ selected_format_id = formats[format_options.index(selected_format)]['format_id']
 
49
 
50
+ if st.button('Download'):
51
+ with st.spinner('Downloading...'):
52
+ download_video(url, selected_format_id)
53
+ st.success('Download complete!')
54
+ else:
55
+ st.error("No formats available.")
56
+ except Exception as e:
57
+ st.error(f"Error fetching video info: {e}")
58