maaz21 commited on
Commit
76b59ce
·
verified ·
1 Parent(s): 8f9557a

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +24 -20
src/streamlit_app.py CHANGED
@@ -1,5 +1,6 @@
1
  import streamlit as st
2
  import yt_dlp
 
3
  import os
4
 
5
  st.title("📥 Multi-Platform Video Downloader")
@@ -13,28 +14,32 @@ if st.button("Download"):
13
  else:
14
  with st.spinner("Downloading video..."):
15
  try:
16
- # Setup output directory
17
- os.makedirs("downloads", exist_ok=True)
18
-
19
- # yt-dlp options with spoofed User-Agent
20
- ydl_opts = {
21
- 'outtmpl': 'downloads/%(title)s.%(ext)s',
22
- 'format': 'bestvideo+bestaudio/best',
23
- 'merge_output_format': 'mp4',
24
- 'noplaylist': True,
25
- 'quiet': True,
26
- 'http_headers': {
27
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
28
  }
29
- }
30
 
31
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
32
- info = ydl.extract_info(url, download=True)
33
- filename = ydl.prepare_filename(info)
 
 
34
 
35
- st.success("✅ Download completed!")
36
- with open(filename, 'rb') as f:
37
- st.download_button(label="📥 Click to Download", data=f, file_name=os.path.basename(filename), mime="video/mp4")
 
 
 
 
38
 
39
  except yt_dlp.utils.DownloadError as e:
40
  error_msg = str(e)
@@ -42,4 +47,3 @@ if st.button("Download"):
42
  st.error("⚠️ This video requires login. Only public videos can be downloaded.")
43
  else:
44
  st.error(f"❌ Download failed: {error_msg}")
45
-
 
1
  import streamlit as st
2
  import yt_dlp
3
+ import tempfile
4
  import os
5
 
6
  st.title("📥 Multi-Platform Video Downloader")
 
14
  else:
15
  with st.spinner("Downloading video..."):
16
  try:
17
+ # Use a temporary directory
18
+ with tempfile.TemporaryDirectory() as tmp_dir:
19
+ ydl_opts = {
20
+ 'outtmpl': os.path.join(tmp_dir, '%(title)s.%(ext)s'),
21
+ 'format': 'bestvideo+bestaudio/best',
22
+ 'merge_output_format': 'mp4',
23
+ 'noplaylist': True,
24
+ 'quiet': True,
25
+ 'http_headers': {
26
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
27
+ }
 
28
  }
 
29
 
30
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
31
+ info = ydl.extract_info(url, download=True)
32
+ filename = ydl.prepare_filename(info)
33
+
34
+ st.success("✅ Download completed!")
35
 
36
+ with open(filename, 'rb') as f:
37
+ st.download_button(
38
+ label="📥 Click to Download",
39
+ data=f,
40
+ file_name=os.path.basename(filename),
41
+ mime="video/mp4"
42
+ )
43
 
44
  except yt_dlp.utils.DownloadError as e:
45
  error_msg = str(e)
 
47
  st.error("⚠️ This video requires login. Only public videos can be downloaded.")
48
  else:
49
  st.error(f"❌ Download failed: {error_msg}")