embyhelper.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. package emby_helper
  2. import (
  3. "fmt"
  4. "github.com/allanpk716/ChineseSubFinder/internal/common"
  5. embyHelper "github.com/allanpk716/ChineseSubFinder/internal/pkg/emby_api"
  6. "github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
  7. "github.com/allanpk716/ChineseSubFinder/internal/pkg/settings"
  8. "github.com/allanpk716/ChineseSubFinder/internal/pkg/sub_parser_hub"
  9. "github.com/allanpk716/ChineseSubFinder/internal/types/emby"
  10. "github.com/allanpk716/ChineseSubFinder/internal/types/language"
  11. "github.com/panjf2000/ants/v2"
  12. "golang.org/x/net/context"
  13. "path"
  14. "path/filepath"
  15. "sort"
  16. "strings"
  17. "sync"
  18. "time"
  19. )
  20. type EmbyHelper struct {
  21. embyApi *embyHelper.EmbyApi
  22. EmbyConfig settings.EmbySettings
  23. threads int
  24. timeOut time.Duration
  25. listLock sync.Mutex
  26. }
  27. func NewEmbyHelper(embyConfig settings.EmbySettings) *EmbyHelper {
  28. em := EmbyHelper{EmbyConfig: embyConfig}
  29. em.embyApi = embyHelper.NewEmbyApi(embyConfig)
  30. em.threads = 6
  31. em.timeOut = 60 * time.Second
  32. return &em
  33. }
  34. func (em *EmbyHelper) GetRecentlyAddVideoList() ([]emby.EmbyMixInfo, map[string][]emby.EmbyMixInfo, error) {
  35. // 获取最近的影片列表
  36. items, err := em.embyApi.GetRecentlyItems()
  37. if err != nil {
  38. return nil, nil, err
  39. }
  40. // 获取电影和连续剧的文件夹名称
  41. var EpisodeIdList = make([]string, 0)
  42. var MovieIdList = make([]string, 0)
  43. log_helper.GetLogger().Debugln("-----------------")
  44. log_helper.GetLogger().Debugln("GetRecentlyAddVideoList - GetRecentlyItems Count", len(items.Items))
  45. // 分类
  46. for index, item := range items.Items {
  47. if item.Type == videoTypeEpisode {
  48. // 这个里面可能混有其他的内容,比如目标是连续剧,但是 emby_helper 其实会把其他的混合内容也标记进去
  49. EpisodeIdList = append(EpisodeIdList, item.Id)
  50. log_helper.GetLogger().Debugln("Episode:", index, item.SeriesName, item.ParentIndexNumber, item.IndexNumber)
  51. } else if item.Type == videoTypeMovie {
  52. // 这个里面可能混有其他的内容,比如目标是连续剧,但是 emby_helper 其实会把其他的混合内容也标记进去
  53. MovieIdList = append(MovieIdList, item.Id)
  54. log_helper.GetLogger().Debugln("Movie:", index, item.Name)
  55. } else {
  56. log_helper.GetLogger().Debugln("GetRecentlyItems - Is not a goal video type:", index, item.Name, item.Type)
  57. }
  58. }
  59. // 过滤出有效的电影、连续剧的资源出来
  60. filterMovieList, err := em.filterEmbyVideoList(MovieIdList, true)
  61. if err != nil {
  62. return nil, nil, err
  63. }
  64. filterSeriesList, err := em.filterEmbyVideoList(EpisodeIdList, false)
  65. if err != nil {
  66. return nil, nil, err
  67. }
  68. // 输出调试信息
  69. log_helper.GetLogger().Debugln("-----------------")
  70. log_helper.GetLogger().Debugln("filterEmbyVideoList found valid movie", len(filterMovieList))
  71. for index, info := range filterMovieList {
  72. log_helper.GetLogger().Debugln(index, info.VideoFileName)
  73. }
  74. log_helper.GetLogger().Debugln("-----------------")
  75. log_helper.GetLogger().Debugln("filterEmbyVideoList found valid series", len(filterSeriesList))
  76. for index, info := range filterSeriesList {
  77. log_helper.GetLogger().Debugln(index, info.VideoFileName)
  78. }
  79. log_helper.GetLogger().Debugln("-----------------")
  80. // 将没有字幕的找出来
  81. noSubMovieList, err := em.filterNoChineseSubVideoList(filterMovieList)
  82. if err != nil {
  83. return nil, nil, err
  84. }
  85. log_helper.GetLogger().Debugln("-----------------")
  86. noSubSeriesList, err := em.filterNoChineseSubVideoList(filterSeriesList)
  87. if err != nil {
  88. return nil, nil, err
  89. }
  90. // 输出调试信息
  91. log_helper.GetLogger().Debugln("-----------------")
  92. log_helper.GetLogger().Debugln("filterNoChineseSubVideoList found no chinese movie", len(noSubMovieList))
  93. for index, info := range filterMovieList {
  94. log_helper.GetLogger().Debugln(index, info.VideoFileName)
  95. }
  96. log_helper.GetLogger().Debugln("-----------------")
  97. log_helper.GetLogger().Debugln("filterNoChineseSubVideoList found no chinese series", len(noSubSeriesList))
  98. for index, info := range filterSeriesList {
  99. log_helper.GetLogger().Debugln(index, info.VideoFileName)
  100. }
  101. // 需要将连续剧零散的每一集,进行合并到一个连续剧下面,也就是这个连续剧有那些需要更新的
  102. var seriesMap = make(map[string][]emby.EmbyMixInfo)
  103. for _, info := range noSubSeriesList {
  104. _, ok := seriesMap[info.VideoFolderName]
  105. if ok == false {
  106. // 不存在则新建初始化
  107. seriesMap[info.VideoFolderName] = make([]emby.EmbyMixInfo, 0)
  108. }
  109. seriesMap[info.VideoFolderName] = append(seriesMap[info.VideoFolderName], info)
  110. }
  111. return noSubMovieList, seriesMap, nil
  112. }
  113. // RefreshEmbySubList 字幕下载完毕一次,就可以触发一次这个。并发 6 线程去刷新
  114. func (em *EmbyHelper) RefreshEmbySubList() (bool, error) {
  115. if em.embyApi == nil {
  116. return false, nil
  117. }
  118. err := em.embyApi.RefreshRecentlyVideoInfo()
  119. if err != nil {
  120. return false, err
  121. }
  122. return true, nil
  123. }
  124. // findMappingPath 从 Emby 内置路径匹配到物理路径
  125. // X:\电影 - /mnt/share1/电影
  126. // X:\连续剧 - /mnt/share1/连续剧
  127. func (em *EmbyHelper) findMappingPath(mixInfo *emby.EmbyMixInfo, isMovieOrSeries bool) bool {
  128. // 这里进行路径匹配的时候需要考虑嵌套路径的问题
  129. // 比如,映射了 /电影 以及 /电影/AA ,那么如果有一部电影 /电影/AA/xx/xx.mkv 那么,应该匹配的是最长的路径 /电影/AA
  130. matchedEmbyPaths := make([]string, 0)
  131. if isMovieOrSeries == true {
  132. // 电影的情况
  133. for _, embyPath := range em.EmbyConfig.MoviePathsMapping {
  134. if strings.HasPrefix(mixInfo.VideoInfo.Path, embyPath) == true {
  135. matchedEmbyPaths = append(matchedEmbyPaths, embyPath)
  136. }
  137. }
  138. } else {
  139. // 连续剧的情况
  140. for _, embyPath := range em.EmbyConfig.SeriesPathsMapping {
  141. if strings.HasPrefix(mixInfo.VideoInfo.Path, embyPath) == true {
  142. matchedEmbyPaths = append(matchedEmbyPaths, embyPath)
  143. }
  144. }
  145. }
  146. if len(matchedEmbyPaths) < 1 {
  147. return false
  148. }
  149. // 排序得到匹配上的路径,最长的那个
  150. pathSlices := sortStringSliceByLength(matchedEmbyPaths)
  151. // 然后还需要从这个最长的路径,从 map 中找到对应的物理路径
  152. // nowPhRootPath 这个路径是映射的根目录,如果里面再次嵌套 子文件夹 再到连续剧目录,则是个问题,会丢失子文件夹目录
  153. nowPhRootPath := ""
  154. if isMovieOrSeries == true {
  155. // 电影的情况
  156. for physicalPath, embyPath := range em.EmbyConfig.MoviePathsMapping {
  157. if embyPath == pathSlices[0].Path {
  158. nowPhRootPath = physicalPath
  159. break
  160. }
  161. }
  162. } else {
  163. // 连续剧的情况
  164. for physicalPath, embyPath := range em.EmbyConfig.SeriesPathsMapping {
  165. if embyPath == pathSlices[0].Path {
  166. nowPhRootPath = physicalPath
  167. break
  168. }
  169. }
  170. }
  171. // 如果匹配不上
  172. if nowPhRootPath == "" {
  173. return false
  174. }
  175. if isMovieOrSeries == true {
  176. // 电影
  177. mixInfo.PhysicalVideoFileFullPath = strings.ReplaceAll(mixInfo.VideoInfo.Path, pathSlices[0].Path, nowPhRootPath)
  178. // 因为电影搜索的时候使用的是完整的视频目录,所以这个字段并不重要
  179. //mixInfo.PhysicalRootPath = strings.ReplaceAll(mixInfo.VideoInfo.Path, pathSlices[0].Path, nowPhRootPath)
  180. // 这个电影的文件夹
  181. mixInfo.VideoFolderName = filepath.Base(filepath.Dir(mixInfo.VideoInfo.Path))
  182. mixInfo.VideoFileName = filepath.Base(mixInfo.VideoInfo.Path)
  183. } else {
  184. // 连续剧
  185. ancestorIndex := -1
  186. // 找到连续剧文件夹这一层
  187. for i, ancestor := range mixInfo.Ancestors {
  188. if ancestor.Type == "Series" {
  189. ancestorIndex = i
  190. break
  191. }
  192. }
  193. if ancestorIndex == -1 {
  194. // 说明没有找到连续剧文件夹的名称,那么就应该跳过
  195. return false
  196. }
  197. mixInfo.PhysicalVideoFileFullPath = strings.ReplaceAll(mixInfo.VideoInfo.Path, pathSlices[0].Path, nowPhRootPath)
  198. mixInfo.PhysicalRootPath = mixInfo.Ancestors[ancestorIndex+1].Path
  199. // 这个剧集的文件夹
  200. mixInfo.VideoFolderName = filepath.Base(mixInfo.Ancestors[ancestorIndex].Path)
  201. mixInfo.VideoFileName = filepath.Base(mixInfo.VideoInfo.Path)
  202. }
  203. return true
  204. }
  205. func (em *EmbyHelper) filterEmbyVideoList(videoIdList []string, isMovieOrSeries bool) ([]emby.EmbyMixInfo, error) {
  206. var filterVideoEmbyInfo = make([]emby.EmbyMixInfo, 0)
  207. queryFunc := func(m string) (*emby.EmbyMixInfo, error) {
  208. info, err := em.embyApi.GetItemVideoInfo(m)
  209. if err != nil {
  210. return nil, err
  211. }
  212. ancs, err := em.embyApi.GetItemAncestors(m)
  213. if err != nil {
  214. return nil, err
  215. }
  216. mixInfo := emby.EmbyMixInfo{Ancestors: ancs, VideoInfo: info}
  217. if isMovieOrSeries == true {
  218. // 电影
  219. // 过滤掉不符合要求的,拼接绝对路径
  220. isFit := em.findMappingPath(&mixInfo, isMovieOrSeries)
  221. if isFit == false {
  222. return nil, err
  223. }
  224. } else {
  225. // 连续剧
  226. // 过滤掉不符合要求的,拼接绝对路径
  227. isFit := em.findMappingPath(&mixInfo, isMovieOrSeries)
  228. if isFit == false {
  229. return nil, err
  230. }
  231. }
  232. return &mixInfo, nil
  233. }
  234. p, err := ants.NewPoolWithFunc(em.threads, func(inData interface{}) {
  235. data := inData.(InputData)
  236. defer data.Wg.Done()
  237. ctx, cancel := context.WithTimeout(context.Background(), em.timeOut)
  238. defer cancel()
  239. done := make(chan OutData, 1)
  240. panicChan := make(chan interface{}, 1)
  241. go func() {
  242. defer func() {
  243. if p := recover(); p != nil {
  244. panicChan <- p
  245. }
  246. }()
  247. info, err := queryFunc(data.Id)
  248. outData := OutData{
  249. Info: info,
  250. Err: err,
  251. }
  252. done <- outData
  253. }()
  254. select {
  255. case outData := <-done:
  256. // 收到结果,需要加锁
  257. if outData.Err != nil {
  258. log_helper.GetLogger().Errorln("filterEmbyVideoList.NewPoolWithFunc got Err", outData.Err)
  259. return
  260. }
  261. if outData.Info == nil {
  262. return
  263. }
  264. em.listLock.Lock()
  265. filterVideoEmbyInfo = append(filterVideoEmbyInfo, *outData.Info)
  266. em.listLock.Unlock()
  267. return
  268. case p := <-panicChan:
  269. log_helper.GetLogger().Errorln("filterEmbyVideoList.NewPoolWithFunc got panic", p)
  270. case <-ctx.Done():
  271. log_helper.GetLogger().Errorln("filterEmbyVideoList.NewPoolWithFunc got time out", ctx.Err())
  272. return
  273. }
  274. })
  275. if err != nil {
  276. return nil, err
  277. }
  278. defer p.Release()
  279. wg := sync.WaitGroup{}
  280. // 获取视频的 Emby 信息
  281. for _, m := range videoIdList {
  282. wg.Add(1)
  283. err = p.Invoke(InputData{Id: m, Wg: &wg})
  284. if err != nil {
  285. log_helper.GetLogger().Errorln("filterEmbyVideoList ants.Invoke", err)
  286. }
  287. }
  288. wg.Wait()
  289. return filterVideoEmbyInfo, nil
  290. }
  291. func (em *EmbyHelper) filterNoChineseSubVideoList(videoList []emby.EmbyMixInfo) ([]emby.EmbyMixInfo, error) {
  292. currentTime := time.Now()
  293. dayRange3Months, _ := time.ParseDuration(common.DownloadSubDuring3Months)
  294. dayRange7Days, _ := time.ParseDuration(common.DownloadSubDuring7Days)
  295. var noSubVideoList = make([]emby.EmbyMixInfo, 0)
  296. // TODO 这里有一种情况需要考虑的,如果内置有中文的字幕,那么是否需要跳过,目前暂定的一定要有外置的字幕
  297. for _, info := range videoList {
  298. needDlSub3Month := false
  299. // 3个月内,或者没有字幕都要进行下载
  300. if info.VideoInfo.PremiereDate.Add(dayRange3Months).After(currentTime) == true {
  301. // 需要下载的
  302. needDlSub3Month = true
  303. }
  304. // 这个影片只要有一个符合字幕要求的,就可以跳过
  305. // 外置中文字幕
  306. haveExternalChineseSub := false
  307. for _, stream := range info.VideoInfo.MediaStreams {
  308. // 首先找到外置的字幕文件
  309. if stream.IsExternal == true && stream.IsTextSubtitleStream == true && stream.SupportsExternalStream == true {
  310. // 然后字幕的格式以及语言命名要符合本程序的定义,有字幕
  311. if sub_parser_hub.IsEmbySubCodecWanted(stream.Codec) == true &&
  312. sub_parser_hub.IsEmbySubChineseLangStringWanted(stream.Language) == true {
  313. haveExternalChineseSub = true
  314. break
  315. } else {
  316. continue
  317. }
  318. }
  319. }
  320. // 内置中文字幕
  321. haveInsideChineseSub := false
  322. for _, stream := range info.VideoInfo.MediaStreams {
  323. if stream.IsExternal == false &&
  324. sub_parser_hub.IsEmbySubChineseLangStringWanted(stream.Language) {
  325. haveInsideChineseSub = true
  326. break
  327. }
  328. }
  329. // 比如,创建的时间在3个月内,然后没有额外下载的中文字幕,都符合要求
  330. if haveExternalChineseSub == false {
  331. // 没有外置字幕
  332. // 如果创建了7天,且有内置的中文字幕,那么也不进行下载了
  333. if info.VideoInfo.DateCreated.Add(dayRange7Days).After(currentTime) == false && haveInsideChineseSub == true {
  334. log_helper.GetLogger().Debugln("Create Over 7 Days, And It Has Inside ChineseSub, Than Skip", info.VideoFileName)
  335. continue
  336. }
  337. //// 如果创建了三个月,还是没有字幕,那么也不进行下载了
  338. //if info.VideoInfo.DateCreated.Add(dayRange3Months).After(currentTime) == false {
  339. // continue
  340. //}
  341. // 没有中文字幕就加入下载列表
  342. noSubVideoList = append(noSubVideoList, info)
  343. } else {
  344. // 有外置字幕
  345. // 如果视频发布时间超过两年了,有字幕就直接跳过了,一般字幕稳定了
  346. if currentTime.Year()-2 > info.VideoInfo.PremiereDate.Year() {
  347. log_helper.GetLogger().Debugln("Create Over 2 Years, And It Has External ChineseSub, Than Skip", info.VideoFileName)
  348. continue
  349. }
  350. // 有中文字幕,且如果在三个月内,则需要继续下载字幕`
  351. if needDlSub3Month == true {
  352. noSubVideoList = append(noSubVideoList, info)
  353. }
  354. }
  355. }
  356. return noSubVideoList, nil
  357. }
  358. // GetInternalEngSubAndExChineseEnglishSub 获取对应 videoId 的内置英文字幕,外置中文字幕(只要是带有中文的都算,简体、繁体、简英、繁英,需要后续额外的判断)字幕
  359. func (em *EmbyHelper) GetInternalEngSubAndExChineseEnglishSub(videoId string) (bool, []emby.SubInfo, []emby.SubInfo, error) {
  360. // 先刷新以下这个资源,避免找到的字幕不存在了
  361. err := em.embyApi.UpdateVideoSubList(videoId)
  362. if err != nil {
  363. return false, nil, nil, err
  364. }
  365. // 获取这个资源的信息
  366. videoInfo, err := em.embyApi.GetItemVideoInfo(videoId)
  367. if err != nil {
  368. return false, nil, nil, err
  369. }
  370. // 获取 MediaSources ID,这里强制使用第一个视频源(因为 emby 运行有多个版本的视频指向到一个视频ID上,比如一个 web 一个 蓝光)
  371. mediaSourcesId := videoInfo.MediaSources[0].Id
  372. // 视频文件名称带后缀名
  373. videoFileName := filepath.Base(videoInfo.Path)
  374. videoFileNameWithOutExt := strings.ReplaceAll(videoFileName, path.Ext(videoFileName), "")
  375. // TODO 后续会新增一个功能,从视频中提取音频文件,然后识别转为字符,再进行与字幕的匹配
  376. // 获取是否有内置的英文字幕,如果没有则无需继续往下
  377. /*
  378. 这里有个梗,读取到的英文内置字幕很可能是残缺的,比如,基地 S01E04 Eng 第一个 Default Forced Sub,就不对,内容的 Dialogue 很少。
  379. 然后第二个 Eng 字幕才对。那么考虑到兼容性, 可能后续有短视频,也就不能简单的按 Dialogue 的多少去衡量。大概会做一个功能。方案有两个:
  380. 1. 读取到视频的总长度,然后再分析 Dialogue 的时间出现的部分与整体时间轴的占比,又或者是 Dialogue 之间的连续成都分析,这个有待测试。
  381. 2. 还有一个更加粗暴的方案,把所有的 Eng 都识别出来,然后找最多的 Dialogue 来做为正确的来使用(够粗暴吧)
  382. */
  383. var insideEngSUbIndexList = make([]int, 0)
  384. for _, stream := range videoInfo.MediaStreams {
  385. if stream.IsExternal == false && stream.Language == language.Emby_English_eng && stream.Codec == streamCodec {
  386. insideEngSUbIndexList = append(insideEngSUbIndexList, stream.Index)
  387. }
  388. }
  389. // 没有找到则跳过
  390. if len(insideEngSUbIndexList) == 0 {
  391. return false, nil, nil, nil
  392. }
  393. // 再内置英文字幕能找到的前提下,就可以先找中文的外置字幕,目前版本只能考虑双语字幕
  394. // 内置英文字幕,这里把 srt 和 ass 的都导出来
  395. var inSubList = make([]emby.SubInfo, 0)
  396. // 外置中文双语字幕
  397. var exSubList = make([]emby.SubInfo, 0)
  398. tmpFileNameWithOutExt := ""
  399. for _, stream := range videoInfo.MediaStreams {
  400. // 首先找到外置的字幕文件
  401. if stream.IsExternal == true && stream.IsTextSubtitleStream == true && stream.SupportsExternalStream == true {
  402. // 然后字幕的格式以及语言命名要符合本程序的定义,有字幕
  403. if sub_parser_hub.IsEmbySubCodecWanted(stream.Codec) == true &&
  404. sub_parser_hub.IsEmbySubChineseLangStringWanted(stream.Language) == true {
  405. tmpFileName := filepath.Base(stream.Path)
  406. // 去除 .default 或者 .forced
  407. //tmpFileName = strings.ReplaceAll(tmpFileName, subparser.Sub_Ext_Mark_Default, "")
  408. //tmpFileName = strings.ReplaceAll(tmpFileName, subparser.Sub_Ext_Mark_Forced, "")
  409. tmpFileNameWithOutExt = strings.ReplaceAll(tmpFileName, path.Ext(tmpFileName), "")
  410. exSubList = append(exSubList, *emby.NewSubInfo(tmpFileNameWithOutExt+"."+stream.Codec, "."+stream.Codec, stream.Index))
  411. } else {
  412. continue
  413. }
  414. }
  415. }
  416. // 没有找到则跳过
  417. if len(exSubList) == 0 {
  418. return false, nil, nil, nil
  419. }
  420. /*
  421. 把之前 Internal 英文字幕的 SubInfo 实例的信息补充完整
  422. 但是也不是绝对的,因为后续去 emby 下载字幕的时候,需要与外置字幕的后缀名一致
  423. 这里开始去下载字幕
  424. 先下载内置的文的
  425. 因为上面下载内置英文字幕的梗,所以,需要预先下载多个内置的英文字幕下来,用体积最大(相同后缀名)的那个来作为最后的输出
  426. */
  427. // 那么现在先下载相同格式(.srt)的两个字幕
  428. InsideEngSubIndex := 0
  429. if len(insideEngSUbIndexList) == 1 {
  430. // 如果就找到一个内置字幕,就默认这个
  431. InsideEngSubIndex = insideEngSUbIndexList[0]
  432. } else {
  433. // 如果找到不止一个就需要判断
  434. var tmpSubContentLenList = make([]int, 0)
  435. for _, index := range insideEngSUbIndexList {
  436. // TODO 这里默认是去 Emby 去拿字幕,但是其实可以缓存在视频文件同级的目录下,这样后续就无需多次下载了,毕竟每次下载都需要读取完整的视频
  437. subFileData, err := em.embyApi.GetSubFileData(videoId, mediaSourcesId, fmt.Sprintf("%d", index), common.SubExtSRT)
  438. if err != nil {
  439. return false, nil, nil, err
  440. }
  441. tmpSubContentLenList = append(tmpSubContentLenList, len(subFileData))
  442. }
  443. maxContentLen := -1
  444. for index, contentLen := range tmpSubContentLenList {
  445. if maxContentLen < contentLen {
  446. maxContentLen = contentLen
  447. InsideEngSubIndex = insideEngSUbIndexList[index]
  448. }
  449. }
  450. }
  451. // 这里才是下载最佳的那个字幕
  452. for i := 0; i < 2; i++ {
  453. tmpExt := common.SubExtSRT
  454. if i == 1 {
  455. tmpExt = common.SubExtASS
  456. }
  457. subFileData, err := em.embyApi.GetSubFileData(videoId, mediaSourcesId, fmt.Sprintf("%d", InsideEngSubIndex), tmpExt)
  458. if err != nil {
  459. return false, nil, nil, err
  460. }
  461. tmpInSubInfo := emby.NewSubInfo(videoFileNameWithOutExt+tmpExt, tmpExt, InsideEngSubIndex)
  462. tmpInSubInfo.Content = []byte(subFileData)
  463. inSubList = append(inSubList, *tmpInSubInfo)
  464. }
  465. // 再下载外置的
  466. for i, subInfo := range exSubList {
  467. subFileData, err := em.embyApi.GetSubFileData(videoId, mediaSourcesId, fmt.Sprintf("%d", subInfo.EmbyStreamIndex), subInfo.Ext)
  468. if err != nil {
  469. return false, nil, nil, err
  470. }
  471. exSubList[i].Content = []byte(subFileData)
  472. }
  473. return true, inSubList, exSubList, nil
  474. }
  475. type InputData struct {
  476. Id string
  477. Wg *sync.WaitGroup
  478. }
  479. type OutData struct {
  480. Info *emby.EmbyMixInfo
  481. Err error
  482. }
  483. type PathSlice struct {
  484. Path string
  485. }
  486. type PathSlices []PathSlice
  487. func (a PathSlices) Len() int { return len(a) }
  488. func (a PathSlices) Less(i, j int) bool { return len(a[i].Path) < len(a[j].Path) }
  489. func (a PathSlices) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  490. func sortStringSliceByLength(m []string) PathSlices {
  491. p := make(PathSlices, len(m))
  492. i := 0
  493. for _, v := range m {
  494. p[i] = PathSlice{v}
  495. i++
  496. }
  497. sort.Sort(sort.Reverse(p))
  498. return p
  499. }
  500. const (
  501. videoTypeEpisode = "Episode"
  502. videoTypeMovie = "Movie"
  503. streamCodec = "subrip"
  504. )