Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| from pytube import YouTube | |
| import subprocess | |
| import os | |
| def download_youtube_video(url): | |
| yt = YouTube(url) | |
| video = yt.streams.filter(only_audio=True).first() | |
| video.download() | |
| def convert_to_mp3(video_file): | |
| audio_file = video_file.replace(".webm", ".mp3").replace(".mkv", ".mp3") | |
| subprocess.run(['ffmpeg', '-i', video_file, '-vn', '-acodec', 'libmp3lame', audio_file]) | |
| return audio_file | |
| def main(): | |
| st.title('YouTube Video to MP3 Converter') | |
| youtube_url = st.text_input('Enter YouTube video URL:') | |
| if st.button('Convert to MP3'): | |
| if youtube_url: | |
| st.text('Downloading video...') | |
| download_youtube_video(youtube_url) | |
| video_filename = YouTube(youtube_url).title + ".mp4" | |
| st.text('Converting to MP3...') | |
| mp3_file = convert_to_mp3(video_filename) | |
| st.success(f"Conversion completed! MP3 file saved as: {mp3_file}") | |
| st.text('Download your file below:') | |
| if os.path.exists(mp3_file): | |
| with open(mp3_file, 'rb') as f: | |
| data = f.read() | |
| st.download_button(label='Click here to download', data=data, file_name='converted_audio.mp3', mime='audio/mp3') | |
| if __name__ == "__main__": | |
| main() | |