sudo-soldier commited on
Commit
27009b8
Β·
verified Β·
1 Parent(s): 9588f0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -65
app.py CHANGED
@@ -9,18 +9,15 @@ logging.basicConfig(level=logging.INFO)
9
  DOWNLOAD_FOLDER = './output'
10
  COOKIES_PATH = './cookies.txt'
11
 
12
- # Ensure the download folder exists
13
- os.makedirs(DOWNLOAD_FOLDER, exist_ok=True)
14
 
15
  def create_readme():
16
- readme_path = os.path.join(DOWNLOAD_FOLDER, 'readme.txt')
17
  try:
18
- with open(readme_path, 'w') as readme_file:
19
- readme_file.write(
20
- "Android: copy the .mp3 to the ringtones folder and reboot device.\n"
21
- "iPhone: drag and drop the .m4r file in iTunes and sync device."
22
- )
23
- return f"readme.txt created at: {readme_path}"
24
  except Exception as e:
25
  return f"Failed to create readme.txt: {str(e)}"
26
 
@@ -32,54 +29,54 @@ def get_video_filename():
32
 
33
  def download_video(url):
34
  try:
35
- output_template = os.path.join(DOWNLOAD_FOLDER, '%(title)s.%(ext)s')
36
- command = ['yt-dlp', '-f', 'mp4', '-o', output_template, url]
37
 
38
  if os.path.exists(COOKIES_PATH):
39
- command += ['--cookies', COOKIES_PATH]
40
  else:
41
  return f"Error: Cookies file {COOKIES_PATH} not found."
42
 
43
- logging.info(f"Running command: {' '.join(command)}")
44
  process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
45
  stdout, stderr = process.communicate()
46
 
47
  logging.info(stdout.decode())
48
- if process.returncode != 0:
49
- return f"Error downloading video: {stderr.decode()}"
50
-
51
- video_filename = get_video_filename()
52
- if not video_filename:
53
- return "Error: Downloaded video file not found."
54
-
55
- return f"Video downloaded: {video_filename}\n" + create_readme()
56
 
 
 
 
 
 
 
 
 
57
  except Exception as e:
58
- return f"Exception during video download: {str(e)}"
59
-
60
  def android_download():
61
  try:
62
- video_filename = get_video_filename(f'{DOWNLOAD_FOLDER}/%(title)s.mp4')
63
- output_file = f'{DOWNLOAD_FOLDER}/AndroidRingtone.mp3'
64
- if video_filename:
65
- command = ['ffmpeg', '-i', video_filename, '-t', '20', '-q:a', '0', '-map', 'a', output_file]
66
- process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
67
- stdout, stderr = process.communicate()
68
-
69
- if process.returncode == 0:
70
- return "Android ringtone created successfully", output_file
71
- else:
72
- return f"Error creating Android ringtone: {stderr.decode()}", None
73
  else:
74
- return "Error: Video file not found for Android ringtone.", None
75
  except Exception as e:
76
- return f"Failed to create Android ringtone: {str(e)}", None
77
-
78
 
79
  def iphone_download():
80
  try:
81
- input_file = f'{DOWNLOAD_FOLDER}/AndroidRingtone.mp3'
82
- output_file = f'{DOWNLOAD_FOLDER}/iPhoneRingtone.m4r'
83
  command = ['ffmpeg', '-i', input_file, '-t', '20', '-acodec', 'aac', '-b:a', '128k', '-f', 'mp4', output_file]
84
  process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
85
  stdout, stderr = process.communicate()
@@ -87,46 +84,40 @@ def iphone_download():
87
  if process.returncode == 0:
88
  return "iPhone ringtone created successfully", output_file
89
  else:
90
- return f"Error creating iPhone ringtone: {stderr.decode()}", None
91
  except Exception as e:
92
- return f"Failed to create iPhone ringtone: {str(e)}", None
93
-
94
 
95
  def paste_from_clipboard():
96
- try:
97
- return pyperclip.paste()
98
- except Exception as e:
99
- return f"Clipboard error: {str(e)}"
100
 
101
  def clear_input():
102
- return "", ""
103
-
104
- # Gradio UI
105
 
106
  with gr.Blocks() as iface:
107
- gr.Markdown("## 🎡 YouTube Ringtone Creator")
108
- url_input = gr.Textbox(label="Enter YouTube URL", placeholder="Paste URL here")
109
- status_output = gr.Textbox(label="Status", interactive=False)
110
-
111
- android_download_link = gr.File(label="Android Ringtone", interactive=False)
112
- iphone_download_link = gr.File(label="iPhone Ringtone", interactive=False)
113
 
 
 
114
 
115
- android_file = gr.File(label="Download Android Ringtone (.mp3)")
116
- iphone_file = gr.File(label="Download iPhone Ringtone (.m4r)")
 
 
117
 
 
 
 
118
 
119
- download_button = gr.Button("πŸ“₯ Download Video")
120
- android_button = gr.Button("🎢 Create Android Ringtone")
121
- iphone_button = gr.Button("πŸ“± Create iPhone Ringtone")
122
- paste_button = gr.Button("πŸ“‹ Paste URL from Clipboard")
123
- clear_button = gr.Button("🧹 Clear")
124
 
125
- download_button.click(fn=download_video, inputs=url_input, outputs=status_output)
126
  android_button.click(android_download, outputs=[status_output, android_file])
127
  iphone_button.click(iphone_download, outputs=[status_output, iphone_file])
128
- paste_button.click(fn=paste_from_clipboard, outputs=url_input)
129
- clear_button.click(fn=clear_input, outputs=[url_input, status_output])
130
 
131
  iface.launch(share=True)
132
 
@@ -136,6 +127,7 @@ iface.launch(share=True)
136
 
137
 
138
 
 
139
 
140
 
141
 
 
9
  DOWNLOAD_FOLDER = './output'
10
  COOKIES_PATH = './cookies.txt'
11
 
12
+ if not os.path.exists(DOWNLOAD_FOLDER):
13
+ os.makedirs(DOWNLOAD_FOLDER)
14
 
15
  def create_readme():
16
+ path = os.path.join(DOWNLOAD_FOLDER, 'readme.txt')
17
  try:
18
+ with open(path, 'w') as f:
19
+ f.write("Android: Copy .mp3 to ringtones folder and reboot. iPhone: Drag .m4r into iTunes and sync.")
20
+ return f"readme.txt created at: {path}"
 
 
 
21
  except Exception as e:
22
  return f"Failed to create readme.txt: {str(e)}"
23
 
 
29
 
30
  def download_video(url):
31
  try:
32
+ output_path = os.path.join(DOWNLOAD_FOLDER, '%(title)s.%(ext)s')
33
+ command = ['yt-dlp', '-f', 'mp4', '-o', output_path, url]
34
 
35
  if os.path.exists(COOKIES_PATH):
36
+ command.extend(['--cookies', COOKIES_PATH])
37
  else:
38
  return f"Error: Cookies file {COOKIES_PATH} not found."
39
 
40
+ logging.info(f"Running: {' '.join(command)}")
41
  process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
42
  stdout, stderr = process.communicate()
43
 
44
  logging.info(stdout.decode())
45
+ logging.error(stderr.decode())
 
 
 
 
 
 
 
46
 
47
+ if process.returncode == 0:
48
+ video_file = get_video_filename()
49
+ if video_file:
50
+ return f"Video downloaded: {os.path.basename(video_file)}\n" + create_readme()
51
+ else:
52
+ return "Error: Video file not found."
53
+ else:
54
+ return f"Download error: {stderr.decode()}"
55
  except Exception as e:
56
+ return f"Failed to download video: {str(e)}"
57
+
58
  def android_download():
59
  try:
60
+ video_file = get_video_filename()
61
+ if not video_file:
62
+ return "Error: No video file found.", None
63
+
64
+ output_file = os.path.join(DOWNLOAD_FOLDER, 'AndroidRingtone.mp3')
65
+ command = ['ffmpeg', '-i', video_file, '-t', '20', '-q:a', '0', '-map', 'a', output_file]
66
+ process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
67
+ stdout, stderr = process.communicate()
68
+
69
+ if process.returncode == 0:
70
+ return "Android ringtone created successfully", output_file
71
  else:
72
+ return f"FFmpeg error: {stderr.decode()}", None
73
  except Exception as e:
74
+ return f"Error creating Android ringtone: {str(e)}", None
 
75
 
76
  def iphone_download():
77
  try:
78
+ input_file = os.path.join(DOWNLOAD_FOLDER, 'AndroidRingtone.mp3')
79
+ output_file = os.path.join(DOWNLOAD_FOLDER, 'iPhoneRingtone.m4r')
80
  command = ['ffmpeg', '-i', input_file, '-t', '20', '-acodec', 'aac', '-b:a', '128k', '-f', 'mp4', output_file]
81
  process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
82
  stdout, stderr = process.communicate()
 
84
  if process.returncode == 0:
85
  return "iPhone ringtone created successfully", output_file
86
  else:
87
+ return f"FFmpeg error: {stderr.decode()}", None
88
  except Exception as e:
89
+ return f"Error creating iPhone ringtone: {str(e)}", None
 
90
 
91
  def paste_from_clipboard():
92
+ return pyperclip.paste()
 
 
 
93
 
94
  def clear_input():
95
+ return "", "", None, None
 
 
96
 
97
  with gr.Blocks() as iface:
98
+ gr.Markdown("# 🎡 YouTube to Ringtone Converter")
 
 
 
 
 
99
 
100
+ url_input = gr.Textbox(label="YouTube URL", placeholder="Paste URL here")
101
+ status_output = gr.Textbox(label="Status", interactive=False)
102
 
103
+ with gr.Row():
104
+ download_button = gr.Button("πŸ“₯ Download Video")
105
+ android_button = gr.Button("πŸ“± Android Ringtone")
106
+ iphone_button = gr.Button("🍏 iPhone Ringtone")
107
 
108
+ with gr.Row():
109
+ paste_button = gr.Button("πŸ“‹ Paste from Clipboard")
110
+ clear_button = gr.Button("🧹 Clear")
111
 
112
+ with gr.Row():
113
+ android_file = gr.File(label="⬇️ Android Ringtone (.mp3)", interactive=False)
114
+ iphone_file = gr.File(label="⬇️ iPhone Ringtone (.m4r)", interactive=False)
 
 
115
 
116
+ download_button.click(download_video, inputs=url_input, outputs=status_output)
117
  android_button.click(android_download, outputs=[status_output, android_file])
118
  iphone_button.click(iphone_download, outputs=[status_output, iphone_file])
119
+ paste_button.click(paste_from_clipboard, outputs=url_input)
120
+ clear_button.click(clear_input, outputs=[url_input, status_output, android_file, iphone_file])
121
 
122
  iface.launch(share=True)
123
 
 
127
 
128
 
129
 
130
+
131
 
132
 
133