from tools.interfaces.interface_video_transcriptor_provider import TranscriptProviderInterface from youtube_transcript_api import YouTubeTranscriptApi from youtube_transcript_api._errors import TranscriptsDisabled, NoTranscriptFound import re class YouTubeTranscriptProvider(TranscriptProviderInterface): def get_transcript(self, video_url_or_id: str) -> str: try: if "youtube.com" in video_url_or_id or "youtu.be" in video_url_or_id: match = re.search(r"(?:v=|\/)([0-9A-Za-z_-]{11})", video_url_or_id) video_id = match.group(1) if match else video_url_or_id else: video_id = video_url_or_id transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=['fr', 'en']) return " ".join([t["text"] for t in transcript]) or "(Transcription) Vide." except TranscriptsDisabled: return "(Transcription) Sous-titres désactivés." except NoTranscriptFound: return "(Transcription) Aucune transcription disponible." except Exception as e: return f"(Transcription) Erreur : {e}"