From e02f091ae5cb37afc867564cd59fd494d5d532a6 Mon Sep 17 00:00:00 2001 From: piratf Date: Mon, 7 Oct 2024 20:02:23 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E8=A7=86=E9=A2=91=E6=92=AD?= =?UTF-8?q?=E6=94=BE=E5=99=A8=E5=AD=97=E5=B9=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 目前的获取视频信息接口中字幕 URL 都是空的,只有播放器元数据接口中有 URL,使用 Resty Client 可以成功获取字幕 --- video.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/video.go b/video.go index 2d022f5..e1dbcbb 100644 --- a/video.go +++ b/video.go @@ -1,7 +1,9 @@ package bilibili import ( + "encoding/json" "github.com/go-resty/resty/v2" + "github.com/pkg/errors" ) type VideoParam struct { @@ -827,3 +829,40 @@ func (c *Client) GetVideoPlayerMetaInfo(param GetVideoPlayerMetaInfoParam) (*Vid ) return execute[*VideoPlayerMetaInfo](c, method, url, param) } + +type VideoPlayerSubtitleItem struct { + From float64 `json:"from"` // 字幕开始时间 + To float64 `json:"to"` // 字幕结束时间 + Sid int `json:"sid"` // 字幕 ID + Location int `json:"location"` // 未知 + Content string `json:"content"` // 字幕内容 + Music float64 `json:"music"` // 未知 +} + +type VideoPlayerSubtitle struct { + FontSize float64 `json:"font_size"` // 字体大小 + FontColor string `json:"font_color"` // 字体颜色 + BackgroundAlpha float64 `json:"background_alpha"` // 背景透明度 + BackgroundColor string `json:"background_color"` // 背景颜色 + Stroke string `json:"Stroke"` // 未知 + Type string `json:"type"` // 字幕类型 + Lang string `json:"lang"` // 字幕语言 + Version string `json:"version"` // 字幕版本 + Body []VideoPlayerSubtitleItem `json:"body"` // 字幕内容 +} + +// GetVideoPlayerSubtitle 获取视频播放器字幕 +// subtitleUrl: 从 GetVideoPlayerMetaInfo 获取的 Subtitle.Subtitles[x].SubtitleUrl +func (c *Client) GetVideoPlayerSubtitle(subtitleUrl string) (*VideoPlayerSubtitle, error) { + res, err := c.Resty().NewRequest().Get("https:" + subtitleUrl) + if err != nil { + return nil, errors.Wrap(err, "Failed to get video player subtitle") + } + + data := new(VideoPlayerSubtitle) + if err := json.Unmarshal(res.Body(), data); err != nil { + return nil, errors.Wrap(err, "Failed to unmarshal video player subtitle") + } + + return data, nil +}