diff options
| author | Luke <luke.pulverenti@gmail.com> | 2017-04-20 23:49:50 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-04-20 23:49:50 -0400 |
| commit | f525f5a89e68248a81ab7cfdfa044fbe45e51863 (patch) | |
| tree | f029e380a0867d6e8db7895e63d9a69e0cfd426f /MediaBrowser.MediaEncoding | |
| parent | 5dca8cb077fb544628f62939bbda292d8c91b637 (diff) | |
| parent | b9fe720e736abacc57ffb846e4ce6644bc110f61 (diff) | |
Merge pull request #2591 from MediaBrowser/beta
Beta
Diffstat (limited to 'MediaBrowser.MediaEncoding')
6 files changed, 40 insertions, 195 deletions
diff --git a/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs index c0440e132..e402c2bac 100644 --- a/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs +++ b/MediaBrowser.MediaEncoding/Encoder/BaseEncoder.cs @@ -356,7 +356,7 @@ namespace MediaBrowser.MediaEncoding.Encoder { OpenToken = state.MediaSource.OpenToken - }, false, cancellationToken).ConfigureAwait(false); + }, cancellationToken).ConfigureAwait(false); EncodingHelper.AttachMediaSourceInfo(state, liveStreamResponse.MediaSource, null); diff --git a/MediaBrowser.MediaEncoding/Encoder/EncodingJob.cs b/MediaBrowser.MediaEncoding/Encoder/EncodingJob.cs index 98adb6e20..f74dbce98 100644 --- a/MediaBrowser.MediaEncoding/Encoder/EncodingJob.cs +++ b/MediaBrowser.MediaEncoding/Encoder/EncodingJob.cs @@ -57,7 +57,7 @@ namespace MediaBrowser.MediaEncoding.Encoder private readonly IMediaSourceManager _mediaSourceManager; public EncodingJob(ILogger logger, IMediaSourceManager mediaSourceManager) : - base(logger) + base(logger, TranscodingJobType.Progressive) { _logger = logger; _mediaSourceManager = mediaSourceManager; diff --git a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs index 97f2c57eb..816f14f82 100644 --- a/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs +++ b/MediaBrowser.MediaEncoding/Encoder/MediaEncoder.cs @@ -50,16 +50,6 @@ namespace MediaBrowser.MediaEncoding.Encoder private readonly SemaphoreSlim _thumbnailResourcePool = new SemaphoreSlim(1, 1); /// <summary> - /// The video image resource pool - /// </summary> - private readonly SemaphoreSlim _videoImageResourcePool = new SemaphoreSlim(1, 1); - - /// <summary> - /// The audio image resource pool - /// </summary> - private readonly SemaphoreSlim _audioImageResourcePool = new SemaphoreSlim(2, 2); - - /// <summary> /// The FF probe resource pool /// </summary> private readonly SemaphoreSlim _ffProbeResourcePool = new SemaphoreSlim(2, 2); @@ -674,28 +664,16 @@ namespace MediaBrowser.MediaEncoding.Encoder private bool DetectInterlaced(MediaSourceInfo video, MediaStream videoStream) { - var formats = (video.Container ?? string.Empty).Split(',').ToList(); - var enableInterlacedDection = formats.Contains("vob", StringComparer.OrdinalIgnoreCase) || - formats.Contains("m2ts", StringComparer.OrdinalIgnoreCase) || - formats.Contains("ts", StringComparer.OrdinalIgnoreCase) || - formats.Contains("mpegts", StringComparer.OrdinalIgnoreCase) || - formats.Contains("wtv", StringComparer.OrdinalIgnoreCase); - // If it's mpeg based, assume true if ((videoStream.Codec ?? string.Empty).IndexOf("mpeg", StringComparison.OrdinalIgnoreCase) != -1) { - if (enableInterlacedDection) - { - return true; - } - } - else - { - // If the video codec is not some form of mpeg, then take a shortcut and limit this to containers that are likely to have interlaced content - if (!enableInterlacedDection) - { - return false; - } + var formats = (video.Container ?? string.Empty).Split(',').ToList(); + return formats.Contains("vob", StringComparer.OrdinalIgnoreCase) || + formats.Contains("m2ts", StringComparer.OrdinalIgnoreCase) || + formats.Contains("ts", StringComparer.OrdinalIgnoreCase) || + formats.Contains("mpegts", StringComparer.OrdinalIgnoreCase) || + formats.Contains("wtv", StringComparer.OrdinalIgnoreCase); + } return false; @@ -724,8 +702,6 @@ namespace MediaBrowser.MediaEncoding.Encoder private async Task<string> ExtractImage(string[] inputFiles, string container, int? imageStreamIndex, MediaProtocol protocol, bool isAudio, Video3DFormat? threedFormat, TimeSpan? offset, CancellationToken cancellationToken) { - var resourcePool = isAudio ? _audioImageResourcePool : _videoImageResourcePool; - var inputArgument = GetInputArgument(inputFiles, protocol); if (isAudio) @@ -740,7 +716,7 @@ namespace MediaBrowser.MediaEncoding.Encoder { try { - return await ExtractImageInternal(inputArgument, container, imageStreamIndex, protocol, threedFormat, offset, true, resourcePool, cancellationToken).ConfigureAwait(false); + return await ExtractImageInternal(inputArgument, container, imageStreamIndex, protocol, threedFormat, offset, true, cancellationToken).ConfigureAwait(false); } catch (ArgumentException) { @@ -752,10 +728,10 @@ namespace MediaBrowser.MediaEncoding.Encoder } } - return await ExtractImageInternal(inputArgument, container, imageStreamIndex, protocol, threedFormat, offset, false, resourcePool, cancellationToken).ConfigureAwait(false); + return await ExtractImageInternal(inputArgument, container, imageStreamIndex, protocol, threedFormat, offset, false, cancellationToken).ConfigureAwait(false); } - private async Task<string> ExtractImageInternal(string inputPath, string container, int? imageStreamIndex, MediaProtocol protocol, Video3DFormat? threedFormat, TimeSpan? offset, bool useIFrame, SemaphoreSlim resourcePool, CancellationToken cancellationToken) + private async Task<string> ExtractImageInternal(string inputPath, string container, int? imageStreamIndex, MediaProtocol protocol, Video3DFormat? threedFormat, TimeSpan? offset, bool useIFrame, CancellationToken cancellationToken) { if (string.IsNullOrEmpty(inputPath)) { @@ -835,31 +811,21 @@ namespace MediaBrowser.MediaEncoding.Encoder using (var processWrapper = new ProcessWrapper(process, this, _logger)) { - await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false); - bool ranToCompletion; - try - { - StartProcess(processWrapper); - - var timeoutMs = ConfigurationManager.Configuration.ImageExtractionTimeoutMs; - if (timeoutMs <= 0) - { - timeoutMs = DefaultImageExtractionTimeoutMs; - } + StartProcess(processWrapper); - ranToCompletion = process.WaitForExit(timeoutMs); + var timeoutMs = ConfigurationManager.Configuration.ImageExtractionTimeoutMs; + if (timeoutMs <= 0) + { + timeoutMs = DefaultImageExtractionTimeoutMs; + } - if (!ranToCompletion) - { - StopProcess(processWrapper, 1000); - } + ranToCompletion = process.WaitForExit(timeoutMs); - } - finally + if (!ranToCompletion) { - resourcePool.Release(); + StopProcess(processWrapper, 1000); } var exitCode = ranToCompletion ? processWrapper.ExitCode ?? 0 : -1; @@ -1118,7 +1084,6 @@ namespace MediaBrowser.MediaEncoding.Encoder { if (dispose) { - _videoImageResourcePool.Dispose(); StopProcesses(); } } diff --git a/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.csproj b/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.csproj index ee19c4b66..d02132151 100644 --- a/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.csproj +++ b/MediaBrowser.MediaEncoding/MediaBrowser.MediaEncoding.csproj @@ -37,12 +37,6 @@ <WarningLevel>4</WarningLevel> </PropertyGroup> <ItemGroup> - <Reference Include="UniversalDetector, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL"> - <HintPath>..\packages\UniversalDetector.1.0.1\lib\portable-net45+sl4+wp71+win8+wpa81\UniversalDetector.dll</HintPath> - <Private>True</Private> - </Reference> - </ItemGroup> - <ItemGroup> <Compile Include="..\SharedVersion.cs"> <Link>Properties\SharedVersion.cs</Link> </Compile> diff --git a/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs b/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs index 77b976206..231a2ae85 100644 --- a/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs +++ b/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs @@ -21,7 +21,6 @@ using MediaBrowser.Model.IO; using MediaBrowser.Model.Diagnostics; using MediaBrowser.Model.Dto; using MediaBrowser.Model.Text; -using UniversalDetector; namespace MediaBrowser.MediaEncoding.Subtitles { @@ -197,17 +196,20 @@ namespace MediaBrowser.MediaEncoding.Subtitles { if (requiresCharset) { - var charset = await GetSubtitleFileCharacterSet(path, language, protocol, cancellationToken).ConfigureAwait(false); + var bytes = await GetBytes(path, protocol, cancellationToken).ConfigureAwait(false); + + var charset = _textEncoding.GetDetectedEncodingName(bytes, language); + _logger.Debug("charset {0} detected for {1}", charset ?? "null", path); if (!string.IsNullOrEmpty(charset)) { - using (var fs = await GetStream(path, protocol, cancellationToken).ConfigureAwait(false)) + using (var inputStream = _memoryStreamProvider.CreateNew(bytes)) { - using (var reader = new StreamReader(fs, GetEncoding(charset))) + using (var reader = new StreamReader(inputStream, _textEncoding.GetEncodingFromCharset(charset))) { var text = await reader.ReadToEndAsync().ConfigureAwait(false); - var bytes = Encoding.UTF8.GetBytes(text); + bytes = Encoding.UTF8.GetBytes(text); return _memoryStreamProvider.CreateNew(bytes); } @@ -218,28 +220,6 @@ namespace MediaBrowser.MediaEncoding.Subtitles return _fileSystem.OpenRead(path); } - private Encoding GetEncoding(string charset) - { - if (string.IsNullOrWhiteSpace(charset)) - { - throw new ArgumentNullException("charset"); - } - - _logger.Debug("Getting encoding object for character set: {0}", charset); - - try - { - return Encoding.GetEncoding(charset); - } - catch (ArgumentException) - { - charset = charset.Replace("-", string.Empty); - _logger.Debug("Getting encoding object for character set: {0}", charset); - - return Encoding.GetEncoding(charset); - } - } - private async Task<Tuple<string, MediaProtocol, string, bool>> GetReadableFile(string mediaPath, string[] inputFiles, MediaProtocol protocol, @@ -724,126 +704,33 @@ namespace MediaBrowser.MediaEncoding.Subtitles public async Task<string> GetSubtitleFileCharacterSet(string path, string language, MediaProtocol protocol, CancellationToken cancellationToken) { - if (protocol == MediaProtocol.File) - { - var fileEncoding = _textEncoding.GetFileEncoding(path); - - if (fileEncoding != null && fileEncoding.Equals(Encoding.UTF8)) - { - return string.Empty; - } - } - - var charset = await DetectCharset(path, language, protocol, cancellationToken).ConfigureAwait(false); - - if (!string.IsNullOrWhiteSpace(charset)) - { - if (string.Equals(charset, "utf-8", StringComparison.OrdinalIgnoreCase)) - { - return null; - } - - return charset; - } + var bytes = await GetBytes(path, protocol, cancellationToken).ConfigureAwait(false); - if (!string.IsNullOrWhiteSpace(language)) - { - return GetSubtitleFileCharacterSetFromLanguage(language); - } + var charset = _textEncoding.GetDetectedEncodingName(bytes, language); - return null; - } + _logger.Debug("charset {0} detected for {1}", charset ?? "null", path); - public string GetSubtitleFileCharacterSetFromLanguage(string language) - { - // https://developer.xamarin.com/api/type/System.Text.Encoding/ - - switch (language.ToLower()) - { - case "hun": - return "windows-1252"; - case "pol": - case "cze": - case "ces": - case "slo": - case "slk": - case "slv": - case "srp": - case "hrv": - case "rum": - case "ron": - case "rup": - case "alb": - case "sqi": - return "windows-1250"; - case "ara": - return "windows-1256"; - case "heb": - return "windows-1255"; - case "grc": - case "gre": - return "windows-1253"; - case "crh": - case "ota": - case "tur": - return "windows-1254"; - case "rus": - return "windows-1251"; - case "vie": - return "windows-1258"; - case "kor": - return "cp949"; - default: - return "windows-1252"; - } + return charset; } - private async Task<string> DetectCharset(string path, string language, MediaProtocol protocol, CancellationToken cancellationToken) + private async Task<byte[]> GetBytes(string path, MediaProtocol protocol, CancellationToken cancellationToken) { - try + if (protocol == MediaProtocol.Http) { - using (var file = await GetStream(path, protocol, cancellationToken).ConfigureAwait(false)) + using (var file = await _httpClient.Get(path, cancellationToken).ConfigureAwait(false)) { - var detector = new CharsetDetector(); - detector.Feed(file); - detector.DataEnd(); - - var charset = detector.Charset; - - if (!string.IsNullOrWhiteSpace(charset)) + using (var memoryStream = new MemoryStream()) { - _logger.Info("UniversalDetector detected charset {0} for {1}", charset, path); - } + await file.CopyToAsync(memoryStream).ConfigureAwait(false); + memoryStream.Position = 0; - // This is often incorrectly indetected. If this happens, try to use other techniques instead - if (string.Equals("x-mac-cyrillic", charset, StringComparison.OrdinalIgnoreCase)) - { - if (!string.IsNullOrWhiteSpace(language)) - { - return null; - } + return memoryStream.ToArray(); } - - return charset; } } - catch (IOException ex) - { - _logger.ErrorException("Error attempting to determine subtitle charset from {0}", ex, path); - } - - return null; - } - - private async Task<Stream> GetStream(string path, MediaProtocol protocol, CancellationToken cancellationToken) - { - if (protocol == MediaProtocol.Http) - { - return await _httpClient.Get(path, cancellationToken).ConfigureAwait(false); - } if (protocol == MediaProtocol.File) { - return _fileSystem.GetFileStream(path, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.ReadWrite); + return _fileSystem.ReadAllBytes(path); } throw new ArgumentOutOfRangeException("protocol"); diff --git a/MediaBrowser.MediaEncoding/packages.config b/MediaBrowser.MediaEncoding/packages.config index bac7840a5..6b8deb9c9 100644 --- a/MediaBrowser.MediaEncoding/packages.config +++ b/MediaBrowser.MediaEncoding/packages.config @@ -1,4 +1,3 @@ <?xml version="1.0" encoding="utf-8"?> <packages> - <package id="UniversalDetector" version="1.0.1" targetFramework="net46" /> </packages>
\ No newline at end of file |
