Update app.py
Browse files
app.py
CHANGED
|
@@ -79,6 +79,7 @@ class MediaDownloader:
|
|
| 79 |
if progress_callback:
|
| 80 |
progress_callback(0.1, "Starting download...")
|
| 81 |
|
|
|
|
| 82 |
output_template = str(self.output_dir / "%(title)s.%(ext)s")
|
| 83 |
cmd = [
|
| 84 |
"yt-dlp",
|
|
@@ -94,13 +95,14 @@ class MediaDownloader:
|
|
| 94 |
if result.returncode != 0:
|
| 95 |
return DownloadResult(success=False, error=result.stderr)
|
| 96 |
|
| 97 |
-
# Find downloaded file
|
| 98 |
files = list(self.output_dir.glob("*"))
|
| 99 |
media_files = [f for f in files if f.suffix.lower() in SUPPORTED_MEDIA]
|
| 100 |
|
| 101 |
if not media_files:
|
| 102 |
return DownloadResult(success=False, error="No media file found after download")
|
| 103 |
|
|
|
|
| 104 |
downloaded_file = max(media_files, key=lambda x: x.stat().st_mtime)
|
| 105 |
|
| 106 |
if progress_callback:
|
|
@@ -177,7 +179,7 @@ class SpeechTranscriber:
|
|
| 177 |
if progress_callback:
|
| 178 |
progress_callback(0.1, "Transcribing...")
|
| 179 |
|
| 180 |
-
# Convert language code (ar-EG
|
| 181 |
lang_code = language.split('-')[0] if '-' in language else language
|
| 182 |
|
| 183 |
if hasattr(self, 'use_openai'):
|
|
@@ -210,13 +212,17 @@ def generate_srt(segments, output_path: Path):
|
|
| 210 |
"""Generate SRT subtitle file"""
|
| 211 |
with open(output_path, 'w', encoding='utf-8') as f:
|
| 212 |
for i, seg in enumerate(segments, 1):
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
|
| 217 |
f.write(f"{i}\n")
|
| 218 |
f.write(f"{start} --> {end}\n")
|
| 219 |
-
f.write(f"{
|
| 220 |
|
| 221 |
def format_timestamp(seconds: float) -> str:
|
| 222 |
"""Format seconds to SRT timestamp"""
|
|
@@ -255,7 +261,7 @@ class TranscriptionPipeline:
|
|
| 255 |
progress(0.1, desc="Downloading...")
|
| 256 |
download_result = self.downloader.download_url(
|
| 257 |
source_url.strip(),
|
| 258 |
-
lambda p, msg: progress(p * 0.3, desc=msg)
|
| 259 |
)
|
| 260 |
if not download_result.success:
|
| 261 |
return f"❌ Download failed: {download_result.error}", None, None, None
|
|
@@ -267,18 +273,21 @@ class TranscriptionPipeline:
|
|
| 267 |
progress(0.4, desc="Converting to WAV...")
|
| 268 |
conversion_result = self.converter.convert(
|
| 269 |
source_path,
|
| 270 |
-
lambda p, msg: progress(0.4 + p * 0.2, desc=msg)
|
| 271 |
)
|
| 272 |
if not conversion_result.success:
|
| 273 |
return f"❌ Conversion failed: {conversion_result.error}", None, None, None
|
| 274 |
|
| 275 |
# Step 3: Transcribe
|
| 276 |
progress(0.6, desc="Transcribing audio...")
|
|
|
|
|
|
|
| 277 |
transcription_result = self.transcriber.transcribe(
|
| 278 |
conversion_result.wav_path,
|
| 279 |
language=language,
|
| 280 |
-
lambda p, msg: progress(0.6 + p * 0.4, desc=msg)
|
| 281 |
)
|
|
|
|
| 282 |
if not transcription_result.success:
|
| 283 |
return f"❌ Transcription failed: {transcription_result.error}", None, None, None
|
| 284 |
|
|
|
|
| 79 |
if progress_callback:
|
| 80 |
progress_callback(0.1, "Starting download...")
|
| 81 |
|
| 82 |
+
# Using %(title)s.%(ext)s template
|
| 83 |
output_template = str(self.output_dir / "%(title)s.%(ext)s")
|
| 84 |
cmd = [
|
| 85 |
"yt-dlp",
|
|
|
|
| 95 |
if result.returncode != 0:
|
| 96 |
return DownloadResult(success=False, error=result.stderr)
|
| 97 |
|
| 98 |
+
# Find downloaded file (most recently modified in the dir)
|
| 99 |
files = list(self.output_dir.glob("*"))
|
| 100 |
media_files = [f for f in files if f.suffix.lower() in SUPPORTED_MEDIA]
|
| 101 |
|
| 102 |
if not media_files:
|
| 103 |
return DownloadResult(success=False, error="No media file found after download")
|
| 104 |
|
| 105 |
+
# Get the newest file
|
| 106 |
downloaded_file = max(media_files, key=lambda x: x.stat().st_mtime)
|
| 107 |
|
| 108 |
if progress_callback:
|
|
|
|
| 179 |
if progress_callback:
|
| 180 |
progress_callback(0.1, "Transcribing...")
|
| 181 |
|
| 182 |
+
# Convert language code (ar-EG -> ar)
|
| 183 |
lang_code = language.split('-')[0] if '-' in language else language
|
| 184 |
|
| 185 |
if hasattr(self, 'use_openai'):
|
|
|
|
| 212 |
"""Generate SRT subtitle file"""
|
| 213 |
with open(output_path, 'w', encoding='utf-8') as f:
|
| 214 |
for i, seg in enumerate(segments, 1):
|
| 215 |
+
# Handle differences between whisper lib versions
|
| 216 |
+
start_time = seg.start if hasattr(seg, 'start') else seg.get('start', 0)
|
| 217 |
+
end_time = seg.end if hasattr(seg, 'end') else seg.get('end', 0)
|
| 218 |
+
text_content = seg.text if hasattr(seg, 'text') else seg.get('text', "")
|
| 219 |
+
|
| 220 |
+
start = format_timestamp(start_time)
|
| 221 |
+
end = format_timestamp(end_time)
|
| 222 |
|
| 223 |
f.write(f"{i}\n")
|
| 224 |
f.write(f"{start} --> {end}\n")
|
| 225 |
+
f.write(f"{text_content.strip()}\n\n")
|
| 226 |
|
| 227 |
def format_timestamp(seconds: float) -> str:
|
| 228 |
"""Format seconds to SRT timestamp"""
|
|
|
|
| 261 |
progress(0.1, desc="Downloading...")
|
| 262 |
download_result = self.downloader.download_url(
|
| 263 |
source_url.strip(),
|
| 264 |
+
progress_callback=lambda p, msg: progress(p * 0.3, desc=msg)
|
| 265 |
)
|
| 266 |
if not download_result.success:
|
| 267 |
return f"❌ Download failed: {download_result.error}", None, None, None
|
|
|
|
| 273 |
progress(0.4, desc="Converting to WAV...")
|
| 274 |
conversion_result = self.converter.convert(
|
| 275 |
source_path,
|
| 276 |
+
progress_callback=lambda p, msg: progress(0.4 + p * 0.2, desc=msg)
|
| 277 |
)
|
| 278 |
if not conversion_result.success:
|
| 279 |
return f"❌ Conversion failed: {conversion_result.error}", None, None, None
|
| 280 |
|
| 281 |
# Step 3: Transcribe
|
| 282 |
progress(0.6, desc="Transcribing audio...")
|
| 283 |
+
|
| 284 |
+
# ===== FIXED CALL HERE =====
|
| 285 |
transcription_result = self.transcriber.transcribe(
|
| 286 |
conversion_result.wav_path,
|
| 287 |
language=language,
|
| 288 |
+
progress_callback=lambda p, msg: progress(0.6 + p * 0.4, desc=msg)
|
| 289 |
)
|
| 290 |
+
|
| 291 |
if not transcription_result.success:
|
| 292 |
return f"❌ Transcription failed: {transcription_result.error}", None, None, None
|
| 293 |
|