Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -334,20 +334,21 @@ def merge_videos_and_audios(video_files=None, audio_files=None, orig_vol=1.0, mu
|
|
| 334 |
|
| 335 |
# --- Gradio App Using Blocks ---
|
| 336 |
|
| 337 |
-
def gradio_merge_files(
|
| 338 |
"""
|
| 339 |
Gradio endpoint to merge videos and/or audio from binary file uploads.
|
| 340 |
Args:
|
| 341 |
-
|
| 342 |
orig_vol: Volume for original video audio (0.0 to 1.0)
|
| 343 |
music_vol: Volume for background audio (0.0 to 1.0)
|
|
|
|
| 344 |
Returns:
|
| 345 |
Path to the merged file (video or audio) or error message
|
| 346 |
"""
|
| 347 |
try:
|
| 348 |
-
logger.info(f"Received {len(
|
| 349 |
|
| 350 |
-
if not
|
| 351 |
error_msg = "Error: Please upload at least 2 files."
|
| 352 |
logger.error(error_msg)
|
| 353 |
return error_msg, None
|
|
@@ -356,23 +357,38 @@ def gradio_merge_files(file_objects, orig_vol, music_vol):
|
|
| 356 |
temp_dir = tempfile.mkdtemp()
|
| 357 |
all_files = []
|
| 358 |
|
| 359 |
-
#
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
continue
|
| 364 |
-
#
|
| 365 |
-
original_filename = os.path.basename(
|
| 366 |
-
# Ensure the filename matches expected patterns
|
| 367 |
if not re.match(r'file\d+\.mp4', original_filename, re.IGNORECASE) and \
|
| 368 |
not re.match(r'audio\d+\.(mp3|wav)', original_filename, re.IGNORECASE):
|
| 369 |
logger.warning(f"Filename {original_filename} does not match expected pattern; skipping")
|
| 370 |
continue
|
| 371 |
# Create a temporary file path
|
| 372 |
temp_file_path = os.path.join(temp_dir, original_filename)
|
| 373 |
-
#
|
| 374 |
with open(temp_file_path, 'wb') as temp_file:
|
| 375 |
-
|
| 376 |
all_files.append(temp_file_path)
|
| 377 |
logger.info(f"Saved uploaded file to {temp_file_path}")
|
| 378 |
|
|
@@ -444,7 +460,7 @@ if __name__ == "__main__":
|
|
| 444 |
gr.Markdown("For API usage, send binary files via multipart/form-data. Name videos as file1.mp4, file2.mp4, etc., and audios as audio1.mp3, audio2.mp3, etc.")
|
| 445 |
|
| 446 |
with gr.Row():
|
| 447 |
-
file_input = gr.File(label="Upload Files (Videos: .mp4, Audios: .mp3/.wav)", type="
|
| 448 |
|
| 449 |
with gr.Row():
|
| 450 |
orig_vol_input = gr.Slider(minimum=0.0, maximum=1.0, value=1.0, step=0.05, label="Original Video Audio Volume")
|
|
|
|
| 334 |
|
| 335 |
# --- Gradio App Using Blocks ---
|
| 336 |
|
| 337 |
+
def gradio_merge_files(file_binaries, orig_vol, music_vol, file_names=None):
|
| 338 |
"""
|
| 339 |
Gradio endpoint to merge videos and/or audio from binary file uploads.
|
| 340 |
Args:
|
| 341 |
+
file_binaries: List of binary data (bytes objects)
|
| 342 |
orig_vol: Volume for original video audio (0.0 to 1.0)
|
| 343 |
music_vol: Volume for background audio (0.0 to 1.0)
|
| 344 |
+
file_names: List of original filenames (passed separately for API calls)
|
| 345 |
Returns:
|
| 346 |
Path to the merged file (video or audio) or error message
|
| 347 |
"""
|
| 348 |
try:
|
| 349 |
+
logger.info(f"Received {len(file_binaries) if file_binaries else 0} binary files")
|
| 350 |
|
| 351 |
+
if not file_binaries or len(file_binaries) < 2:
|
| 352 |
error_msg = "Error: Please upload at least 2 files."
|
| 353 |
logger.error(error_msg)
|
| 354 |
return error_msg, None
|
|
|
|
| 357 |
temp_dir = tempfile.mkdtemp()
|
| 358 |
all_files = []
|
| 359 |
|
| 360 |
+
# When called via the UI, Gradio provides binary data but not filenames directly.
|
| 361 |
+
# When called via API (e.g., n8n), we need to get filenames from the request.
|
| 362 |
+
# For UI testing, infer filenames based on index; for API, use provided file_names.
|
| 363 |
+
if file_names is None:
|
| 364 |
+
# Fallback for UI: assign temporary filenames (not ideal for production)
|
| 365 |
+
file_names = []
|
| 366 |
+
for idx, binary in enumerate(file_binaries):
|
| 367 |
+
ext = ".mp4" if idx % 2 == 0 else ".mp3" # Dummy assignment for UI testing
|
| 368 |
+
file_names.append(f"temp_file_{idx}{ext}")
|
| 369 |
+
logger.warning("No file names provided; using temporary names for UI testing. For API calls, pass file_names.")
|
| 370 |
+
|
| 371 |
+
if len(file_names) != len(file_binaries):
|
| 372 |
+
error_msg = f"Error: Mismatch between file binaries ({len(file_binaries)}) and file names ({len(file_names)})."
|
| 373 |
+
logger.error(error_msg)
|
| 374 |
+
return error_msg, None
|
| 375 |
+
|
| 376 |
+
# Save each binary file to the temporary directory
|
| 377 |
+
for binary, filename in zip(file_binaries, file_names):
|
| 378 |
+
if binary is None:
|
| 379 |
+
logger.warning(f"Skipping None binary data for file {filename}")
|
| 380 |
continue
|
| 381 |
+
# Validate filename
|
| 382 |
+
original_filename = os.path.basename(filename)
|
|
|
|
| 383 |
if not re.match(r'file\d+\.mp4', original_filename, re.IGNORECASE) and \
|
| 384 |
not re.match(r'audio\d+\.(mp3|wav)', original_filename, re.IGNORECASE):
|
| 385 |
logger.warning(f"Filename {original_filename} does not match expected pattern; skipping")
|
| 386 |
continue
|
| 387 |
# Create a temporary file path
|
| 388 |
temp_file_path = os.path.join(temp_dir, original_filename)
|
| 389 |
+
# Write the binary data to the temporary file
|
| 390 |
with open(temp_file_path, 'wb') as temp_file:
|
| 391 |
+
temp_file.write(binary)
|
| 392 |
all_files.append(temp_file_path)
|
| 393 |
logger.info(f"Saved uploaded file to {temp_file_path}")
|
| 394 |
|
|
|
|
| 460 |
gr.Markdown("For API usage, send binary files via multipart/form-data. Name videos as file1.mp4, file2.mp4, etc., and audios as audio1.mp3, audio2.mp3, etc.")
|
| 461 |
|
| 462 |
with gr.Row():
|
| 463 |
+
file_input = gr.File(label="Upload Files (Videos: .mp4, Audios: .mp3/.wav)", type="binary", file_count="multiple")
|
| 464 |
|
| 465 |
with gr.Row():
|
| 466 |
orig_vol_input = gr.Slider(minimum=0.0, maximum=1.0, value=1.0, step=0.05, label="Original Video Audio Volume")
|