get_subtitles
Generate subtitle files for YouTube videos in your preferred language and format by simply providing the video URL. Ideal for accessibility and localization projects.
Instructions
Generate subtitle files for a YouTube video
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| format | No | srt | |
| language | No | en | |
| url | Yes |
Implementation Reference
- The handler function for the 'get_subtitles' tool. It validates input, processes the YouTube video if needed using helper functions, calls the external API to fetch subtitles in the specified language and format, and returns the subtitle content or an error message.@mcp_server.tool(name="get_subtitles", description="Generate subtitle files for a YouTube video") async def get_subtitles(url: str, language: str = "en", format: str = "srt") -> str: """Generate subtitle files for a YouTube video. This tool processes a video and generates subtitle files in the specified format and language. It first checks if the subtitles already exist before processing the video to optimize performance. If the requested language is not available, it automatically requests a translation first. Args: url: The YouTube video URL language: Language code for subtitles (e.g., "en", "fr", "es") format: Subtitle format, either "srt" or "vtt" (default: "srt") Returns: The subtitles content as text """ logger.info(f"Getting subtitles for URL: {url}, language: {language}, format: {format}") # Validate format if format not in ["srt", "vtt"]: error_msg = f"Invalid format: {format}. Must be 'srt' or 'vtt'." logger.error(error_msg) return f"Error: {error_msg}" # Process the video to ensure it's ready success, video_id, error_message = await process_video(url) if not success: logger.error(f"Failed to process video: {error_message}") return f"Error: {error_message}" # Get the subtitles from the API subtitles_response = await make_yt_api_request( f"/api/videos/{video_id}/subtitles", params={"language": language, "format": format} ) if not subtitles_response: error_msg = f"Failed to retrieve subtitles for language: {language}, format: {format}" logger.error(error_msg) return f"Error: {error_msg}" # The response could be the subtitles as text or a JSON object with an error if isinstance(subtitles_response, dict) and "error" in subtitles_response: error_msg = subtitles_response["error"] logger.error(f"API error: {error_msg}") return f"Error: {error_msg}" return subtitles_response