SubtitleResolver.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 string[] SubtitleExtensions = new[]
  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. try
  45. {
  46. AddExternalSubtitleStreams(streams, video.GetInternalMetadataPath(), video.Path, startIndex, directoryService, clearCache);
  47. }
  48. catch (IOException)
  49. {
  50. }
  51. return streams;
  52. }
  53. public List<string> GetExternalSubtitleFiles(Video video,
  54. IDirectoryService directoryService,
  55. bool clearCache)
  56. {
  57. var list = new List<string>();
  58. if (!video.IsFileProtocol)
  59. {
  60. return list;
  61. }
  62. var streams = GetExternalSubtitleStreams(video, 0, directoryService, clearCache);
  63. foreach (var stream in streams)
  64. {
  65. list.Add(stream.Path);
  66. }
  67. return list;
  68. }
  69. private void AddExternalSubtitleStreams(List<MediaStream> streams, string folder,
  70. string videoPath,
  71. int startIndex,
  72. IDirectoryService directoryService,
  73. bool clearCache)
  74. {
  75. var files = directoryService.GetFilePaths(folder, clearCache).OrderBy(i => i).ToArray();
  76. AddExternalSubtitleStreams(streams, videoPath, startIndex, files);
  77. }
  78. public void AddExternalSubtitleStreams(List<MediaStream> streams,
  79. string videoPath,
  80. int startIndex,
  81. string[] files)
  82. {
  83. var videoFileNameWithoutExtension = Path.GetFileNameWithoutExtension(videoPath);
  84. videoFileNameWithoutExtension = NormalizeFilenameForSubtitleComparison(videoFileNameWithoutExtension);
  85. foreach (var fullName in files)
  86. {
  87. var extension = Path.GetExtension(fullName);
  88. if (!SubtitleExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase))
  89. {
  90. continue;
  91. }
  92. var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fullName);
  93. fileNameWithoutExtension = NormalizeFilenameForSubtitleComparison(fileNameWithoutExtension);
  94. if (!string.Equals(videoFileNameWithoutExtension, fileNameWithoutExtension, StringComparison.OrdinalIgnoreCase) &&
  95. !fileNameWithoutExtension.StartsWith(videoFileNameWithoutExtension + ".", StringComparison.OrdinalIgnoreCase))
  96. {
  97. continue;
  98. }
  99. var codec = Path.GetExtension(fullName).ToLower().TrimStart('.');
  100. if (string.Equals(codec, "txt", StringComparison.OrdinalIgnoreCase))
  101. {
  102. codec = "srt";
  103. }
  104. // If the subtitle file matches the video file name
  105. if (string.Equals(videoFileNameWithoutExtension, fileNameWithoutExtension, StringComparison.OrdinalIgnoreCase))
  106. {
  107. streams.Add(new MediaStream
  108. {
  109. Index = startIndex++,
  110. Type = MediaStreamType.Subtitle,
  111. IsExternal = true,
  112. Path = fullName,
  113. Codec = codec
  114. });
  115. }
  116. else if (fileNameWithoutExtension.StartsWith(videoFileNameWithoutExtension + ".", StringComparison.OrdinalIgnoreCase))
  117. {
  118. var isForced = fullName.IndexOf(".forced.", StringComparison.OrdinalIgnoreCase) != -1 ||
  119. fullName.IndexOf(".foreign.", StringComparison.OrdinalIgnoreCase) != -1;
  120. var isDefault = fullName.IndexOf(".default.", StringComparison.OrdinalIgnoreCase) != -1;
  121. // Support xbmc naming conventions - 300.spanish.srt
  122. var language = fileNameWithoutExtension
  123. .Replace(".forced", string.Empty, StringComparison.OrdinalIgnoreCase)
  124. .Replace(".foreign", string.Empty, StringComparison.OrdinalIgnoreCase)
  125. .Replace(".default", string.Empty, StringComparison.OrdinalIgnoreCase)
  126. .Split('.')
  127. .LastOrDefault();
  128. // Try to translate to three character code
  129. // Be flexible and check against both the full and three character versions
  130. var culture = _localization.FindLanguageInfo(language);
  131. if (culture != null)
  132. {
  133. language = culture.ThreeLetterISOLanguageName;
  134. }
  135. streams.Add(new MediaStream
  136. {
  137. Index = startIndex++,
  138. Type = MediaStreamType.Subtitle,
  139. IsExternal = true,
  140. Path = fullName,
  141. Codec = codec,
  142. Language = language,
  143. IsForced = isForced,
  144. IsDefault = isDefault
  145. });
  146. }
  147. }
  148. }
  149. private string NormalizeFilenameForSubtitleComparison(string filename)
  150. {
  151. // Try to account for sloppy file naming
  152. filename = filename.Replace("_", string.Empty);
  153. filename = filename.Replace(" ", string.Empty);
  154. // can't normalize this due to languages such as pt-br
  155. //filename = filename.Replace("-", string.Empty);
  156. //filename = filename.Replace(".", string.Empty);
  157. return filename;
  158. }
  159. }
  160. }