RandomPersonRR commited on
Commit
4280e2f
Β·
verified Β·
1 Parent(s): d188c5a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -44
app.py CHANGED
@@ -34,8 +34,9 @@ async def run_subprocess(cmd, use_fdkaac=False):
34
  print(f"[DEBUG] Command succeeded:\n{stdout.decode()}\n")
35
  return stdout.decode(), stderr.decode()
36
 
37
- async def convert_video_task(input_path, downscale, faster, use_mp3, audio_only, custom_bitrate, video_bitrate):
38
  if use_mp3:
 
39
  output_audio = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.mp3")
40
  ffmpeg_audio_cmd = [
41
  'ffmpeg', '-y', '-i', input_path, '-vn',
@@ -73,24 +74,27 @@ async def convert_video_task(input_path, downscale, faster, use_mp3, audio_only,
73
  video_output = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.mp4")
74
 
75
  # Extract mono WAV audio
 
76
  await run_subprocess([
77
  'ffmpeg', '-y', '-i', input_path, '-ac', '1', '-ar', '8000', audio_wav
78
  ])
79
 
80
  # Compress WAV with fdkaac
 
81
  await run_subprocess([
82
  './fdkaac', '-b', '1k', '-C', '-f', '2', '-G', '1', '-w', '8000',
83
  '-o', audio_output, audio_wav
84
  ], use_fdkaac=True)
85
 
86
  # Compress video without audio
 
87
  video_cmd = [
88
  'ffmpeg', '-y', '-hwaccel', accel, '-i', input_path
89
  ]
90
 
91
  if downscale:
92
  video_cmd += ['-vf', 'scale=-2:144']
93
-
94
  if custom_bitrate:
95
  video_cmd += ['-b:v', f"{int(video_bitrate)}k"]
96
  else:
@@ -102,26 +106,16 @@ async def convert_video_task(input_path, downscale, faster, use_mp3, audio_only,
102
  await run_subprocess(video_cmd)
103
 
104
  if audio_only:
105
- # User wants audio only, no merged video
106
- # Clean intermediates except audio_output
107
- for f in [audio_wav, video_output]:
108
- try:
109
- os.remove(f)
110
- except FileNotFoundError:
111
- pass
112
  return audio_output, None
113
 
114
- # Merge audio + video into final file
 
115
  merged_output = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.mp4")
116
  await run_subprocess([
117
- 'ffmpeg', '-y',
118
- '-i', video_output,
119
- '-i', audio_output,
120
- '-c', 'copy',
121
- merged_output
122
  ])
123
 
124
- # Cleanup intermediate files
125
  for f in [audio_wav, audio_output, video_output]:
126
  try:
127
  os.remove(f)
@@ -130,34 +124,47 @@ async def convert_video_task(input_path, downscale, faster, use_mp3, audio_only,
130
 
131
  return None, merged_output
132
 
133
- async def process_conversion(use_youtube, youtube_url, video_file, downscale, faster, use_mp3, audio_only, custom_bitrate, video_bitrate):
134
  try:
135
- if use_youtube:
136
- if not youtube_url:
137
- return "Error: YouTube URL required.", None
138
- yt_uuid = str(uuid.uuid4())
139
- yt_out = os.path.join(UPLOAD_FOLDER, yt_uuid + ".%(ext)s")
140
- yt_cmd = ['yt-dlp', '-o', yt_out, '-f', 'b', youtube_url]
141
- await run_subprocess(yt_cmd)
142
- pattern = os.path.join(UPLOAD_FOLDER, yt_uuid + ".*")
143
- files = glob.glob(pattern)
144
- if not files:
145
- return "Download failed.", None
146
- input_path = files[0]
147
- else:
148
- if not video_file:
149
- return "No video provided.", None
150
- ext = os.path.splitext(video_file.name)[1]
151
- input_path = os.path.join(UPLOAD_FOLDER, f"{uuid.uuid4()}{ext}")
152
- shutil.copy2(video_file.name, input_path)
153
-
154
- audio_out, video_out = await convert_video_task(
155
- input_path, downscale, faster, use_mp3, audio_only, custom_bitrate, video_bitrate
156
- )
 
 
 
 
157
 
158
- if audio_only:
159
- return audio_out, audio_out
160
- return video_out, video_out
 
 
 
 
 
 
 
 
 
161
  except Exception as e:
162
  return f"Error: {str(e)}", None
163
 
@@ -174,9 +181,9 @@ with gr.Blocks(theme=gr.themes.Default(primary_hue="rose")) as demo:
174
 
175
  with gr.Group():
176
  with gr.Row():
177
- use_youtube = gr.Checkbox(label="πŸ”— Use YouTube URL (This is useless on HF)", value=False)
178
  youtube_url = gr.Textbox(label="YouTube URL", placeholder="Paste YouTube URL here")
179
- video_file = gr.File(label="Upload Video File")
180
 
181
  gr.Markdown("### βš™οΈ **Conversion Settings**")
182
  with gr.Group():
 
34
  print(f"[DEBUG] Command succeeded:\n{stdout.decode()}\n")
35
  return stdout.decode(), stderr.decode()
36
 
37
+ async def convert_video_task(input_path, downscale, faster, use_mp3, audio_only, custom_bitrate, video_bitrate, progress):
38
  if use_mp3:
39
+ progress.update(1, desc="Converting audio to MP3...")
40
  output_audio = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.mp3")
41
  ffmpeg_audio_cmd = [
42
  'ffmpeg', '-y', '-i', input_path, '-vn',
 
74
  video_output = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.mp4")
75
 
76
  # Extract mono WAV audio
77
+ progress.update(1, desc="Splitting Audio and Video...")
78
  await run_subprocess([
79
  'ffmpeg', '-y', '-i', input_path, '-ac', '1', '-ar', '8000', audio_wav
80
  ])
81
 
82
  # Compress WAV with fdkaac
83
+ progress.update(1, desc="Converting audio to AAC which is lower quality...")
84
  await run_subprocess([
85
  './fdkaac', '-b', '1k', '-C', '-f', '2', '-G', '1', '-w', '8000',
86
  '-o', audio_output, audio_wav
87
  ], use_fdkaac=True)
88
 
89
  # Compress video without audio
90
+ progress.update(1, desc="Making the video quality lower...")
91
  video_cmd = [
92
  'ffmpeg', '-y', '-hwaccel', accel, '-i', input_path
93
  ]
94
 
95
  if downscale:
96
  video_cmd += ['-vf', 'scale=-2:144']
97
+
98
  if custom_bitrate:
99
  video_cmd += ['-b:v', f"{int(video_bitrate)}k"]
100
  else:
 
106
  await run_subprocess(video_cmd)
107
 
108
  if audio_only:
 
 
 
 
 
 
 
109
  return audio_output, None
110
 
111
+ # Merging video + audio
112
+ progress.update(1, desc="Mixing audio and video together...")
113
  merged_output = os.path.join(CONVERTED_FOLDER, f"{uuid.uuid4()}.mp4")
114
  await run_subprocess([
115
+ 'ffmpeg', '-y', '-i', video_output, '-i', audio_output, '-c', 'copy', merged_output
 
 
 
 
116
  ])
117
 
118
+ # Cleanup intermediates
119
  for f in [audio_wav, audio_output, video_output]:
120
  try:
121
  os.remove(f)
 
124
 
125
  return None, merged_output
126
 
127
+ async def process_conversion(use_youtube, youtube_url, video_file, downscale, faster, use_mp3, audio_only, custom_bitrate, video_bitrate, progress=gr.Progress()):
128
  try:
129
+ with progress.tqdm(total=5, desc="Starting...") as pbar:
130
+ if use_youtube:
131
+ pbar.update(1, desc="Downloading video...")
132
+ if not youtube_url:
133
+ return "Error: YouTube URL required.", None
134
+ yt_uuid = str(uuid.uuid4())
135
+ yt_out = os.path.join(UPLOAD_FOLDER, yt_uuid + ".%(ext)s")
136
+ yt_cmd = ['yt-dlp', '-o', yt_out, '-f', 'b', youtube_url]
137
+ await run_subprocess(yt_cmd)
138
+ pattern = os.path.join(UPLOAD_FOLDER, yt_uuid + ".*")
139
+ files = glob.glob(pattern)
140
+ if not files:
141
+ return "Download failed.", None
142
+ input_path = files[0]
143
+ else:
144
+ pbar.update(1, desc="Preparing input video...")
145
+ if not video_file:
146
+ return "No video provided.", None
147
+ ext = os.path.splitext(video_file.name)[1]
148
+ input_path = os.path.join(UPLOAD_FOLDER, f"{uuid.uuid4()}{ext}")
149
+ shutil.copy2(video_file.name, input_path)
150
+
151
+ if use_mp3:
152
+ pbar.update(1, desc="Converting audio to MP3...")
153
+ else:
154
+ pbar.update(1, desc="Converting audio to AAC which is lower quality...")
155
 
156
+ audio_out, video_out = await convert_video_task(
157
+ input_path, downscale, faster, use_mp3, audio_only, custom_bitrate, video_bitrate, progress=pbar
158
+ )
159
+
160
+ if not use_mp3:
161
+ pbar.update(1, desc="Making the video quality lower...")
162
+
163
+ pbar.update(1, desc="Finishing up...")
164
+
165
+ if audio_only:
166
+ return audio_out, audio_out
167
+ return video_out, video_out
168
  except Exception as e:
169
  return f"Error: {str(e)}", None
170
 
 
181
 
182
  with gr.Group():
183
  with gr.Row():
184
+ use_youtube = gr.Checkbox(label="πŸ”— Use YouTube URL (Useless on Huggingface, works when ran locally probably.)", value=False)
185
  youtube_url = gr.Textbox(label="YouTube URL", placeholder="Paste YouTube URL here")
186
+ video_file = gr.File(label="πŸ“ Upload Video File")
187
 
188
  gr.Markdown("### βš™οΈ **Conversion Settings**")
189
  with gr.Group():