jitendra.kasaudhan commited on
Commit
a29e553
β€’
1 Parent(s): 1dc0740

Add code to download youtube video

Browse files
Files changed (4) hide show
  1. .devcontainer/devcontainer.json +22 -0
  2. README.md +3 -3
  3. app.py +85 -0
  4. requirements.txt +2 -0
.devcontainer/devcontainer.json ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // For format details, see https://aka.ms/devcontainer.json. For config options, see the
2
+ // README at: https://github.com/devcontainers/templates/tree/main/src/python
3
+ {
4
+ "name": "Python 3",
5
+ // Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
6
+ "image": "mcr.microsoft.com/devcontainers/python:1-3.9-bookworm"
7
+
8
+ // Features to add to the dev container. More info: https://containers.dev/features.
9
+ // "features": {},
10
+
11
+ // Use 'forwardPorts' to make a list of ports inside the container available locally.
12
+ // "forwardPorts": [],
13
+
14
+ // Use 'postCreateCommand' to run commands after the container is created.
15
+ // "postCreateCommand": "pip3 install --user -r requirements.txt",
16
+
17
+ // Configure tool-specific properties.
18
+ // "customizations": {},
19
+
20
+ // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
21
+ // "remoteUser": "root"
22
+ }
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
  title: Youtube Video Downloader
3
- emoji: πŸ‘
4
- colorFrom: indigo
5
- colorTo: indigo
6
  sdk: streamlit
7
  sdk_version: 1.26.0
8
  app_file: app.py
 
1
  ---
2
  title: Youtube Video Downloader
3
+ emoji: πŸ“Š
4
+ colorFrom: pink
5
+ colorTo: red
6
  sdk: streamlit
7
  sdk_version: 1.26.0
8
  app_file: app.py
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from pytube import YouTube
3
+ import os
4
+ import base64
5
+ import time
6
+
7
+ #@deprecated: not used anymore due to streamlit download_button feature
8
+ def get_binary_file_downloader_html(bin_file, file_label='File'):
9
+ with open(bin_file, 'rb') as f:
10
+ data = f.read()
11
+ bin_str = base64.b64encode(data).decode()
12
+ href = f'<a href="data:application/octet-stream;base64,{bin_str}" download="{os.path.basename(bin_file)}">Download {file_label}</a>'
13
+ return href
14
+
15
+ def on_progress(stream, chunk, bytes_remaining):
16
+ print(' progress:', bytes_remaining, " ", end='\r', flush=True)
17
+
18
+ def on_complete(stream, filename):
19
+ print('--- Completed ---')
20
+ print('stream:', stream)
21
+ print('filename:', filename)
22
+
23
+ def delete_file(filepath):
24
+ if os.path.exists(filepath):
25
+ os.remove(filepath)
26
+
27
+ def on_download_file_click(filepath):
28
+ # sleep Xsec can delete file
29
+ time.sleep(2)
30
+ delete_file(filepath)
31
+
32
+ def main():
33
+ # Set the title and background color
34
+ st.title("YouTube video downloader πŸŽ₯")
35
+ st.markdown('<style>h1{color: orange; text-align: center;}</style>', unsafe_allow_html=True)
36
+
37
+ # st.subheader('Built with the Llama 2 πŸ¦™, Haystack, Streamlit and ❀️')
38
+ # st.markdown('<style>h3{color: pink; text-align: center;}</style>', unsafe_allow_html=True)
39
+
40
+ # Expander for app details
41
+ with st.expander("About the App (Works well on chrome browser)"):
42
+ st.write("This app allows you to download video from YouTube url.")
43
+ st.write("Enter a YouTube URL in the input box below and click on 'Submit'")
44
+
45
+ # Input box for YouTube URL
46
+ youtube_url = st.text_input("Enter YouTube URL")
47
+
48
+ # Submit button
49
+ if st.button("Submit") and youtube_url:
50
+ progress_bar = st.progress(0, text='Processing...')
51
+ yt = YouTube(youtube_url)
52
+ yt.register_on_progress_callback(on_progress)
53
+ yt.register_on_complete_callback(on_complete)
54
+ # extract only audio
55
+ video = yt.streams.first()
56
+ # video = yt.streams.get_highest_resolution()
57
+
58
+ initial_filename = yt.title
59
+ # download the file
60
+ destination = '.' # current folder
61
+ out_file = video.download(output_path=destination, filename=str(initial_filename))
62
+
63
+ # save the file
64
+ new_file = initial_filename
65
+ # base, ext = os.path.splitext(out_file)
66
+ # new_file = '-'.join(initial_filename.split(' '))[-15:] + '.mp3' # base[0:10] + '.mp3'
67
+ # os.rename(out_file, new_file)
68
+
69
+ # Display layout with 2 rows
70
+ st.video(youtube_url)
71
+ # st.header("Audio file in MP3 format")
72
+ # st.write(new_file)
73
+ # st.markdown(get_binary_file_downloader_html('photo.jpg', 'Picture'), unsafe_allow_html=True)
74
+ # st.markdown(get_binary_file_downloader_html('data.csv', 'My Data'), unsafe_allow_html=True)
75
+ # st.markdown(get_binary_file_downloader_html(new_file, 'Audio'), unsafe_allow_html=True)
76
+
77
+ # download video file
78
+ with open(new_file, 'rb') as f:
79
+ st.download_button('Download Video', f, file_name=new_file, on_click=on_download_file_click(new_file))
80
+
81
+ progress_bar.progress(100, text='DONE')
82
+ st.success('Successfull!!')
83
+
84
+ if __name__ == "__main__":
85
+ main()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ streamlit
2
+ pytube