File size: 2,748 Bytes
a148336
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3df1079
1ac0332
 
da0725d
ab28a19
136c2ab
 
4bb038f
ab28a19
a148336
ab28a19
16e732a
a148336
 
19487f1
16e732a
3df1079
ab28a19
a148336
 
 
 
 
 
136c2ab
ab28a19
fc41fb4
 
ab28a19
a148336
19487f1
ab28a19
1ac0332
 
ab28a19
 
 
 
 
 
 
fcdb38d
16e732a
ff5d792
271a312
e3aff9f
136c2ab
fc41fb4
5ba25ef
4bb038f
19487f1
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
83
84
85
86
87
88
89
90
91
92
from pydub import AudioSegment
import re

os.makedirs("downloads", exist_ok=True)

def sanitize_filename(filename):
    """Sanitize the filename by removing or replacing special characters."""
    return re.sub(r'[^a-zA-Z0-9_-]', '_', filename)

def process_youtube_or_audio(url, recorded_audio):
    """Process YouTube URL or recorded audio to create ringtones."""
    try:
        filename = None
        song_name = None

        if url:
            ydl_opts = {
                'format': 'bestaudio/best',
                'outtmpl': 'downloads/%(id)s.%(ext)s',
                'cookiefile': 'cookies.txt'
            }
            with yt_dlp.YoutubeDL(ydl_opts) as ydl:
                info = ydl.extract_info(url, download=True)
                filename = os.path.join('downloads', f"{info['id']}.webm")
                song_name = sanitize_filename(info['title'])

        elif recorded_audio:
            filename = recorded_audio
            song_name = "recorded_audio"
        
        if not filename or not os.path.exists(filename):
            return None, None

        mp3_filename = f"downloads/{song_name}.mp3"
        if not os.path.exists(mp3_filename):
            audio = AudioSegment.from_file(filename)
            audio.export(mp3_filename, format="mp3")

        ringtone_filename_m4r = f"downloads/{song_name}.m4r"
        if not os.path.exists(ringtone_filename_m4r):
            ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000]  # 20 seconds
            ringtone_audio.export(ringtone_filename_m4r, format="mp4")

        return mp3_filename, ringtone_filename_m4r

    except Exception as e:
        print(f"Error: {e}")
        return None, None

# Gradio UI with FontAwesome
with gr.Blocks() as interface:
    gr.HTML("""
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
        <h1><i class="fa fa-music"></i> Python YouTube Ringtones</h1>
        <p>Insert a URL to create ringtones or record your own audio.</p>
    """)

    with gr.Row():
        gr.HTML('<p><i class="fa fa-link"></i> Enter YouTube URL:</p>')
        youtube_url = gr.Textbox(placeholder="Paste the URL here...")

    with gr.Row():
        gr.HTML('<p><i class="fa fa-microphone"></i> Record Audio:</p>')
        audio_record = gr.Audio(sources=["microphone"], type="filepath")

    with gr.Row():
        mp3_download = gr.File(label="Download Android Ringtone")
        iphone_ringtone = gr.File(label="Download iPhone Ringtone")

    process_button = gr.Button("🎵 Create Ringtones")
    process_button.click(process_youtube_or_audio, inputs=[youtube_url, audio_record], outputs=[mp3_download, iphone_ringtone])

interface.launch(share=True)