An open API service indexing awesome lists of open source software.

https://github.com/aliencube/youtube-subtitles-extractor

This is the library to retrieve subtitles from a given YouTube video, inspired by https://github.com/devhims/youtube-caption-extractor
https://github.com/aliencube/youtube-subtitles-extractor

caption hacktoberfest subtitle video youtube

Last synced: 7 months ago
JSON representation

This is the library to retrieve subtitles from a given YouTube video, inspired by https://github.com/devhims/youtube-caption-extractor

Awesome Lists containing this project

README

          

# ![Icon](https://raw.githubusercontent.com/aliencube/youtube-subtitles-extractor/main/icons/YouTubeSubtitlesExtractor-64x64.png) YouTube Subtitles Extractor

![Build and Test](https://github.com/aliencube/youtube-subtitles-extractor/workflows/Build%20and%20Test/badge.svg) [![downloads](https://img.shields.io/nuget/dt/Aliencube.YouTubeSubtitlesExtractor.svg)](https://www.nuget.org/packages/Aliencube.YouTubeSubtitlesExtractor) [![version](https://img.shields.io/nuget/v/Aliencube.YouTubeSubtitlesExtractor.svg)](https://www.nuget.org/packages/Aliencube.YouTubeSubtitlesExtractor)

This is the NuGet package library that retrieves subtitles from a given YouTube video, inspired by [@devhims](https://github.com/devhims)' [YouTube Caption Extractor](https://github.com/devhims/youtube-caption-extractor).

## Known Issues

- If you use this library on your [Blazor WebAssembly](https://learn.microsoft.com/aspnet/core/blazor/hosting-models#blazor-webassembly) project, you might encounter the [CORS error](https://developer.mozilla.org/docs/Web/HTTP/CORS). The only workaround is to use a facade API to retrieve the subtitles.

## Getting Started

1. Install the [NuGet package](https://www.nuget.org/packages/Aliencube.YouTubeSubtitlesExtractor) to your project.
1. Add the namespace to your code.

```csharp
using Aliencube.YouTubeSubtitlesExtractor;
```

1. Get the YouTube video URL.

```csharp
var youtubeUrl = "https://www.youtube.com/watch?v=i8tMiWHK05M";
```

1. Create an instance of `YouTubeVideo` class.

```csharp
var http = new HttpClient();
var youtube = new YouTubeVideo(http);
```

1. Extract subtitles from the given YouTube video URL. There are a few options to extract subtitles.

```csharp
// Extract video details from the given YouTube video URL.
VideoDetails details = await youtube.ExtractVideoDetailsAsync(youtubeUrl);

// Extract a single subtitle from the given YouTube video URL.
// - defaults to English (en)
Subtitle subtitle = await youtube.ExtractSubtitleAsync(youtubeUrl);

// Extract a single subtitle from the given YouTube video URL with the specified language code.
// eg) Korean (ko)
Subtitle subtitle = await youtube.ExtractSubtitleAsync(youtubeUrl, "ko");

// Extract list of subtitles from the given VideoOptions instance.
// eg) English and Korean (ko)
var options = new VideoOptions { Url = youtubeUrl, LanguageCodes = { "en", "ko" } };
List subtitles = await youtube.ExtractSubtitlesAsync(options);
```

## Sample Console App

You can find a sample console app to extract YouTube video details from [here](./samples/YouTubeSubtitlesExtractor.ConsoleApp). Alternatively, run the following command to run the console app.

```bash
dotnet run --project ./samples/YouTubeSubtitlesExtractor.ConsoleApp
```

Then, enter a YouTube link to get the details and available language codes. After you enter the language code, it will display the subtitles in the given language.

## Issues or Feedbacks

Please leave any issues or feedbacks on the [GitHub Issue page](https://github.com/aliencube/youtube-subtitles-extractor/issues).

## TO-DOs

- [ ] devcontainer settings

## Acknowledgments

- Icons made by [Freepik](https://www.flaticon.com/authors/freepik) from [flaticon.com](https://flaticon.com/).