Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -96,7 +96,8 @@ def convert_ebook_to_audio(ebook_file, language_display, output_format, embed_co
|
|
96 |
Converts an ebook file to an audiobook using Calibre and espeak-ng.
|
97 |
"""
|
98 |
if not ebook_file:
|
99 |
-
|
|
|
100 |
|
101 |
# Check required commands based on selection
|
102 |
calibre_convert_ok = check_command("ebook-convert")
|
@@ -113,17 +114,17 @@ def convert_ebook_to_audio(ebook_file, language_display, output_format, embed_co
|
|
113 |
if not oggenc_ok and output_format == 'ogg': missing.append("oggenc (for OGG)")
|
114 |
|
115 |
if missing:
|
116 |
-
error_msg = f"Error
|
117 |
-
logging.error(error_msg)
|
118 |
-
#
|
119 |
-
return None, None,
|
120 |
|
121 |
|
122 |
temp_dir = tempfile.mkdtemp(prefix="ebook_audio_")
|
123 |
logging.info(f"Created temporary directory: {temp_dir}")
|
124 |
status_updates = ["Conversion started..."]
|
125 |
cover_image_path_final = None
|
126 |
-
audio_output_path_final = None
|
127 |
|
128 |
try:
|
129 |
input_ebook_path = ebook_file.name # Gradio provides a temp path for the upload
|
@@ -218,23 +219,23 @@ def convert_ebook_to_audio(ebook_file, language_display, output_format, embed_co
|
|
218 |
logging.info("Ebook successfully converted to TXT.")
|
219 |
except subprocess.CalledProcessError as e:
|
220 |
stderr_decoded = e.stderr.decode(errors='ignore') if e.stderr else "No stderr"
|
221 |
-
error_msg = f"Error
|
222 |
-
status_updates.append(f"β
|
223 |
-
logging.error(
|
224 |
-
#
|
225 |
-
return None, cover_image_path_final,
|
226 |
except Exception as e:
|
227 |
-
error_msg = f"An unexpected error occurred during ebook conversion
|
228 |
-
status_updates.append(f"β
|
229 |
-
logging.error(
|
230 |
-
return None, cover_image_path_final,
|
231 |
|
232 |
# Check if TXT file was actually created and is not empty
|
233 |
if not os.path.exists(txt_output_path) or os.path.getsize(txt_output_path) == 0:
|
234 |
-
error_msg = "Error
|
235 |
-
status_updates.append(f"β
|
236 |
-
logging.error(
|
237 |
-
return None, cover_image_path_final,
|
238 |
|
239 |
# --- Step 3: Convert TXT to Audio ---
|
240 |
progress(0.6, desc="Converting TXT to Audio")
|
@@ -352,27 +353,28 @@ def convert_ebook_to_audio(ebook_file, language_display, output_format, embed_co
|
|
352 |
f"**Command:**\n```\n{cmd_str}\n```\n" \
|
353 |
f"**Exit Status:** {exit_status_str}\n\n" \
|
354 |
f"**Output/Error:**\n```\n{error_details}\n```"
|
355 |
-
|
|
|
356 |
# --- END MODIFIED ERROR HANDLING ---
|
357 |
|
358 |
except FileNotFoundError as e:
|
359 |
missing_cmd = e.filename # Usually contains the missing command
|
360 |
-
error_msg = f"Error
|
361 |
-
status_updates.append(f"β {
|
362 |
-
logging.error(
|
363 |
-
return None, cover_image_path_final,
|
364 |
except Exception as e:
|
365 |
-
error_msg = f"An unexpected error occurred during audio generation
|
366 |
-
status_updates.append(f"β
|
367 |
-
logging.error(
|
368 |
-
return None, cover_image_path_final,
|
369 |
|
370 |
# Check if audio file exists and has size
|
371 |
if not os.path.exists(audio_output_path) or os.path.getsize(audio_output_path) < 1024: # Check for > 1KB as a basic sanity check
|
372 |
-
error_msg = f"Error
|
373 |
-
status_updates.append(f"β
|
374 |
-
logging.error(
|
375 |
-
return None, cover_image_path_final,
|
376 |
|
377 |
|
378 |
# --- Step 4: Embed Cover Art (Optional) ---
|
@@ -384,12 +386,24 @@ def convert_ebook_to_audio(ebook_file, language_display, output_format, embed_co
|
|
384 |
cover_data = img_f.read()
|
385 |
|
386 |
# Determine mimetype using PIL
|
387 |
-
|
388 |
-
|
389 |
-
|
|
|
|
|
|
|
|
|
|
|
390 |
if not mime_type:
|
391 |
-
|
392 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
393 |
|
394 |
|
395 |
logging.info(f"Attempting to embed cover art ({mime_type}) into {audio_output_path}")
|
@@ -400,23 +414,39 @@ def convert_ebook_to_audio(ebook_file, language_display, output_format, embed_co
|
|
400 |
|
401 |
# Clear existing images before adding new one (optional, prevents duplicates)
|
402 |
try:
|
|
|
403 |
if isinstance(audio, (MP3, EasyMP3)):
|
404 |
-
audio.tags
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
405 |
elif isinstance(audio, FLAC):
|
406 |
-
audio.
|
|
|
|
|
407 |
elif isinstance(audio, MP4):
|
408 |
if 'covr' in audio:
|
409 |
del audio['covr']
|
|
|
410 |
# OggVorbis picture removal is more complex, might need specific key deletion
|
411 |
elif isinstance(audio, OggVorbis) and "metadata_block_picture" in audio:
|
412 |
del audio["metadata_block_picture"]
|
413 |
-
|
414 |
-
|
|
|
|
|
|
|
415 |
except Exception as e:
|
416 |
logging.warning(f"Could not clear existing artwork before embedding: {e}")
|
417 |
|
418 |
|
419 |
# Embedding logic differs by format
|
|
|
420 |
if isinstance(audio, (MP3, EasyMP3)):
|
421 |
if audio.tags is None: audio.add_tags() # Ensure tags exist
|
422 |
audio.tags.add(
|
@@ -428,6 +458,7 @@ def convert_ebook_to_audio(ebook_file, language_display, output_format, embed_co
|
|
428 |
data=cover_data
|
429 |
)
|
430 |
)
|
|
|
431 |
elif isinstance(audio, FLAC):
|
432 |
pic = mutagen.flac.Picture()
|
433 |
pic.data = cover_data
|
@@ -435,16 +466,26 @@ def convert_ebook_to_audio(ebook_file, language_display, output_format, embed_co
|
|
435 |
pic.mime = mime_type
|
436 |
# pic.width, pic.height, pic.depth = ... # Optionally get dimensions from PIL
|
437 |
audio.add_picture(pic)
|
|
|
438 |
elif isinstance(audio, OggVorbis):
|
439 |
# Ogg uses base64 encoded pictures in METADATA_BLOCK_PICTURE tag
|
440 |
import base64
|
441 |
-
pic_data = base64.b64encode(cover_data).decode('ascii')
|
442 |
# This field expects a FLAC Picture block, base64 encoded.
|
443 |
pic = mutagen.flac.Picture()
|
444 |
pic.data = cover_data
|
445 |
pic.type = mutagen.id3.PictureType.COVER_FRONT
|
446 |
pic.mime = mime_type
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
447 |
audio["metadata_block_picture"] = [base64.b64encode(pic.write()).decode("ascii")]
|
|
|
448 |
|
449 |
elif isinstance(audio, MP4):
|
450 |
if mime_type == 'image/jpeg':
|
@@ -457,23 +498,27 @@ def convert_ebook_to_audio(ebook_file, language_display, output_format, embed_co
|
|
457 |
|
458 |
if pic_format != MP4Cover.FORMAT_UNDEFINED:
|
459 |
audio['covr'] = [MP4Cover(cover_data, imageformat=pic_format)]
|
|
|
460 |
|
461 |
-
# Add other metadata (optional)
|
462 |
try:
|
463 |
-
# Use easy=True for simpler metadata access if needed elsewhere
|
464 |
audio_easy = mutagen.File(audio_output_path, easy=True)
|
465 |
if audio_easy is not None:
|
466 |
-
|
467 |
-
|
468 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
469 |
except Exception as tag_err:
|
470 |
logging.warning(f"Could not set basic title/artist tags: {tag_err}")
|
471 |
-
# If easy tags failed, save the main audio object (with picture)
|
472 |
-
if audio is not None: audio.save()
|
473 |
-
else:
|
474 |
-
# If easy tags succeeded, save the main audio object too (if necessary, though easy.save might suffice)
|
475 |
-
if audio is not None: audio.save()
|
476 |
|
|
|
|
|
|
|
477 |
|
478 |
status_updates.append("β
Cover art embedded successfully.")
|
479 |
logging.info("Cover art embedded successfully.")
|
@@ -497,25 +542,20 @@ def convert_ebook_to_audio(ebook_file, language_display, output_format, embed_co
|
|
497 |
|
498 |
# Return paths for Gradio components
|
499 |
final_status = "\n".join(status_updates)
|
500 |
-
# Need to return a *copy* of the file outside the temp dir, or Gradio might lose it after cleanup
|
501 |
-
# However, Gradio usually handles temp files well if returned directly. Let's try direct return first.
|
502 |
-
# If issues arise, copy the file to a more stable temp location managed by Gradio if possible, or just let the user download.
|
503 |
logging.info(f"Returning audio: {audio_output_path_final}, cover: {cover_image_path_final}")
|
504 |
-
# Return audio path
|
505 |
-
return audio_output_path_final, cover_image_path_final, final_status
|
506 |
|
507 |
except Exception as e:
|
508 |
error_msg = f"An unexpected error occurred in the main process: {e}"
|
509 |
status_updates.append(f"β {error_msg}")
|
510 |
logging.error(error_msg, exc_info=True)
|
511 |
-
|
|
|
512 |
|
513 |
finally:
|
514 |
# --- Cleanup ---
|
515 |
-
#
|
516 |
-
# Gradio should handle the returned file paths, but clean the temp dir *contents* just in case.
|
517 |
-
# It's safer to let Gradio manage the returned files' lifecycle.
|
518 |
-
# We'll clean the intermediate files (.txt, original cover if converted).
|
519 |
try:
|
520 |
if 'txt_output_path' in locals() and os.path.exists(txt_output_path):
|
521 |
os.remove(txt_output_path)
|
@@ -526,26 +566,27 @@ def convert_ebook_to_audio(ebook_file, language_display, output_format, embed_co
|
|
526 |
os.path.exists(cover_output_path_temp)):
|
527 |
os.remove(cover_output_path_temp)
|
528 |
logging.info(f"Removed intermediate file: {cover_output_path_temp}")
|
529 |
-
|
530 |
-
#
|
531 |
-
#
|
532 |
-
# For safety, let's rely on OS/Gradio temp file cleanup unless memory becomes an issue.
|
533 |
if 'temp_dir' in locals() and os.path.exists(temp_dir):
|
534 |
-
logging.info(f"
|
535 |
-
# To force cleanup (may break Gradio display):
|
536 |
# shutil.rmtree(temp_dir, ignore_errors=True)
|
537 |
# logging.info(f"Attempted cleanup of temp dir: {temp_dir}")
|
538 |
|
539 |
-
|
540 |
except OSError as e:
|
541 |
-
logging.warning(f"
|
542 |
|
543 |
|
544 |
# --- Gradio Interface Definition ---
|
545 |
|
546 |
available_voices = get_espeak_voices()
|
547 |
voice_choices = list(available_voices.keys())
|
548 |
-
|
|
|
|
|
|
|
549 |
|
550 |
# Check for external tools on startup and display warnings if needed
|
551 |
startup_warnings = []
|
@@ -558,9 +599,10 @@ if not MUTAGEN_AVAILABLE: startup_warnings.append("Python 'mutagen' library (nee
|
|
558 |
|
559 |
startup_message = ""
|
560 |
if startup_warnings:
|
|
|
561 |
startup_message = (
|
562 |
"**β οΈ Startup Warning: The following components might be missing or not found in PATH:**\n\n"
|
563 |
-
f"- {
|
564 |
"Please install them for full functionality. Check console logs for details."
|
565 |
)
|
566 |
print("-" * 60)
|
@@ -604,15 +646,16 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
604 |
# Use filepath for image to avoid potential base64 encoding issues with large images
|
605 |
cover_image = gr.Image(label="Extracted Cover Art", type="filepath", interactive=False, height=200, width=200)
|
606 |
# Use filepath for audio for consistency and potentially better handling of large files
|
|
|
607 |
audio_output_player = gr.Audio(label="Generated Audiobook", type="filepath", interactive=False)
|
608 |
-
#
|
609 |
-
audio_output_download = gr.File(label="Download Audiobook File", interactive=False)
|
610 |
|
611 |
# Connect components
|
612 |
submit_button.click(
|
613 |
fn=convert_ebook_to_audio,
|
614 |
inputs=[ebook_input, lang_dropdown, format_dropdown, cover_checkbox],
|
615 |
-
|
|
|
616 |
)
|
617 |
|
618 |
# --- Launch the App ---
|
@@ -621,4 +664,5 @@ if __name__ == "__main__":
|
|
621 |
print("Ensure Calibre (ebook-convert, ebook-meta), espeak-ng, lame, and oggenc are installed and in your system PATH.")
|
622 |
if not voice_choices:
|
623 |
print("\nWARNING: Could not retrieve any voices from espeak-ng. The language dropdown will be limited or empty!\n")
|
624 |
-
|
|
|
|
96 |
Converts an ebook file to an audiobook using Calibre and espeak-ng.
|
97 |
"""
|
98 |
if not ebook_file:
|
99 |
+
# Return None for audio, None for cover, and the error message
|
100 |
+
return None, None, "**Error:** No ebook file provided."
|
101 |
|
102 |
# Check required commands based on selection
|
103 |
calibre_convert_ok = check_command("ebook-convert")
|
|
|
114 |
if not oggenc_ok and output_format == 'ogg': missing.append("oggenc (for OGG)")
|
115 |
|
116 |
if missing:
|
117 |
+
error_msg = f"**Error:** Missing required command(s):\n- {', '.join(missing)}\n\nPlease install them and ensure they are in your system PATH."
|
118 |
+
logging.error(error_msg.replace("**Error:** ","").replace("\n- "," ").replace("\n"," ")) # Log plain text
|
119 |
+
# Return None for audio, None for cover, and the error message
|
120 |
+
return None, None, error_msg
|
121 |
|
122 |
|
123 |
temp_dir = tempfile.mkdtemp(prefix="ebook_audio_")
|
124 |
logging.info(f"Created temporary directory: {temp_dir}")
|
125 |
status_updates = ["Conversion started..."]
|
126 |
cover_image_path_final = None
|
127 |
+
audio_output_path_final = None # Keep track of the final audio path
|
128 |
|
129 |
try:
|
130 |
input_ebook_path = ebook_file.name # Gradio provides a temp path for the upload
|
|
|
219 |
logging.info("Ebook successfully converted to TXT.")
|
220 |
except subprocess.CalledProcessError as e:
|
221 |
stderr_decoded = e.stderr.decode(errors='ignore') if e.stderr else "No stderr"
|
222 |
+
error_msg = f"**Error:** Calibre conversion failed.\n```\n{stderr_decoded or e}\n```"
|
223 |
+
status_updates.append(f"β Calibre conversion failed.") # Keep status short
|
224 |
+
logging.error(f"Error during Calibre conversion: {stderr_decoded or e}")
|
225 |
+
# Return None for audio, the extracted cover (if any), and the error message
|
226 |
+
return None, cover_image_path_final, "\n".join(status_updates) + f"\n\n{error_msg}"
|
227 |
except Exception as e:
|
228 |
+
error_msg = f"**Error:** An unexpected error occurred during ebook conversion:\n{e}"
|
229 |
+
status_updates.append(f"β Unexpected conversion error.")
|
230 |
+
logging.error(f"Unexpected error during ebook conversion: {e}", exc_info=True)
|
231 |
+
return None, cover_image_path_final, "\n".join(status_updates) + f"\n\n{error_msg}"
|
232 |
|
233 |
# Check if TXT file was actually created and is not empty
|
234 |
if not os.path.exists(txt_output_path) or os.path.getsize(txt_output_path) == 0:
|
235 |
+
error_msg = "**Error:** Calibre finished, but the output TXT file is missing or empty.\nThis can happen with image-based ebooks (like comics/scans) or DRM-protected files."
|
236 |
+
status_updates.append(f"β TXT output empty/missing.")
|
237 |
+
logging.error("Calibre finished, but the output TXT file is missing or empty.")
|
238 |
+
return None, cover_image_path_final, "\n".join(status_updates) + f"\n\n{error_msg}"
|
239 |
|
240 |
# --- Step 3: Convert TXT to Audio ---
|
241 |
progress(0.6, desc="Converting TXT to Audio")
|
|
|
353 |
f"**Command:**\n```\n{cmd_str}\n```\n" \
|
354 |
f"**Exit Status:** {exit_status_str}\n\n" \
|
355 |
f"**Output/Error:**\n```\n{error_details}\n```"
|
356 |
+
# Return None for audio, the cover (if any), and the combined status/error message
|
357 |
+
return None, cover_image_path_final, "\n".join(status_updates) + f"\n\n{md_error_details}"
|
358 |
# --- END MODIFIED ERROR HANDLING ---
|
359 |
|
360 |
except FileNotFoundError as e:
|
361 |
missing_cmd = e.filename # Usually contains the missing command
|
362 |
+
error_msg = f"**Error:** Command `{missing_cmd}` not found for {output_format.upper()} output.\nPlease install it and ensure it's in your system PATH."
|
363 |
+
status_updates.append(f"β Command '{missing_cmd}' not found.")
|
364 |
+
logging.error(f"Error: Command '{missing_cmd}' not found.")
|
365 |
+
return None, cover_image_path_final, "\n".join(status_updates) + f"\n\n{error_msg}"
|
366 |
except Exception as e:
|
367 |
+
error_msg = f"**Error:** An unexpected error occurred during audio generation:\n{e}"
|
368 |
+
status_updates.append(f"β Unexpected audio error.")
|
369 |
+
logging.error(f"An unexpected error occurred during audio generation: {e}", exc_info=True)
|
370 |
+
return None, cover_image_path_final, "\n".join(status_updates) + f"\n\n{error_msg}"
|
371 |
|
372 |
# Check if audio file exists and has size
|
373 |
if not os.path.exists(audio_output_path) or os.path.getsize(audio_output_path) < 1024: # Check for > 1KB as a basic sanity check
|
374 |
+
error_msg = f"**Error:** Audio generation command finished, but the output file '{Path(audio_output_path).name}' is missing or too small. Check logs for details."
|
375 |
+
status_updates.append(f"β Audio output missing/small.")
|
376 |
+
logging.error(f"Audio output file missing or too small: {audio_output_path}")
|
377 |
+
return None, cover_image_path_final, "\n".join(status_updates) + f"\n\n{error_msg}"
|
378 |
|
379 |
|
380 |
# --- Step 4: Embed Cover Art (Optional) ---
|
|
|
386 |
cover_data = img_f.read()
|
387 |
|
388 |
# Determine mimetype using PIL
|
389 |
+
mime_type = None
|
390 |
+
try:
|
391 |
+
img = Image.open(cover_image_path_final)
|
392 |
+
mime_type = Image.MIME.get(img.format)
|
393 |
+
img.close()
|
394 |
+
except Exception as pil_err:
|
395 |
+
logging.warning(f"Could not determine MIME type using PIL: {pil_err}")
|
396 |
+
|
397 |
if not mime_type:
|
398 |
+
# Basic fallback based on extension
|
399 |
+
ext = Path(cover_image_path_final).suffix.lower()
|
400 |
+
if ext == ".jpg" or ext == ".jpeg":
|
401 |
+
mime_type = 'image/jpeg'
|
402 |
+
elif ext == ".png":
|
403 |
+
mime_type = 'image/png'
|
404 |
+
else:
|
405 |
+
mime_type = 'image/jpeg' # Default guess if extension unknown/unsupported
|
406 |
+
logging.warning(f"Defaulting cover MIME type to {mime_type}")
|
407 |
|
408 |
|
409 |
logging.info(f"Attempting to embed cover art ({mime_type}) into {audio_output_path}")
|
|
|
414 |
|
415 |
# Clear existing images before adding new one (optional, prevents duplicates)
|
416 |
try:
|
417 |
+
tags_modified = False
|
418 |
if isinstance(audio, (MP3, EasyMP3)):
|
419 |
+
if audio.tags and 'APIC:' in audio.tags:
|
420 |
+
del audio.tags['APIC:'] # Common key format
|
421 |
+
tags_modified = True
|
422 |
+
# Also try deleting all APIC frames regardless of description
|
423 |
+
if audio.tags:
|
424 |
+
apic_keys = [k for k in audio.tags.keys() if k.startswith('APIC')]
|
425 |
+
for k in apic_keys:
|
426 |
+
del audio.tags[k]
|
427 |
+
tags_modified = True
|
428 |
elif isinstance(audio, FLAC):
|
429 |
+
if audio.pictures:
|
430 |
+
audio.clear_pictures()
|
431 |
+
tags_modified = True
|
432 |
elif isinstance(audio, MP4):
|
433 |
if 'covr' in audio:
|
434 |
del audio['covr']
|
435 |
+
tags_modified = True
|
436 |
# OggVorbis picture removal is more complex, might need specific key deletion
|
437 |
elif isinstance(audio, OggVorbis) and "metadata_block_picture" in audio:
|
438 |
del audio["metadata_block_picture"]
|
439 |
+
tags_modified = True
|
440 |
+
|
441 |
+
if tags_modified:
|
442 |
+
audio.save() # Save after deletion before adding
|
443 |
+
audio = mutagen.File(audio_output_path, easy=False) # Re-load
|
444 |
except Exception as e:
|
445 |
logging.warning(f"Could not clear existing artwork before embedding: {e}")
|
446 |
|
447 |
|
448 |
# Embedding logic differs by format
|
449 |
+
save_needed = False
|
450 |
if isinstance(audio, (MP3, EasyMP3)):
|
451 |
if audio.tags is None: audio.add_tags() # Ensure tags exist
|
452 |
audio.tags.add(
|
|
|
458 |
data=cover_data
|
459 |
)
|
460 |
)
|
461 |
+
save_needed = True
|
462 |
elif isinstance(audio, FLAC):
|
463 |
pic = mutagen.flac.Picture()
|
464 |
pic.data = cover_data
|
|
|
466 |
pic.mime = mime_type
|
467 |
# pic.width, pic.height, pic.depth = ... # Optionally get dimensions from PIL
|
468 |
audio.add_picture(pic)
|
469 |
+
save_needed = True
|
470 |
elif isinstance(audio, OggVorbis):
|
471 |
# Ogg uses base64 encoded pictures in METADATA_BLOCK_PICTURE tag
|
472 |
import base64
|
|
|
473 |
# This field expects a FLAC Picture block, base64 encoded.
|
474 |
pic = mutagen.flac.Picture()
|
475 |
pic.data = cover_data
|
476 |
pic.type = mutagen.id3.PictureType.COVER_FRONT
|
477 |
pic.mime = mime_type
|
478 |
+
# Add required fields if possible (otherwise defaults might work)
|
479 |
+
img = Image.open(cover_image_path_final)
|
480 |
+
pic.width = img.width
|
481 |
+
pic.height = img.height
|
482 |
+
# Determine color depth (e.g., 24 for RGB, 32 for RGBA)
|
483 |
+
pic.depth = {'RGB': 24, 'RGBA': 32, 'L': 8}.get(img.mode, 24)
|
484 |
+
img.close()
|
485 |
+
|
486 |
+
# Encode the full picture block
|
487 |
audio["metadata_block_picture"] = [base64.b64encode(pic.write()).decode("ascii")]
|
488 |
+
save_needed = True
|
489 |
|
490 |
elif isinstance(audio, MP4):
|
491 |
if mime_type == 'image/jpeg':
|
|
|
498 |
|
499 |
if pic_format != MP4Cover.FORMAT_UNDEFINED:
|
500 |
audio['covr'] = [MP4Cover(cover_data, imageformat=pic_format)]
|
501 |
+
save_needed = True
|
502 |
|
503 |
+
# Add other metadata (optional) - Use easy=True for simpler access
|
504 |
try:
|
|
|
505 |
audio_easy = mutagen.File(audio_output_path, easy=True)
|
506 |
if audio_easy is not None:
|
507 |
+
if 'title' not in audio_easy or not audio_easy['title']:
|
508 |
+
audio_easy['title'] = base_filename
|
509 |
+
save_needed = True
|
510 |
+
if 'artist' not in audio_easy or not audio_easy['artist']:
|
511 |
+
audio_easy['artist'] = "Generated Audiobook"
|
512 |
+
save_needed = True
|
513 |
+
if save_needed:
|
514 |
+
audio_easy.save() # Save easy tags if modified
|
515 |
+
save_needed = False # Prevent double save if only easy tags changed
|
516 |
except Exception as tag_err:
|
517 |
logging.warning(f"Could not set basic title/artist tags: {tag_err}")
|
|
|
|
|
|
|
|
|
|
|
518 |
|
519 |
+
# Save the main audio object if changes were made (picture or direct tags)
|
520 |
+
if save_needed and audio is not None:
|
521 |
+
audio.save()
|
522 |
|
523 |
status_updates.append("β
Cover art embedded successfully.")
|
524 |
logging.info("Cover art embedded successfully.")
|
|
|
542 |
|
543 |
# Return paths for Gradio components
|
544 |
final_status = "\n".join(status_updates)
|
|
|
|
|
|
|
545 |
logging.info(f"Returning audio: {audio_output_path_final}, cover: {cover_image_path_final}")
|
546 |
+
# Return audio path for Audio component, cover path for Image, status for Textbox
|
547 |
+
return audio_output_path_final, cover_image_path_final, final_status
|
548 |
|
549 |
except Exception as e:
|
550 |
error_msg = f"An unexpected error occurred in the main process: {e}"
|
551 |
status_updates.append(f"β {error_msg}")
|
552 |
logging.error(error_msg, exc_info=True)
|
553 |
+
# Return None for audio, cover path (if extracted), and the error status
|
554 |
+
return None, cover_image_path_final, f"**Error:** An unexpected critical error occurred.\nCheck logs for details.\n{e}"
|
555 |
|
556 |
finally:
|
557 |
# --- Cleanup ---
|
558 |
+
# Clean intermediate files. Let Gradio handle the returned files.
|
|
|
|
|
|
|
559 |
try:
|
560 |
if 'txt_output_path' in locals() and os.path.exists(txt_output_path):
|
561 |
os.remove(txt_output_path)
|
|
|
566 |
os.path.exists(cover_output_path_temp)):
|
567 |
os.remove(cover_output_path_temp)
|
568 |
logging.info(f"Removed intermediate file: {cover_output_path_temp}")
|
569 |
+
|
570 |
+
# Optionally schedule full temp dir removal if Gradio doesn't handle it.
|
571 |
+
# For now, assume Gradio manages the returned file paths.
|
|
|
572 |
if 'temp_dir' in locals() and os.path.exists(temp_dir):
|
573 |
+
logging.info(f"Temp dir '{temp_dir}' contains output files. Skipping immediate deletion.")
|
574 |
+
# To force cleanup (may break Gradio display if files aren't copied):
|
575 |
# shutil.rmtree(temp_dir, ignore_errors=True)
|
576 |
# logging.info(f"Attempted cleanup of temp dir: {temp_dir}")
|
577 |
|
|
|
578 |
except OSError as e:
|
579 |
+
logging.warning(f"Error during cleanup of intermediate files: {e}")
|
580 |
|
581 |
|
582 |
# --- Gradio Interface Definition ---
|
583 |
|
584 |
available_voices = get_espeak_voices()
|
585 |
voice_choices = list(available_voices.keys())
|
586 |
+
# Try to find a more specific default like en-US, otherwise fall back
|
587 |
+
default_voice_options = ["English (en-US) (en-us)", "English (United States) (en-us)", "English (en)", "en"]
|
588 |
+
default_voice = next((v for v in default_voice_options if v in voice_choices), (voice_choices[0] if voice_choices else "en"))
|
589 |
+
|
590 |
|
591 |
# Check for external tools on startup and display warnings if needed
|
592 |
startup_warnings = []
|
|
|
599 |
|
600 |
startup_message = ""
|
601 |
if startup_warnings:
|
602 |
+
warning_list = "\n- ".join(startup_warnings)
|
603 |
startup_message = (
|
604 |
"**β οΈ Startup Warning: The following components might be missing or not found in PATH:**\n\n"
|
605 |
+
f"- {warning_list}\n\n"
|
606 |
"Please install them for full functionality. Check console logs for details."
|
607 |
)
|
608 |
print("-" * 60)
|
|
|
646 |
# Use filepath for image to avoid potential base64 encoding issues with large images
|
647 |
cover_image = gr.Image(label="Extracted Cover Art", type="filepath", interactive=False, height=200, width=200)
|
648 |
# Use filepath for audio for consistency and potentially better handling of large files
|
649 |
+
# The gr.Audio component includes download functionality.
|
650 |
audio_output_player = gr.Audio(label="Generated Audiobook", type="filepath", interactive=False)
|
651 |
+
# REMOVED: audio_output_download = gr.File(label="Download Audiobook File", interactive=False)
|
|
|
652 |
|
653 |
# Connect components
|
654 |
submit_button.click(
|
655 |
fn=convert_ebook_to_audio,
|
656 |
inputs=[ebook_input, lang_dropdown, format_dropdown, cover_checkbox],
|
657 |
+
# Map outputs to the player, image, and status box.
|
658 |
+
outputs=[audio_output_player, cover_image, status_textbox] # MODIFIED
|
659 |
)
|
660 |
|
661 |
# --- Launch the App ---
|
|
|
664 |
print("Ensure Calibre (ebook-convert, ebook-meta), espeak-ng, lame, and oggenc are installed and in your system PATH.")
|
665 |
if not voice_choices:
|
666 |
print("\nWARNING: Could not retrieve any voices from espeak-ng. The language dropdown will be limited or empty!\n")
|
667 |
+
# Add share=True for a public link, server_name="0.0.0.0" for Docker/network access
|
668 |
+
demo.launch()
|