add a method to use custom fallback fonts for subtitle rendering

This commit is contained in:
nyanmisaka 2020-07-24 21:19:56 +08:00
parent 912946a427
commit c888b34a62
2 changed files with 58 additions and 0 deletions

View File

@ -6,6 +6,7 @@ using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
@ -122,8 +123,15 @@ namespace MediaBrowser.Api.Subtitles
public int SegmentLength { get; set; }
}
[Route("/FallbackFont/Font", "GET", Summary = "Gets the fallback font file")]
[Authenticated]
public class GetFallbackFont
{
}
public class SubtitleService : BaseApiService
{
private readonly IServerConfigurationManager _serverConfigurationManager;
private readonly ILibraryManager _libraryManager;
private readonly ISubtitleManager _subtitleManager;
private readonly ISubtitleEncoder _subtitleEncoder;
@ -145,6 +153,7 @@ namespace MediaBrowser.Api.Subtitles
IAuthorizationContext authContext)
: base(logger, serverConfigurationManager, httpResultFactory)
{
_serverConfigurationManager = serverConfigurationManager;
_libraryManager = libraryManager;
_subtitleManager = subtitleManager;
_subtitleEncoder = subtitleEncoder;
@ -298,5 +307,49 @@ namespace MediaBrowser.Api.Subtitles
}
});
}
public async Task<object> Get(GetFallbackFont request)
{
var fallbackFontPath = EncodingConfigurationExtensions.GetEncodingOptions(_serverConfigurationManager).FallbackFontPath;
if (!string.IsNullOrEmpty(fallbackFontPath))
{
var directoryService = new DirectoryService(_fileSystem);
try
{
// 10 Megabytes
var maxSize = 10485760;
var fontFile = directoryService.GetFile(fallbackFontPath);
var fileSize = fontFile?.Length;
if (fileSize != null && fileSize > 0)
{
Logger.LogInformation("Fallback font size is {0} Bytes", fileSize);
if (fileSize <= maxSize)
{
return await ResultFactory.GetStaticFileResult(Request, fontFile.FullName);
}
Logger.LogInformation("The selected font is too large. Maximum allowed size is 10 Megabytes");
}
else
{
Logger.LogInformation("The selected font is null or empty");
}
}
catch (Exception ex)
{
Logger.LogError(ex, "Error reading fallback font file");
}
}
else
{
Logger.LogInformation("The path of fallback font has not been set");
}
return string.Empty;
}
}
}

View File

@ -9,6 +9,10 @@ namespace MediaBrowser.Model.Configuration
public string TranscodingTempPath { get; set; }
public string FallbackFontPath { get; set; }
public bool EnableFallbackFont { get; set; }
public double DownMixAudioBoost { get; set; }
public bool EnableThrottling { get; set; }
@ -49,6 +53,7 @@ namespace MediaBrowser.Model.Configuration
public EncodingOptions()
{
EnableFallbackFont = false;
DownMixAudioBoost = 2;
EnableThrottling = false;
ThrottleDelaySeconds = 180;