Spaces:
Sleeping
Sleeping
m4b bug fix
Browse files
app.py
CHANGED
|
@@ -159,14 +159,46 @@ def parse_uploaded_file(file_path):
|
|
| 159 |
return None, None
|
| 160 |
|
| 161 |
async def convert_to_m4b(mp3_path, output_filename):
|
| 162 |
-
"""Convert MP3 to M4B format using
|
| 163 |
try:
|
| 164 |
-
|
|
|
|
| 165 |
m4b_path = tempfile.NamedTemporaryFile(delete=False, suffix=".m4b").name
|
| 166 |
-
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
logger.info(f"Converted to M4B: {m4b_path}")
|
| 169 |
return m4b_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
except Exception as e:
|
| 171 |
logger.error(f"Failed to convert to M4B: {e}")
|
| 172 |
return None
|
|
|
|
| 159 |
return None, None
|
| 160 |
|
| 161 |
async def convert_to_m4b(mp3_path, output_filename):
|
| 162 |
+
"""Convert MP3 to M4B format using ffmpeg directly (supports large files)"""
|
| 163 |
try:
|
| 164 |
+
import subprocess
|
| 165 |
+
|
| 166 |
m4b_path = tempfile.NamedTemporaryFile(delete=False, suffix=".m4b").name
|
| 167 |
+
|
| 168 |
+
# Use ffmpeg directly for conversion (avoids pydub's 4GB limit)
|
| 169 |
+
cmd = [
|
| 170 |
+
'ffmpeg', '-y', # Overwrite output
|
| 171 |
+
'-i', mp3_path, # Input file
|
| 172 |
+
'-c:a', 'aac', # Audio codec
|
| 173 |
+
'-b:a', '128k', # Audio bitrate
|
| 174 |
+
'-f', 'ipod', # M4B/M4A format
|
| 175 |
+
m4b_path
|
| 176 |
+
]
|
| 177 |
+
|
| 178 |
+
logger.info(f"Running ffmpeg conversion: {' '.join(cmd)}")
|
| 179 |
+
|
| 180 |
+
result = subprocess.run(
|
| 181 |
+
cmd,
|
| 182 |
+
capture_output=True,
|
| 183 |
+
text=True,
|
| 184 |
+
timeout=3600 # 1 hour timeout for large files
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
if result.returncode != 0:
|
| 188 |
+
logger.error(f"ffmpeg error: {result.stderr}")
|
| 189 |
+
if os.path.exists(m4b_path):
|
| 190 |
+
os.remove(m4b_path)
|
| 191 |
+
return None
|
| 192 |
+
|
| 193 |
logger.info(f"Converted to M4B: {m4b_path}")
|
| 194 |
return m4b_path
|
| 195 |
+
|
| 196 |
+
except FileNotFoundError:
|
| 197 |
+
logger.error("ffmpeg not found. Please install ffmpeg to use M4B format.")
|
| 198 |
+
return None
|
| 199 |
+
except subprocess.TimeoutExpired:
|
| 200 |
+
logger.error("ffmpeg conversion timed out")
|
| 201 |
+
return None
|
| 202 |
except Exception as e:
|
| 203 |
logger.error(f"Failed to convert to M4B: {e}")
|
| 204 |
return None
|