BertChristiaens commited on
Commit
a9cea26
1 Parent(s): 5a51f76
Files changed (1) hide show
  1. app.py +14 -2
app.py CHANGED
@@ -26,6 +26,13 @@ def download_video(url):
26
  print(error_code)
27
  return error_code, info
28
 
 
 
 
 
 
 
 
29
  def main():
30
  """This method has a text input field, radio button and a button for downloading the video as mp3."""
31
  st.title('Youtube to mp3')
@@ -34,8 +41,9 @@ def main():
34
 
35
  if st.button('Download video'):
36
  with st.spinner('Downloading video'):
 
 
37
  error_code, info = download_video(url)
38
- print(info.keys())
39
 
40
  st.session_state['latest_video'] = url
41
  st.session_state['latest_title'] = info['fulltitle']
@@ -48,13 +56,17 @@ def main():
48
  if os.path.isfile('audio.mp3') and st.session_state.get('latest_video'):
49
  video_url = st.session_state.get('latest_video', '/')
50
  video_title = st.session_state.get('latest_title', '/')
 
51
  st.write(f"Last downloaded video is: {video_title} with url {video_url}")
52
  st.audio('audio.mp3')
53
  buffer = BytesIO()
54
  with open('audio.mp3', 'rb') as f:
55
  buffer.write(f.read())
56
  timestamp = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
57
- st.download_button(label='Download mp3', data=buffer.getvalue(), file_name=f"{video_title.replace(' ', '-')}.mp3', mime='audio/mp3")
 
 
 
58
 
59
  if __name__ == '__main__':
60
  main()
 
26
  print(error_code)
27
  return error_code, info
28
 
29
+ def clean_files():
30
+ if os.path.isfile('audio'):
31
+ os.remove('audio')
32
+ if os.path.isfile('audio.mp3'):
33
+ os.remove('audio.mp3')
34
+
35
+
36
  def main():
37
  """This method has a text input field, radio button and a button for downloading the video as mp3."""
38
  st.title('Youtube to mp3')
 
41
 
42
  if st.button('Download video'):
43
  with st.spinner('Downloading video'):
44
+ clean_files()
45
+
46
  error_code, info = download_video(url)
 
47
 
48
  st.session_state['latest_video'] = url
49
  st.session_state['latest_title'] = info['fulltitle']
 
56
  if os.path.isfile('audio.mp3') and st.session_state.get('latest_video'):
57
  video_url = st.session_state.get('latest_video', '/')
58
  video_title = st.session_state.get('latest_title', '/')
59
+
60
  st.write(f"Last downloaded video is: {video_title} with url {video_url}")
61
  st.audio('audio.mp3')
62
  buffer = BytesIO()
63
  with open('audio.mp3', 'rb') as f:
64
  buffer.write(f.read())
65
  timestamp = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
66
+ st.download_button(label='Download mp3',
67
+ data=buffer.getvalue(),
68
+ file_name=f"{video_title.replace(' ', '-')}.mp3",
69
+ mime="audio/mp3")
70
 
71
  if __name__ == '__main__':
72
  main()