2019-01-13 20:03:10 +00:00
|
|
|
using System;
|
2014-05-16 19:16:29 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
2019-01-13 19:26:31 +00:00
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
2016-10-24 02:45:23 +00:00
|
|
|
using MediaBrowser.Model.Globalization;
|
2014-05-16 19:16:29 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.MediaInfo
|
|
|
|
{
|
2021-11-27 19:35:18 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Resolves external subtitles for videos.
|
|
|
|
/// </summary>
|
2014-05-16 19:16:29 +00:00
|
|
|
public class SubtitleResolver
|
|
|
|
{
|
|
|
|
private readonly ILocalizationManager _localization;
|
|
|
|
|
2021-11-27 19:35:18 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="SubtitleResolver"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="localization">The localization manager.</param>
|
2020-04-11 10:29:04 +00:00
|
|
|
public SubtitleResolver(ILocalizationManager localization)
|
2014-05-16 19:16:29 +00:00
|
|
|
{
|
|
|
|
_localization = localization;
|
|
|
|
}
|
|
|
|
|
2021-11-27 19:35:18 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Retrieves the external subtitle streams for the provided video.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="video">The video to search from.</param>
|
|
|
|
/// <param name="startIndex">The stream index to start adding subtitle streams at.</param>
|
|
|
|
/// <param name="directoryService">The directory service to search for files.</param>
|
|
|
|
/// <param name="clearCache">True if the directory service cache should be cleared before searching.</param>
|
|
|
|
/// <returns>The external subtitle streams located.</returns>
|
2020-04-11 10:29:04 +00:00
|
|
|
public List<MediaStream> GetExternalSubtitleStreams(
|
|
|
|
Video video,
|
2020-04-11 13:37:24 +00:00
|
|
|
int startIndex,
|
|
|
|
IDirectoryService directoryService,
|
|
|
|
bool clearCache)
|
2014-05-16 19:16:29 +00:00
|
|
|
{
|
|
|
|
var streams = new List<MediaStream>();
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
if (!video.IsFileProtocol)
|
|
|
|
{
|
|
|
|
return streams;
|
|
|
|
}
|
|
|
|
|
|
|
|
AddExternalSubtitleStreams(streams, video.ContainingFolderPath, video.Path, startIndex, directoryService, clearCache);
|
2017-11-05 21:51:23 +00:00
|
|
|
|
|
|
|
startIndex += streams.Count;
|
|
|
|
|
2019-02-24 14:47:59 +00:00
|
|
|
string folder = video.GetInternalMetadataPath();
|
|
|
|
|
|
|
|
if (!Directory.Exists(folder))
|
|
|
|
{
|
|
|
|
return streams;
|
|
|
|
}
|
|
|
|
|
2017-11-05 21:51:23 +00:00
|
|
|
try
|
|
|
|
{
|
2019-02-24 14:47:59 +00:00
|
|
|
AddExternalSubtitleStreams(streams, folder, video.Path, startIndex, directoryService, clearCache);
|
2017-11-05 21:51:23 +00:00
|
|
|
}
|
|
|
|
catch (IOException)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
return streams;
|
|
|
|
}
|
|
|
|
|
2021-11-27 19:35:18 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Locates the external subtitle files for the provided video.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="video">The video to search from.</param>
|
|
|
|
/// <param name="directoryService">The directory service to search for files.</param>
|
|
|
|
/// <param name="clearCache">True if the directory service cache should be cleared before searching.</param>
|
|
|
|
/// <returns>The external subtitle file paths located.</returns>
|
2021-05-23 22:30:41 +00:00
|
|
|
public IEnumerable<string> GetExternalSubtitleFiles(
|
2020-09-07 11:20:39 +00:00
|
|
|
Video video,
|
|
|
|
IDirectoryService directoryService,
|
|
|
|
bool clearCache)
|
2017-11-05 21:51:23 +00:00
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
if (!video.IsFileProtocol)
|
|
|
|
{
|
2021-05-23 22:30:41 +00:00
|
|
|
yield break;
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var streams = GetExternalSubtitleStreams(video, 0, directoryService, clearCache);
|
|
|
|
|
2017-11-05 21:51:23 +00:00
|
|
|
foreach (var stream in streams)
|
|
|
|
{
|
2021-05-23 22:30:41 +00:00
|
|
|
yield return stream.Path;
|
2017-11-05 21:51:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-27 19:35:18 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Extracts the subtitle files from the provided list and adds them to the list of streams.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="streams">The list of streams to add external subtitles to.</param>
|
|
|
|
/// <param name="videoPath">The path to the video file.</param>
|
|
|
|
/// <param name="startIndex">The stream index to start adding subtitle streams at.</param>
|
|
|
|
/// <param name="files">The files to add if they are subtitles.</param>
|
2020-09-07 11:20:39 +00:00
|
|
|
public void AddExternalSubtitleStreams(
|
|
|
|
List<MediaStream> streams,
|
2018-09-12 17:26:21 +00:00
|
|
|
string videoPath,
|
|
|
|
int startIndex,
|
2021-05-23 22:30:41 +00:00
|
|
|
IReadOnlyList<string> files)
|
2017-11-05 21:51:23 +00:00
|
|
|
{
|
2021-05-16 12:49:11 +00:00
|
|
|
var videoFileNameWithoutExtension = NormalizeFilenameForSubtitleComparison(videoPath);
|
2014-05-16 19:16:29 +00:00
|
|
|
|
2021-05-23 22:30:41 +00:00
|
|
|
for (var i = 0; i < files.Count; i++)
|
2014-05-16 19:16:29 +00:00
|
|
|
{
|
2021-05-23 22:30:41 +00:00
|
|
|
var fullName = files[i];
|
2021-05-16 12:49:11 +00:00
|
|
|
var extension = Path.GetExtension(fullName.AsSpan());
|
|
|
|
if (!IsSubtitleExtension(extension))
|
2017-11-05 21:51:23 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-05-16 12:49:11 +00:00
|
|
|
var fileNameWithoutExtension = NormalizeFilenameForSubtitleComparison(fullName);
|
2014-05-16 19:16:29 +00:00
|
|
|
|
2021-05-16 12:49:11 +00:00
|
|
|
MediaStream mediaStream;
|
2016-11-08 18:44:23 +00:00
|
|
|
|
2021-05-16 12:49:11 +00:00
|
|
|
// The subtitle filename must either be equal to the video filename or start with the video filename followed by a dot
|
|
|
|
if (videoFileNameWithoutExtension.Equals(fileNameWithoutExtension, StringComparison.OrdinalIgnoreCase))
|
2014-05-16 19:16:29 +00:00
|
|
|
{
|
2021-05-16 12:49:11 +00:00
|
|
|
mediaStream = new MediaStream
|
2014-05-16 19:16:29 +00:00
|
|
|
{
|
|
|
|
Index = startIndex++,
|
|
|
|
Type = MediaStreamType.Subtitle,
|
|
|
|
IsExternal = true,
|
2021-05-16 12:49:11 +00:00
|
|
|
Path = fullName
|
|
|
|
};
|
2014-05-16 19:16:29 +00:00
|
|
|
}
|
2021-05-16 18:16:47 +00:00
|
|
|
else if (fileNameWithoutExtension.Length > videoFileNameWithoutExtension.Length
|
2021-05-16 12:49:11 +00:00
|
|
|
&& fileNameWithoutExtension[videoFileNameWithoutExtension.Length] == '.'
|
|
|
|
&& fileNameWithoutExtension.StartsWith(videoFileNameWithoutExtension, StringComparison.OrdinalIgnoreCase))
|
2014-05-16 19:16:29 +00:00
|
|
|
{
|
2021-05-16 12:49:11 +00:00
|
|
|
var isForced = fullName.Contains(".forced.", StringComparison.OrdinalIgnoreCase)
|
|
|
|
|| fullName.Contains(".foreign.", StringComparison.OrdinalIgnoreCase);
|
2014-05-16 19:16:29 +00:00
|
|
|
|
2021-05-16 12:49:11 +00:00
|
|
|
var isDefault = fullName.Contains(".default.", StringComparison.OrdinalIgnoreCase);
|
2015-12-26 18:35:53 +00:00
|
|
|
|
2014-05-16 19:16:29 +00:00
|
|
|
// Support xbmc naming conventions - 300.spanish.srt
|
2021-05-16 12:49:11 +00:00
|
|
|
var languageSpan = fileNameWithoutExtension;
|
|
|
|
while (languageSpan.Length > 0)
|
|
|
|
{
|
|
|
|
var lastDot = languageSpan.LastIndexOf('.');
|
2021-11-27 19:13:21 +00:00
|
|
|
if (lastDot < videoFileNameWithoutExtension.Length)
|
|
|
|
{
|
|
|
|
languageSpan = ReadOnlySpan<char>.Empty;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-05-16 12:49:11 +00:00
|
|
|
var currentSlice = languageSpan[lastDot..];
|
|
|
|
if (currentSlice.Equals(".default", StringComparison.OrdinalIgnoreCase)
|
|
|
|
|| currentSlice.Equals(".forced", StringComparison.OrdinalIgnoreCase)
|
|
|
|
|| currentSlice.Equals(".foreign", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
languageSpan = languageSpan[..lastDot];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
languageSpan = languageSpan[(lastDot + 1)..];
|
|
|
|
break;
|
|
|
|
}
|
2014-05-16 19:16:29 +00:00
|
|
|
|
2021-05-23 22:30:41 +00:00
|
|
|
var language = languageSpan.ToString();
|
2021-11-27 19:13:21 +00:00
|
|
|
if (string.IsNullOrWhiteSpace(language))
|
|
|
|
{
|
|
|
|
language = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Try to translate to three character code
|
|
|
|
// Be flexible and check against both the full and three character versions
|
|
|
|
var culture = _localization.FindLanguageInfo(language);
|
2014-05-16 19:16:29 +00:00
|
|
|
|
2021-11-27 19:13:21 +00:00
|
|
|
language = culture == null ? language : culture.ThreeLetterISOLanguageName;
|
|
|
|
}
|
2014-05-16 19:16:29 +00:00
|
|
|
|
2021-05-16 12:49:11 +00:00
|
|
|
mediaStream = new MediaStream
|
2014-05-16 19:16:29 +00:00
|
|
|
{
|
|
|
|
Index = startIndex++,
|
|
|
|
Type = MediaStreamType.Subtitle,
|
|
|
|
IsExternal = true,
|
|
|
|
Path = fullName,
|
|
|
|
Language = language,
|
2015-12-26 18:35:53 +00:00
|
|
|
IsForced = isForced,
|
|
|
|
IsDefault = isDefault
|
2021-05-16 12:49:11 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
continue;
|
2014-05-16 19:16:29 +00:00
|
|
|
}
|
2021-05-16 12:49:11 +00:00
|
|
|
|
|
|
|
mediaStream.Codec = extension.TrimStart('.').ToString().ToLowerInvariant();
|
|
|
|
|
|
|
|
streams.Add(mediaStream);
|
2014-05-16 19:16:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-16 12:49:11 +00:00
|
|
|
private static bool IsSubtitleExtension(ReadOnlySpan<char> extension)
|
|
|
|
{
|
|
|
|
return extension.Equals(".srt", StringComparison.OrdinalIgnoreCase)
|
|
|
|
|| extension.Equals(".ssa", StringComparison.OrdinalIgnoreCase)
|
|
|
|
|| extension.Equals(".ass", StringComparison.OrdinalIgnoreCase)
|
|
|
|
|| extension.Equals(".sub", StringComparison.OrdinalIgnoreCase)
|
|
|
|
|| extension.Equals(".vtt", StringComparison.OrdinalIgnoreCase)
|
|
|
|
|| extension.Equals(".smi", StringComparison.OrdinalIgnoreCase)
|
|
|
|
|| extension.Equals(".sami", StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static ReadOnlySpan<char> NormalizeFilenameForSubtitleComparison(string filename)
|
2015-12-26 18:35:53 +00:00
|
|
|
{
|
|
|
|
// Try to account for sloppy file naming
|
2020-09-07 11:20:39 +00:00
|
|
|
filename = filename.Replace("_", string.Empty, StringComparison.Ordinal);
|
|
|
|
filename = filename.Replace(" ", string.Empty, StringComparison.Ordinal);
|
2021-05-16 12:49:11 +00:00
|
|
|
return Path.GetFileNameWithoutExtension(filename.AsSpan());
|
|
|
|
}
|
2015-12-26 18:35:53 +00:00
|
|
|
|
2021-05-16 12:49:11 +00:00
|
|
|
private void AddExternalSubtitleStreams(
|
|
|
|
List<MediaStream> streams,
|
|
|
|
string folder,
|
|
|
|
string videoPath,
|
|
|
|
int startIndex,
|
|
|
|
IDirectoryService directoryService,
|
|
|
|
bool clearCache)
|
|
|
|
{
|
2021-05-31 11:55:54 +00:00
|
|
|
var files = directoryService.GetFilePaths(folder, clearCache, true);
|
2015-12-26 18:35:53 +00:00
|
|
|
|
2021-05-16 12:49:11 +00:00
|
|
|
AddExternalSubtitleStreams(streams, videoPath, startIndex, files);
|
2015-12-26 18:35:53 +00:00
|
|
|
}
|
2014-05-16 19:16:29 +00:00
|
|
|
}
|
|
|
|
}
|