SubtitleResolver.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using MediaBrowser.Controller.Entities;
  6. using MediaBrowser.Controller.Providers;
  7. using MediaBrowser.Model.Entities;
  8. using MediaBrowser.Model.Extensions;
  9. using MediaBrowser.Model.Globalization;
  10. using MediaBrowser.Model.IO;
  11. namespace MediaBrowser.Providers.MediaInfo
  12. {
  13. public class SubtitleResolver
  14. {
  15. private readonly ILocalizationManager _localization;
  16. private readonly IFileSystem _fileSystem;
  17. private static readonly HashSet<string> SubtitleExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
  18. {
  19. ".srt",
  20. ".ssa",
  21. ".ass",
  22. ".sub",
  23. ".smi",
  24. ".sami",
  25. ".vtt"
  26. };
  27. public SubtitleResolver(ILocalizationManager localization, IFileSystem fileSystem)
  28. {
  29. _localization = localization;
  30. _fileSystem = fileSystem;
  31. }
  32. public List<MediaStream> GetExternalSubtitleStreams(Video video,
  33. int startIndex,
  34. IDirectoryService directoryService,
  35. bool clearCache)
  36. {
  37. var streams = new List<MediaStream>();
  38. if (!video.IsFileProtocol)
  39. {
  40. return streams;
  41. }
  42. AddExternalSubtitleStreams(streams, video.ContainingFolderPath, video.Path, startIndex, directoryService, clearCache);
  43. startIndex += streams.Count;
  44. string folder = video.GetInternalMetadataPath();
  45. if (!Directory.Exists(folder))
  46. {
  47. return streams;
  48. }
  49. try
  50. {
  51. AddExternalSubtitleStreams(streams, folder, video.Path, startIndex, directoryService, clearCache);
  52. }
  53. catch (IOException)
  54. {
  55. }
  56. return streams;
  57. }
  58. public List<string> GetExternalSubtitleFiles(Video video,
  59. IDirectoryService directoryService,
  60. bool clearCache)
  61. {
  62. var list = new List<string>();
  63. if (!video.IsFileProtocol)
  64. {
  65. return list;
  66. }
  67. var streams = GetExternalSubtitleStreams(video, 0, directoryService, clearCache);
  68. foreach (var stream in streams)
  69. {
  70. list.Add(stream.Path);
  71. }
  72. return list;
  73. }
  74. private void AddExternalSubtitleStreams(List<MediaStream> streams, string folder,
  75. string videoPath,
  76. int startIndex,
  77. IDirectoryService directoryService,
  78. bool clearCache)
  79. {
  80. var files = directoryService.GetFilePaths(folder, clearCache).OrderBy(i => i).ToArray();
  81. AddExternalSubtitleStreams(streams, videoPath, startIndex, files);
  82. }
  83. public void AddExternalSubtitleStreams(List<MediaStream> streams,
  84. string videoPath,
  85. int startIndex,
  86. string[] files)
  87. {
  88. var videoFileNameWithoutExtension = Path.GetFileNameWithoutExtension(videoPath);
  89. videoFileNameWithoutExtension = NormalizeFilenameForSubtitleComparison(videoFileNameWithoutExtension);
  90. foreach (var fullName in files)
  91. {
  92. var extension = Path.GetExtension(fullName);
  93. if (!SubtitleExtensions.Contains(extension))
  94. {
  95. continue;
  96. }
  97. var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fullName);
  98. fileNameWithoutExtension = NormalizeFilenameForSubtitleComparison(fileNameWithoutExtension);
  99. if (!string.Equals(videoFileNameWithoutExtension, fileNameWithoutExtension, StringComparison.OrdinalIgnoreCase) &&
  100. !fileNameWithoutExtension.StartsWith(videoFileNameWithoutExtension + ".", StringComparison.OrdinalIgnoreCase))
  101. {
  102. continue;
  103. }
  104. var codec = Path.GetExtension(fullName).ToLowerInvariant().TrimStart('.');
  105. if (string.Equals(codec, "txt", StringComparison.OrdinalIgnoreCase))
  106. {
  107. codec = "srt";
  108. }
  109. // If the subtitle file matches the video file name
  110. if (string.Equals(videoFileNameWithoutExtension, fileNameWithoutExtension, StringComparison.OrdinalIgnoreCase))
  111. {
  112. streams.Add(new MediaStream
  113. {
  114. Index = startIndex++,
  115. Type = MediaStreamType.Subtitle,
  116. IsExternal = true,
  117. Path = fullName,
  118. Codec = codec
  119. });
  120. }
  121. else if (fileNameWithoutExtension.StartsWith(videoFileNameWithoutExtension + ".", StringComparison.OrdinalIgnoreCase))
  122. {
  123. var isForced = fullName.IndexOf(".forced.", StringComparison.OrdinalIgnoreCase) != -1 ||
  124. fullName.IndexOf(".foreign.", StringComparison.OrdinalIgnoreCase) != -1;
  125. var isDefault = fullName.IndexOf(".default.", StringComparison.OrdinalIgnoreCase) != -1;
  126. // Support xbmc naming conventions - 300.spanish.srt
  127. var language = fileNameWithoutExtension
  128. .Replace(".forced", string.Empty, StringComparison.OrdinalIgnoreCase)
  129. .Replace(".foreign", string.Empty, StringComparison.OrdinalIgnoreCase)
  130. .Replace(".default", string.Empty, StringComparison.OrdinalIgnoreCase)
  131. .Split('.')
  132. .LastOrDefault();
  133. // Try to translate to three character code
  134. // Be flexible and check against both the full and three character versions
  135. var culture = _localization.FindLanguageInfo(language);
  136. if (culture != null)
  137. {
  138. language = culture.ThreeLetterISOLanguageName;
  139. }
  140. streams.Add(new MediaStream
  141. {
  142. Index = startIndex++,
  143. Type = MediaStreamType.Subtitle,
  144. IsExternal = true,
  145. Path = fullName,
  146. Codec = codec,
  147. Language = language,
  148. IsForced = isForced,
  149. IsDefault = isDefault
  150. });
  151. }
  152. }
  153. }
  154. private string NormalizeFilenameForSubtitleComparison(string filename)
  155. {
  156. // Try to account for sloppy file naming
  157. filename = filename.Replace("_", string.Empty);
  158. filename = filename.Replace(" ", string.Empty);
  159. // can't normalize this due to languages such as pt-br
  160. //filename = filename.Replace("-", string.Empty);
  161. //filename = filename.Replace(".", string.Empty);
  162. return filename;
  163. }
  164. }
  165. }