File size: 5,298 Bytes
147c80a
1c67333
0849a69
 
 
105e5dd
0849a69
105e5dd
ebff356
f5d080d
 
ebff356
f5d080d
 
105e5dd
ebff356
 
 
0849a69
 
 
 
105e5dd
0849a69
 
 
 
 
 
ebff356
0849a69
 
 
fe3623b
ae98033
ebff356
 
 
 
ae98033
7a5da51
 
fe3623b
0849a69
 
 
7a5da51
 
 
 
0849a69
 
 
 
 
 
6542b78
0849a69
 
 
105e5dd
0849a69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5d080d
0849a69
 
 
 
 
 
 
105e5dd
0849a69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebff356
c3db4af
ebff356
c3db4af
 
ebff356
c3db4af
 
ebff356
c3db4af
 
 
 
 
 
ebff356
 
c3db4af
 
 
 
 
ebff356
0849a69
 
c846fe2
 
b52d3e9
 
7a5da51
b52d3e9
ae98033
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import gradio as gr
import subprocess
import os
import logging
import pyperclip

logging.basicConfig(level=logging.INFO)

# Use the current working directory (Hugging Face compatible)
DOWNLOAD_FOLDER = './output'

# Ensure the directory exists
if not os.path.exists(DOWNLOAD_FOLDER):
    os.makedirs(DOWNLOAD_FOLDER)

# Specify the path for the cookies file (assuming it's in the same directory as this script)
COOKIES_PATH = './cookies.txt'

def create_readme():
    """Creates a readme.txt file with the specified text"""
    readme_path = os.path.join(DOWNLOAD_FOLDER, 'readme.txt')
    
    try:
        with open(readme_path, 'w') as readme_file:
            readme_file.write("Android copy the .mp3 to the ringtones folder and reboot device. iPhone drag and drop the .m4r file in iTunes and sync device.")
        return f"readme.txt created at: {readme_path}"
    except Exception as e:
        return f"Failed to create readme.txt: {str(e)}"

def download_video(url):
    try:
        output_path = os.path.join(DOWNLOAD_FOLDER, '%(title)s.mp4')
        command = ['yt-dlp', '-f', 'mp4', '-o', output_path, url]

        # Add cookies to the yt-dlp command if the cookies.txt exists
        if os.path.exists(COOKIES_PATH):
            command.extend(['--cookies', COOKIES_PATH])
        else:
            return f"Error: Cookies file {COOKIES_PATH} not found."

        # Debugging: Print the command to see if cookies are added correctly
        logging.info(f"Running command: {' '.join(command)}")

        process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdout, stderr = process.communicate()

        # Debugging: Print the output to help identify the error
        logging.info(f"STDOUT: {stdout.decode()}")
        logging.error(f"STDERR: {stderr.decode()}")

        if process.returncode == 0:
            video_filename = get_video_filename(output_path)
            if video_filename:
                return f"Video downloaded: {video_filename}\n" + create_readme()
            else:
                return "Error: Video file not found."
        else:
            return f"Error downloading video: {stderr.decode()}"
    except Exception as e:
        return f"Failed to download video: {str(e)}"

def android_download():
    try:
        video_filename = get_video_filename(f'{DOWNLOAD_FOLDER}/%(title)s.mp4')
        if video_filename:
            command = ['ffmpeg', '-i', video_filename, '-t', '20', '-q:a', '0', '-map', 'a', f'{DOWNLOAD_FOLDER}/AndroidRingtone.mp3']
            process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            stdout, stderr = process.communicate()

            if process.returncode == 0:
                return "Android ringtone created successfully"
            else:
                return f"Error creating Android ringtone: {stderr.decode()}"
        else:
            return "Error: Video file not found for Android ringtone."
    except Exception as e:
        return f"Failed to create Android ringtone: {str(e)}"

def iphone_download():
    try:
        command = ['ffmpeg', '-i', f'{DOWNLOAD_FOLDER}/AndroidRingtone.mp3', '-t', '20', '-acodec', 'aac', '-b:a', '128k', '-f', 'mp4', f'{DOWNLOAD_FOLDER}/iPhoneRingtone.m4r']
        process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdout, stderr = process.communicate()

        if process.returncode == 0:
            return "iPhone ringtone created successfully"
        else:
            return f"Error creating iPhone ringtone: {stderr.decode()}"
    except Exception as e:
        return f"Failed to create iPhone ringtone: {str(e)}"

def get_video_filename(output_path):
    """Finds and returns the actual downloaded filename"""
    title = os.path.basename(output_path).replace('%(title)s', '').strip()
    video_filename = os.path.join(DOWNLOAD_FOLDER, f"{title}.mp4")

    if os.path.exists(video_filename):
        return video_filename
    else:
        for file in os.listdir(DOWNLOAD_FOLDER):
            if file.endswith(".mp4"):
                return os.path.join(DOWNLOAD_FOLDER, file)

    return None

def paste_from_clipboard():
    """Get URL from clipboard"""
    return pyperclip.paste()

def clear_input():
    """Clear the input and output"""
    return "", ""

# Create the Gradio interface
with gr.Blocks() as iface:
    # Input for URL
    url_input = gr.Textbox(label="Enter YouTube URL", placeholder="Paste URL here")

    # Output for status
    status_output = gr.Textbox(label="Status", interactive=False)

    # Buttons for actions
    download_button = gr.Button("Download Video")
    android_button = gr.Button("Create Android Ringtone")
    iphone_button = gr.Button("Create iPhone Ringtone")
    paste_button = gr.Button("Paste URL from Clipboard")
    clear_button = gr.Button("Clear")

    # Button click actions
    download_button.click(download_video, inputs=url_input, outputs=status_output)
    android_button.click(android_download, outputs=status_output)
    iphone_button.click(iphone_download, outputs=status_output)
    paste_button.click(paste_from_clipboard, outputs=url_input)
    clear_button.click(clear_input, outputs=[url_input, status_output])

# Launch the interface
iface.launch(share=True)