Voici un script python très utile pour récupérer les sous-titres d’une vidéo YouTube. On va utiliser pour cela la librairie YouTube Transcript API.
Prérequis : installer YouTube Transcript API
Lance un terminal et installe la libraire YouTube Transcript API comme ceci :
pip3 install youtube-transcript-api
Lance ce code Python depuis ton terminal
Enregistre le code python suivant dans un fichier youtube-transcript.py
import re
def extract_youtube_video_id(url):
# Pattern pour extraire l'ID de la vidéo YouTube depuis l'URL
pattern = r"(?<=v=)[a-zA-Z0-9_-]+"
# Recherche du pattern dans l'URL
match = re.search(pattern, url)
if match:
return match.group()
else:
return None
# Demande à l'utilisateur de saisir l'URL YouTube
url = input("Veuillez saisir l'URL de la vidéo YouTube : ")
# Extraction de l'ID de la vidéo
videoID = extract_youtube_video_id(url)
if videoID:
print("L'ID de la vidéo YouTube est :", videoID)
else:
print("Impossible de trouver l'ID de la vidéo YouTube dans l'URL fournie.")
#Récupération de la langue des sous-titres
langue = input("Veuillez saisir la langue des sous-titres YouTube : fr, en, es, de ?")
from youtube_transcript_api import YouTubeTranscriptApi
from youtube_transcript_api.formatters import TextFormatter
transcript = YouTubeTranscriptApi.get_transcript(videoID, languages=[langue])
formatter = TextFormatter()
# .format_transcript(transcript) turns the transcript into a TXT string.
txt_formatted = formatter.format_transcript(transcript)
# Now we can write it out to a file.
with open('transcript.txt', 'w', encoding='utf-8') as txt_file:
txt_file.write(txt_formatted)
Execute le script depuis ton terminal
python3 youtube-transcript.py
On va te demander ensuite de fournir ton URL YouTube dans ce format : https://www.youtube.com/watch?v=YpPxMtvUgwU
Et la langue de tes sous-titres, mets fr pour du français

A la suite de l’exécution du script, un fichier transcript.txt situé dans le même répertoire que ton script python contiendra tes sous-titres

Le fichier texte quand je l’ouvre contient bien mes sous-titres

Besoin d’un fichier SRT ?
Pour avoir un fichier SRT à la place du fichier .txt, remplace ceci dans le code :
formatter = TextFormatter()
# .format_transcript(transcript) turns the transcript into a TXT string.
txt_formatted = formatter.format_transcript(transcript)
# Now we can write it out to a file.
with open('transcript.txt', 'w', encoding='utf-8') as txt_file:
txt_file.write(txt_formatted)
par ceci :
from youtube_transcript_api import YouTubeTranscriptApi
from youtube_transcript_api.formatters import SRTFormatter
transcript = YouTubeTranscriptApi.get_transcript(videoID, languages=[langue])
formatter = SRTFormatter()
# .format_transcript(transcript) turns the transcript into a TXT string.
txt_formatted = formatter.format_transcript(transcript)
# Now we can write it out to a file.
with open('transcription.srt', 'w', encoding='utf-8') as txt_file:
txt_file.write(txt_formatted)
Execute le code comme avant :
python3 transcript-youtube.py
Et tu auras cette fois un fichier transcription.srt

Ce sont les sous-titres avec les time codes :

Un Google colab pour faire ces manipulations en ligne sans installer Python sur ta machine
Voici un Google Colab pour générer ses sous-titres en ligne
