Spaces:
Runtime error
Runtime error
BertChristiaens
commited on
Commit
•
f57951a
1
Parent(s):
bfc1e35
add files
Browse files- .gitignore +2 -0
- app.py +59 -0
- requirements.txt +2 -0
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
venv
|
2 |
+
*.mp3
|
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""This is the main module of the streamlit app that allows the user to download youtube videos as mp3 files."""
|
2 |
+
import streamlit as st
|
3 |
+
from yt_dlp import YoutubeDL
|
4 |
+
import os
|
5 |
+
from io import BytesIO
|
6 |
+
from datetime import datetime
|
7 |
+
|
8 |
+
URLS = ['https://www.youtube.com/watch?v=BaW_jenozKc']
|
9 |
+
|
10 |
+
|
11 |
+
ydl_opts = {
|
12 |
+
'format': 'bestaudio/best',
|
13 |
+
'postprocessors': [{
|
14 |
+
'key': 'FFmpegExtractAudio',
|
15 |
+
'preferredcodec': 'mp3',
|
16 |
+
'preferredquality': '192',
|
17 |
+
}],
|
18 |
+
'outtmpl': 'audio'
|
19 |
+
}
|
20 |
+
|
21 |
+
def download_video(url):
|
22 |
+
with YoutubeDL(ydl_opts) as ydl:
|
23 |
+
print(url)
|
24 |
+
error_code = ydl.download([url])
|
25 |
+
info = ydl.extract_info(url, download=False)
|
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')
|
32 |
+
st.write('Enter the url of the youtube video you want to download')
|
33 |
+
url = st.text_input('URL')
|
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']
|
42 |
+
|
43 |
+
if error_code:
|
44 |
+
st.error('Error downloading video')
|
45 |
+
else:
|
46 |
+
st.success('Downloaded video')
|
47 |
+
|
48 |
+
if os.path.isfile('audio.mp3'):
|
49 |
+
video_url = st.session_state.get('latest_video', '/')
|
50 |
+
st.write(f"Last downloaded video is: {st.session_state.get('latest_title', '/')} with url {video_url}")
|
51 |
+
st.audio('audio.mp3')
|
52 |
+
buffer = BytesIO()
|
53 |
+
with open('audio.mp3', 'rb') as f:
|
54 |
+
buffer.write(f.read())
|
55 |
+
timestamp = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
|
56 |
+
st.download_button(label='Download mp3', data=buffer.getvalue(), file_name=f'audio_{timestamp}.mp3', mime='audio/mp3')
|
57 |
+
|
58 |
+
if __name__ == '__main__':
|
59 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
yt-dlp==2023.3.4
|
2 |
+
streamlit
|