Spaces:
Sleeping
Sleeping
TheM1N9
commited on
Commit
·
0104e23
1
Parent(s):
951cbfb
Refactor podcast segment regeneration to remove pydub dependency. Directly concatenate MP3 files for improved performance and simplicity.
Browse files
app.py
CHANGED
|
@@ -19,7 +19,6 @@ from pathlib import Path
|
|
| 19 |
from main import NotebookMg
|
| 20 |
from dotenv import load_dotenv
|
| 21 |
import logging
|
| 22 |
-
from pydub import AudioSegment
|
| 23 |
|
| 24 |
# Set up logging
|
| 25 |
logging.basicConfig(level=logging.DEBUG)
|
|
@@ -256,7 +255,6 @@ async def regenerate_segment(
|
|
| 256 |
logger.info(f"Deleted existing segment file: {segment_path}")
|
| 257 |
except Exception as e:
|
| 258 |
logger.error(f"Error deleting existing segment file: {str(e)}")
|
| 259 |
-
# Continue anyway as we'll overwrite the file
|
| 260 |
|
| 261 |
# Save the regenerated segment
|
| 262 |
audio_bytes = b"".join(audio_data)
|
|
@@ -273,30 +271,22 @@ async def regenerate_segment(
|
|
| 273 |
except Exception as e:
|
| 274 |
logger.error(f"Error deleting existing podcast file: {str(e)}")
|
| 275 |
|
| 276 |
-
#
|
| 277 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
|
| 279 |
-
|
| 280 |
-
segment_files = sorted(
|
| 281 |
-
[f for f in OUTPUT_DIR.glob(f"{base_name}_segment_*.mp3")],
|
| 282 |
-
key=lambda x: int(x.stem.split("_")[-1]),
|
| 283 |
-
)
|
| 284 |
-
|
| 285 |
-
logger.info(f"Found {len(segment_files)} segments to combine")
|
| 286 |
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
pause = AudioSegment.silent(duration=300) # 300ms pause
|
| 293 |
-
combined_audio += audio_segment + pause
|
| 294 |
-
except Exception as e:
|
| 295 |
-
logger.error(f"Error processing segment {segment_file}: {str(e)}")
|
| 296 |
-
raise
|
| 297 |
|
| 298 |
-
# Save the new complete podcast
|
| 299 |
-
combined_audio.export(str(podcast_path), format="mp3")
|
| 300 |
logger.info(
|
| 301 |
f"Successfully generated new podcast with {len(segment_files)} segments"
|
| 302 |
)
|
|
|
|
| 19 |
from main import NotebookMg
|
| 20 |
from dotenv import load_dotenv
|
| 21 |
import logging
|
|
|
|
| 22 |
|
| 23 |
# Set up logging
|
| 24 |
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
| 255 |
logger.info(f"Deleted existing segment file: {segment_path}")
|
| 256 |
except Exception as e:
|
| 257 |
logger.error(f"Error deleting existing segment file: {str(e)}")
|
|
|
|
| 258 |
|
| 259 |
# Save the regenerated segment
|
| 260 |
audio_bytes = b"".join(audio_data)
|
|
|
|
| 271 |
except Exception as e:
|
| 272 |
logger.error(f"Error deleting existing podcast file: {str(e)}")
|
| 273 |
|
| 274 |
+
# Instead of using pydub, we'll concatenate the MP3 files directly
|
| 275 |
+
with open(podcast_path, "wb") as outfile:
|
| 276 |
+
# Get all segment files and sort them correctly
|
| 277 |
+
segment_files = sorted(
|
| 278 |
+
[f for f in OUTPUT_DIR.glob(f"{base_name}_segment_*.mp3")],
|
| 279 |
+
key=lambda x: int(x.stem.split("_")[-1]),
|
| 280 |
+
)
|
| 281 |
|
| 282 |
+
logger.info(f"Found {len(segment_files)} segments to combine")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
|
| 284 |
+
# Concatenate all MP3 files
|
| 285 |
+
for segment_file in segment_files:
|
| 286 |
+
with open(segment_file, "rb") as infile:
|
| 287 |
+
outfile.write(infile.read())
|
| 288 |
+
# No pause between segments in this simple approach
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
|
|
|
|
|
|
|
| 290 |
logger.info(
|
| 291 |
f"Successfully generated new podcast with {len(segment_files)} segments"
|
| 292 |
)
|