File size: 1,296 Bytes
0a96c8a
 
 
0cbe932
0a96c8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7b695e
0cbe932
 
 
 
0a96c8a
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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()