File size: 2,152 Bytes
ab28a19
 
 
 
 
e3aff9f
ab28a19
118a5f9
 
ab28a19
 
 
 
ff5d792
ab28a19
 
 
 
118a5f9
ab28a19
 
 
 
 
 
e3aff9f
ab28a19
118a5f9
e3aff9f
118a5f9
e3aff9f
118a5f9
 
 
e3aff9f
118a5f9
 
ab28a19
e3aff9f
ab28a19
 
e3aff9f
ab28a19
e3aff9f
 
ab28a19
 
16e732a
3c7f2ec
e3aff9f
16e732a
ab28a19
 
e3aff9f
ab28a19
e3aff9f
ab28a19
 
e3aff9f
 
ab28a19
118a5f9
e3aff9f
ab28a19
 
 
 
 
 
 
 
 
fcdb38d
16e732a
ff5d792
271a312
e3aff9f
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
import gradio as gr
import yt_dlp
import os
from pydub import AudioSegment

def process_youtube_url(url, uploaded_file, state):
    try:
        filename = None
        
        if url:
            ydl_opts = {
                'format': 'bestaudio/best',
                'outtmpl': 'downloads/%(id)s.%(ext)s',
                'cookiefile': 'cookies.txt'  # YT
            }

            with yt_dlp.YoutubeDL(ydl_opts) as ydl:
                info = ydl.extract_info(url, download=True)
            
            filename = os.path.join('downloads', f"{info['id']}.webm")

        elif uploaded_file:
            filename = uploaded_file.name

        else:
            raise ValueError("No YouTube URL or MP3 file provided.")

        if not os.path.exists(filename):
            raise FileNotFoundError(f"File not found: {filename}")

        mp3_filename = 'Ringtone.mp3'
        audio = AudioSegment.from_file(filename)
        audio.export(mp3_filename, format="mp3")

        ringtone_filename_m4a = 'temp_ringtone.m4a'
        ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000]  # 20 sec clip
        ringtone_audio.export(ringtone_filename_m4a, format="mp4")

        ringtone_filename_m4r = 'Apple.m4r'
        os.rename(ringtone_filename_m4a, ringtone_filename_m4r)

        return mp3_filename, ringtone_filename_m4r, state

    except Exception:
        return None, None, state

with gr.Blocks() as interface:
    gr.HTML("""
        <h1>Python YouTube Ringtones</h1>
        <p>Insert a URL to make ringtones or Upload an MP3 to convert</p>
    """)
    
    with gr.Row():
        youtube_url = gr.Textbox(label="Enter YouTube URL ", placeholder="Paste the URL here...")
        mp3_upload = gr.File(label="Upload MP3 (Optional)", type="filepath")
        state = gr.State()
    
    with gr.Row():
        mp3_download = gr.File(label="Android Ringtone")
        iphone_ringtone = gr.File(label="iPhone Ringtone")

    process_button = gr.Button("Create Ringtones")
    process_button.click(process_youtube_url, inputs=[youtube_url, mp3_upload, state], outputs=[mp3_download, iphone_ringtone])

interface.launch()