jitendra.kasaudhan commited on
Commit
4adae43
·
1 Parent(s): 324f617

Delete audio file after download

Browse files
Files changed (1) hide show
  1. app.py +15 -5
app.py CHANGED
@@ -2,6 +2,7 @@ import streamlit as st
2
  from pytube import YouTube
3
  import os
4
  import base64
 
5
 
6
  def get_binary_file_downloader_html(bin_file, file_label='File'):
7
  with open(bin_file, 'rb') as f:
@@ -18,6 +19,14 @@ def on_complete(stream, filename):
18
  print('stream:', stream)
19
  print('filename:', filename)
20
 
 
 
 
 
 
 
 
 
21
 
22
  def main():
23
  # Set the title and background color
@@ -28,7 +37,7 @@ def main():
28
  # st.markdown('<style>h3{color: pink; text-align: center;}</style>', unsafe_allow_html=True)
29
 
30
  # Expander for app details
31
- with st.expander("About the App"):
32
  st.write("This app allows you to download MP3 audio from YouTube video url.")
33
  st.write("Enter a YouTube URL in the input box below and click on 'Submit'")
34
 
@@ -44,13 +53,14 @@ def main():
44
  # extract only audio
45
  video = yt.streams.filter(only_audio=True).first()
46
 
 
47
  # download the file
48
  destination = '.' # current folder
49
- out_file = video.download(output_path=destination)
50
 
51
  # save the file
52
  base, ext = os.path.splitext(out_file)
53
- new_file = 'audio_file' + '.mp3' # base[0:10] + '.mp3'
54
  os.rename(out_file, new_file)
55
 
56
  # Display layout with 2 rows
@@ -63,8 +73,8 @@ def main():
63
 
64
  # download audio file
65
  with open(new_file, 'rb') as f:
66
- st.download_button('Download Audio', f, file_name=new_file)
67
-
68
  progress_bar.progress(100, text='DONE')
69
  st.success('Successfull!!')
70
 
 
2
  from pytube import YouTube
3
  import os
4
  import base64
5
+ import time
6
 
7
  def get_binary_file_downloader_html(bin_file, file_label='File'):
8
  with open(bin_file, 'rb') as f:
 
19
  print('stream:', stream)
20
  print('filename:', filename)
21
 
22
+ def delete_file(filepath):
23
+ if os.path.exists(filepath):
24
+ os.remove(filepath)
25
+
26
+ def on_download_file_click(filepath):
27
+ # sleep Xsec can delete file
28
+ time.sleep(2)
29
+ delete_file(filepath)
30
 
31
  def main():
32
  # Set the title and background color
 
37
  # st.markdown('<style>h3{color: pink; text-align: center;}</style>', unsafe_allow_html=True)
38
 
39
  # Expander for app details
40
+ with st.expander("About the App (Works well on chrome browser)"):
41
  st.write("This app allows you to download MP3 audio from YouTube video url.")
42
  st.write("Enter a YouTube URL in the input box below and click on 'Submit'")
43
 
 
53
  # extract only audio
54
  video = yt.streams.filter(only_audio=True).first()
55
 
56
+ initial_filename = yt.title
57
  # download the file
58
  destination = '.' # current folder
59
+ out_file = video.download(output_path=destination, filename=str(initial_filename))
60
 
61
  # save the file
62
  base, ext = os.path.splitext(out_file)
63
+ new_file = '-'.join(initial_filename.split(' '))[-15:] + '.mp3' # base[0:10] + '.mp3'
64
  os.rename(out_file, new_file)
65
 
66
  # Display layout with 2 rows
 
73
 
74
  # download audio file
75
  with open(new_file, 'rb') as f:
76
+ st.download_button('Download Audio', f, file_name=new_file, on_click=on_download_file_click(new_file))
77
+
78
  progress_bar.progress(100, text='DONE')
79
  st.success('Successfull!!')
80