Python 3 Selenium Youtube Bot to Scrape Video Subtitles & Transcript and Save it inside Text File Using Video ID on Command Line

 

YouTube Transcript/Subtitle API (including automatically generated subtitles and subtitle translations)

Donate Build Status Coverage Status MIT license image image

This is a python API which allows you to get the transcript/subtitles for a given YouTube video. It also works for automatically generated subtitles, supports translating subtitles and it does not require a headless browser, like other selenium based solutions do!

Install

It is recommended to install this module by using pip:

If you want to use it from source, you’ll have to install the dependencies manually:

You can either integrate this module into an existing application, or just use it via an CLI.

API

The easiest way to get a transcript for a given video is to execute:

This will return a list of dictionaries looking somewhat like this:

You can also add the languages param if you want to make sure the transcripts are retrieved in your desired language (it defaults to english).

It’s a list of language codes in a descending priority. In this example it will first try to fetch the german transcript ('de') and then fetch the english transcript ('en') if it fails to do so. If you want to find out which languages are available first, have a look at list_transcripts()

To get transcripts for a list of video ids you can call:

languages also is optional here.

List available transcripts

If you want to list all transcripts which are available for a given video you can call:

This will return a TranscriptList object which is iterable and provides methods to filter the list of transcripts for specific languages and types, like:

By default this module always picks manually created transcripts over automatically created ones, if a transcript in the requested language is available both manually created and generated. The TranscriptList allows you to bypass this default behaviour by searching for specific transcript types:

The methods find_generated_transcriptfind_manually_created_transcriptfind_generated_transcript return Transcript objects. They contain metadata regarding the transcript:

and provide the method, which allows you to fetch the actual transcript data:

Translate transcript

YouTube has a feature which allows you to automatically translate subtitles. This module also makes it possible to access this feature. To do so Transcript objects provide a translate() method, which returns a new translated Transcript object:

By example

Using Formatters

Formatters are meant to be an additional layer of processing of the transcript you pass it. The goal is to convert the transcript from its Python data type into a consistent string of a given “format”. Such as a basic text (.txt) or even formats that have a defined specification such as JSON (.json), WebVTT format (.vtt), Comma-separated format (.csv), etc…

The formatters submodule provides a few basic formatters to wrap around you transcript data in cases where you might want to do something such as output a specific format then write that format to a file. Maybe to backup/store and run another script against at a later time.

We provided a few subclasses of formatters to use:

  • JSONFormatter
  • PrettyPrintFormatter
  • TextFormatter
  • WebVTTFormatter (a basic implementation)

Here is how to import from the formatters module.

Provided Formatter Example

Lets say we wanted to retrieve a transcript and write that transcript as a JSON file in the same format as the API returned it as. That would look something like this:

Passing extra keyword arguments

Since JSONFormatter leverages json.dumps() you can also forward keyword arguments into .format_transcript(transcript) such as making your file output prettier by forwarding the indent=2 keyword argument.

Custom Formatter Example

You can implement your own formatter class. Just inherit from the Formatter base class and ensure you implement the format_transcript(self, transcript, **kwargs) and format_transcripts(self, transcripts, **kwargs) methods which should ultimately return a string when called on your formatter instance.

CLI

Execute the CLI script using the video ids as parameters and the results will be printed out to the command line:

The CLI also gives you the option to provide a list of preferred languages:

You can also specify if you want to exclude automatically generated or manually created subtitles:

If you would prefer to write it into a file or pipe it into another application, you can also output the results as json using the following line:

Translating transcripts using the CLI is also possible:

If you are not sure which languages are available for a given video you can call, to list all available transcripts:

If a video’s ID starts with a hyphen you’ll have to mask the hyphen using \ to prevent the CLI from mistaking it for a argument name. For example to get the transcript for the video with the ID -abc123 run:

Proxy

You can specify a https proxy, which will be used during the requests to YouTube:

As the proxies dict is passed on to the requests.get(...) call, it follows the format used by the requests library.

Using the CLI:

Cookies

Some videos are age restricted, so this module won’t be able to access those videos without some sort of authentication. To do this, you will need to have access to the desired video in a browser. Then, you will need to download that pages cookies into a text file. You can use the Chrome extension cookies.txt or the Firefox extension cookies.txt.

Once you have that, you can use it with the module to access age-restricted videos’ captions like so.

Using the CLI:

 

 

https://github.com/jdepoix/youtube-transcript-api

 

 

Leave a Reply