YouTubeTranscript

Get YouTube transcripts

DefinitionResourceCreateNotebook

ClearAll[YouTubeTranscript];
YouTubeTranscript[videoID_String]:=
Module[{url,page,captionURLs,transcript},

(*Construct video URL*)
url="https://www.youtube.com/watch?v="<>videoID;

(*Fetch the video page*)
page=Import[url,"Text"];
If[!StringQ[page], 
ResourceFunction["MessageFailure"]["Cannot fetch video page."];
Return[$Failed]
];

(*Extract caption track URLs*)
captionURLs=Flatten@StringCases[page,"\"captionTracks\":["~~Shortest[x__]~~"]":>x];
captionURLs=StringReplace[captionURLs,"\\u0026"->"&"];
captionURLs=Flatten[StringCases[captionURLs,"\"baseUrl\":\""~~Shortest[x__]~~"\"":>x]];

(*Check if captions are found*)
If[captionURLs==={},
ResourceFunction["MessageFailure"]["No captions available for this video."],
(*ELSE*)

(*Fetch the first caption track*)
transcript=Import[First[captionURLs],"Plaintext"];

(*Return formatted transcript*)
If[!StringQ[transcript],
ResourceFunction["MessageFailure"]["No transcript text found."];
$Failed,
(*ELSE*)
StringReplace[transcript,"&#39;"->"'"]
]
]
];

Documentation

UsageResourceCreateNotebook

Details & OptionsResourceCreateNotebook

YouTubeTranscript extracts the captions of the video, if they exist.

The transcript is returned as plain text.

The YouTube Data API has usage quotas

Not all YouTube videos have automatic or manual captions. If no captions are available, the script returns a message indicating this.

YouTubeTranscript processes "captionTracks" of the YouTube Data API, which is a field of YouTube's video metadata.

The field "captionTracks" is an array of objects, where each object represents a single caption track (e.g., for a specific language or type).

From "captionTracks" the "baseURL" string is extracted, which is the URL to fetch the caption content.

ExamplesResourceCreateNotebook

Basic Examples

Get a video transcript:

transcript=YouTubeTranscript["ewU83vHwN8Y"];
transcript//StringLength

Here is an excerpt:

SeedRandom[332];
lines=StringSplit[transcript,"\n"];
p=RandomInteger[Length[lines]-10];
lines[[p;;p+10]]//StringRiffle[#,"\n"]&

Scope

If the video identifier is not found or the video has no captions Failure object is returned. For example:

YouTubeTranscript["89328ewU83vHwN8Y"]

Options

Applications

Get a video transcript:

transcript=YouTubeTranscript["_yUW-TGGKOc"];

Show the number of characters, words, and lines:

Clear[TextStats];
TextStats[txt_String]:=AssociationThread[{"Characters","Words","Lines"},Through[{StringLength,Length@*TextWords,Length[StringSplit[#,"\n"]]&}[txt]]];
TextStats[transcript]

Summarize the transcript:

LLMResourceFunction["Summarize"][transcript]

Properties and Relations

Possible Issues

Neat Examples

Get a video transcript and show table of themes:

transcript=YouTubeTranscript["_yUW-TGGKOc"];
Clear[GridTableFormFromJSON];
GridTableFormFromJSON[json_String]:=ResourceFunction["GridTableForm"][Dataset[Association/@ImportString[StringReplace[json,{"```json"->"","```"->""}],"JSON"]]/.{x_String:>Style[x,FontFamily->"Times New Roman"]}];
GridTableFormFromJSON[LLMResourceFunction["ThemeTableJSON"][transcript, "article", 30]]

Source & Additional Information

Contributed ByResourceCreateNotebook

Anton Antonov

KeywordsResourceCreateNotebook

YouTube

Video

Transcript

CategoriesResourceCreateNotebook

Related SymbolsResourceCreateNotebook

Related Resource ObjectsResourceCreateNotebook

VideoReverse

ImportVTT

Source/Reference CitationResourceCreateNotebook

Source, reference or citation information

LinksResourceCreateNotebook

Live CEOing Ep 886: Design Review of LLMGraph - YouTube

Live CEOing Ep 887: Design Review of Control and System Modeling - YouTube

TestsResourceCreateNotebook

StringQ[YouTubeTranscript["ewU83vHwN8Y"]]

CompatibilityResourceCreateNotebook

Wolfram Language VersionResourceCreateNotebook

14.0+

Operating SystemResourceCreateNotebook

EnvironmentsResourceCreateNotebook

Cloud SupportResourceCreateNotebook

Required FeaturesResourceCreateNotebook

Author NotesResourceCreateNotebook

Additional information about limitations, issues, etc.

Submission NotesResourceCreateNotebook

I do not know how to link to the prompt "ThemeTableJSON".