Saving a YouTube Video transcript to a text file

Learning that lasts. Online courses from $14.99

When watching YouTube video lectures, I often wish to save the dialog to a text file that I can search. You can obtain a text transcript of the audio for a YouTube video lecture by clicking on the three dots to the right of "Share", which will give you the option to "Open transcript."

YouTube open transcript option

You will then see a transcript displayed to the right of the video like the one shown below.

YouTube transcript

You can select all of the text in the transcript and copy it to a text editor, by clicking to the left of "00:00" and then dragging the mouse downwards utnil you've highlighted all of the text in the transcript. For many operating systems and browsers, you can then hit Ctrl-C to copy the text which you can paste into a text editor with Ctrl-V But when I would do so, the text would look similar to the following.

00:29
The multiverse gives me chills.
00:34
What could be more startling, arresting than many universes,
00:38
multiple universe, innumerable universes,
00:41
perhaps an infinite number of universes?
00:46
Our own one single universe alone is astonishingly vast.
00:52
Even the small part we see has two trillion galaxies,
00:56
and many galaxies have a hundred billion or more stars
00:59
and even more planets.
01:04
Now leap to the unimaginable multiverse.

But I want the timestamps to be on the same line as the accompanying text. I can use a regular expression (regexp) in a text editor such as vi or vim to make that change. The vi editor is usually present on Linux and macOS systems and you can download a version for Microsoft Windows systems. To put the timestamp on the same line as the associated text, I can hit the Esc key and then the colon key to enter command mode in the editor and then type the following command at the colon prompt:

1,$ s/\(:\d\d\)\n/\1 /

The "$" indicates the last line in the file and 1,$ indicates I want the command to apply from the first line in the file to the last line. The "s" indicates to the editor that I want to enter a "substitute" command. The pattern between the set of forward slashes, i.e., /, is what I want the editor to search for on each line. That pattern is \(:\d\d\)\n. I put a backslash, i.e., a \, before characters that have special meaning in the pattern, because that character is an "escape character" that will allow me to employ an alternate meaning for the character that follows. I want to look for lines that have a colon followed by two digits and then the newline character. The two instances of \d after the colon in the pattern indicate I want to look for a colon followed by two occurrences of any digits. The \n represents the newline character. I put the :\d\d in parentheses, since I want the editor to remember that part of the search pattern, because I need to reuse it in the replacement string. The replacement string is between the last two occurrences of the slash character and is a \1 followed by a space. The \1 tells the editor that I want to put the first saved pattern in that place. In this case the saved pattern is the colon followed by two digits, i.e., what appeared within the first set, and in this case only set, of parentheses. When I enter the command, I will then see the lines changed as follows.

00:29 The multiverse gives me chills.
00:34 What could be more startling, arresting than many universes,
00:38 multiple universe, innumerable universes,
00:41 perhaps an infinite number of universes?
00:46 Our own one single universe alone is astonishingly vast.
00:52 Even the small part we see has two trillion galaxies,
00:56 and many galaxies have a hundred billion or more stars
00:59 and even more planets.
01:04 Now leap to the unimaginable multiverse.

If I want to add a blank line between each line of text as with the YouTube display of the text, I can then hit Esc and then the colon (":") key to enter the command below at the colon prompt.

1,$ s/$/\r/

The transcript will then look as follows.

00:29 The multiverse gives me chills.

00:34 What could be more startling, arresting than many universes,

00:38 multiple universe, innumerable universes,

00:41 perhaps an infinite number of universes?

00:46 Our own one single universe alone is astonishingly vast.

00:52 Even the small part we see has two trillion galaxies,

00:56 and many galaxies have a hundred billion or more stars

00:59 and even more planets.

01:04 Now leap to the unimaginable multiverse.