AudioResolver.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Runtime.CompilerServices;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Emby.Naming.Audio;
  8. using Emby.Naming.Common;
  9. using Jellyfin.Extensions;
  10. using MediaBrowser.Controller.Entities;
  11. using MediaBrowser.Controller.MediaEncoding;
  12. using MediaBrowser.Controller.Providers;
  13. using MediaBrowser.Model.Dlna;
  14. using MediaBrowser.Model.Dto;
  15. using MediaBrowser.Model.Entities;
  16. using MediaBrowser.Model.Globalization;
  17. using MediaBrowser.Model.MediaInfo;
  18. namespace MediaBrowser.Providers.MediaInfo
  19. {
  20. public class AudioResolver
  21. {
  22. private readonly ILocalizationManager _localizationManager;
  23. private readonly IMediaEncoder _mediaEncoder;
  24. private readonly NamingOptions _namingOptions;
  25. public AudioResolver(
  26. ILocalizationManager localizationManager,
  27. IMediaEncoder mediaEncoder,
  28. NamingOptions namingOptions)
  29. {
  30. _localizationManager = localizationManager;
  31. _mediaEncoder = mediaEncoder;
  32. _namingOptions = namingOptions;
  33. }
  34. public async IAsyncEnumerable<MediaStream> GetExternalAudioStreams(
  35. Video video,
  36. int startIndex,
  37. IDirectoryService directoryService,
  38. bool clearCache,
  39. [EnumeratorCancellation] CancellationToken cancellationToken)
  40. {
  41. cancellationToken.ThrowIfCancellationRequested();
  42. if (!video.IsFileProtocol)
  43. {
  44. yield break;
  45. }
  46. IEnumerable<string> paths = GetExternalAudioFiles(video, directoryService, clearCache);
  47. foreach (string path in paths)
  48. {
  49. string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path);
  50. Model.MediaInfo.MediaInfo mediaInfo = await GetMediaInfo(path, cancellationToken);
  51. foreach (MediaStream mediaStream in mediaInfo.MediaStreams)
  52. {
  53. mediaStream.Index = startIndex++;
  54. mediaStream.Type = MediaStreamType.Audio;
  55. mediaStream.IsExternal = true;
  56. mediaStream.Path = path;
  57. mediaStream.IsDefault = false;
  58. mediaStream.Title = null;
  59. if (string.IsNullOrEmpty(mediaStream.Language))
  60. {
  61. // Try to translate to three character code
  62. // Be flexible and check against both the full and three character versions
  63. var language = StringExtensions.RightPart(fileNameWithoutExtension, '.').ToString();
  64. if (language != fileNameWithoutExtension)
  65. {
  66. var culture = _localizationManager.FindLanguageInfo(language);
  67. language = culture == null ? language : culture.ThreeLetterISOLanguageName;
  68. mediaStream.Language = language;
  69. }
  70. }
  71. yield return mediaStream;
  72. }
  73. }
  74. }
  75. public IEnumerable<string> GetExternalAudioFiles(
  76. Video video,
  77. IDirectoryService directoryService,
  78. bool clearCache)
  79. {
  80. if (!video.IsFileProtocol)
  81. {
  82. yield break;
  83. }
  84. // Check if video folder exists
  85. string folder = video.ContainingFolderPath;
  86. if (!Directory.Exists(folder))
  87. {
  88. yield break;
  89. }
  90. string videoFileNameWithoutExtension = Path.GetFileNameWithoutExtension(video.Path);
  91. var files = directoryService.GetFilePaths(folder, clearCache, true);
  92. for (int i = 0; i < files.Count; i++)
  93. {
  94. string file = files[i];
  95. if (!AudioFileParser.IsAudioFile(file, _namingOptions))
  96. {
  97. continue;
  98. }
  99. string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
  100. // The audio filename must either be equal to the video filename or start with the video filename followed by a dot
  101. if (videoFileNameWithoutExtension.Equals(fileNameWithoutExtension, StringComparison.OrdinalIgnoreCase)
  102. || (fileNameWithoutExtension.Length > videoFileNameWithoutExtension.Length
  103. && fileNameWithoutExtension[videoFileNameWithoutExtension.Length] == '.'
  104. && fileNameWithoutExtension.StartsWith(videoFileNameWithoutExtension, StringComparison.OrdinalIgnoreCase)))
  105. {
  106. yield return file;
  107. }
  108. }
  109. }
  110. private Task<Model.MediaInfo.MediaInfo> GetMediaInfo(string path, CancellationToken cancellationToken)
  111. {
  112. cancellationToken.ThrowIfCancellationRequested();
  113. return _mediaEncoder.GetMediaInfo(
  114. new MediaInfoRequest
  115. {
  116. MediaType = DlnaProfileType.Audio,
  117. MediaSource = new MediaSourceInfo
  118. {
  119. Path = path,
  120. Protocol = MediaProtocol.File
  121. }
  122. },
  123. cancellationToken);
  124. }
  125. }
  126. }