FFProbeVideoInfo.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. #pragma warning disable CA1068, CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using MediaBrowser.Common.Configuration;
  9. using MediaBrowser.Controller.Chapters;
  10. using MediaBrowser.Controller.Configuration;
  11. using MediaBrowser.Controller.Entities;
  12. using MediaBrowser.Controller.Entities.Movies;
  13. using MediaBrowser.Controller.Entities.TV;
  14. using MediaBrowser.Controller.Library;
  15. using MediaBrowser.Controller.MediaEncoding;
  16. using MediaBrowser.Controller.Persistence;
  17. using MediaBrowser.Controller.Providers;
  18. using MediaBrowser.Controller.Subtitles;
  19. using MediaBrowser.Model.Configuration;
  20. using MediaBrowser.Model.Dlna;
  21. using MediaBrowser.Model.Dto;
  22. using MediaBrowser.Model.Entities;
  23. using MediaBrowser.Model.Globalization;
  24. using MediaBrowser.Model.MediaInfo;
  25. using MediaBrowser.Model.Providers;
  26. using Microsoft.Extensions.Logging;
  27. namespace MediaBrowser.Providers.MediaInfo
  28. {
  29. public class FFProbeVideoInfo
  30. {
  31. private readonly ILogger<FFProbeVideoInfo> _logger;
  32. private readonly IMediaEncoder _mediaEncoder;
  33. private readonly IItemRepository _itemRepo;
  34. private readonly IBlurayExaminer _blurayExaminer;
  35. private readonly ILocalizationManager _localization;
  36. private readonly IEncodingManager _encodingManager;
  37. private readonly IServerConfigurationManager _config;
  38. private readonly ISubtitleManager _subtitleManager;
  39. private readonly IChapterManager _chapterManager;
  40. private readonly ILibraryManager _libraryManager;
  41. private readonly AudioResolver _audioResolver;
  42. private readonly SubtitleResolver _subtitleResolver;
  43. private readonly IMediaSourceManager _mediaSourceManager;
  44. public FFProbeVideoInfo(
  45. ILogger<FFProbeVideoInfo> logger,
  46. IMediaSourceManager mediaSourceManager,
  47. IMediaEncoder mediaEncoder,
  48. IItemRepository itemRepo,
  49. IBlurayExaminer blurayExaminer,
  50. ILocalizationManager localization,
  51. IEncodingManager encodingManager,
  52. IServerConfigurationManager config,
  53. ISubtitleManager subtitleManager,
  54. IChapterManager chapterManager,
  55. ILibraryManager libraryManager,
  56. AudioResolver audioResolver,
  57. SubtitleResolver subtitleResolver)
  58. {
  59. _logger = logger;
  60. _mediaSourceManager = mediaSourceManager;
  61. _mediaEncoder = mediaEncoder;
  62. _itemRepo = itemRepo;
  63. _blurayExaminer = blurayExaminer;
  64. _localization = localization;
  65. _encodingManager = encodingManager;
  66. _config = config;
  67. _subtitleManager = subtitleManager;
  68. _chapterManager = chapterManager;
  69. _libraryManager = libraryManager;
  70. _audioResolver = audioResolver;
  71. _subtitleResolver = subtitleResolver;
  72. }
  73. public async Task<ItemUpdateType> ProbeVideo<T>(
  74. T item,
  75. MetadataRefreshOptions options,
  76. CancellationToken cancellationToken)
  77. where T : Video
  78. {
  79. BlurayDiscInfo? blurayDiscInfo = null;
  80. Model.MediaInfo.MediaInfo? mediaInfoResult = null;
  81. if (!item.IsShortcut || options.EnableRemoteContentProbe)
  82. {
  83. if (item.VideoType == VideoType.Dvd)
  84. {
  85. // Get list of playable .vob files
  86. var vobs = _mediaEncoder.GetPrimaryPlaylistVobFiles(item.Path, null);
  87. // Return if no playable .vob files are found
  88. if (vobs.Count == 0)
  89. {
  90. _logger.LogError("No playable .vob files found in DVD structure, skipping FFprobe.");
  91. return ItemUpdateType.MetadataImport;
  92. }
  93. // Fetch metadata of first .vob file
  94. mediaInfoResult = await GetMediaInfo(
  95. new Video
  96. {
  97. Path = vobs[0]
  98. },
  99. cancellationToken).ConfigureAwait(false);
  100. // Sum up the runtime of all .vob files skipping the first .vob
  101. for (var i = 1; i < vobs.Count; i++)
  102. {
  103. var tmpMediaInfo = await GetMediaInfo(
  104. new Video
  105. {
  106. Path = vobs[i]
  107. },
  108. cancellationToken).ConfigureAwait(false);
  109. mediaInfoResult.RunTimeTicks += tmpMediaInfo.RunTimeTicks;
  110. }
  111. }
  112. else if (item.VideoType == VideoType.BluRay)
  113. {
  114. // Get BD disc information
  115. blurayDiscInfo = GetBDInfo(item.Path);
  116. // Get playable .m2ts files
  117. var m2ts = _mediaEncoder.GetPrimaryPlaylistM2tsFiles(item.Path);
  118. // Return if no playable .m2ts files are found
  119. if (blurayDiscInfo is null || blurayDiscInfo.Files.Length == 0 || m2ts.Count == 0)
  120. {
  121. _logger.LogError("No playable .m2ts files found in Blu-ray structure, skipping FFprobe.");
  122. return ItemUpdateType.MetadataImport;
  123. }
  124. // Fetch metadata of first .m2ts file
  125. mediaInfoResult = await GetMediaInfo(
  126. new Video
  127. {
  128. Path = m2ts[0]
  129. },
  130. cancellationToken).ConfigureAwait(false);
  131. }
  132. else
  133. {
  134. mediaInfoResult = await GetMediaInfo(item, cancellationToken).ConfigureAwait(false);
  135. }
  136. cancellationToken.ThrowIfCancellationRequested();
  137. }
  138. await Fetch(item, cancellationToken, mediaInfoResult, blurayDiscInfo, options).ConfigureAwait(false);
  139. return ItemUpdateType.MetadataImport;
  140. }
  141. private Task<Model.MediaInfo.MediaInfo> GetMediaInfo(
  142. Video item,
  143. CancellationToken cancellationToken)
  144. {
  145. cancellationToken.ThrowIfCancellationRequested();
  146. var path = item.Path;
  147. var protocol = item.PathProtocol ?? MediaProtocol.File;
  148. if (item.IsShortcut)
  149. {
  150. path = item.ShortcutPath;
  151. protocol = _mediaSourceManager.GetPathProtocol(path);
  152. }
  153. return _mediaEncoder.GetMediaInfo(
  154. new MediaInfoRequest
  155. {
  156. ExtractChapters = true,
  157. MediaType = DlnaProfileType.Video,
  158. MediaSource = new MediaSourceInfo
  159. {
  160. Path = path,
  161. Protocol = protocol,
  162. VideoType = item.VideoType,
  163. IsoType = item.IsoType
  164. }
  165. },
  166. cancellationToken);
  167. }
  168. protected async Task Fetch(
  169. Video video,
  170. CancellationToken cancellationToken,
  171. Model.MediaInfo.MediaInfo? mediaInfo,
  172. BlurayDiscInfo? blurayInfo,
  173. MetadataRefreshOptions options)
  174. {
  175. List<MediaStream> mediaStreams = new List<MediaStream>();
  176. IReadOnlyList<MediaAttachment> mediaAttachments;
  177. ChapterInfo[] chapters;
  178. // Add external streams before adding the streams from the file to preserve stream IDs on remote videos
  179. await AddExternalSubtitlesAsync(video, mediaStreams, options, cancellationToken).ConfigureAwait(false);
  180. await AddExternalAudioAsync(video, mediaStreams, options, cancellationToken).ConfigureAwait(false);
  181. var startIndex = mediaStreams.Count == 0 ? 0 : (mediaStreams.Max(i => i.Index) + 1);
  182. if (mediaInfo is not null)
  183. {
  184. foreach (var mediaStream in mediaInfo.MediaStreams)
  185. {
  186. mediaStream.Index = startIndex++;
  187. mediaStreams.Add(mediaStream);
  188. }
  189. mediaAttachments = mediaInfo.MediaAttachments;
  190. video.TotalBitrate = mediaInfo.Bitrate;
  191. video.RunTimeTicks = mediaInfo.RunTimeTicks;
  192. video.Size = mediaInfo.Size;
  193. video.Container = mediaInfo.Container;
  194. chapters = mediaInfo.Chapters ?? Array.Empty<ChapterInfo>();
  195. if (blurayInfo is not null)
  196. {
  197. FetchBdInfo(video, ref chapters, mediaStreams, blurayInfo);
  198. }
  199. }
  200. else
  201. {
  202. foreach (var mediaStream in video.GetMediaStreams())
  203. {
  204. if (!mediaStream.IsExternal)
  205. {
  206. mediaStream.Index = startIndex++;
  207. mediaStreams.Add(mediaStream);
  208. }
  209. }
  210. mediaAttachments = Array.Empty<MediaAttachment>();
  211. chapters = Array.Empty<ChapterInfo>();
  212. }
  213. var libraryOptions = _libraryManager.GetLibraryOptions(video);
  214. if (mediaInfo is not null)
  215. {
  216. FetchEmbeddedInfo(video, mediaInfo, options, libraryOptions);
  217. FetchPeople(video, mediaInfo, options);
  218. video.Timestamp = mediaInfo.Timestamp;
  219. video.Video3DFormat ??= mediaInfo.Video3DFormat;
  220. }
  221. if (libraryOptions.AllowEmbeddedSubtitles == EmbeddedSubtitleOptions.AllowText || libraryOptions.AllowEmbeddedSubtitles == EmbeddedSubtitleOptions.AllowNone)
  222. {
  223. _logger.LogDebug("Disabling embedded image subtitles for {Path} due to DisableEmbeddedImageSubtitles setting", video.Path);
  224. mediaStreams.RemoveAll(i => i.Type == MediaStreamType.Subtitle && !i.IsExternal && !i.IsTextSubtitleStream);
  225. }
  226. if (libraryOptions.AllowEmbeddedSubtitles == EmbeddedSubtitleOptions.AllowImage || libraryOptions.AllowEmbeddedSubtitles == EmbeddedSubtitleOptions.AllowNone)
  227. {
  228. _logger.LogDebug("Disabling embedded text subtitles for {Path} due to DisableEmbeddedTextSubtitles setting", video.Path);
  229. mediaStreams.RemoveAll(i => i.Type == MediaStreamType.Subtitle && !i.IsExternal && i.IsTextSubtitleStream);
  230. }
  231. var videoStream = mediaStreams.FirstOrDefault(i => i.Type == MediaStreamType.Video);
  232. video.Height = videoStream?.Height ?? 0;
  233. video.Width = videoStream?.Width ?? 0;
  234. video.DefaultVideoStreamIndex = videoStream?.Index;
  235. video.HasSubtitles = mediaStreams.Any(i => i.Type == MediaStreamType.Subtitle);
  236. _itemRepo.SaveMediaStreams(video.Id, mediaStreams, cancellationToken);
  237. if (mediaAttachments.Any())
  238. {
  239. _itemRepo.SaveMediaAttachments(video.Id, mediaAttachments, cancellationToken);
  240. }
  241. if (options.MetadataRefreshMode == MetadataRefreshMode.FullRefresh
  242. || options.MetadataRefreshMode == MetadataRefreshMode.Default)
  243. {
  244. if (_config.Configuration.DummyChapterDuration > 0 && chapters.Length == 0 && mediaStreams.Any(i => i.Type == MediaStreamType.Video))
  245. {
  246. chapters = CreateDummyChapters(video);
  247. }
  248. NormalizeChapterNames(chapters);
  249. var extractDuringScan = false;
  250. if (libraryOptions is not null)
  251. {
  252. extractDuringScan = libraryOptions.ExtractChapterImagesDuringLibraryScan;
  253. }
  254. await _encodingManager.RefreshChapterImages(video, options.DirectoryService, chapters, extractDuringScan, false, cancellationToken).ConfigureAwait(false);
  255. _chapterManager.SaveChapters(video.Id, chapters);
  256. }
  257. }
  258. private void NormalizeChapterNames(ChapterInfo[] chapters)
  259. {
  260. for (int i = 0; i < chapters.Length; i++)
  261. {
  262. string? name = chapters[i].Name;
  263. // Check if the name is empty and/or if the name is a time
  264. // Some ripping programs do that.
  265. if (string.IsNullOrWhiteSpace(name)
  266. || TimeSpan.TryParse(name, out _))
  267. {
  268. chapters[i].Name = string.Format(
  269. CultureInfo.InvariantCulture,
  270. _localization.GetLocalizedString("ChapterNameValue"),
  271. (i + 1).ToString(CultureInfo.InvariantCulture));
  272. }
  273. }
  274. }
  275. private void FetchBdInfo(Video video, ref ChapterInfo[] chapters, List<MediaStream> mediaStreams, BlurayDiscInfo blurayInfo)
  276. {
  277. if (blurayInfo.Files.Length <= 1)
  278. {
  279. return;
  280. }
  281. // Use BD Info if it has multiple m2ts. Otherwise, treat it like a video file and rely more on ffprobe output
  282. int? currentHeight = null;
  283. int? currentWidth = null;
  284. int? currentBitRate = null;
  285. var videoStream = mediaStreams.FirstOrDefault(s => s.Type == MediaStreamType.Video);
  286. // Grab the values that ffprobe recorded
  287. if (videoStream is not null)
  288. {
  289. currentBitRate = videoStream.BitRate;
  290. currentWidth = videoStream.Width;
  291. currentHeight = videoStream.Height;
  292. }
  293. // Fill video properties from the BDInfo result
  294. mediaStreams.Clear();
  295. mediaStreams.AddRange(blurayInfo.MediaStreams);
  296. if (blurayInfo.RunTimeTicks.HasValue && blurayInfo.RunTimeTicks.Value > 0)
  297. {
  298. video.RunTimeTicks = blurayInfo.RunTimeTicks;
  299. }
  300. if (blurayInfo.Chapters is not null)
  301. {
  302. double[] brChapter = blurayInfo.Chapters;
  303. chapters = new ChapterInfo[brChapter.Length];
  304. for (int i = 0; i < brChapter.Length; i++)
  305. {
  306. chapters[i] = new ChapterInfo
  307. {
  308. StartPositionTicks = TimeSpan.FromSeconds(brChapter[i]).Ticks
  309. };
  310. }
  311. }
  312. videoStream = mediaStreams.FirstOrDefault(s => s.Type == MediaStreamType.Video);
  313. // Use the ffprobe values if these are empty
  314. if (videoStream is not null)
  315. {
  316. videoStream.BitRate = videoStream.BitRate.GetValueOrDefault() == 0 ? currentBitRate : videoStream.BitRate;
  317. videoStream.Width = videoStream.Width.GetValueOrDefault() == 0 ? currentWidth : videoStream.Width;
  318. videoStream.Height = videoStream.Height.GetValueOrDefault() == 0 ? currentHeight : videoStream.Height;
  319. }
  320. }
  321. /// <summary>
  322. /// Gets information about the longest playlist on a bdrom.
  323. /// </summary>
  324. /// <param name="path">The path.</param>
  325. /// <returns>VideoStream.</returns>
  326. private BlurayDiscInfo? GetBDInfo(string path)
  327. {
  328. ArgumentException.ThrowIfNullOrEmpty(path);
  329. try
  330. {
  331. return _blurayExaminer.GetDiscInfo(path);
  332. }
  333. catch (Exception ex)
  334. {
  335. _logger.LogError(ex, "Error getting BDInfo");
  336. return null;
  337. }
  338. }
  339. private void FetchEmbeddedInfo(Video video, Model.MediaInfo.MediaInfo data, MetadataRefreshOptions refreshOptions, LibraryOptions libraryOptions)
  340. {
  341. var replaceData = refreshOptions.ReplaceAllMetadata;
  342. if (!video.IsLocked && !video.LockedFields.Contains(MetadataField.OfficialRating))
  343. {
  344. if (string.IsNullOrWhiteSpace(video.OfficialRating) || replaceData)
  345. {
  346. video.OfficialRating = data.OfficialRating;
  347. }
  348. }
  349. if (!video.IsLocked && !video.LockedFields.Contains(MetadataField.Genres))
  350. {
  351. if (video.Genres.Length == 0 || replaceData)
  352. {
  353. video.Genres = Array.Empty<string>();
  354. foreach (var genre in data.Genres)
  355. {
  356. video.AddGenre(genre);
  357. }
  358. }
  359. }
  360. if (!video.IsLocked && !video.LockedFields.Contains(MetadataField.Studios))
  361. {
  362. if (video.Studios.Length == 0 || replaceData)
  363. {
  364. video.SetStudios(data.Studios);
  365. }
  366. }
  367. if (!video.IsLocked && video is MusicVideo musicVideo)
  368. {
  369. if (string.IsNullOrEmpty(musicVideo.Album) || replaceData)
  370. {
  371. musicVideo.Album = data.Album;
  372. }
  373. if (musicVideo.Artists.Count == 0 || replaceData)
  374. {
  375. musicVideo.Artists = data.Artists;
  376. }
  377. }
  378. if (data.ProductionYear.HasValue)
  379. {
  380. if (!video.ProductionYear.HasValue || replaceData)
  381. {
  382. video.ProductionYear = data.ProductionYear;
  383. }
  384. }
  385. if (data.PremiereDate.HasValue)
  386. {
  387. if (!video.PremiereDate.HasValue || replaceData)
  388. {
  389. video.PremiereDate = data.PremiereDate;
  390. }
  391. }
  392. if (data.IndexNumber.HasValue)
  393. {
  394. if (!video.IndexNumber.HasValue || replaceData)
  395. {
  396. video.IndexNumber = data.IndexNumber;
  397. }
  398. }
  399. if (data.ParentIndexNumber.HasValue)
  400. {
  401. if (!video.ParentIndexNumber.HasValue || replaceData)
  402. {
  403. video.ParentIndexNumber = data.ParentIndexNumber;
  404. }
  405. }
  406. if (!video.IsLocked && !video.LockedFields.Contains(MetadataField.Name))
  407. {
  408. if (!string.IsNullOrWhiteSpace(data.Name) && libraryOptions.EnableEmbeddedTitles)
  409. {
  410. // Separate option to use the embedded name for extras because it will often be the same name as the movie
  411. if (!video.ExtraType.HasValue || libraryOptions.EnableEmbeddedExtrasTitles)
  412. {
  413. video.Name = data.Name;
  414. }
  415. }
  416. if (!string.IsNullOrWhiteSpace(data.ForcedSortName))
  417. {
  418. video.ForcedSortName = data.ForcedSortName;
  419. }
  420. }
  421. // If we don't have a ProductionYear try and get it from PremiereDate
  422. if (video.PremiereDate.HasValue && !video.ProductionYear.HasValue)
  423. {
  424. video.ProductionYear = video.PremiereDate.Value.ToLocalTime().Year;
  425. }
  426. if (!video.IsLocked && !video.LockedFields.Contains(MetadataField.Overview))
  427. {
  428. if (string.IsNullOrWhiteSpace(video.Overview) || replaceData)
  429. {
  430. video.Overview = data.Overview;
  431. }
  432. }
  433. }
  434. private void FetchPeople(Video video, Model.MediaInfo.MediaInfo data, MetadataRefreshOptions options)
  435. {
  436. if (video.IsLocked
  437. || video.LockedFields.Contains(MetadataField.Cast)
  438. || data.People.Length == 0)
  439. {
  440. return;
  441. }
  442. if (options.ReplaceAllMetadata || _libraryManager.GetPeople(video).Count == 0)
  443. {
  444. var people = new List<PersonInfo>();
  445. foreach (var person in data.People)
  446. {
  447. PeopleHelper.AddPerson(people, new PersonInfo
  448. {
  449. Name = person.Name,
  450. Type = person.Type,
  451. Role = person.Role
  452. });
  453. }
  454. _libraryManager.UpdatePeople(video, people);
  455. }
  456. }
  457. /// <summary>
  458. /// Adds the external subtitles.
  459. /// </summary>
  460. /// <param name="video">The video.</param>
  461. /// <param name="currentStreams">The current streams.</param>
  462. /// <param name="options">The refreshOptions.</param>
  463. /// <param name="cancellationToken">The cancellation token.</param>
  464. /// <returns>Task.</returns>
  465. private async Task AddExternalSubtitlesAsync(
  466. Video video,
  467. List<MediaStream> currentStreams,
  468. MetadataRefreshOptions options,
  469. CancellationToken cancellationToken)
  470. {
  471. var startIndex = currentStreams.Count == 0 ? 0 : (currentStreams.Select(i => i.Index).Max() + 1);
  472. var externalSubtitleStreams = await _subtitleResolver.GetExternalStreamsAsync(video, startIndex, options.DirectoryService, false, cancellationToken).ConfigureAwait(false);
  473. var enableSubtitleDownloading = options.MetadataRefreshMode == MetadataRefreshMode.Default ||
  474. options.MetadataRefreshMode == MetadataRefreshMode.FullRefresh;
  475. var subtitleOptions = _config.GetConfiguration<SubtitleOptions>("subtitles");
  476. var libraryOptions = _libraryManager.GetLibraryOptions(video);
  477. string[] subtitleDownloadLanguages;
  478. bool skipIfEmbeddedSubtitlesPresent;
  479. bool skipIfAudioTrackMatches;
  480. bool requirePerfectMatch;
  481. bool enabled;
  482. if (libraryOptions.SubtitleDownloadLanguages is null)
  483. {
  484. subtitleDownloadLanguages = subtitleOptions.DownloadLanguages;
  485. skipIfEmbeddedSubtitlesPresent = subtitleOptions.SkipIfEmbeddedSubtitlesPresent;
  486. skipIfAudioTrackMatches = subtitleOptions.SkipIfAudioTrackMatches;
  487. requirePerfectMatch = subtitleOptions.RequirePerfectMatch;
  488. enabled = (subtitleOptions.DownloadEpisodeSubtitles &&
  489. video is Episode) ||
  490. (subtitleOptions.DownloadMovieSubtitles &&
  491. video is Movie);
  492. }
  493. else
  494. {
  495. subtitleDownloadLanguages = libraryOptions.SubtitleDownloadLanguages;
  496. skipIfEmbeddedSubtitlesPresent = libraryOptions.SkipSubtitlesIfEmbeddedSubtitlesPresent;
  497. skipIfAudioTrackMatches = libraryOptions.SkipSubtitlesIfAudioTrackMatches;
  498. requirePerfectMatch = libraryOptions.RequirePerfectSubtitleMatch;
  499. enabled = true;
  500. }
  501. if (enableSubtitleDownloading && enabled)
  502. {
  503. var downloadedLanguages = await new SubtitleDownloader(
  504. _logger,
  505. _subtitleManager).DownloadSubtitles(
  506. video,
  507. currentStreams.Concat(externalSubtitleStreams).ToList(),
  508. skipIfEmbeddedSubtitlesPresent,
  509. skipIfAudioTrackMatches,
  510. requirePerfectMatch,
  511. subtitleDownloadLanguages,
  512. libraryOptions.DisabledSubtitleFetchers,
  513. libraryOptions.SubtitleFetcherOrder,
  514. true,
  515. cancellationToken).ConfigureAwait(false);
  516. // Rescan
  517. if (downloadedLanguages.Count > 0)
  518. {
  519. externalSubtitleStreams = await _subtitleResolver.GetExternalStreamsAsync(video, startIndex, options.DirectoryService, true, cancellationToken).ConfigureAwait(false);
  520. }
  521. }
  522. video.SubtitleFiles = externalSubtitleStreams.Select(i => i.Path).Distinct().ToArray();
  523. currentStreams.AddRange(externalSubtitleStreams);
  524. }
  525. /// <summary>
  526. /// Adds the external audio.
  527. /// </summary>
  528. /// <param name="video">The video.</param>
  529. /// <param name="currentStreams">The current streams.</param>
  530. /// <param name="options">The refreshOptions.</param>
  531. /// <param name="cancellationToken">The cancellation token.</param>
  532. private async Task AddExternalAudioAsync(
  533. Video video,
  534. List<MediaStream> currentStreams,
  535. MetadataRefreshOptions options,
  536. CancellationToken cancellationToken)
  537. {
  538. var startIndex = currentStreams.Count == 0 ? 0 : currentStreams.Max(i => i.Index) + 1;
  539. var externalAudioStreams = await _audioResolver.GetExternalStreamsAsync(video, startIndex, options.DirectoryService, false, cancellationToken).ConfigureAwait(false);
  540. video.AudioFiles = externalAudioStreams.Select(i => i.Path).Distinct().ToArray();
  541. currentStreams.AddRange(externalAudioStreams);
  542. }
  543. /// <summary>
  544. /// Creates dummy chapters.
  545. /// </summary>
  546. /// <param name="video">The video.</param>
  547. /// <returns>An array of dummy chapters.</returns>
  548. internal ChapterInfo[] CreateDummyChapters(Video video)
  549. {
  550. var runtime = video.RunTimeTicks.GetValueOrDefault();
  551. // Only process files with a runtime higher than 0 and lower than 12h. The latter are likely corrupted.
  552. if (runtime < 0 || runtime > TimeSpan.FromHours(12).Ticks)
  553. {
  554. throw new ArgumentException(
  555. string.Format(
  556. CultureInfo.InvariantCulture,
  557. "{0} has an invalid runtime of {1} minutes",
  558. video.Name,
  559. TimeSpan.FromTicks(runtime).TotalMinutes));
  560. }
  561. long dummyChapterDuration = TimeSpan.FromSeconds(_config.Configuration.DummyChapterDuration).Ticks;
  562. if (runtime <= dummyChapterDuration)
  563. {
  564. return Array.Empty<ChapterInfo>();
  565. }
  566. int chapterCount = (int)(runtime / dummyChapterDuration);
  567. var chapters = new ChapterInfo[chapterCount];
  568. long currentChapterTicks = 0;
  569. for (int i = 0; i < chapterCount; i++)
  570. {
  571. chapters[i] = new ChapterInfo
  572. {
  573. StartPositionTicks = currentChapterTicks
  574. };
  575. currentChapterTicks += dummyChapterDuration;
  576. }
  577. return chapters;
  578. }
  579. }
  580. }