youtube_transcript_api

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 30, 2025 License: MIT Imports: 11 Imported by: 0

README

YouTube Transcript API - Go Implementation

Go Reference Go Report Card

This is a Go implementation of the YouTube Transcript API, ported from jdepoix/youtube-transcript-api (Python version).

This project maintains the same API design and feature set as the original Python version, reimplemented in Go for better performance and type safety.

Documentation: pkg.go.dev/github.com/dogslee/youtube_transcript_api

Features

  • Get available transcript list for YouTube videos
  • Download transcript content in specified languages
  • Support for both manually created and auto-generated transcripts
  • Support for transcript translation
  • Multiple output formats (JSON, SRT, WebVTT, plain text)
  • Proxy configuration support (generic proxy and Webshare proxy)
  • Command-line tool with batch processing support

Requirements

  • Go version: 1.19.0 or higher

For detailed compatibility information, please refer to COMPATIBILITY.md

Installation

As a Library
go get github.com/dogslee/youtube_transcript_api
Install Command-Line Tool

Build and install with a custom executable name:

# Clone the repository
git clone https://github.com/dogslee/youtube_transcript_api.git
cd youtube_transcript_api

# Build and install with custom name
go build -o youtube-transcript-api ./cmd
sudo mv youtube-transcript-api /usr/local/bin/

# Or install to your local bin directory
go build -o ~/go/bin/youtube-transcript-api ./cmd

After installation, ensure that /usr/local/bin or ~/go/bin is in your PATH environment variable, then you can use it:

youtube-transcript-api dQw4w9WgXcQ
Method 2: Using go install (with alias)

Install using go install and create an alias:

go install github.com/dogslee/youtube_transcript_api/cmd@latest

# Create an alias (add to your ~/.bashrc, ~/.zshrc, etc.)
alias youtube-transcript-api='cmd'

# Or create a symbolic link
ln -s $(go env GOPATH)/bin/cmd $(go env GOPATH)/bin/youtube-transcript-api
Method 3: Using Install Script (Easiest)

Use the provided install script:

# Clone the repository
git clone https://github.com/dogslee/youtube_transcript_api.git
cd youtube_transcript_api

# Run the install script
./install.sh

# Or install to a custom directory
INSTALL_DIR=~/bin ./install.sh

The script will:

  • Build the binary with the proper name (youtube-transcript-api)
  • Install it to the appropriate directory
  • Check if the directory is in your PATH
  • Provide instructions if PATH needs to be updated

Uninstallation:

To uninstall the command-line tool:

# If installed via Method 1
sudo rm /usr/local/bin/youtube-transcript-api
# Or
rm ~/go/bin/youtube-transcript-api

# If installed via Method 2
rm $(go env GOPATH)/bin/cmd
rm $(go env GOPATH)/bin/youtube-transcript-api  # if symlink was created

Usage Examples

As a Library
package main

import (
    "fmt"
    yt "github.com/dogslee/youtube_transcript_api"
)

func main() {
    // Create API instance
    api, err := yt.NewYouTubeTranscriptApi(nil)
    if err != nil {
        panic(err)
    }
    
    // Fetch transcript
    transcript, err := api.Fetch("video_id", []string{"en"}, false)
    if err != nil {
        panic(err)
    }
    
    // Print transcript
    for _, snippet := range transcript.Snippets {
        fmt.Printf("[%.2f] %s\n", snippet.Start, snippet.Text)
    }
}
Get Available Transcript List
api, _ := yt.NewYouTubeTranscriptApi(nil)
transcriptList, err := api.List("video_id")
if err != nil {
    panic(err)
}

// Find transcript in specified languages
transcript, err := transcriptList.FindTranscript([]string{"en", "zh"})
if err != nil {
    panic(err)
}

// Fetch transcript content
fetched, err := transcript.Fetch(false)
if err != nil {
    panic(err)
}
Using Proxy
// Generic proxy
proxyConfig, _ := yt.NewGenericProxyConfig("http://proxy.example.com:8080", "")

// Webshare proxy
proxyConfig, _ := yt.NewWebshareProxyConfig(
    "username",
    "password",
    nil, // filterIPLocations
    10,  // retriesWhenBlocked
    "",  // domainName (use default)
    0,   // proxyPort (use default)
)

api, _ := yt.NewYouTubeTranscriptApi(proxyConfig)
Format Output
formatterLoader := yt.NewFormatterLoader()

// JSON format
jsonFormatter, _ := formatterLoader.Load("json")
jsonOutput, _ := jsonFormatter.FormatTranscript(transcript)

// SRT format
srtFormatter, _ := formatterLoader.Load("srt")
srtOutput, _ := srtFormatter.FormatTranscript(transcript)

// WebVTT format
webvttFormatter, _ := formatterLoader.Load("webvtt")
webvttOutput, _ := webvttFormatter.FormatTranscript(transcript)

// Plain text format
textFormatter, _ := formatterLoader.Load("text")
textOutput, _ := textFormatter.FormatTranscript(transcript)

Command-Line Tool

Installation Methods

Method 1: Build with Custom Name (Recommended)

Build and install with a custom executable name:

# Clone the repository
git clone https://github.com/dogslee/youtube_transcript_api.git
cd youtube_transcript_api

# Build and install
go build -o youtube-transcript-api ./cmd
sudo mv youtube-transcript-api /usr/local/bin/

# Or install to your local bin directory
go build -o ~/go/bin/youtube-transcript-api ./cmd

Method 2: Using go install (with alias)

Install using go install and create an alias or symbolic link:

go install github.com/dogslee/youtube_transcript_api/cmd@latest

# Create an alias (add to your ~/.bashrc, ~/.zshrc, etc.)
alias youtube-transcript-api='cmd'

# Or create a symbolic link
ln -s $(go env GOPATH)/bin/cmd $(go env GOPATH)/bin/youtube-transcript-api
Usage Examples
# Fetch transcript
youtube-transcript-api dQw4w9WgXcQ

# List available transcripts
youtube-transcript-api --list-transcripts dQw4w9WgXcQ

# Specify languages
youtube-transcript-api --languages "en zh" dQw4w9WgXcQ

# Specify output format
youtube-transcript-api --format json dQw4w9WgXcQ

# Translate transcript
youtube-transcript-api --translate zh dQw4w9WgXcQ

# Use proxy
youtube-transcript-api --http-proxy "http://proxy.example.com:8080" dQw4w9WgXcQ

API Documentation

YouTubeTranscriptApi

The main API interface.

NewYouTubeTranscriptApi(proxyConfig ProxyConfig) (*YouTubeTranscriptApi, error)

Create a new API instance.

Parameters:

  • proxyConfig: Optional proxy configuration

Returns:

  • *YouTubeTranscriptApi: API instance
  • error: Error information
Fetch(videoID string, languages []string, preserveFormatting bool) (*FetchedTranscript, error)

Fetch transcript for a single video.

Parameters:

  • videoID: Video ID (not the full URL)
  • languages: List of language codes (ordered by priority)
  • preserveFormatting: Whether to preserve HTML formatting tags

Returns:

  • *FetchedTranscript: Fetched transcript
  • error: Error information
List(videoID string) (*TranscriptList, error)

Get the list of available transcripts for a video.

Parameters:

  • videoID: Video ID

Returns:

  • *TranscriptList: Transcript list
  • error: Error information
TranscriptList

Transcript list object.

FindTranscript(languageCodes []string) (*Transcript, error)

Find transcript (preferring manually created ones).

FindManuallyCreatedTranscript(languageCodes []string) (*Transcript, error)

Find only manually created transcripts.

FindGeneratedTranscript(languageCodes []string) (*Transcript, error)

Find only auto-generated transcripts.

Transcript

Transcript object.

Fetch(preserveFormatting bool) (*FetchedTranscript, error)

Fetch the actual transcript content.

Translate(languageCode string) (*Transcript, error)

Translate to the specified language.

Formatter

Formatter interface.

FormatTranscript(transcript *FetchedTranscript) (string, error)

Format a single transcript.

FormatTranscripts(transcripts []*FetchedTranscript) (string, error)

Format multiple transcripts.

Error Handling

All errors implement the error interface. Main error types include:

  • CouldNotRetrieveTranscript: Base class for transcript retrieval failures
  • VideoUnavailable: Video is unavailable
  • VideoUnplayable: Video is unplayable
  • TranscriptsDisabled: Transcripts are disabled
  • NoTranscriptFound: No transcript found
  • RequestBlocked: Request blocked (IP banned)
  • AgeRestricted: Age-restricted video
  • And more...

Notes

  1. Thread Safety: YouTubeTranscriptApi is not thread-safe. In multi-threaded environments, each thread needs to create its own instance.

  2. IP Bans: YouTube may ban IPs that make frequent requests. It is recommended to use proxies or rotate IPs.

  3. Cookie Authentication: Cookie authentication is currently not supported, so transcripts for age-restricted videos cannot be retrieved.

  4. API Changes: YouTube may change its API structure, which may cause some features to fail.

Acknowledgments

This project is a Go implementation ported from jdepoix/youtube-transcript-api.

Thanks to the original project author @jdepoix and contributors for creating and maintaining the excellent Python version, which provided important reference and design foundation for this project.

The original project uses the MIT license, and this project also uses the MIT license to maintain license consistency.

License

This project is licensed under the MIT License.

Copyright (c) 2025 dogslee

The MIT License is a permissive open-source license that allows you to freely use, modify, and distribute the code, as long as you retain the copyright notice and license text.

Documentation

Overview

Package youtube_transcript_api provides a Go implementation of the YouTube Transcript API. It allows you to retrieve transcripts/subtitles for YouTube videos, including automatically generated subtitles, without requiring an API key or headless browser.

This package is a Go port of the Python youtube-transcript-api library (https://github.com/jdepoix/youtube-transcript-api).

Example usage:

api, err := youtube_transcript_api.NewYouTubeTranscriptApi(nil)
if err != nil {
	log.Fatal(err)
}

transcript, err := api.Fetch("video_id", []string{"en"}, false)
if err != nil {
	log.Fatal(err)
}

for _, snippet := range transcript.Snippets {
	fmt.Printf("[%.2f] %s\n", snippet.Start, snippet.Text)
}

Index

Constants

View Source
const (
	WebshareDefaultDomainName = "p.webshare.io"
	WebshareDefaultPort       = 80
)
View Source
const (
	WatchURLTemplate        = "https://www.youtube.com/watch?v=%s"
	InnertubeAPIURLTemplate = "https://www.youtube.com/youtubei/v1/player?key=%s"
	ThumbnailURLTemplate    = "https://img.youtube.com/vi/%s/default.jpg"
)

YouTube API 相关常量

View Source
const Version = "1.0.0"

Variables

View Source
var InnertubeContext = map[string]interface{}{
	"context": map[string]interface{}{
		"client": map[string]interface{}{
			"clientName":    "ANDROID",
			"clientVersion": "20.10.38",
		},
	},
}

InnertubeContext 是调用 YouTube InnerTube API 时使用的客户端上下文

Functions

func SetupHTTPClientProxy

func SetupHTTPClientProxy(client *HTTPClient, proxyConfig ProxyConfig) error

SetupHTTPClientProxy 为 HTTP 客户端设置代理

Types

type AgeRestricted

type AgeRestricted struct {
	*CouldNotRetrieveTranscript
}

AgeRestricted 年龄限制视频

func NewAgeRestricted

func NewAgeRestricted(videoID string) *AgeRestricted

func (*AgeRestricted) Cause

func (e *AgeRestricted) Cause() string

type CLIConfig

type CLIConfig struct {
	VideoIDs               []string
	ListTranscripts        bool
	Languages              []string
	ExcludeGenerated       bool
	ExcludeManuallyCreated bool
	Format                 string
	Translate              string
	WebshareProxyUsername  string
	WebshareProxyPassword  string
	HTTPProxy              string
	HTTPSProxy             string
}

CLIConfig 命令行配置

type CookieError

type CookieError struct {
	*YouTubeTranscriptApiException
}

CookieError Cookie 相关错误

type CookieInvalid

type CookieInvalid struct {
	*CookieError
	Path string
}

CookieInvalid Cookie 无效

func NewCookieInvalid

func NewCookieInvalid(path string) *CookieInvalid

type CookiePathInvalid

type CookiePathInvalid struct {
	*CookieError
	Path string
}

CookiePathInvalid Cookie 路径无效

func NewCookiePathInvalid

func NewCookiePathInvalid(path string) *CookiePathInvalid

type CouldNotRetrieveTranscript

type CouldNotRetrieveTranscript struct {
	*YouTubeTranscriptApiException
	VideoID string
}

CouldNotRetrieveTranscript 无法获取字幕的基类

func (*CouldNotRetrieveTranscript) Cause

func (*CouldNotRetrieveTranscript) Error

type FailedToCreateConsentCookie

type FailedToCreateConsentCookie struct {
	*CouldNotRetrieveTranscript
}

FailedToCreateConsentCookie 创建同意 Cookie 失败

func NewFailedToCreateConsentCookie

func NewFailedToCreateConsentCookie(videoID string) *FailedToCreateConsentCookie

func (*FailedToCreateConsentCookie) Cause

type FetchedTranscript

type FetchedTranscript struct {
	Title        string // 视频标题
	ThumbnailURL string // 视频封面URL
	Snippets     []FetchedTranscriptSnippet
	VideoID      string // 视频ID
	Language     string // 字幕语言
	LanguageCode string // 字幕语言代码
	IsGenerated  bool   // 是否是自动生成的字幕
}

FetchedTranscript 表示一个完整的已获取字幕

func (*FetchedTranscript) ToRawData

func (ft *FetchedTranscript) ToRawData() []map[string]interface{}

ToRawData 转换为原始数据格式(用于 JSON 序列化)

type FetchedTranscriptSnippet

type FetchedTranscriptSnippet struct {
	Text     string  // 字幕文本内容
	Start    float64 // 字幕在视频中出现的开始时间(秒)
	Duration float64 // 字幕在屏幕上显示的持续时间(秒,注意:不是语音时长,可能存在重叠)
}

FetchedTranscriptSnippet 表示一个字幕片段

type Formatter

type Formatter interface {
	FormatTranscript(transcript *FetchedTranscript) (string, error)
	FormatTranscripts(transcripts []*FetchedTranscript) (string, error)
}

Formatter 格式化器接口

type FormatterLoader

type FormatterLoader struct {
	// contains filtered or unexported fields
}

FormatterLoader 格式化器加载器

func NewFormatterLoader

func NewFormatterLoader() *FormatterLoader

NewFormatterLoader 创建格式化器加载器

func (*FormatterLoader) Load

func (fl *FormatterLoader) Load(formatterType string) (Formatter, error)

Load 加载指定类型的格式化器

type GenericProxyConfig

type GenericProxyConfig struct {
	HTTPURL  string
	HTTPSURL string
}

GenericProxyConfig 通用 HTTP/HTTPS/SOCKS 代理配置

func NewGenericProxyConfig

func NewGenericProxyConfig(httpURL, httpsURL string) (*GenericProxyConfig, error)

NewGenericProxyConfig 创建通用代理配置

func (*GenericProxyConfig) PreventKeepingConnectionsAlive

func (g *GenericProxyConfig) PreventKeepingConnectionsAlive() bool

func (*GenericProxyConfig) RetriesWhenBlocked

func (g *GenericProxyConfig) RetriesWhenBlocked() int

func (*GenericProxyConfig) ToProxyURLs

func (g *GenericProxyConfig) ToProxyURLs() (httpURL, httpsURL string)

type HTTPClient

type HTTPClient struct {
	Headers    map[string]string
	HTTPProxy  *url.URL
	HTTPSProxy *url.URL
	Jar        *cookiejar.Jar
	// contains filtered or unexported fields
}

HTTPClient HTTP 客户端包装

func NewHTTPClient

func NewHTTPClient() (*HTTPClient, error)

NewHTTPClient 创建新的 HTTP 客户端

func (*HTTPClient) Get

func (c *HTTPClient) Get(url string) (*http.Response, error)

Get 发送 GET 请求

func (*HTTPClient) Post

func (c *HTTPClient) Post(url string, contentType string, body io.Reader) (*http.Response, error)

Post 发送 POST 请求

type InvalidProxyConfig

type InvalidProxyConfig struct {
	Message string
}

InvalidProxyConfig 代理配置无效错误

func (*InvalidProxyConfig) Error

func (e *InvalidProxyConfig) Error() string

type InvalidVideoId

type InvalidVideoId struct {
	*CouldNotRetrieveTranscript
}

InvalidVideoId 无效的视频 ID

func NewInvalidVideoId

func NewInvalidVideoId(videoID string) *InvalidVideoId

func (*InvalidVideoId) Cause

func (e *InvalidVideoId) Cause() string

type IpBlocked

type IpBlocked struct {
	*RequestBlocked
}

IpBlocked IP 被封禁

func NewIpBlocked

func NewIpBlocked(videoID string) *IpBlocked

func (*IpBlocked) Cause

func (e *IpBlocked) Cause() string

type JSONFormatter

type JSONFormatter struct{}

JSONFormatter JSON 格式输出

func (*JSONFormatter) FormatTranscript

func (f *JSONFormatter) FormatTranscript(transcript *FetchedTranscript) (string, error)

func (*JSONFormatter) FormatTranscripts

func (f *JSONFormatter) FormatTranscripts(transcripts []*FetchedTranscript) (string, error)

type NoTranscriptFound

type NoTranscriptFound struct {
	*CouldNotRetrieveTranscript
	RequestedLanguageCodes []string
	TranscriptData         *TranscriptList
}

NoTranscriptFound 未找到字幕

func NewNoTranscriptFound

func NewNoTranscriptFound(videoID string, requestedLanguageCodes []string, transcriptData *TranscriptList) *NoTranscriptFound

func (*NoTranscriptFound) Cause

func (e *NoTranscriptFound) Cause() string

type NotTranslatable

type NotTranslatable struct {
	*CouldNotRetrieveTranscript
}

NotTranslatable 不可翻译

func NewNotTranslatable

func NewNotTranslatable(videoID string) *NotTranslatable

func (*NotTranslatable) Cause

func (e *NotTranslatable) Cause() string

type PlayabilityFailedReason

type PlayabilityFailedReason string

PlayabilityFailedReason 视频无法播放的原因

const (
	PlayabilityFailedReasonBotDetected      PlayabilityFailedReason = "Sign in to confirm you're not a bot"
	PlayabilityFailedReasonAgeRestricted    PlayabilityFailedReason = "This video may be inappropriate for some users."
	PlayabilityFailedReasonVideoUnavailable PlayabilityFailedReason = "This video is unavailable"
)

type PlayabilityStatus

type PlayabilityStatus string

PlayabilityStatus 视频可播放性状态

const (
	PlayabilityStatusOK            PlayabilityStatus = "OK"
	PlayabilityStatusError         PlayabilityStatus = "ERROR"
	PlayabilityStatusLoginRequired PlayabilityStatus = "LOGIN_REQUIRED"
)

type PoTokenRequired

type PoTokenRequired struct {
	*CouldNotRetrieveTranscript
}

PoTokenRequired 需要 PO Token

func NewPoTokenRequired

func NewPoTokenRequired(videoID string) *PoTokenRequired

func (*PoTokenRequired) Cause

func (e *PoTokenRequired) Cause() string

type PrettyPrintFormatter

type PrettyPrintFormatter struct{}

PrettyPrintFormatter 美化打印格式

func (*PrettyPrintFormatter) FormatTranscript

func (f *PrettyPrintFormatter) FormatTranscript(transcript *FetchedTranscript) (string, error)

func (*PrettyPrintFormatter) FormatTranscripts

func (f *PrettyPrintFormatter) FormatTranscripts(transcripts []*FetchedTranscript) (string, error)

type ProxyConfig

type ProxyConfig interface {
	// ToProxyURLs 返回代理 URL 映射(http 和 https)
	ToProxyURLs() (httpURL, httpsURL string)
	// PreventKeepingConnectionsAlive 是否阻止保持连接(用于轮换代理)
	PreventKeepingConnectionsAlive() bool
	// RetriesWhenBlocked 被阻止时的重试次数
	RetriesWhenBlocked() int
}

ProxyConfig 代理配置接口

type RequestBlocked

type RequestBlocked struct {
	*CouldNotRetrieveTranscript
	// contains filtered or unexported fields
}

RequestBlocked 请求被阻止(IP 封禁)

func NewRequestBlocked

func NewRequestBlocked(videoID string) *RequestBlocked

func (*RequestBlocked) Cause

func (e *RequestBlocked) Cause() string

func (*RequestBlocked) WithProxyConfig

func (e *RequestBlocked) WithProxyConfig(proxyConfig ProxyConfig) *RequestBlocked

type SRTFormatter

type SRTFormatter struct {
	*TextBasedFormatter
}

SRTFormatter SRT 字幕文件格式

func NewSRTFormatter

func NewSRTFormatter() *SRTFormatter

func (*SRTFormatter) FormatTranscript

func (f *SRTFormatter) FormatTranscript(transcript *FetchedTranscript) (string, error)

func (*SRTFormatter) FormatTranscripts

func (f *SRTFormatter) FormatTranscripts(transcripts []*FetchedTranscript) (string, error)

type TextBasedFormatter

type TextBasedFormatter struct {
	*TextFormatter
}

TextBasedFormatter 基于文本的格式化器基类(用于 SRT 和 WebVTT)

type TextFormatter

type TextFormatter struct{}

TextFormatter 纯文本格式(无时间戳)

func (*TextFormatter) FormatTranscript

func (f *TextFormatter) FormatTranscript(transcript *FetchedTranscript) (string, error)

func (*TextFormatter) FormatTranscripts

func (f *TextFormatter) FormatTranscripts(transcripts []*FetchedTranscript) (string, error)

type Transcript

type Transcript struct {
	VideoID string

	Title                string
	ThumbnailURL         string
	Language             string
	LanguageCode         string
	IsGenerated          bool
	TranslationLanguages []TranslationLanguage
	// contains filtered or unexported fields
}

Transcript 表示一个可用的字幕资源

func NewTranscript

func NewTranscript(
	httpClient *HTTPClient,
	videoID string,
	title string,
	thumbnailURL string,
	url string,
	language string,
	languageCode string,
	isGenerated bool,
	translationLanguages []TranslationLanguage,
) *Transcript

NewTranscript 创建新的 Transcript 对象

func (*Transcript) Fetch

func (t *Transcript) Fetch(preserveFormatting bool) (*FetchedTranscript, error)

Fetch 获取实际字幕内容

func (*Transcript) IsTranslatable

func (t *Transcript) IsTranslatable() bool

IsTranslatable 检查是否可翻译

func (*Transcript) String

func (t *Transcript) String() string

String 返回字符串表示

func (*Transcript) Translate

func (t *Transcript) Translate(languageCode string) (*Transcript, error)

Translate 翻译到指定语言

type TranscriptList

type TranscriptList struct {
	VideoID string
	// contains filtered or unexported fields
}

TranscriptList 表示某个视频的所有可用字幕列表

func BuildTranscriptList

func BuildTranscriptList(httpClient *HTTPClient, videoID string, videoDetailsJSON map[string]interface{}, captionsJSON map[string]interface{}) (*TranscriptList, error)

BuildTranscriptList 从 JSON 数据构建 TranscriptList

func NewTranscriptList

func NewTranscriptList(
	videoID string,
	manuallyCreatedTranscripts map[string]*Transcript,
	generatedTranscripts map[string]*Transcript,
	translationLanguages []TranslationLanguage,
) *TranscriptList

NewTranscriptList 创建新的 TranscriptList

func (*TranscriptList) FindGeneratedTranscript

func (tl *TranscriptList) FindGeneratedTranscript(languageCodes []string) (*Transcript, error)

FindGeneratedTranscript 仅查找自动生成的字幕

func (*TranscriptList) FindManuallyCreatedTranscript

func (tl *TranscriptList) FindManuallyCreatedTranscript(languageCodes []string) (*Transcript, error)

FindManuallyCreatedTranscript 仅查找手动创建的字幕

func (*TranscriptList) FindTranscript

func (tl *TranscriptList) FindTranscript(languageCodes []string) (*Transcript, error)

FindTranscript 查找字幕(优先手动创建)

func (*TranscriptList) String

func (tl *TranscriptList) String() string

String 返回字符串表示

type TranscriptListFetcher

type TranscriptListFetcher struct {
	// contains filtered or unexported fields
}

TranscriptListFetcher 字幕列表获取器

func NewTranscriptListFetcher

func NewTranscriptListFetcher(httpClient *HTTPClient, proxyConfig ProxyConfig) *TranscriptListFetcher

NewTranscriptListFetcher 创建新的 TranscriptListFetcher

func (*TranscriptListFetcher) Fetch

func (tlf *TranscriptListFetcher) Fetch(videoID string) (*TranscriptList, error)

Fetch 获取视频的字幕列表

type TranscriptParser

type TranscriptParser struct {
	// contains filtered or unexported fields
}

TranscriptParser 字幕解析器

func NewTranscriptParser

func NewTranscriptParser(preserveFormatting bool) *TranscriptParser

NewTranscriptParser 创建新的字幕解析器

func (*TranscriptParser) Parse

func (tp *TranscriptParser) Parse(rawData string) ([]FetchedTranscriptSnippet, error)

Parse 解析 XML 字幕数据

type TranscriptsDisabled

type TranscriptsDisabled struct {
	*CouldNotRetrieveTranscript
}

TranscriptsDisabled 字幕已禁用

func NewTranscriptsDisabled

func NewTranscriptsDisabled(videoID string) *TranscriptsDisabled

func (*TranscriptsDisabled) Cause

func (e *TranscriptsDisabled) Cause() string

type TranslationLanguage

type TranslationLanguage struct {
	Language     string
	LanguageCode string
}

TranslationLanguage 表示可翻译的语言

type TranslationLanguageNotAvailable

type TranslationLanguageNotAvailable struct {
	*CouldNotRetrieveTranscript
}

TranslationLanguageNotAvailable 翻译语言不可用

func NewTranslationLanguageNotAvailable

func NewTranslationLanguageNotAvailable(videoID string) *TranslationLanguageNotAvailable

func (*TranslationLanguageNotAvailable) Cause

type VideoUnavailable

type VideoUnavailable struct {
	*CouldNotRetrieveTranscript
}

VideoUnavailable 视频不可用

func NewVideoUnavailable

func NewVideoUnavailable(videoID string) *VideoUnavailable

func (*VideoUnavailable) Cause

func (e *VideoUnavailable) Cause() string

type VideoUnplayable

type VideoUnplayable struct {
	*CouldNotRetrieveTranscript
	Reason     string
	SubReasons []string
}

VideoUnplayable 视频无法播放

func NewVideoUnplayable

func NewVideoUnplayable(videoID string, reason string, subReasons []string) *VideoUnplayable

func (*VideoUnplayable) Cause

func (e *VideoUnplayable) Cause() string

type WebVTTFormatter

type WebVTTFormatter struct {
	*TextBasedFormatter
}

WebVTTFormatter WebVTT 字幕文件格式

func NewWebVTTFormatter

func NewWebVTTFormatter() *WebVTTFormatter

func (*WebVTTFormatter) FormatTranscript

func (f *WebVTTFormatter) FormatTranscript(transcript *FetchedTranscript) (string, error)

func (*WebVTTFormatter) FormatTranscripts

func (f *WebVTTFormatter) FormatTranscripts(transcripts []*FetchedTranscript) (string, error)

type WebshareProxyConfig

type WebshareProxyConfig struct {
	*GenericProxyConfig
	ProxyUsername           string
	ProxyPassword           string
	FilterIPLocations       []string
	RetriesWhenBlockedCount int
	DomainName              string
	ProxyPort               int
}

WebshareProxyConfig Webshare 轮换住宅代理配置

func NewWebshareProxyConfig

func NewWebshareProxyConfig(
	proxyUsername string,
	proxyPassword string,
	filterIPLocations []string,
	retriesWhenBlocked int,
	domainName string,
	proxyPort int,
) (*WebshareProxyConfig, error)

NewWebshareProxyConfig 创建 Webshare 代理配置

func (*WebshareProxyConfig) PreventKeepingConnectionsAlive

func (w *WebshareProxyConfig) PreventKeepingConnectionsAlive() bool

func (*WebshareProxyConfig) RetriesWhenBlocked

func (w *WebshareProxyConfig) RetriesWhenBlocked() int

func (*WebshareProxyConfig) ToProxyURLs

func (w *WebshareProxyConfig) ToProxyURLs() (httpURL, httpsURL string)

func (*WebshareProxyConfig) URL

func (w *WebshareProxyConfig) URL() string

URL 生成 Webshare 代理 URL

type YouTubeDataUnparsable

type YouTubeDataUnparsable struct {
	*CouldNotRetrieveTranscript
}

YouTubeDataUnparsable YouTube 数据无法解析

func NewYouTubeDataUnparsable

func NewYouTubeDataUnparsable(videoID string) *YouTubeDataUnparsable

func (*YouTubeDataUnparsable) Cause

func (e *YouTubeDataUnparsable) Cause() string

type YouTubeRequestFailed

type YouTubeRequestFailed struct {
	*CouldNotRetrieveTranscript
	Reason string
}

YouTubeRequestFailed YouTube 请求失败

func NewYouTubeRequestFailed

func NewYouTubeRequestFailed(videoID string, err error) *YouTubeRequestFailed

func (*YouTubeRequestFailed) Cause

func (e *YouTubeRequestFailed) Cause() string

type YouTubeTranscriptApi

type YouTubeTranscriptApi struct {
	// contains filtered or unexported fields
}

YouTubeTranscriptApi 主要的 API 接口

func NewYouTubeTranscriptApi

func NewYouTubeTranscriptApi(proxyConfig ProxyConfig) (*YouTubeTranscriptApi, error)

NewYouTubeTranscriptApi 创建新的 YouTubeTranscriptApi 实例 注意:由于 HTTPClient 不是线程安全的,在多线程环境中,每个线程需要创建独立的实例

func (*YouTubeTranscriptApi) Fetch

func (api *YouTubeTranscriptApi) Fetch(videoID string, languages []string, preserveFormatting bool) (*FetchedTranscript, error)

Fetch 获取单个视频的字幕 这是调用 list().find_transcript(languages).fetch(preserve_formatting) 的快捷方式

func (*YouTubeTranscriptApi) List

func (api *YouTubeTranscriptApi) List(videoID string) (*TranscriptList, error)

List 获取视频的可用字幕列表

type YouTubeTranscriptApiException

type YouTubeTranscriptApiException struct {
	Message string
}

YouTubeTranscriptApiException 是所有异常的基类

func (*YouTubeTranscriptApiException) Error

type YouTubeTranscriptCLI

type YouTubeTranscriptCLI struct {
	// contains filtered or unexported fields
}

YouTubeTranscriptCLI 命令行工具

func NewYouTubeTranscriptCLI

func NewYouTubeTranscriptCLI(config CLIConfig) *YouTubeTranscriptCLI

NewYouTubeTranscriptCLI 创建新的命令行工具实例

func (*YouTubeTranscriptCLI) Run

func (cli *YouTubeTranscriptCLI) Run() (string, error)

Run 运行命令行工具

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL