File size: 3,696 Bytes
2c19cb5
105e5dd
 
 
 
dd81a22
105e5dd
a58d3c7
105e5dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd81a22
 
 
 
105e5dd
dd81a22
 
 
 
 
 
 
105e5dd
 
 
 
 
a58d3c7
a3cf940
 
a58d3c7
a3cf940
 
105e5dd
 
2a7f166
105e5dd
 
 
 
 
 
 
 
 
 
 
 
 
a58d3c7
105e5dd
 
 
 
126430d
105e5dd
 
 
126430d
105e5dd
 
 
2c19cb5
a3cf940
a58d3c7
dd81a22
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
93
94
95
96
97
98
99
100
101
import os
import gradio as gr
import yt_dlp
from pydub import AudioSegment
import re
import subprocess

# Create downloads directory safely
if not os.path.exists("downloads"):
    os.makedirs("downloads")

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

        # Use ffmpeg to convert mp3 to m4a
        m4a_filename = f"downloads/{song_name}.m4a"
        if not os.path.exists(m4a_filename):
            subprocess.run(['ffmpeg', '-i', mp3_filename, '-vn', '-acodec', 'aac', '-b:a', '192k', m4a_filename])

        # Trim the first 20 seconds
        m4r_filename = f"downloads/{song_name}.m4r"
        if not os.path.exists(m4r_filename):
            # Rename the .m4a to .m4r
            os.rename(m4a_filename, m4r_filename)

        return mp3_filename, m4r_filename

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

# Gradio UI with Improved Layout
with gr.Blocks(css="""
    body { font-family: Arial, sans-serif; text-align: center; }
    .light-btn { background-color: #f0f0f0; color: #333; border: 2px solid #ccc; padding: 10px 20px; font-size: 16px; cursor: pointer; }
    .light-btn:hover { background-color: #e0e0e0; }
""") 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>&nbsp;PYTRR</h1>
        <p>Enter a YouTube URL or record your own audio to create a ringtone.</p>
    """)

    with gr.Row():
        with gr.Column(scale=1, min_width=250):
            gr.HTML('<label><i class="fa fa-link"></i> YouTube URL</label>')
            youtube_url = gr.Textbox(placeholder="Paste YouTube URL here...")

        with gr.Column(scale=1, min_width=250):
            gr.HTML('<label><i class="fa fa-microphone"></i> Record Audio</label>')
            audio_record = gr.Audio(sources=["microphone"], type="filepath")

    with gr.Row():
        process_button = gr.Button("🎵 Create Ringtones", elem_classes="light-btn")

    with gr.Row():
        with gr.Column(scale=1, min_width=250):
            gr.HTML('<label><i class="fa fa-android"></i> Android Ringtone</label>')
            mp3_download = gr.File(label="MP3")

        with gr.Column(scale=1, min_width=250):
            gr.HTML('<label><i class="fa fa-apple"></i> iPhone Ringtone</label>')
            iphone_ringtone = gr.File(label="M4R")

    process_button.click(process_youtube_or_audio, inputs=[youtube_url, audio_record], outputs=[mp3_download, iphone_ringtone])

interface.launch(share=True)