jellyfin/MediaBrowser.Providers/MediaInfo/SubtitleResolver.cs

138 lines
5.5 KiB
C#
Raw Normal View History

2015-01-17 21:12:02 +01:00
using MediaBrowser.Model.Extensions;
2014-07-26 19:30:15 +02:00
using MediaBrowser.Common.IO;
2014-05-16 21:16:29 +02:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Localization;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2015-10-04 06:23:11 +02:00
using CommonIO;
2014-05-16 21:16:29 +02:00
namespace MediaBrowser.Providers.MediaInfo
{
public class SubtitleResolver
{
private readonly ILocalizationManager _localization;
2014-07-26 19:30:15 +02:00
private readonly IFileSystem _fileSystem;
2014-05-16 21:16:29 +02:00
2014-07-26 19:30:15 +02:00
public SubtitleResolver(ILocalizationManager localization, IFileSystem fileSystem)
2014-05-16 21:16:29 +02:00
{
_localization = localization;
2014-07-26 19:30:15 +02:00
_fileSystem = fileSystem;
2014-05-16 21:16:29 +02:00
}
public IEnumerable<MediaStream> GetExternalSubtitleStreams(Video video,
int startIndex,
IDirectoryService directoryService,
bool clearCache)
{
2014-07-26 19:30:15 +02:00
var files = GetSubtitleFiles(video, directoryService, _fileSystem, clearCache);
2014-05-16 21:16:29 +02:00
var streams = new List<MediaStream>();
2014-07-26 19:30:15 +02:00
var videoFileNameWithoutExtension = _fileSystem.GetFileNameWithoutExtension(video.Path);
2014-05-16 21:16:29 +02:00
foreach (var file in files)
{
var fullName = file.FullName;
2014-07-26 19:30:15 +02:00
var fileNameWithoutExtension = _fileSystem.GetFileNameWithoutExtension(file);
2014-05-16 21:16:29 +02:00
var codec = Path.GetExtension(fullName).ToLower().TrimStart('.');
// If the subtitle file matches the video file name
if (string.Equals(videoFileNameWithoutExtension, fileNameWithoutExtension, StringComparison.OrdinalIgnoreCase))
{
streams.Add(new MediaStream
{
Index = startIndex++,
Type = MediaStreamType.Subtitle,
IsExternal = true,
Path = fullName,
Codec = codec
});
}
else if (fileNameWithoutExtension.StartsWith(videoFileNameWithoutExtension + ".", StringComparison.OrdinalIgnoreCase))
{
var isForced = fullName.IndexOf(".forced.", StringComparison.OrdinalIgnoreCase) != -1 ||
fullName.IndexOf(".foreign.", StringComparison.OrdinalIgnoreCase) != -1;
// Support xbmc naming conventions - 300.spanish.srt
var language = fileNameWithoutExtension
.Replace(".forced", string.Empty, StringComparison.OrdinalIgnoreCase)
.Replace(".foreign", string.Empty, StringComparison.OrdinalIgnoreCase)
.Split('.')
.LastOrDefault();
// Try to translate to three character code
// Be flexible and check against both the full and three character versions
var culture = _localization.GetCultures()
.FirstOrDefault(i => string.Equals(i.DisplayName, language, StringComparison.OrdinalIgnoreCase) || string.Equals(i.Name, language, StringComparison.OrdinalIgnoreCase) || string.Equals(i.ThreeLetterISOLanguageName, language, StringComparison.OrdinalIgnoreCase) || string.Equals(i.TwoLetterISOLanguageName, language, StringComparison.OrdinalIgnoreCase));
if (culture != null)
{
language = culture.ThreeLetterISOLanguageName;
}
streams.Add(new MediaStream
{
Index = startIndex++,
Type = MediaStreamType.Subtitle,
IsExternal = true,
Path = fullName,
Codec = codec,
Language = language,
IsForced = isForced
});
}
}
return streams;
}
private static IEnumerable<string> SubtitleExtensions
{
get
{
return new[] { ".srt", ".ssa", ".ass", ".sub" };
}
}
2015-10-04 05:38:46 +02:00
public static IEnumerable<FileSystemMetadata> GetSubtitleFiles(Video video, IDirectoryService directoryService, IFileSystem fileSystem, bool clearCache)
2014-05-16 21:16:29 +02:00
{
var containingPath = video.ContainingFolderPath;
if (string.IsNullOrEmpty(containingPath))
{
throw new ArgumentException(string.Format("Cannot search for items that don't have a path: {0} {1}", video.Name, video.Id));
}
var files = directoryService.GetFiles(containingPath, clearCache);
2014-07-26 19:30:15 +02:00
var videoFileNameWithoutExtension = fileSystem.GetFileNameWithoutExtension(video.Path);
2014-05-16 21:16:29 +02:00
return files.Where(i =>
{
if (!i.Attributes.HasFlag(FileAttributes.Directory) &&
SubtitleExtensions.Contains(i.Extension, StringComparer.OrdinalIgnoreCase))
{
2014-07-26 19:30:15 +02:00
var fileNameWithoutExtension = fileSystem.GetFileNameWithoutExtension(i);
2014-05-16 21:16:29 +02:00
if (string.Equals(videoFileNameWithoutExtension, fileNameWithoutExtension, StringComparison.OrdinalIgnoreCase))
{
return true;
}
if (fileNameWithoutExtension.StartsWith(videoFileNameWithoutExtension + ".", StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
return false;
});
}
}
}