2020-06-19 18:24:13 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
public class SubtitleResolver
|
|
|
|
{
|
|
|
|
private readonly ILocalizationManager _localization;
|
|
|
|
|
2020-04-11 10:29:04 +00:00
|
|
|
public SubtitleResolver(ILocalizationManager localization)
|
2014-05-16 19:16:29 +00:00
|
|
|
{
|
|
|
|
_localization = localization;
|
|
|
|
}
|
|
|
|
|
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-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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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('.');
|
|
|
|
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
|
|
|
|
|
|
|
// Try to translate to three character code
|
|
|
|
// Be flexible and check against both the full and three character versions
|
2021-05-23 22:30:41 +00:00
|
|
|
var language = languageSpan.ToString();
|
2018-09-12 17:26:21 +00:00
|
|
|
var culture = _localization.FindLanguageInfo(language);
|
2014-05-16 19:16:29 +00:00
|
|
|
|
2021-05-28 17:01:08 +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-23 22:30:41 +00:00
|
|
|
var files = directoryService.GetSortedFilePaths(folder, clearCache);
|
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
|
|
|
}
|
|
|
|
}
|