jpjp9292 commited on
Commit
561e61e
1 Parent(s): df69275

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import yt_dlp
3
+ import os
4
+ import tempfile
5
+
6
+ # Set the title of the app
7
+ st.title("YouTube Video Downloader")
8
+
9
+ # Input field for YouTube video URL
10
+ video_url = st.text_input("Enter YouTube Video URL:")
11
+
12
+ # Function to download video
13
+ def download_video(url):
14
+ # Create a temporary directory to store the downloaded video
15
+ with tempfile.TemporaryDirectory() as tmpdirname:
16
+ # Define download options
17
+ ydl_opts = {
18
+ 'format': 'bestvideo+bestaudio/best',
19
+ 'outtmpl': os.path.join(tmpdirname, '%(title)s.%(ext)s'),
20
+ 'merge_output_format': 'webm', # Download format as .webm
21
+ }
22
+
23
+ # Download the video using yt-dlp
24
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
25
+ # Use a progress hook to update the progress bar
26
+ def progress_hook(d):
27
+ if d['status'] == 'downloading':
28
+ st.progress(d['downloaded_bytes'] / d['total_bytes'])
29
+ elif d['status'] == 'finished':
30
+ st.success("Download completed!")
31
+
32
+ ydl.params['progress_hooks'] = [progress_hook]
33
+ ydl.download([url])
34
+
35
+ # Return the path of the downloaded video
36
+ return os.path.join(tmpdirname, f"{ydl.prepare_filename(ydl.extract_info(url))}")
37
+
38
+ # Download button
39
+ if st.button("Download Video"):
40
+ if video_url:
41
+ # Display progress bar
42
+ st.write("Downloading video...")
43
+ downloaded_file_path = download_video(video_url)
44
+
45
+ # Provide a download link
46
+ st.write("Download completed! Click below to download your video:")
47
+ st.markdown(f"[Download Video]({downloaded_file_path})", unsafe_allow_html=True)
48
+ else:
49
+ st.error("Please enter a valid YouTube URL.")