sudo-soldier commited on
Commit
493b19b
·
verified ·
1 Parent(s): 8483228

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -41
app.py CHANGED
@@ -6,23 +6,21 @@ import pyperclip
6
 
7
  logging.basicConfig(level=logging.INFO)
8
 
9
- # Use the current working directory (Hugging Face compatible)
10
  DOWNLOAD_FOLDER = './output'
 
11
 
12
- # Ensure the directory exists
13
  if not os.path.exists(DOWNLOAD_FOLDER):
14
  os.makedirs(DOWNLOAD_FOLDER)
15
 
16
- # Specify the path for the cookies file (assuming it's in the same directory as this script)
17
- COOKIES_PATH = './cookies.txt'
18
-
19
  def create_readme():
20
- """Creates a readme.txt file with the specified text"""
21
  readme_path = os.path.join(DOWNLOAD_FOLDER, 'readme.txt')
22
-
23
  try:
24
  with open(readme_path, 'w') as readme_file:
25
- 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.")
 
 
 
26
  return f"readme.txt created at: {readme_path}"
27
  except Exception as e:
28
  return f"Failed to create readme.txt: {str(e)}"
@@ -32,19 +30,16 @@ def download_video(url):
32
  output_path = os.path.join(DOWNLOAD_FOLDER, '%(title)s.mp4')
33
  command = ['yt-dlp', '-f', 'mp4', '-o', output_path, url]
34
 
35
- # Add cookies to the yt-dlp command if the cookies.txt exists
36
  if os.path.exists(COOKIES_PATH):
37
  command.extend(['--cookies', COOKIES_PATH])
38
  else:
39
  return f"Error: Cookies file {COOKIES_PATH} not found."
40
 
41
- # Debugging: Print the command to see if cookies are added correctly
42
  logging.info(f"Running command: {' '.join(command)}")
43
 
44
  process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
45
  stdout, stderr = process.communicate()
46
 
47
- # Debugging: Print the output to help identify the error
48
  logging.info(f"STDOUT: {stdout.decode()}")
49
  logging.error(f"STDERR: {stderr.decode()}")
50
 
@@ -63,44 +58,47 @@ def android_download():
63
  try:
64
  video_filename = get_video_filename(f'{DOWNLOAD_FOLDER}/%(title)s.mp4')
65
  if video_filename:
66
- command = ['ffmpeg', '-i', video_filename, '-t', '20', '-q:a', '0', '-map', 'a', f'{DOWNLOAD_FOLDER}/AndroidRingtone.mp3']
 
67
  process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
68
- stdout, stderr = process.communicate()
69
 
70
  if process.returncode == 0:
71
- return "Android ringtone created successfully"
72
  else:
73
- return f"Error creating Android ringtone: {stderr.decode()}"
74
  else:
75
- return "Error: Video file not found for Android ringtone."
76
  except Exception as e:
77
- return f"Failed to create Android ringtone: {str(e)}"
78
 
79
  def iphone_download():
80
  try:
81
- command = ['ffmpeg', '-i', f'{DOWNLOAD_FOLDER}/AndroidRingtone.mp3', '-t', '20', '-acodec', 'aac', '-b:a', '128k', '-f', 'mp4', f'{DOWNLOAD_FOLDER}/iPhoneRingtone.m4r']
 
 
 
 
 
 
 
 
 
82
  process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
83
- stdout, stderr = process.communicate()
84
 
85
  if process.returncode == 0:
86
- return "iPhone ringtone created successfully"
87
  else:
88
- return f"Error creating iPhone ringtone: {stderr.decode()}"
89
  except Exception as e:
90
- return f"Failed to create iPhone ringtone: {str(e)}"
91
 
92
  def get_video_filename(output_path):
93
  """Finds and returns the actual downloaded filename"""
94
- title = os.path.basename(output_path).replace('%(title)s', '').strip()
95
- video_filename = os.path.join(DOWNLOAD_FOLDER, f"{title}.mp4")
96
-
97
- if os.path.exists(video_filename):
98
- return video_filename
99
- else:
100
- for file in os.listdir(DOWNLOAD_FOLDER):
101
- if file.endswith(".mp4"):
102
- return os.path.join(DOWNLOAD_FOLDER, file)
103
-
104
  return None
105
 
106
  def paste_from_clipboard():
@@ -111,35 +109,34 @@ def clear_input():
111
  """Clear the input and output"""
112
  return "", ""
113
 
114
- # Create the Gradio interface
115
  with gr.Blocks() as iface:
116
- # Input for URL
117
  url_input = gr.Textbox(label="Enter YouTube URL", placeholder="Paste URL here")
118
-
119
- # Output for status
120
  status_output = gr.Textbox(label="Status", interactive=False)
121
 
122
- # Buttons for actions
 
 
123
  download_button = gr.Button("Download Video")
124
  android_button = gr.Button("Create Android Ringtone")
125
  iphone_button = gr.Button("Create iPhone Ringtone")
126
  paste_button = gr.Button("Paste URL from Clipboard")
127
  clear_button = gr.Button("Clear")
128
 
129
- # Button click actions
130
  download_button.click(download_video, inputs=url_input, outputs=status_output)
131
- android_button.click(android_download, outputs=status_output)
132
- iphone_button.click(iphone_download, outputs=status_output)
133
  paste_button.click(paste_from_clipboard, outputs=url_input)
134
  clear_button.click(clear_input, outputs=[url_input, status_output])
135
 
136
- # Launch the interface
137
  iface.launch(share=True)
138
 
139
 
140
 
141
 
142
 
 
143
 
144
 
145
 
 
6
 
7
  logging.basicConfig(level=logging.INFO)
8
 
 
9
  DOWNLOAD_FOLDER = './output'
10
+ COOKIES_PATH = './cookies.txt'
11
 
12
+ # Ensure the download folder exists
13
  if not os.path.exists(DOWNLOAD_FOLDER):
14
  os.makedirs(DOWNLOAD_FOLDER)
15
 
 
 
 
16
  def create_readme():
 
17
  readme_path = os.path.join(DOWNLOAD_FOLDER, 'readme.txt')
 
18
  try:
19
  with open(readme_path, 'w') as readme_file:
20
+ readme_file.write(
21
+ "Android: copy the .mp3 to the ringtones folder and reboot device.\n"
22
+ "iPhone: drag and drop the .m4r file in iTunes and sync device."
23
+ )
24
  return f"readme.txt created at: {readme_path}"
25
  except Exception as e:
26
  return f"Failed to create readme.txt: {str(e)}"
 
30
  output_path = os.path.join(DOWNLOAD_FOLDER, '%(title)s.mp4')
31
  command = ['yt-dlp', '-f', 'mp4', '-o', output_path, url]
32
 
 
33
  if os.path.exists(COOKIES_PATH):
34
  command.extend(['--cookies', COOKIES_PATH])
35
  else:
36
  return f"Error: Cookies file {COOKIES_PATH} not found."
37
 
 
38
  logging.info(f"Running command: {' '.join(command)}")
39
 
40
  process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
41
  stdout, stderr = process.communicate()
42
 
 
43
  logging.info(f"STDOUT: {stdout.decode()}")
44
  logging.error(f"STDERR: {stderr.decode()}")
45
 
 
58
  try:
59
  video_filename = get_video_filename(f'{DOWNLOAD_FOLDER}/%(title)s.mp4')
60
  if video_filename:
61
+ android_path = f'{DOWNLOAD_FOLDER}/AndroidRingtone.mp3'
62
+ command = ['ffmpeg', '-i', video_filename, '-t', '20', '-q:a', '0', '-map', 'a', android_path]
63
  process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
64
+ process.communicate()
65
 
66
  if process.returncode == 0:
67
+ return "Android ringtone created successfully", android_path
68
  else:
69
+ return f"Error creating Android ringtone: {process.stderr.read().decode()}", None
70
  else:
71
+ return "Error: Video file not found for Android ringtone.", None
72
  except Exception as e:
73
+ return f"Failed to create Android ringtone: {str(e)}", None
74
 
75
  def iphone_download():
76
  try:
77
+ iphone_path = f'{DOWNLOAD_FOLDER}/iPhoneRingtone.m4r'
78
+ command = [
79
+ 'ffmpeg',
80
+ '-i', f'{DOWNLOAD_FOLDER}/AndroidRingtone.mp3',
81
+ '-t', '20',
82
+ '-acodec', 'aac',
83
+ '-b:a', '128k',
84
+ '-f', 'mp4',
85
+ iphone_path
86
+ ]
87
  process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
88
+ process.communicate()
89
 
90
  if process.returncode == 0:
91
+ return "iPhone ringtone created successfully", iphone_path
92
  else:
93
+ return f"Error creating iPhone ringtone: {process.stderr.read().decode()}", None
94
  except Exception as e:
95
+ return f"Failed to create iPhone ringtone: {str(e)}", None
96
 
97
  def get_video_filename(output_path):
98
  """Finds and returns the actual downloaded filename"""
99
+ for file in os.listdir(DOWNLOAD_FOLDER):
100
+ if file.endswith(".mp4"):
101
+ return os.path.join(DOWNLOAD_FOLDER, file)
 
 
 
 
 
 
 
102
  return None
103
 
104
  def paste_from_clipboard():
 
109
  """Clear the input and output"""
110
  return "", ""
111
 
112
+ # Gradio UI
113
  with gr.Blocks() as iface:
114
+ gr.Markdown("## 🎵 YouTube Ringtone Creator")
115
  url_input = gr.Textbox(label="Enter YouTube URL", placeholder="Paste URL here")
 
 
116
  status_output = gr.Textbox(label="Status", interactive=False)
117
 
118
+ android_download_link = gr.File(label="Download Android Ringtone", interactive=False)
119
+ iphone_download_link = gr.File(label="Download iPhone Ringtone", interactive=False)
120
+
121
  download_button = gr.Button("Download Video")
122
  android_button = gr.Button("Create Android Ringtone")
123
  iphone_button = gr.Button("Create iPhone Ringtone")
124
  paste_button = gr.Button("Paste URL from Clipboard")
125
  clear_button = gr.Button("Clear")
126
 
 
127
  download_button.click(download_video, inputs=url_input, outputs=status_output)
128
+ android_button.click(android_download, outputs=[status_output, android_download_link])
129
+ iphone_button.click(iphone_download, outputs=[status_output, iphone_download_link])
130
  paste_button.click(paste_from_clipboard, outputs=url_input)
131
  clear_button.click(clear_input, outputs=[url_input, status_output])
132
 
 
133
  iface.launch(share=True)
134
 
135
 
136
 
137
 
138
 
139
+
140
 
141
 
142