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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -65
app.py CHANGED
@@ -10,8 +10,7 @@ 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')
@@ -25,88 +24,86 @@ def create_readme():
25
  except Exception as e:
26
  return f"Failed to create readme.txt: {str(e)}"
27
 
 
 
 
 
 
 
28
  def download_video(url):
29
  try:
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
-
46
- if process.returncode == 0:
47
- video_filename = get_video_filename(output_path)
48
- if video_filename:
49
- return f"Video downloaded: {video_filename}\n" + create_readme()
50
- else:
51
- return "Error: Video file not found."
52
- else:
53
  return f"Error downloading video: {stderr.decode()}"
 
 
 
 
 
 
 
54
  except Exception as e:
55
- return f"Failed to download video: {str(e)}"
56
 
57
  def android_download():
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():
105
- """Get URL from clipboard"""
106
- return pyperclip.paste()
 
 
107
 
108
  def clear_input():
109
- """Clear the input and output"""
110
  return "", ""
111
 
112
  # Gradio UI
@@ -115,20 +112,20 @@ with gr.Blocks() as iface:
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
 
@@ -137,6 +134,7 @@ iface.launch(share=True)
137
 
138
 
139
 
 
140
 
141
 
142
 
 
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')
 
24
  except Exception as e:
25
  return f"Failed to create readme.txt: {str(e)}"
26
 
27
+ def get_video_filename():
28
+ for file in os.listdir(DOWNLOAD_FOLDER):
29
+ if file.endswith(".mp4"):
30
+ return os.path.join(DOWNLOAD_FOLDER, file)
31
+ return None
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()
63
+ if not video_filename:
 
 
 
 
 
 
 
 
 
 
64
  return "Error: Video file not found for Android ringtone.", None
65
+
66
+ android_path = os.path.join(DOWNLOAD_FOLDER, 'AndroidRingtone.mp3')
67
+ command = ['ffmpeg', '-y', '-i', video_filename, '-t', '20', '-q:a', '0', '-map', 'a', android_path]
68
+ process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
69
+ _, stderr = process.communicate()
70
+
71
+ if process.returncode == 0 and os.path.exists(android_path):
72
+ return "Android ringtone created successfully.", android_path
73
+ else:
74
+ return f"Error creating Android ringtone: {stderr.decode()}", None
75
  except Exception as e:
76
+ return f"Exception during Android ringtone creation: {str(e)}", None
77
 
78
  def iphone_download():
79
  try:
80
+ android_path = os.path.join(DOWNLOAD_FOLDER, 'AndroidRingtone.mp3')
81
+ iphone_path = os.path.join(DOWNLOAD_FOLDER, 'iPhoneRingtone.m4r')
82
+
83
+ if not os.path.exists(android_path):
84
+ return "Error: Android ringtone not found. Create it first.", None
85
+
86
  command = [
87
+ 'ffmpeg', '-y', '-i', android_path, '-t', '20',
88
+ '-acodec', 'aac', '-b:a', '128k', '-f', 'ipod', iphone_path
 
 
 
 
 
89
  ]
90
  process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
91
+ _, stderr = process.communicate()
92
 
93
+ if process.returncode == 0 and os.path.exists(iphone_path):
94
+ return "iPhone ringtone created successfully.", iphone_path
95
  else:
96
+ return f"Error creating iPhone ringtone: {stderr.decode()}", None
97
  except Exception as e:
98
+ return f"Exception during iPhone ringtone creation: {str(e)}", None
 
 
 
 
 
 
 
99
 
100
  def paste_from_clipboard():
101
+ try:
102
+ return pyperclip.paste()
103
+ except Exception as e:
104
+ return f"Clipboard error: {str(e)}"
105
 
106
  def clear_input():
 
107
  return "", ""
108
 
109
  # Gradio UI
 
112
  url_input = gr.Textbox(label="Enter YouTube URL", placeholder="Paste URL here")
113
  status_output = gr.Textbox(label="Status", interactive=False)
114
 
115
+ android_download_link = gr.File(label="Android Ringtone", interactive=False)
116
+ iphone_download_link = gr.File(label="iPhone Ringtone", interactive=False)
117
 
118
+ download_button = gr.Button("📥 Download Video")
119
+ android_button = gr.Button("🎶 Create Android Ringtone")
120
+ iphone_button = gr.Button("📱 Create iPhone Ringtone")
121
+ paste_button = gr.Button("📋 Paste URL from Clipboard")
122
+ clear_button = gr.Button("🧹 Clear")
123
 
124
+ download_button.click(fn=download_video, inputs=url_input, outputs=status_output)
125
+ android_button.click(fn=android_download, outputs=[status_output, android_download_link])
126
+ iphone_button.click(fn=iphone_download, outputs=[status_output, iphone_download_link])
127
+ paste_button.click(fn=paste_from_clipboard, outputs=url_input)
128
+ clear_button.click(fn=clear_input, outputs=[url_input, status_output])
129
 
130
  iface.launch(share=True)
131
 
 
134
 
135
 
136
 
137
+
138
 
139
 
140