diff --git a/.env.example b/.env.example index a90348c..8e36cfa 100644 --- a/.env.example +++ b/.env.example @@ -51,7 +51,8 @@ TELEGRAM_BOT_TOKEN=YOUR_TOKEN_HERE # SENTRY_DSN= # SENTRY_TRACES_SAMPLE_RATE=0.1 -# --- YouTube transcript (optional proxies / language priority) +# --- YouTube captions (REQUIRED on cloud / VPS: YouTube bans datacenter IPs) +# Without this, every link fails with "YouTube blocked this server", even English videos. # YOUTUBE_TRANSCRIPT_LANGUAGE_PRIORITY=en,de # YOUTUBE_HTTP_PROXY_URL= # YOUTUBE_HTTPS_PROXY_URL= diff --git a/BACKEND_DEPLOYMENT_HANDOFF.md b/BACKEND_DEPLOYMENT_HANDOFF.md index 083a627..2eeb21d 100644 --- a/BACKEND_DEPLOYMENT_HANDOFF.md +++ b/BACKEND_DEPLOYMENT_HANDOFF.md @@ -56,9 +56,21 @@ Values below match `app/core/config.py`. Names are the usual `UPPER_SNAKE` env n | `TRANSCRIPT_CACHE_ENABLED` | `true` | Disk cache for repeat URLs. | | `TRANSCRIPT_CACHE_TTL_HOURS` | `72` | Cache freshness. | -### Optional (YouTube IP blocks / proxies) +### Required on cloud hosts (YouTube IP blocks) -`WEBSHARE_PROXY_*`, `YOUTUBE_HTTP_PROXY_URL`, `YOUTUBE_HTTPS_PROXY_URL`, `YOUTUBE_TRANSCRIPT_LANGUAGE_PRIORITY` β€” see repo README / `youtube-transcript-api` docs. +YouTube blocks most datacenter IPs. The Telegram bot and `/extract` then fail for **every** link, including English videos. The old user text talked about English-only captions. That was an IP block. + +Set a **residential** proxy on the API **and** the bot process (same env): + +| Variable | Purpose | +|----------|---------| +| `WEBSHARE_PROXY_USERNAME` | Webshare rotating residential user | +| `WEBSHARE_PROXY_PASSWORD` | Webshare password | +| `WEBSHARE_PROXY_LOCATIONS` | Optional country codes, e.g. `us,de` | + +Or generic HTTP(S) proxies: `YOUTUBE_HTTP_PROXY_URL`, `YOUTUBE_HTTPS_PROXY_URL`. + +Without one of these, cloud Telegram will keep failing. Local home IPs often work with no proxy. --- diff --git a/README.md b/README.md index 36f2975..ed8303f 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,9 @@ Core Value: **Fast β†’ Reliable β†’ Structured β†’ Downloadable.** ## 🌟 Key Features - **Multi-Platform Support**: Extract transcripts from **YouTube** and **Vimeo** instantly. -- **Clean Formats**: Download as **TXT, PDF, and DOCX**. +- **Clean Formats**: Download as **TXT, PDF, and DOCX**, or **SRT / VTT** subtitles with cue timings. Files are named after the video title. - **Timestamps**: Toggle perfectly formatted timestamps on or off. -- **Anonymous & Secure**: No accounts required. Generated files auto-delete after ~1 hour (TTL). Optional short-term caption cache (hours) for reliability. +- **Anonymous & Secure**: No accounts required. Generated files auto-delete after ~1 hour (TTL). Optional short-term caption cache (`TRANSCRIPT_CACHE_TTL_HOURS`) is pruned by the same cleanup loop. - **Rate Limited & Guardrailed**: Separate extract vs convert quotas, max video duration (with caption-based fallback), segment/size caps, and timeout protections. - **Telemetry & Analytics**: Built-in lightweight JSON-structured logging and analytics tracking. @@ -48,13 +48,13 @@ transcript/ ### 1. Web Application (`/web`) A premium, modern SaaS web app interface designed with: - 3D Video Carousel hero section. -- Preview-First Results Flow: Shows a snippet of the transcript immediately, followed by format selection (TXT/PDF/DOCX). +- Preview-First Results Flow: Shows a snippet of the transcript immediately, followed by format selection (TXT/PDF/DOCX/SRT/VTT). - Clean state-machine interactions (Idle β†’ Processing β†’ Success/Error). ### 2. Telegram Bot (`app/bot/telegram_bot.py`) A seriously structured, minimal utility bot: - **Instant Processing**: Send a URL, get a file. No conversational fluff. -- **In-Memory Preferences**: Remembers your preferred format (e.g., PDF) and timestamp preferences across sessions. +- **In-Memory Preferences**: Remembers your preferred format (e.g., PDF) and timestamp setting while the bot process runs (reset on restart/deploy). - **Single Message UX**: Status updates (Extracting... $\rightarrow$ Formatting... $\rightarrow$ Ready) happen within a single edited message to keep chat history clean. - **Branded Onboarding**: Clean `/start` experience with inline keyboard routing. diff --git a/app/api/v1_routes.py b/app/api/v1_routes.py index cb85f36..9083f04 100644 --- a/app/api/v1_routes.py +++ b/app/api/v1_routes.py @@ -15,11 +15,18 @@ verify_rate_limit_events, verify_rate_limit_extract, ) -from app.core.errors import AppError, ErrorCode, success_response +from app.core.errors import AppError, ErrorCode, TranscriptFetchError, success_response from app.services.analytics_service import AnalyticsService from app.services.extract_guardrails import enforce_transcript_guardrails -from app.services.file_service import TEMP_DIR, FileGenerator -from app.services.formatter_service import TranscriptFormatter +from app.services.file_service import ( + TEMP_DIR, + FileGenerator, + content_disposition, + download_filename, + read_sidecar, + write_sidecar, +) +from app.services.formatter_service import SUBTITLE_FORMATS, TranscriptFormatter from app.services.metadata_service import MetadataService from app.services.telemetry_service import MAX_PAYLOAD_BYTES, TelemetryService from app.services.transcript_service import get_transcript_from_url @@ -28,7 +35,14 @@ router = APIRouter(prefix="/v1") -_ALLOWED_EXTENSIONS = {".txt", ".pdf", ".docx"} +_CONVERT_FORMATS = ("txt", "pdf", "docx", *SUBTITLE_FORMATS) +_ALLOWED_EXTENSIONS = {f".{fmt}" for fmt in _CONVERT_FORMATS} +_MEDIA_TYPES = { + ".pdf": "application/pdf", + ".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + ".srt": "application/x-subrip", + ".vtt": "text/vtt", +} _convert_semaphore = asyncio.Semaphore(settings.convert_max_concurrency) _UUID_HEX_RE = re.compile(r"^[0-9a-f]{32}$") @@ -109,17 +123,11 @@ async def extract_transcript(request: ExtractRequest): except Exception as e: err_msg = str(e) processing_time_ms = int((time.time() - start_time) * 1000) - el = err_msg.lower() - if "disabled" in el: - code = ErrorCode.TRANSCRIPT_NOT_AVAILABLE - elif "unavailable" in el or "not found" in el or "no transcript or captions" in el: - code = ErrorCode.TRANSCRIPT_NOT_AVAILABLE - elif "youtube blocked" in el or "only available in english" in el: - code = ErrorCode.TRANSCRIPT_NOT_AVAILABLE - elif "unsupported" in el: - code = ErrorCode.INVALID_URL + if isinstance(e, TranscriptFetchError): + code = e.code else: code = ErrorCode.INTERNAL_ERROR + logger.exception("Unexpected transcript fetch failure") MetadataService.log_failure( url=request.url, platform=platform, @@ -194,6 +202,9 @@ async def extract_transcript(request: ExtractRequest): text_sidecar = TEMP_DIR / f"{filename}.raw" text_sidecar.write_text(formatted_text, encoding="utf-8") + # Segments feed SRT/VTT convert; title names every download of this transcript + await asyncio.to_thread(write_sidecar, filename, "segments", transcript_data["segments"]) + write_sidecar(filename, "meta", {"title": transcript_data.get("title")}) MetadataService.log_success( video_id=transcript_data["video_id"], @@ -213,7 +224,7 @@ async def extract_transcript(request: ExtractRequest): processing_time_ms=processing_time_ms, ) - preview_text = formatted_text[:1500] + preview_text = formatted_text[: settings.preview_chars] expires_at = ( datetime.now(timezone.utc) + timedelta(hours=settings.file_ttl_hours) ).isoformat() @@ -245,20 +256,14 @@ async def download_file(file_id: str): raise AppError(ErrorCode.FILE_EXPIRED, "File not found or expired.", 404) extension = file_path.suffix.lower() - media_type = "text/plain" - if extension == ".pdf": - media_type = "application/pdf" - elif extension == ".docx": - media_type = ( - "application/vnd.openxmlformats-officedocument.wordprocessingml.document" - ) + meta = read_sidecar(file_id, "meta") or {} + filename = download_filename(meta.get("title"), extension) return FileResponse( path=str(file_path), - media_type=media_type, - filename=f"transcript{extension}", + media_type=_MEDIA_TYPES.get(extension, "text/plain"), headers={ - "Content-Disposition": f"attachment; filename=transcript{extension}", + "Content-Disposition": content_disposition(filename), "Cache-Control": "no-store", }, ) @@ -266,15 +271,15 @@ async def download_file(file_id: str): class ConvertRequest(BaseModel): file_id: str - format: str # txt, pdf, docx + format: str # txt, pdf, docx, srt, vtt @router.post("/convert", dependencies=[Depends(verify_rate_limit_convert)]) async def convert_file(request: ConvertRequest): - if request.format not in ("txt", "pdf", "docx"): + if request.format not in _CONVERT_FORMATS: raise AppError( ErrorCode.CONVERT_FAILED, - "Unsupported format. Use txt, pdf, or docx.", + "Unsupported format. Use txt, pdf, docx, srt, or vtt.", 400, ) @@ -290,18 +295,26 @@ async def convert_file(request: ConvertRequest): } ) - sidecar_path = TEMP_DIR / f"{request.file_id}.raw" - if not sidecar_path.exists(): - raise AppError(ErrorCode.FILE_EXPIRED, "Source text not found or expired.", 404) - - formatted_text = sidecar_path.read_text(encoding="utf-8") - - if len(formatted_text) > settings.max_raw_text_length: - raise AppError( - ErrorCode.CONVERT_FAILED, - "Transcript is too large to convert to this format.", - 400, + if request.format in SUBTITLE_FORMATS: + segments = await asyncio.to_thread(read_sidecar, request.file_id, "segments") + if not isinstance(segments, list): + raise AppError(ErrorCode.FILE_EXPIRED, "Source captions not found or expired.", 404) + # Segment count/size were already capped by the extract guardrails + content = await asyncio.to_thread( + TranscriptFormatter.format_subtitles, segments, request.format ) + else: + sidecar_path = TEMP_DIR / f"{request.file_id}.raw" + if not sidecar_path.exists(): + raise AppError(ErrorCode.FILE_EXPIRED, "Source text not found or expired.", 404) + content = sidecar_path.read_text(encoding="utf-8") + + if len(content) > settings.max_raw_text_length: + raise AppError( + ErrorCode.CONVERT_FAILED, + "Transcript is too large to convert to this format.", + 400, + ) # Non-blocking acquire β€” no TOCTOU with locked() pre-check try: @@ -316,7 +329,7 @@ async def convert_file(request: ConvertRequest): try: try: file_path = await asyncio.to_thread( - FileGenerator.generate_file, formatted_text, request.format + FileGenerator.generate_file, content, request.format ) except Exception as e: logger.error("Convert generation failed: %s", e) @@ -329,6 +342,10 @@ async def convert_file(request: ConvertRequest): finally: _convert_semaphore.release() + meta = read_sidecar(request.file_id, "meta") + if meta: + write_sidecar(filename, "meta", meta) + return success_response( { "file_download_url": f"/api/v1/download/{filename}", diff --git a/app/bot/telegram_bot.py b/app/bot/telegram_bot.py index bc3c491..6bc1a9f 100644 --- a/app/bot/telegram_bot.py +++ b/app/bot/telegram_bot.py @@ -18,16 +18,13 @@ from dotenv import load_dotenv from app.utils.validators import detect_platform -from app.services.transcript_service import ( - get_transcript_from_url, - YOUTUBE_TRANSCRIPT_UNAVAILABLE_EN_ONLY, -) -from app.services.formatter_service import TranscriptFormatter -from app.services.file_service import FileGenerator +from app.services.transcript_service import get_transcript_from_url +from app.services.formatter_service import SUBTITLE_FORMATS, TranscriptFormatter +from app.services.file_service import FileGenerator, download_filename from app.services.rate_limit_service import check_rate_limit from app.services.metadata_service import MetadataService from app.services.extract_guardrails import enforce_transcript_guardrails -from app.core.errors import AppError, ErrorCode +from app.core.errors import AppError, ErrorCode, TranscriptFetchError from app.core.config import settings from app.utils.logging_config import logger from app.services.analytics_service import AnalyticsService @@ -125,6 +122,7 @@ def _escape_md2(text: str) -> str: "\\β€’ YouTube: English captions only at this time\n" "\\β€’ TXT \\(default\\)\n" "\\β€’ PDF / DOCX\n" + "\\β€’ SRT / VTT subtitles\n" "\\β€’ Optional timestamps\n" "\\β€’ No account required\n" "\\β€’ Download files expire after about 1 hour\n\n" @@ -137,7 +135,7 @@ def _escape_md2(text: str) -> str: "*How to use Transcript Flow*\n\n" "1\\. Send a YouTube or Vimeo link\\.\n" "2\\. Preview the transcript\\.\n" - "3\\. Download as TXT, PDF, or DOCX\\.\n\n" + "3\\. Download as TXT, PDF, DOCX, SRT, or VTT\\.\n\n" "Power users:\n" "`/extract `\n\n" "If extraction fails:\n" @@ -150,7 +148,9 @@ def _escape_md2(text: str) -> str: "*Available formats*\n\n" "TXT \\– Clean plain text\n" "PDF \\– Printable document\n" - "DOCX \\– Editable Word file\n\n" + "DOCX \\– Editable Word file\n" + "SRT \\– Subtitles with timings\n" + "VTT \\– Web subtitles with timings\n\n" "Optional:\n" "\\β€’ Include timestamps\n\n" "Default format is TXT\\." @@ -200,6 +200,10 @@ def _ready_keyboard(include_timestamps: bool) -> InlineKeyboardMarkup: InlineKeyboardButton("Download PDF", callback_data="dl|pdf"), InlineKeyboardButton("Download DOCX", callback_data="dl|docx"), ], + [ + InlineKeyboardButton("Download SRT", callback_data="dl|srt"), + InlineKeyboardButton("Download VTT", callback_data="dl|vtt"), + ], [ InlineKeyboardButton("Preview", callback_data="preview"), InlineKeyboardButton(ts_text, callback_data="ts_toggle"), @@ -235,6 +239,17 @@ def _start_keyboard() -> InlineKeyboardMarkup: ]) +async def _build_file(transcript_data: dict, formatted_text: str, ext: str) -> str: + """Generate *ext* off the event loop (PDF builds can take seconds).""" + if ext in SUBTITLE_FORMATS: + content = await asyncio.to_thread( + TranscriptFormatter.format_subtitles, transcript_data["segments"], ext + ) + else: + content = formatted_text + return await asyncio.to_thread(FileGenerator.generate_file, content, ext) + + def _extract_url_from_text(text: str) -> str | None: """Return the first YouTube/Vimeo URL found in *text*, or None.""" match = _URL_PATTERN.search(text) @@ -308,8 +323,8 @@ async def _process_url( # State B β€” Formatting ------------------------------------------- await status_msg.edit_text("Transcript Flow\n\nFormatting transcript\u2026") - formatted_text = TranscriptFormatter.format( - transcript_data["segments"], format_type=fmt_type + formatted_text = await asyncio.to_thread( + TranscriptFormatter.format, transcript_data["segments"], fmt_type ) enforce_transcript_guardrails(transcript_data, formatted_text=formatted_text) metrics = MetadataService.calculate_metrics(formatted_text) @@ -318,7 +333,7 @@ async def _process_url( await status_msg.edit_text("Transcript Flow\n\nPreparing file\u2026") video_id = transcript_data.get("video_id", "unknown") - file_path = FileGenerator.generate_file(formatted_text, file_ext) + file_path = await _build_file(transcript_data, formatted_text, file_ext) # State D β€” Ready ------------------------------------------------ ready = _ready_text( @@ -371,21 +386,12 @@ async def _process_url( ) await status_msg.edit_text(msg) except Exception as e: - err_msg = str(e).lower() - logger.error("Error processing URL: %s", e) - - if ( - "disabled" in err_msg - or "not found" in err_msg - or "unavailable" in err_msg - or "no transcript or captions" in err_msg - ): - friendly = ERROR_MESSAGES[ErrorCode.TRANSCRIPT_NOT_AVAILABLE] - error_code = "TRANSCRIPT_NOT_AVAILABLE" - elif "youtube blocked" in err_msg or "only available in english" in err_msg: - friendly = YOUTUBE_TRANSCRIPT_UNAVAILABLE_EN_ONLY - error_code = "TRANSCRIPT_NOT_AVAILABLE" + if isinstance(e, TranscriptFetchError): + # Service messages are written for end users (e.g. "disabled", "blocked") + friendly = e.message + error_code = e.code.value else: + logger.exception("Error processing URL") friendly = ERROR_MESSAGES[ErrorCode.INTERNAL_ERROR] error_code = "INTERNAL_ERROR" @@ -584,29 +590,29 @@ async def _handle_download(query, context: ContextTypes.DEFAULT_TYPE, ext: str) try: formatted_text = context.user_data.get("formatted_text") - video_id = context.user_data.get("video_id", "unknown") + transcript_data = context.user_data.get("transcript_data") or {} user_id = str(query.from_user.id) _emit_event("tg_download_clicked", user_id, {"file_format": ext}) - if not formatted_text: + if not formatted_text or not transcript_data: await query.answer("Session expired. Send the link again.") context.user_data["_processing"] = False return - # Re-use cached file if extension matches, otherwise regenerate --- - cached_ext = context.user_data.get("file_ext") - if ext == cached_ext and context.user_data.get("file_path"): - file_path = context.user_data["file_path"] + # Re-use cached file if extension matches and TTL cleanup hasn't removed it + cached_path = context.user_data.get("file_path") + if ext == context.user_data.get("file_ext") and cached_path and os.path.exists(cached_path): + file_path = cached_path else: - file_path = FileGenerator.generate_file(formatted_text, ext) + file_path = await _build_file(transcript_data, formatted_text, ext) # Send file ------------------------------------------------------- with open(file_path, "rb") as f: await context.bot.send_document( chat_id=query.message.chat_id, document=f, - filename=f"transcriptflow_{video_id}.{ext}", + filename=download_filename(transcript_data.get("title"), ext), ) # Update preference ------------------------------------------------ @@ -705,14 +711,14 @@ async def _handle_timestamp_toggle(query, context: ContextTypes.DEFAULT_TYPE) -> # Re-format with new timestamp state ---------------------------------- fmt_type = _format_type_for_prefs(new_ts) - formatted_text = TranscriptFormatter.format( - transcript_data["segments"], format_type=fmt_type + formatted_text = await asyncio.to_thread( + TranscriptFormatter.format, transcript_data["segments"], fmt_type ) metrics = MetadataService.calculate_metrics(formatted_text) # Regenerate file in current preferred format ------------------------- file_ext = prefs["last_file_format"] - file_path = FileGenerator.generate_file(formatted_text, file_ext) + file_path = await _build_file(transcript_data, formatted_text, file_ext) # Update cache -------------------------------------------------------- context.user_data["formatted_text"] = formatted_text diff --git a/app/cleanup.py b/app/cleanup.py index 667834c..cd385c0 100644 --- a/app/cleanup.py +++ b/app/cleanup.py @@ -9,11 +9,13 @@ visible to both the web service and the cron job. Deletes all files in the file storage directory older than FILE_TTL_HOURS. -Covers base .txt files, converted .pdf/.docx, and .raw sidecars. +Covers base .txt files, converted .pdf/.docx/.srt/.vtt, and sidecars. +Also prunes transcript cache entries older than TRANSCRIPT_CACHE_TTL_HOURS. """ from app.services.file_service import TEMP_DIR, FileGenerator from app.core.config import settings +from app.services.transcript_cache_service import prune_transcript_cache from app.utils.logging_config import logger @@ -24,7 +26,10 @@ def main() -> None: TEMP_DIR, ) deleted = FileGenerator.cleanup_old_files(max_age_hours=settings.file_ttl_hours) - logger.info("Cleanup cron: done, deleted %s file(s).", deleted) + pruned = prune_transcript_cache() + logger.info( + "Cleanup cron: done, deleted %s file(s), pruned %s cache entr(ies).", deleted, pruned + ) if __name__ == "__main__": diff --git a/app/core/errors.py b/app/core/errors.py index 74c0f45..1178c97 100644 --- a/app/core/errors.py +++ b/app/core/errors.py @@ -27,6 +27,20 @@ def __init__(self, code: ErrorCode, message: str, status_code: int = 400): super().__init__(message) +class TranscriptFetchError(Exception): + """ + Upstream transcript failure with a user-safe message. + + Raised by the YouTube/Vimeo services so interfaces (API, Telegram) can map + failures by type instead of matching on message text. + """ + + def __init__(self, code: ErrorCode, message: str): + self.code = code + self.message = message + super().__init__(message) + + def success_response(data: Dict[str, Any]) -> Dict[str, Any]: """Wraps any payload in the standard success envelope.""" return {"success": True, **data} diff --git a/app/main.py b/app/main.py index d94f204..4035018 100644 --- a/app/main.py +++ b/app/main.py @@ -8,6 +8,7 @@ from app.core.config import settings from app.core.errors import AppError, ErrorCode, error_response from app.services.file_service import FileGenerator, TEMP_DIR +from app.services.transcript_cache_service import prune_transcript_cache from app.utils.logging_config import logger @@ -98,6 +99,9 @@ async def _cleanup_loop() -> None: ) if deleted: logger.info("In-process cleanup deleted %s file(s) from %s", deleted, TEMP_DIR) + pruned = await asyncio.to_thread(prune_transcript_cache) + if pruned: + logger.info("In-process cleanup pruned %s expired cache entr(ies)", pruned) except asyncio.CancelledError: raise except Exception as e: diff --git a/app/services/file_service.py b/app/services/file_service.py index 01d7d3e..88b370b 100644 --- a/app/services/file_service.py +++ b/app/services/file_service.py @@ -1,7 +1,12 @@ import html +import json +import re import time +import unicodedata import uuid from pathlib import Path +from typing import Any +from urllib.parse import quote from docx import Document @@ -26,6 +31,49 @@ def resolve_temp_dir() -> Path: TEMP_DIR = resolve_temp_dir() +_UNSAFE_FILENAME_CHARS = re.compile(r'[\x00-\x1f\x7f<>:"/\\|?*]+') +_MAX_FILENAME_STEM = 80 + + +def download_filename(title: str | None, extension: str) -> str: + """ + Human filename for a download, e.g. "My Talk.pdf". Strips path separators, + control and reserved characters; falls back to "transcript". + """ + stem = _UNSAFE_FILENAME_CHARS.sub(" ", title or "") + stem = " ".join(stem.split()).strip(" .")[:_MAX_FILENAME_STEM].rstrip(" .") + return f"{stem or 'transcript'}.{extension.lstrip('.')}" + + +def content_disposition(filename: str) -> str: + """Attachment header with an ASCII fallback plus RFC 5987 UTF-8 name.""" + ascii_name = ( + unicodedata.normalize("NFKD", filename).encode("ascii", "ignore").decode("ascii") + ) + ascii_name = ascii_name.replace('"', "").strip() or "transcript" + if "." not in ascii_name.lstrip("."): + ascii_name = f"transcript{Path(filename).suffix}" + return ( + f'attachment; filename="{ascii_name}"; ' + f"filename*=UTF-8''{quote(filename, safe='')}" + ) + + +def write_sidecar(file_id: str, suffix: str, data: Any) -> None: + """JSON sidecar next to a generated file (never downloadable: see _validate_file_id).""" + (TEMP_DIR / f"{file_id}.{suffix}.json").write_text( + json.dumps(data, ensure_ascii=False), encoding="utf-8" + ) + + +def read_sidecar(file_id: str, suffix: str) -> Any | None: + path = TEMP_DIR / f"{file_id}.{suffix}.json" + try: + return json.loads(path.read_text(encoding="utf-8")) + except (OSError, ValueError): + return None + + class FileGenerator: @staticmethod def _get_temp_filepath(extension: str) -> Path: @@ -36,8 +84,8 @@ def _get_temp_filepath(extension: str) -> Path: return TEMP_DIR / filename @staticmethod - def generate_txt(content: str) -> str: - filepath = FileGenerator._get_temp_filepath("txt") + def generate_txt(content: str, extension: str = "txt") -> str: + filepath = FileGenerator._get_temp_filepath(extension) with open(filepath, "w", encoding="utf-8") as f: f.write(content) return str(filepath) @@ -92,6 +140,8 @@ def generate_file(content: str, file_format: str) -> str: return FileGenerator.generate_docx(content) if file_format == "pdf": return FileGenerator.generate_pdf(content) + if file_format in ("srt", "vtt"): + return FileGenerator.generate_txt(content, file_format) return FileGenerator.generate_txt(content) @staticmethod diff --git a/app/services/formatter_service.py b/app/services/formatter_service.py index 0464fd5..a3ebe81 100644 --- a/app/services/formatter_service.py +++ b/app/services/formatter_service.py @@ -1,5 +1,10 @@ import re -from typing import List, Dict +from typing import List, Dict, Tuple + +# Timed formats built from caption segments rather than the formatted prose. +SUBTITLE_FORMATS = ("srt", "vtt") +# Floor for zero/negative-length cues so every subtitle is visible. +_MIN_CUE_SECONDS = 0.5 # YouTube auto-captions and many ASR tracks use >> as a hard turn / beat marker. _ARTIFACT_GTGT = re.compile(r"\s*>>\s*") @@ -149,6 +154,68 @@ def format_with_timestamps(transcript: List[Dict]) -> str: formatted_text += f"{timestamp} {line}\n" return formatted_text + @staticmethod + def _subtitle_cues(transcript: List[Dict]) -> List[Tuple[float, float, str]]: + """ + (start, end, text) per non-empty cue. Auto-captions overlap heavily, so each + cue ends no later than the next one starts β€” otherwise players stack lines. + """ + entries = [] + for entry in transcript: + text = _ARTIFACT_GTGT.sub(" ", entry.get("text") or "") + # A blank line inside a cue would end it early in SRT/VTT + text = "\n".join( + _MULTI_SPACE.sub(" ", line).strip() for line in text.splitlines() if line.strip() + ) + if text: + start = max(0.0, float(entry.get("start") or 0)) + entries.append((start, start + float(entry.get("duration") or 0), text)) + + cues = [] + for i, (start, end, text) in enumerate(entries): + if i + 1 < len(entries) and entries[i + 1][0] > start: + end = min(end, entries[i + 1][0]) + if end <= start: + end = start + _MIN_CUE_SECONDS + cues.append((start, end, text)) + return cues + + @staticmethod + def _subtitle_timestamp(seconds: float, ms_sep: str) -> str: + total_ms = int(round(seconds * 1000)) + h, rem = divmod(total_ms, 3_600_000) + m, rem = divmod(rem, 60_000) + s, ms = divmod(rem, 1000) + return f"{h:02}:{m:02}:{s:02}{ms_sep}{ms:03}" + + @staticmethod + def format_srt(transcript: List[Dict]) -> str: + ts = TranscriptFormatter._subtitle_timestamp + blocks = [ + f"{i}\n{ts(start, ',')} --> {ts(end, ',')}\n{text}\n" + for i, (start, end, text) in enumerate( + TranscriptFormatter._subtitle_cues(transcript), start=1 + ) + ] + return "\n".join(blocks) + + @staticmethod + def format_vtt(transcript: List[Dict]) -> str: + ts = TranscriptFormatter._subtitle_timestamp + blocks = [ + f"{ts(start, '.')} --> {ts(end, '.')}\n{text}\n" + for start, end, text in TranscriptFormatter._subtitle_cues(transcript) + ] + return "WEBVTT\n\n" + "\n".join(blocks) + + @staticmethod + def format_subtitles(transcript: List[Dict], file_format: str) -> str: + if file_format == "srt": + return TranscriptFormatter.format_srt(transcript) + if file_format == "vtt": + return TranscriptFormatter.format_vtt(transcript) + raise ValueError(f"Not a subtitle format: {file_format}") + @staticmethod def format(transcript: List[Dict], format_type: str = "clean") -> str: if format_type == "timestamp": diff --git a/app/services/transcript_cache_service.py b/app/services/transcript_cache_service.py index 295944e..fbd8572 100644 --- a/app/services/transcript_cache_service.py +++ b/app/services/transcript_cache_service.py @@ -65,3 +65,27 @@ def write_transcript_cache(provider: str, video_id: str, data: Dict[str, Any]) - tmp.unlink() except OSError: pass + + +def prune_transcript_cache() -> int: + """ + Delete cache entries older than the TTL (plus stale .tmp leftovers). + Reads already ignore expired entries; this keeps the directory from growing + forever and caption text from outliving the advertised retention. + TTL 0 means entries never expire, so nothing is pruned. + """ + ttl_sec = max(0, int(settings.transcript_cache_ttl_hours * 3600)) + directory = cache_dir() + if not ttl_sec or not directory.is_dir(): + return 0 + + now = time.time() + deleted = 0 + for path in directory.glob("*.json*"): + try: + if path.is_file() and now - path.stat().st_mtime > ttl_sec: + path.unlink() + deleted += 1 + except OSError: + pass + return deleted diff --git a/app/services/transcript_service.py b/app/services/transcript_service.py index dbb79b1..922aa05 100644 --- a/app/services/transcript_service.py +++ b/app/services/transcript_service.py @@ -8,6 +8,7 @@ from youtube_transcript_api import ( YouTubeTranscriptApi, + CouldNotRetrieveTranscript, Transcript, TranscriptList, TranscriptsDisabled, @@ -26,11 +27,12 @@ write_transcript_cache, ) from app.core.config import settings +from app.core.errors import ErrorCode, TranscriptFetchError -# User-facing when YouTube blocks our IP (shown in API, bot, etc.). +# User-facing when YouTube IpBlocked / RequestBlocked (cloud IPs are often banned). YOUTUBE_TRANSCRIPT_UNAVAILABLE_EN_ONLY = ( - "Transcripts are only available in English at this time, and we could not load " - "captions for this video. Please try again later." + "We could not load captions because YouTube blocked this server. " + "Please try again later." ) # Fallback search order after the client’s target language (never English-only). @@ -132,36 +134,31 @@ def _youtube_api() -> YouTubeTranscriptApi: def get_transcript_from_url(url: str) -> Dict: - """Fetch transcript. YouTube captions are always resolved to English (native or auto-translate).""" + """ + Fetch transcript. YouTube captions are always resolved to English (native or auto-translate). + Raises TranscriptFetchError for expected failures; anything else is a bug. + """ try: platform = detect_platform(url) + video_id = ( + validate_youtube_url(url) if platform == "youtube" else validate_vimeo_url(url) + ) + except ValueError as e: + raise TranscriptFetchError(ErrorCode.INVALID_URL, str(e)) from e - if platform == "youtube": - video_id = validate_youtube_url(url) - cached = read_transcript_cache("youtube", video_id) - if cached is not None: - out = dict(cached) - if url: - out["source_url"] = url - return out - result = get_youtube_transcript(video_id, url=url) - write_transcript_cache("youtube", video_id, result) - return result - if platform == "vimeo": - video_id = validate_vimeo_url(url) - cached = read_transcript_cache("vimeo", video_id) - if cached is not None: - out = dict(cached) - if url: - out["source_url"] = url - return out - result = get_vimeo_transcript(video_id, url=url) - write_transcript_cache("vimeo", video_id, result) - return result - raise ValueError(f"Unsupported platform: {platform}") + cached = read_transcript_cache(platform, video_id) + if cached is not None: + out = dict(cached) + if url: + out["source_url"] = url + return out - except ValueError as e: - raise Exception(str(e)) from e + if platform == "youtube": + result = get_youtube_transcript(video_id, url=url) + else: + result = get_vimeo_transcript(video_id, url=url) + write_transcript_cache(platform, video_id, result) + return result def _get_youtube_metadata(video_id: str) -> Dict: @@ -294,6 +291,23 @@ def _youtube_transcript_fresh_core( } +def _youtube_fetch_error(e: CouldNotRetrieveTranscript) -> TranscriptFetchError: + """Map youtube-transcript-api failures to a user-safe TranscriptFetchError.""" + na = ErrorCode.TRANSCRIPT_NOT_AVAILABLE + if isinstance(e, (IpBlocked, RequestBlocked)): + return TranscriptFetchError(na, YOUTUBE_TRANSCRIPT_UNAVAILABLE_EN_ONLY) + if isinstance(e, TranscriptsDisabled): + return TranscriptFetchError(na, "Transcripts are disabled for this video.") + if isinstance(e, VideoUnavailable): + return TranscriptFetchError(na, "Video is unavailable.") + if isinstance(e, NoTranscriptFound): + return TranscriptFetchError( + na, + "No transcript or captions are available for this video in any language we could access.", + ) + return TranscriptFetchError(na, "Captions could not be retrieved for this video.") + + def get_youtube_transcript( video_id: str, languages: Optional[List[str]] = None, @@ -304,52 +318,19 @@ def get_youtube_transcript( English-only: prefer English captions, else translate to English when available. With residential proxy configured, retries a few times on IpBlocked / RequestBlocked. """ - if languages is not None: - try: - return _youtube_transcript_fresh_core(video_id, languages, url) - except TranscriptsDisabled: - raise Exception("Transcripts are disabled for this video.") from None - except VideoUnavailable: - raise Exception("Video is unavailable.") from None - except NoTranscriptFound: - raise Exception( - "No transcript or captions are available for this video in any language we could access." - ) from None - except (IpBlocked, RequestBlocked): - raise Exception(YOUTUBE_TRANSCRIPT_UNAVAILABLE_EN_ONLY) from None - except Exception as e: - err_msg = str(e) - if "No transcript found" in err_msg: - raise Exception( - "No transcript found for the requested languages." - ) from e - raise - - attempts = _youtube_ip_block_attempts() + attempts = 1 if languages is not None else _youtube_ip_block_attempts() for attempt in range(attempts): try: - return _youtube_transcript_fresh_core(video_id, None, url) + return _youtube_transcript_fresh_core(video_id, languages, url) except (IpBlocked, RequestBlocked) as e: if attempt < attempts - 1: delay = settings.youtube_transcript_retry_backoff_seconds * (attempt + 1) time.sleep(delay + random.uniform(0, 0.25)) continue - raise Exception(YOUTUBE_TRANSCRIPT_UNAVAILABLE_EN_ONLY) from e - except TranscriptsDisabled: - raise Exception("Transcripts are disabled for this video.") from None - except VideoUnavailable: - raise Exception("Video is unavailable.") from None - except NoTranscriptFound: - raise Exception( - "No transcript or captions are available for this video in any language we could access." - ) from None - except Exception as e: - err_msg = str(e) - if "No transcript found" in err_msg: - raise Exception( - "No transcript found for the requested languages." - ) from e - raise + raise _youtube_fetch_error(e) from e + except CouldNotRetrieveTranscript as e: + raise _youtube_fetch_error(e) from e + raise AssertionError("unreachable") def get_transcript(video_id: str, languages: Optional[List[str]] = None) -> Optional[Dict]: diff --git a/app/services/vimeo_service.py b/app/services/vimeo_service.py index 0aeec59..f71487b 100644 --- a/app/services/vimeo_service.py +++ b/app/services/vimeo_service.py @@ -5,6 +5,10 @@ from io import StringIO from typing import List, Dict, Optional +from app.core.errors import ErrorCode, TranscriptFetchError + +_NO_CAPTIONS = "No captions are available for this Vimeo video." + def get_vimeo_transcript(video_id: str, url: str = '') -> Dict: """ Fetches the transcript for a given Vimeo video ID. @@ -42,13 +46,13 @@ def get_vimeo_transcript(video_id: str, url: str = '') -> Dict: match = re.search(config_pattern, html_content, re.DOTALL) if not match: - raise Exception("No text tracks found for this video") + raise TranscriptFetchError(ErrorCode.TRANSCRIPT_NOT_AVAILABLE, _NO_CAPTIONS) text_tracks_json = match.group(1) text_tracks = json.loads(text_tracks_json) if not text_tracks or len(text_tracks) == 0: - raise Exception("No transcripts available for this video") + raise TranscriptFetchError(ErrorCode.TRANSCRIPT_NOT_AVAILABLE, _NO_CAPTIONS) # Use the first available text track (preferably English) selected_track = None @@ -62,13 +66,13 @@ def get_vimeo_transcript(video_id: str, url: str = '') -> Dict: selected_track = text_tracks[0] if not selected_track: - raise Exception("No usable text tracks available") + raise TranscriptFetchError(ErrorCode.TRANSCRIPT_NOT_AVAILABLE, _NO_CAPTIONS) language = selected_track.get('lang', 'en') vtt_url = selected_track.get('url') if not vtt_url: - raise Exception("No transcript URL found") + raise TranscriptFetchError(ErrorCode.TRANSCRIPT_NOT_AVAILABLE, _NO_CAPTIONS) # Download the VTT file vtt_response = requests.get(vtt_url, timeout=10) @@ -88,16 +92,26 @@ def get_vimeo_transcript(video_id: str, url: str = '') -> Dict: "segments": segments } + except requests.HTTPError as e: + status = e.response.status_code if e.response is not None else None + if status == 403: + raise TranscriptFetchError( + ErrorCode.TRANSCRIPT_NOT_AVAILABLE, + "This video is private or restricted. Only public Vimeo videos with captions are supported.", + ) from e + if status == 404: + raise TranscriptFetchError( + ErrorCode.TRANSCRIPT_NOT_AVAILABLE, "Video not found. Please check the URL." + ) from e + raise TranscriptFetchError( + ErrorCode.TRANSCRIPT_NOT_AVAILABLE, "Vimeo did not return this video. Try again later." + ) from e except requests.RequestException as e: - if "403" in str(e): - raise Exception("This video is private or restricted. Only public Vimeo videos with captions are supported.") - elif "404" in str(e): - raise Exception("Video not found. Please check the URL.") - raise Exception(f"Failed to fetch Vimeo video: {str(e)}") + raise TranscriptFetchError( + ErrorCode.TRANSCRIPT_NOT_AVAILABLE, "Could not reach Vimeo. Try again later." + ) from e except json.JSONDecodeError as e: - raise Exception(f"Failed to parse Vimeo text tracks: {str(e)}") - except Exception as e: - raise Exception(f"Error extracting Vimeo transcript: {str(e)}") + raise TranscriptFetchError(ErrorCode.TRANSCRIPT_NOT_AVAILABLE, _NO_CAPTIONS) from e def parse_vtt(vtt_content: str) -> List[Dict]: """ @@ -122,7 +136,9 @@ def parse_vtt(vtt_content: str) -> List[Dict]: }) except Exception as e: - raise Exception(f"Failed to parse VTT content: {str(e)}") + raise TranscriptFetchError( + ErrorCode.TRANSCRIPT_NOT_AVAILABLE, "Vimeo captions could not be read." + ) from e return transcript diff --git a/tests/test_export_and_errors.py b/tests/test_export_and_errors.py new file mode 100644 index 0000000..f58ec62 --- /dev/null +++ b/tests/test_export_and_errors.py @@ -0,0 +1,141 @@ +"""Tests for SRT/VTT export, download filenames, cache pruning, and typed fetch errors.""" + +import os +import tempfile +import time +import unittest +from pathlib import Path +from unittest.mock import MagicMock, patch + +import requests +from youtube_transcript_api import AgeRestricted + +from app.core.errors import ErrorCode, TranscriptFetchError +from app.services import transcript_cache_service as tcs +from app.services.file_service import content_disposition, download_filename +from app.services.formatter_service import TranscriptFormatter +from app.services.transcript_service import get_youtube_transcript +from app.services.vimeo_service import get_vimeo_transcript + +_SEGMENTS = [ + {"text": "Hello >> there", "start": 0.0, "duration": 3.0}, # overlaps next cue + {"text": " ", "start": 1.5, "duration": 1.0}, # blank: skipped + {"text": "second", "start": 2.0, "duration": 1.0}, + {"text": "last", "start": 3661.25, "duration": 0.0}, # zero-length: floored +] + + +class TestSubtitleFormats(unittest.TestCase): + def test_srt(self): + out = TranscriptFormatter.format_srt(_SEGMENTS) + self.assertEqual( + out, + "1\n00:00:00,000 --> 00:00:02,000\nHello there\n" + "\n2\n00:00:02,000 --> 00:00:03,000\nsecond\n" + "\n3\n01:01:01,250 --> 01:01:01,750\nlast\n", + ) + + def test_vtt(self): + out = TranscriptFormatter.format_vtt(_SEGMENTS) + self.assertTrue(out.startswith("WEBVTT\n\n00:00:00.000 --> 00:00:02.000\nHello there\n")) + self.assertIn("01:01:01.250 --> 01:01:01.750\nlast\n", out) + + def test_blank_lines_inside_cue_removed(self): + out = TranscriptFormatter.format_srt( + [{"text": "line one\n\n line two ", "start": 0, "duration": 1}] + ) + self.assertEqual(out, "1\n00:00:00,000 --> 00:00:01,000\nline one\nline two\n") + + def test_unknown_subtitle_format_rejected(self): + with self.assertRaises(ValueError): + TranscriptFormatter.format_subtitles(_SEGMENTS, "pdf") + + +class TestDownloadFilename(unittest.TestCase): + def test_uses_title(self): + self.assertEqual(download_filename("My Talk", ".pdf"), "My Talk.pdf") + + def test_strips_unsafe_characters(self): + self.assertEqual( + download_filename('../etc/pass"wd: a|b?', "txt"), "etc pass wd a b.txt" + ) + + def test_falls_back_when_empty(self): + self.assertEqual(download_filename(None, "srt"), "transcript.srt") + self.assertEqual(download_filename(" ... ", "srt"), "transcript.srt") + + def test_truncates_long_titles(self): + self.assertEqual(download_filename("x" * 300, "txt"), "x" * 80 + ".txt") + + def test_content_disposition_non_ascii(self): + header = content_disposition("CafΓ© ζ—₯本.pdf") + self.assertIn('filename="Cafe .pdf"', header) + self.assertIn("filename*=UTF-8''Caf%C3%A9%20%E6%97%A5%E6%9C%AC.pdf", header) + + def test_content_disposition_all_non_ascii_falls_back(self): + self.assertIn('filename="transcript.pdf"', content_disposition("ζ—₯本.pdf")) + + +class TestPruneTranscriptCache(unittest.TestCase): + def setUp(self): + self._tmp = tempfile.TemporaryDirectory() + self.addCleanup(self._tmp.cleanup) + self.dir = Path(self._tmp.name) + + def _file(self, name: str, age_seconds: float) -> Path: + path = self.dir / name + path.write_text("{}", encoding="utf-8") + t = time.time() - age_seconds + os.utime(path, (t, t)) + return path + + def test_deletes_only_expired_entries(self): + old = self._file("youtube_old.json", 3 * 3600) + stale_tmp = self._file("youtube_x.json.tmp", 3 * 3600) + fresh = self._file("youtube_new.json", 60) + with patch.object(tcs.settings, "transcript_cache_ttl_hours", 1), patch.object( + tcs.settings, "transcript_cache_dir", str(self.dir) + ): + self.assertEqual(tcs.prune_transcript_cache(), 2) + self.assertFalse(old.exists()) + self.assertFalse(stale_tmp.exists()) + self.assertTrue(fresh.exists()) + + def test_ttl_zero_never_prunes(self): + old = self._file("youtube_old.json", 10**7) + with patch.object(tcs.settings, "transcript_cache_ttl_hours", 0), patch.object( + tcs.settings, "transcript_cache_dir", str(self.dir) + ): + self.assertEqual(tcs.prune_transcript_cache(), 0) + self.assertTrue(old.exists()) + + +class TestTypedFetchErrors(unittest.TestCase): + @patch("app.services.transcript_service._youtube_transcript_fresh_core") + def test_unmapped_library_error_is_typed(self, mock_core): + mock_core.side_effect = AgeRestricted("vid") + with self.assertRaises(TranscriptFetchError) as ctx: + get_youtube_transcript("vid") + self.assertEqual(ctx.exception.code, ErrorCode.TRANSCRIPT_NOT_AVAILABLE) + self.assertEqual( + ctx.exception.message, "Captions could not be retrieved for this video." + ) + + @patch("app.services.vimeo_service.requests.get") + def test_vimeo_private_video(self, mock_get): + resp = MagicMock(status_code=403) + mock_get.return_value.raise_for_status.side_effect = requests.HTTPError(response=resp) + with self.assertRaises(TranscriptFetchError) as ctx: + get_vimeo_transcript("12345") + self.assertIn("private or restricted", ctx.exception.message) + + @patch("app.services.vimeo_service.requests.get") + def test_vimeo_no_text_tracks(self, mock_get): + mock_get.return_value.text = "Clip on Vimeo" + with self.assertRaises(TranscriptFetchError) as ctx: + get_vimeo_transcript("12345") + self.assertEqual(ctx.exception.code, ErrorCode.TRANSCRIPT_NOT_AVAILABLE) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_transcript_service.py b/tests/test_transcript_service.py index 8b15f06..6d9ce2d 100644 --- a/tests/test_transcript_service.py +++ b/tests/test_transcript_service.py @@ -109,6 +109,19 @@ def test_youtube_retries_then_success_on_ip_block( self.assertEqual(mock_core.call_count, 3) self.assertEqual(mock_sleep.call_count, 2) + @patch("app.services.transcript_service.time.sleep") + @patch("app.services.transcript_service._youtube_ip_block_attempts", return_value=1) + @patch("app.services.transcript_service._youtube_transcript_fresh_core") + def test_youtube_ip_block_exhausted_says_server_blocked( + self, mock_core, _mock_attempts, _mock_sleep + ): + mock_core.side_effect = IpBlocked("vid") + with self.assertRaises(Exception) as ctx: + get_youtube_transcript("vid", url="") + msg = str(ctx.exception).lower() + self.assertIn("youtube blocked this server", msg) + self.assertNotIn("only available in english", msg) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_v1_api.py b/tests/test_v1_api.py index 59edc99..0d426ec 100644 --- a/tests/test_v1_api.py +++ b/tests/test_v1_api.py @@ -5,6 +5,7 @@ from fastapi.testclient import TestClient +from app.core.errors import ErrorCode, TranscriptFetchError from app.main import app client = TestClient(app) @@ -58,7 +59,9 @@ def test_extract_invalid_url(self): @patch("app.api.v1_routes.get_transcript_from_url") def test_extract_transcript_not_available(self, mock_fetch): - mock_fetch.side_effect = Exception("Transcripts are disabled for this video.") + mock_fetch.side_effect = TranscriptFetchError( + ErrorCode.TRANSCRIPT_NOT_AVAILABLE, "Transcripts are disabled for this video." + ) r = client.post( "/api/v1/extract", json={ @@ -113,6 +116,43 @@ def test_convert_txt_returns_download_url(self, mock_fetch): self.assertIn("file_download_url", c) self.assertTrue(c["file_download_url"].startswith("/api/v1/download/")) + def _extract(self, mock_fetch) -> str: + mock_fetch.return_value = {**_TRANSCRIPT} + ex = client.post( + "/api/v1/extract", + json={"url": "https://www.youtube.com/watch?v=testvideo12", "include_timestamps": False}, + ) + self.assertEqual(ex.status_code, 200) + return ex.json()["file_id"] + + @patch("app.api.v1_routes.get_transcript_from_url") + def test_convert_srt_and_vtt_download_with_title(self, mock_fetch): + file_id = self._extract(mock_fetch) + for fmt, first_line in (("srt", "1"), ("vtt", "WEBVTT")): + conv = client.post("/api/v1/convert", json={"file_id": file_id, "format": fmt}) + self.assertEqual(conv.status_code, 200, conv.text) + dl = client.get(conv.json()["file_download_url"]) + self.assertEqual(dl.status_code, 200) + self.assertEqual(dl.text.splitlines()[0], first_line) + self.assertIn("00:00:00", dl.text) + self.assertIn( + f'filename="Unit Test Video.{fmt}"', dl.headers["content-disposition"] + ) + + @patch("app.api.v1_routes.get_transcript_from_url") + def test_txt_download_named_after_title(self, mock_fetch): + file_id = self._extract(mock_fetch) + dl = client.get(f"/api/v1/download/{file_id}") + self.assertEqual(dl.status_code, 200) + self.assertIn('filename="Unit Test Video.txt"', dl.headers["content-disposition"]) + + @patch("app.api.v1_routes.get_transcript_from_url") + def test_download_rejects_json_sidecars(self, mock_fetch): + file_id = self._extract(mock_fetch) + for suffix in ("segments", "meta"): + r = client.get(f"/api/v1/download/{file_id}.{suffix}.json") + self.assertEqual(r.status_code, 400) + def test_convert_rejects_bad_format(self): r = client.post( "/api/v1/convert", diff --git a/web/app/app/layout.tsx b/web/app/app/layout.tsx index bdf2383..b0b98b1 100644 --- a/web/app/app/layout.tsx +++ b/web/app/app/layout.tsx @@ -3,12 +3,12 @@ import type { Metadata } from "next"; export const metadata: Metadata = { title: "Transcript Extractor", description: - "Paste a link, preview transcript, then download as TXT, PDF, or DOCX.", + "Paste a link, preview transcript, then download as TXT, PDF, DOCX, SRT, or VTT.", alternates: { canonical: "/app" }, openGraph: { title: "Transcript Extractor", description: - "Paste a link, preview transcript, then download as TXT, PDF, or DOCX.", + "Paste a link, preview transcript, then download as TXT, PDF, DOCX, SRT, or VTT.", url: "https://usetranscriptflow.com/app", }, }; diff --git a/web/app/app/page.tsx b/web/app/app/page.tsx index 60173b4..58e8938 100644 --- a/web/app/app/page.tsx +++ b/web/app/app/page.tsx @@ -69,7 +69,7 @@ export default function AppPage() {
{[ { num: '01', title: 'Paste link', desc: 'Add a YouTube or Vimeo URL.' }, - { num: '02', title: 'Choose output', desc: 'TXT, PDF, or DOCX. Optional timestamps.' }, + { num: '02', title: 'Choose output', desc: 'TXT, PDF, DOCX, SRT, or VTT. Optional timestamps.' }, { num: '03', title: 'Download', desc: 'Preview in-page, then download the full file.' }, ].map(step => (
diff --git a/web/app/components/home/BentoGrid.tsx b/web/app/components/home/BentoGrid.tsx index 01640bb..9cfc828 100644 --- a/web/app/components/home/BentoGrid.tsx +++ b/web/app/components/home/BentoGrid.tsx @@ -65,7 +65,7 @@ export const BentoGrid: React.FC = () => {

Multi-Format

-

TXT, PDF, DOCX, JSON. Your choice.

+

TXT, PDF, DOCX, SRT, VTT. Your choice.

diff --git a/web/app/components/home/Capabilities.tsx b/web/app/components/home/Capabilities.tsx index aa3a955..de741c9 100644 --- a/web/app/components/home/Capabilities.tsx +++ b/web/app/components/home/Capabilities.tsx @@ -2,7 +2,7 @@ import React from 'react'; const items = [ { label: 'Sources', description: 'YouTube and Vimeo links' }, - { label: 'Output', description: 'TXT (default), PDF, DOCX' }, + { label: 'Output', description: 'TXT (default), PDF, DOCX, SRT, VTT' }, { label: 'Formatting', description: 'Clean text or timestamps' }, { label: 'Preview', description: 'Short preview before download' }, { label: 'Privacy', description: 'No account required' }, diff --git a/web/app/privacy/page.tsx b/web/app/privacy/page.tsx index f18f54a..2fff989 100644 --- a/web/app/privacy/page.tsx +++ b/web/app/privacy/page.tsx @@ -35,7 +35,7 @@ export default function PrivacyPage() {

Transcript Flow lets you submit a public YouTube or Vimeo URL to generate a transcript - file (for example TXT, PDF, or DOCX). You do not need an account to use the web app. + file (for example TXT, PDF, DOCX, SRT, or VTT). You do not need an account to use the web app.

diff --git a/web/components/TranscribeClient.tsx b/web/components/TranscribeClient.tsx index 5236915..f2e23b9 100644 --- a/web/components/TranscribeClient.tsx +++ b/web/components/TranscribeClient.tsx @@ -26,6 +26,16 @@ interface ErrorData { message: string; } +type DownloadFormat = 'txt' | 'pdf' | 'docx' | 'srt' | 'vtt'; + +// TXT is the primary action; the rest go through /convert. +const SECONDARY_FORMATS: { format: DownloadFormat; label: string }[] = [ + { format: 'pdf', label: 'PDF' }, + { format: 'docx', label: 'DOCX' }, + { format: 'srt', label: 'SRT' }, + { format: 'vtt', label: 'VTT' }, +]; + const STEPS = [ 'Extracting captions', 'Formatting transcript', @@ -122,7 +132,7 @@ export function TranscribeClient() { } }; - const handleDownload = async (format: 'txt' | 'pdf' | 'docx') => { + const handleDownload = async (format: DownloadFormat) => { if (!success) return; setDownloading(format); setConvertError(null); @@ -137,7 +147,7 @@ export function TranscribeClient() { return; } - // For PDF/DOCX, call the convert endpoint + // Every other format is generated by the convert endpoint const res = await fetch(`${API_BASE}/api/v1/convert`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, @@ -353,7 +363,7 @@ export function TranscribeClient() {
{/* Download controls */} -
{/* TXT β€” primary */} - {/* PDF β€” secondary */} - - {/* DOCX β€” secondary */} - + {SECONDARY_FORMATS.map(({ format, label }) => ( + + ))} {/* New Transcript link */}