Spaces:
Sleeping
Sleeping
m4b bug fix
Browse files158 unmodified lines159160161162163164165166167168169170171172158 unmodified linesreturn None, Noneasync def convert_to_m4b(mp3_path, output_filename):"""Convert MP3 to M4B format using pydub"""try:audio = AudioSegment.from_mp3(mp3_path)m4b_path = tempfile.NamedTemporaryFile(delete=False, suffix=".m4b").name# Export as m4a (m4b is essentially m4a with audiobook metadata)audio.export(m4b_path, format="ipod", codec="aac")logger.info(f"Converted to M4B: {m4b_path}")return m4b_pathexcept Exception as e:logger.error(f"Failed to convert to M4B: {e}")return None158 unmodified lines159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204158 unmodified linesreturn None, Noneasync def convert_to_m4b(mp3_path, output_filename):"""Convert MP3 to M4B format using ffmpeg directly (supports large files)"""try:import subprocessm4b_path = tempfile.NamedTemporaryFile(delete=False, suffix=".m4b").name# Use ffmpeg directly for conversion (avoids pydub's 4GB limit)cmd = ['ffmpeg', '-y', # Overwrite output'-i', mp3_path, # Input file'-c:a', 'aac', # Audio codec'-b:a', '128k', # Audio bitrate'-f', 'ipod', # M4B/M4A formatm4b_path]logger.info(f"Running ffmpeg conversion: {' '.join(cmd)}")result = subprocess.run(cmd,capture_output=True,text=True,timeout=3600 # 1 hour timeout for large files)if result.returncode != 0:logger.error(f"ffmpeg error: {result.stderr}")if os.path.exists(m4b_path):os.remove(m4b_path)return Nonelogger.info(f"Converted to M4B: {m4b_path}")return m4b_pathexcept FileNotFoundError:logger.error("ffmpeg not found. Please install ffmpeg to use M4B format.")return Noneexcept subprocess.TimeoutExpired:logger.error("ffmpeg conversion timed out")return Noneexcept Exception as e:logger.error(f"Failed to convert to M4B: {e}")return None
app.py+36-4