File size: 3,152 Bytes
7869f71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17e964e
9f4efb0
7869f71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import streamlit as st
from pytube import YouTube
import os
import base64
import time

def get_binary_file_downloader_html(bin_file, file_label='File'):
    with open(bin_file, 'rb') as f:
        data = f.read()
    bin_str = base64.b64encode(data).decode()
    href = f'<a href="data:application/octet-stream;base64,{bin_str}" download="{os.path.basename(bin_file)}">Download {file_label}</a>'
    return href

def on_progress(stream, chunk, bytes_remaining):
    print(' progress:', bytes_remaining, "           ", end='\r', flush=True)

def on_complete(stream, filename):
    print('--- Completed ---')
    print('stream:', stream)
    print('filename:', filename)

def delete_file(filepath):
    if os.path.exists(filepath):
        os.remove(filepath)

def on_download_file_click(filepath):
    # sleep Xsec can delete file
    time.sleep(2)
    delete_file(filepath)

def main():
    # Set the title and background color
    st.title("AIconvert YouTube To MP3 Converter 🎥")
    st.markdown('<style>h1{color: red; text-align: center;}</style>', unsafe_allow_html=True)
    
    # st.subheader('Built with the Llama 2 🦙, Haystack, Streamlit and ❤️')
    # st.markdown('<style>h3{color: pink;  text-align: center;}</style>', unsafe_allow_html=True)

    # Expander for app details
    with st.expander("About the App (Works well on chrome browser)"):
        st.write("This app allows you to download MP3 audio from  YouTube video url.")
        st.write("Enter a YouTube URL in the input box below and click on 'Submit'")

    # Input box for YouTube URL
    youtube_url = st.text_input("Enter YouTube URL")

    # Submit button
    if st.button("Submit") and youtube_url:
        progress_bar = st.progress(0, text='Processing...')
        yt = YouTube(youtube_url)
        yt.register_on_progress_callback(on_progress)
        yt.register_on_complete_callback(on_complete)
        # extract only audio
        video = yt.streams.filter(only_audio=True).first()

        initial_filename = yt.title
        # download the file
        destination = '.' # current folder
        out_file = video.download(output_path=destination, filename=str(initial_filename))

        # save the file
        base, ext = os.path.splitext(out_file)
        new_file = '-'.join(initial_filename.split(' '))[-15:] + '.mp3' # base[0:10] + '.mp3'
        os.rename(out_file, new_file)

        # Display layout with 2 rows
        st.video(youtube_url)
        st.header("Audio file in MP3 format")
        # st.write(new_file)
        # st.markdown(get_binary_file_downloader_html('photo.jpg', 'Picture'), unsafe_allow_html=True)
        # st.markdown(get_binary_file_downloader_html('data.csv', 'My Data'), unsafe_allow_html=True)
        # st.markdown(get_binary_file_downloader_html(new_file, 'Audio'), unsafe_allow_html=True)

        # download audio file
        with open(new_file, 'rb') as f:
            st.download_button('Download Audio', f, file_name=new_file, on_click=on_download_file_click(new_file))
                
        progress_bar.progress(100, text='DONE')
        st.success('Successfull!!')

if __name__ == "__main__":
    main()