File size: 983 Bytes
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
import streamlit as st
from pytube import YouTube
import subprocess

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}")

if __name__ == "__main__":
    main()