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
- Variables
- func SetupHTTPClientProxy(client *HTTPClient, proxyConfig ProxyConfig) error
- type AgeRestricted
- type CLIConfig
- type CookieError
- type CookieInvalid
- type CookiePathInvalid
- type CouldNotRetrieveTranscript
- type FailedToCreateConsentCookie
- type FetchedTranscript
- type FetchedTranscriptSnippet
- type Formatter
- type FormatterLoader
- type GenericProxyConfig
- type HTTPClient
- type InvalidProxyConfig
- type InvalidVideoId
- type IpBlocked
- type JSONFormatter
- type NoTranscriptFound
- type NotTranslatable
- type PlayabilityFailedReason
- type PlayabilityStatus
- type PoTokenRequired
- type PrettyPrintFormatter
- type ProxyConfig
- type RequestBlocked
- type SRTFormatter
- type TextBasedFormatter
- type TextFormatter
- type Transcript
- type TranscriptList
- func (tl *TranscriptList) FindGeneratedTranscript(languageCodes []string) (*Transcript, error)
- func (tl *TranscriptList) FindManuallyCreatedTranscript(languageCodes []string) (*Transcript, error)
- func (tl *TranscriptList) FindTranscript(languageCodes []string) (*Transcript, error)
- func (tl *TranscriptList) String() string
- type TranscriptListFetcher
- type TranscriptParser
- type TranscriptsDisabled
- type TranslationLanguage
- type TranslationLanguageNotAvailable
- type VideoUnavailable
- type VideoUnplayable
- type WebVTTFormatter
- type WebshareProxyConfig
- type YouTubeDataUnparsable
- type YouTubeRequestFailed
- type YouTubeTranscriptApi
- type YouTubeTranscriptApiException
- type YouTubeTranscriptCLI
Constants ¶
const ( )
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 相关常量
const Version = "1.0.0"
Variables ¶
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
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 (e *CouldNotRetrieveTranscript) Cause() string
func (*CouldNotRetrieveTranscript) Error ¶
func (e *CouldNotRetrieveTranscript) Error() string
type FailedToCreateConsentCookie ¶
type FailedToCreateConsentCookie struct {
*CouldNotRetrieveTranscript
}
FailedToCreateConsentCookie 创建同意 Cookie 失败
func NewFailedToCreateConsentCookie ¶
func NewFailedToCreateConsentCookie(videoID string) *FailedToCreateConsentCookie
func (*FailedToCreateConsentCookie) Cause ¶
func (e *FailedToCreateConsentCookie) Cause() string
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 格式化器加载器
type GenericProxyConfig ¶
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 客户端包装
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 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." )
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) 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 查找字幕(优先手动创建)
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 ¶
TranslationLanguage 表示可翻译的语言
type TranslationLanguageNotAvailable ¶
type TranslationLanguageNotAvailable struct {
*CouldNotRetrieveTranscript
}
TranslationLanguageNotAvailable 翻译语言不可用
func NewTranslationLanguageNotAvailable ¶
func NewTranslationLanguageNotAvailable(videoID string) *TranslationLanguageNotAvailable
func (*TranslationLanguageNotAvailable) Cause ¶
func (e *TranslationLanguageNotAvailable) Cause() string
type VideoUnavailable ¶
type VideoUnavailable struct {
}
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 {
}
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)
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 ¶
func (e *YouTubeTranscriptApiException) Error() string
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 运行命令行工具