Browse Source

新增,通过 Emby UserId 和 视频 ID 可以获取这个视频的默认字幕索引是多少

Signed-off-by: allan716 <[email protected]>
allan716 4 years ago
parent
commit
4469636233

+ 18 - 0
internal/pkg/emby_helper/emby.go

@@ -170,6 +170,24 @@ func (em EmbyApi) GetItemVideoInfo(id string) (emby.EmbyVideoInfo, error) {
 	return recItem, nil
 }
 
+// GetItemVideoInfoByUserId 可以拿到这个视频的选择字幕Index,配合 GetItemVideoInfo 使用。 在 API 调试界面 -- UserLibraryService
+func (em EmbyApi) GetItemVideoInfoByUserId(id string) (emby.EmbyVideoInfoByUserId, error) {
+
+	var recItem emby.EmbyVideoInfoByUserId
+
+	_, err := em.getNewClient().R().
+		SetQueryParams(map[string]string{
+			"api_key": em.embyConfig.ApiKey,
+		}).
+		SetResult(&recItem).
+		Get(em.embyConfig.Url + "/emby/Users/" + em.embyConfig.UserId + "/Items/" + id)
+	if err != nil {
+		return emby.EmbyVideoInfoByUserId{}, err
+	}
+
+	return recItem, nil
+}
+
 // UpdateVideoSubList 在 API 调试界面 -- ItemRefreshService
 func (em EmbyApi) UpdateVideoSubList(id string) error {
 

+ 13 - 0
internal/pkg/emby_helper/emby_test.go

@@ -44,6 +44,19 @@ func TestEmbyHelper_GetItemVideoInfo(t *testing.T) {
 	println(videoInfo.Name, videoInfo.Path)
 }
 
+func TestEmbyHelper_GetItemVideoInfoByUserId(t *testing.T) {
+	em := NewEmbyHelper(pkg.GetConfig().EmbyConfig)
+	// 95813 -- 命运夜
+	// 96564 -- The Bad Batch - S01E11
+	// 108766 -- R&M - S05E06
+	videoInfo, err := em.GetItemVideoInfoByUserId("108766")
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	println(videoInfo.Name, videoInfo.Path, "Default Sub Index:", videoInfo.GetDefaultSubIndex())
+}
+
 func TestEmbyHelper_UpdateVideoSubList(t *testing.T) {
 	em := NewEmbyHelper(pkg.GetConfig().EmbyConfig)
 	// 95813 -- 命运夜

+ 27 - 0
internal/types/emby/type.go

@@ -44,6 +44,33 @@ type EmbyVideoInfo struct {
 	} `json:"MediaStreams"`
 }
 
+type EmbyVideoInfoByUserId struct {
+	Name          string    `json:"Name"`
+	OriginalTitle string    `json:"OriginalTitle"`
+	Id            string    `json:"Id"`
+	DateCreated   time.Time `json:"DateCreated,omitempty"`
+	PremiereDate  time.Time `json:"PremiereDate,omitempty"`
+	SortName      string    `json:"SortName,omitempty"`
+	Path          string    `json:"Path"`
+	MediaSources  []struct {
+		Path                       string `json:"Path"`
+		DefaultAudioStreamIndex    int    `json:"DefaultAudioStreamIndex,omitempty"`
+		DefaultSubtitleStreamIndex int    `json:"DefaultSubtitleStreamIndex,omitempty"`
+	} `json:"MediaSources"`
+}
+
+// GetDefaultSubIndex 获取匹配视频字幕的索引,默认值是0,不应该是0,0 就是没有选择或者说关闭
+func (info EmbyVideoInfoByUserId) GetDefaultSubIndex() int {
+
+	for _, mediaSource := range info.MediaSources {
+		if info.Path == mediaSource.Path {
+			return mediaSource.DefaultSubtitleStreamIndex
+		}
+	}
+
+	return 0
+}
+
 type EmbyMixInfo struct {
 	VideoFolderName       string // 电影就是电影的文件夹名称,连续剧就是对应的剧集的 root 文件夹
 	VideoFileName         string // 视频文件名