2014-06-11 14:42:03 +00:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
|
using MediaBrowser.Common.Extensions;
|
2015-03-31 16:24:16 +00:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2014-06-11 14:42:03 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using MediaBrowser.Model.MediaInfo;
|
2014-08-22 15:47:44 +00:00
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2014-06-11 14:42:03 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2016-10-25 19:02:04 +00:00
|
|
|
|
using MediaBrowser.Model.IO;
|
2016-11-01 04:07:12 +00:00
|
|
|
|
using MediaBrowser.Model.Diagnostics;
|
2017-02-16 07:14:12 +00:00
|
|
|
|
using MediaBrowser.Model.Dto;
|
2016-11-08 18:44:23 +00:00
|
|
|
|
using MediaBrowser.Model.Text;
|
2014-06-11 14:42:03 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.MediaEncoding.Subtitles
|
|
|
|
|
{
|
|
|
|
|
public class SubtitleEncoder : ISubtitleEncoder
|
|
|
|
|
{
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IApplicationPaths _appPaths;
|
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
|
private readonly IMediaEncoder _mediaEncoder;
|
2014-08-22 15:47:44 +00:00
|
|
|
|
private readonly IJsonSerializer _json;
|
2015-03-31 16:24:16 +00:00
|
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
|
private readonly IMediaSourceManager _mediaSourceManager;
|
2016-11-08 18:44:23 +00:00
|
|
|
|
private readonly IMemoryStreamFactory _memoryStreamProvider;
|
2016-11-01 04:07:12 +00:00
|
|
|
|
private readonly IProcessFactory _processFactory;
|
2016-11-08 18:44:23 +00:00
|
|
|
|
private readonly ITextEncoding _textEncoding;
|
2014-06-11 14:42:03 +00:00
|
|
|
|
|
2016-11-08 18:44:23 +00:00
|
|
|
|
public SubtitleEncoder(ILibraryManager libraryManager, ILogger logger, IApplicationPaths appPaths, IFileSystem fileSystem, IMediaEncoder mediaEncoder, IJsonSerializer json, IHttpClient httpClient, IMediaSourceManager mediaSourceManager, IMemoryStreamFactory memoryStreamProvider, IProcessFactory processFactory, ITextEncoding textEncoding)
|
2014-06-11 14:42:03 +00:00
|
|
|
|
{
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_appPaths = appPaths;
|
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
|
_mediaEncoder = mediaEncoder;
|
2014-08-22 15:47:44 +00:00
|
|
|
|
_json = json;
|
2015-03-31 16:24:16 +00:00
|
|
|
|
_httpClient = httpClient;
|
|
|
|
|
_mediaSourceManager = mediaSourceManager;
|
2016-10-06 18:55:01 +00:00
|
|
|
|
_memoryStreamProvider = memoryStreamProvider;
|
2016-11-01 04:07:12 +00:00
|
|
|
|
_processFactory = processFactory;
|
|
|
|
|
_textEncoding = textEncoding;
|
2014-06-11 14:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string SubtitleCachePath
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2015-10-05 03:24:24 +00:00
|
|
|
|
return Path.Combine(_appPaths.DataPath, "subtitles");
|
2014-06-11 14:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-28 19:42:24 +00:00
|
|
|
|
private async Task<Stream> ConvertSubtitles(Stream stream,
|
2014-06-11 14:42:03 +00:00
|
|
|
|
string inputFormat,
|
|
|
|
|
string outputFormat,
|
2014-06-11 19:31:33 +00:00
|
|
|
|
long startTimeTicks,
|
2014-08-05 23:59:24 +00:00
|
|
|
|
long? endTimeTicks,
|
2016-05-27 17:17:57 +00:00
|
|
|
|
bool preserveOriginalTimestamps,
|
2014-06-11 14:42:03 +00:00
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
{
|
2016-10-06 18:55:01 +00:00
|
|
|
|
var ms = _memoryStreamProvider.CreateNew();
|
2014-06-11 14:42:03 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2015-07-28 19:42:24 +00:00
|
|
|
|
var reader = GetReader(inputFormat, true);
|
|
|
|
|
|
|
|
|
|
var trackInfo = reader.Parse(stream, cancellationToken);
|
2014-06-11 14:42:03 +00:00
|
|
|
|
|
2016-05-27 17:17:57 +00:00
|
|
|
|
FilterEvents(trackInfo, startTimeTicks, endTimeTicks, preserveOriginalTimestamps);
|
2014-06-11 19:31:33 +00:00
|
|
|
|
|
2014-11-04 12:41:12 +00:00
|
|
|
|
var writer = GetWriter(outputFormat);
|
2014-06-11 14:42:03 +00:00
|
|
|
|
|
2014-11-04 12:41:12 +00:00
|
|
|
|
writer.Write(trackInfo, ms, cancellationToken);
|
2014-06-11 14:42:03 +00:00
|
|
|
|
ms.Position = 0;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
ms.Dispose();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ms;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-05 23:59:24 +00:00
|
|
|
|
private void FilterEvents(SubtitleTrackInfo track, long startPositionTicks, long? endTimeTicks, bool preserveTimestamps)
|
2014-06-11 19:31:33 +00:00
|
|
|
|
{
|
2014-08-05 23:59:24 +00:00
|
|
|
|
// Drop subs that are earlier than what we're looking for
|
|
|
|
|
track.TrackEvents = track.TrackEvents
|
|
|
|
|
.SkipWhile(i => (i.StartPositionTicks - startPositionTicks) < 0 || (i.EndPositionTicks - startPositionTicks) < 0)
|
|
|
|
|
.ToList();
|
2014-06-11 19:31:33 +00:00
|
|
|
|
|
2014-08-05 23:59:24 +00:00
|
|
|
|
if (endTimeTicks.HasValue)
|
2014-06-11 19:31:33 +00:00
|
|
|
|
{
|
2014-08-05 23:59:24 +00:00
|
|
|
|
var endTime = endTimeTicks.Value;
|
|
|
|
|
|
|
|
|
|
track.TrackEvents = track.TrackEvents
|
|
|
|
|
.TakeWhile(i => i.StartPositionTicks <= endTime)
|
|
|
|
|
.ToList();
|
2014-06-11 19:31:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-08-05 23:59:24 +00:00
|
|
|
|
if (!preserveTimestamps)
|
|
|
|
|
{
|
|
|
|
|
foreach (var trackEvent in track.TrackEvents)
|
|
|
|
|
{
|
|
|
|
|
trackEvent.EndPositionTicks -= startPositionTicks;
|
|
|
|
|
trackEvent.StartPositionTicks -= startPositionTicks;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-11 19:31:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-11 14:42:03 +00:00
|
|
|
|
public async Task<Stream> GetSubtitles(string itemId,
|
|
|
|
|
string mediaSourceId,
|
|
|
|
|
int subtitleStreamIndex,
|
|
|
|
|
string outputFormat,
|
2014-06-11 19:31:33 +00:00
|
|
|
|
long startTimeTicks,
|
2014-08-05 23:59:24 +00:00
|
|
|
|
long? endTimeTicks,
|
2016-05-27 17:17:57 +00:00
|
|
|
|
bool preserveOriginalTimestamps,
|
2014-06-11 14:42:03 +00:00
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
{
|
2016-07-07 15:56:09 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(itemId))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("itemId");
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrWhiteSpace(mediaSourceId))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("mediaSourceId");
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-16 07:14:12 +00:00
|
|
|
|
var mediaSources = await _mediaSourceManager.GetPlayackMediaSources(itemId, null, false, new[] { MediaType.Audio, MediaType.Video }, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var mediaSource = mediaSources
|
|
|
|
|
.First(i => string.Equals(i.Id, mediaSourceId, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
|
|
var subtitleStream = mediaSource.MediaStreams
|
|
|
|
|
.First(i => i.Type == MediaStreamType.Subtitle && i.Index == subtitleStreamIndex);
|
|
|
|
|
|
|
|
|
|
var subtitle = await GetSubtitleStream(mediaSource, subtitleStream, cancellationToken)
|
2014-06-11 14:42:03 +00:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
2015-12-27 21:07:28 +00:00
|
|
|
|
var inputFormat = subtitle.Item2;
|
2016-11-18 03:12:03 +00:00
|
|
|
|
var writer = TryGetWriter(outputFormat);
|
2015-12-27 21:07:28 +00:00
|
|
|
|
|
2017-02-16 07:14:12 +00:00
|
|
|
|
// Return the original if we don't have any way of converting it
|
|
|
|
|
if (writer == null)
|
2016-11-18 03:12:03 +00:00
|
|
|
|
{
|
|
|
|
|
return subtitle.Item1;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-16 07:14:12 +00:00
|
|
|
|
// Return the original if the same format is being requested
|
|
|
|
|
// Character encoding was already handled in GetSubtitleStream
|
|
|
|
|
if (string.Equals(inputFormat, outputFormat, StringComparison.OrdinalIgnoreCase))
|
2014-06-11 14:42:03 +00:00
|
|
|
|
{
|
2015-12-27 21:07:28 +00:00
|
|
|
|
return subtitle.Item1;
|
|
|
|
|
}
|
2014-06-11 14:42:03 +00:00
|
|
|
|
|
2015-12-27 21:07:28 +00:00
|
|
|
|
using (var stream = subtitle.Item1)
|
|
|
|
|
{
|
2016-05-27 17:17:57 +00:00
|
|
|
|
return await ConvertSubtitles(stream, inputFormat, outputFormat, startTimeTicks, endTimeTicks, preserveOriginalTimestamps, cancellationToken).ConfigureAwait(false);
|
2014-06-11 14:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-16 07:14:12 +00:00
|
|
|
|
private async Task<Tuple<Stream, string>> GetSubtitleStream(MediaSourceInfo mediaSource,
|
|
|
|
|
MediaStream subtitleStream,
|
2014-06-11 14:42:03 +00:00
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var inputFiles = new[] { mediaSource.Path };
|
|
|
|
|
|
|
|
|
|
if (mediaSource.VideoType.HasValue)
|
|
|
|
|
{
|
2017-02-16 07:14:12 +00:00
|
|
|
|
if (mediaSource.VideoType.Value == VideoType.BluRay || mediaSource.VideoType.Value == VideoType.Dvd)
|
2014-06-11 14:42:03 +00:00
|
|
|
|
{
|
2017-02-16 07:14:12 +00:00
|
|
|
|
var mediaSourceItem = (Video)_libraryManager.GetItemById(new Guid(mediaSource.Id));
|
2014-06-11 14:42:03 +00:00
|
|
|
|
inputFiles = mediaSourceItem.GetPlayableStreamFiles().ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-17 01:56:23 +00:00
|
|
|
|
var fileInfo = await GetReadableFile(mediaSource.Path, inputFiles, mediaSource.Protocol, subtitleStream, cancellationToken).ConfigureAwait(false);
|
2014-06-11 14:42:03 +00:00
|
|
|
|
|
2016-04-03 01:19:27 +00:00
|
|
|
|
var stream = await GetSubtitleStream(fileInfo.Item1, subtitleStream.Language, fileInfo.Item2, fileInfo.Item4, cancellationToken).ConfigureAwait(false);
|
2014-06-11 14:42:03 +00:00
|
|
|
|
|
2015-03-31 16:24:16 +00:00
|
|
|
|
return new Tuple<Stream, string>(stream, fileInfo.Item3);
|
2014-06-11 14:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-03 01:19:27 +00:00
|
|
|
|
private async Task<Stream> GetSubtitleStream(string path, string language, MediaProtocol protocol, bool requiresCharset, CancellationToken cancellationToken)
|
2014-06-11 19:31:33 +00:00
|
|
|
|
{
|
2015-01-18 19:53:34 +00:00
|
|
|
|
if (requiresCharset)
|
2014-06-11 19:31:33 +00:00
|
|
|
|
{
|
2017-04-18 05:53:39 +00:00
|
|
|
|
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);
|
2014-06-11 19:31:33 +00:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(charset))
|
|
|
|
|
{
|
2017-04-18 05:53:39 +00:00
|
|
|
|
using (var inputStream = _memoryStreamProvider.CreateNew(bytes))
|
2014-06-11 19:31:33 +00:00
|
|
|
|
{
|
2017-04-18 05:53:39 +00:00
|
|
|
|
using (var reader = new StreamReader(inputStream, _textEncoding.GetEncodingFromCharset(charset)))
|
2014-06-11 19:31:33 +00:00
|
|
|
|
{
|
|
|
|
|
var text = await reader.ReadToEndAsync().ConfigureAwait(false);
|
|
|
|
|
|
2017-04-18 05:53:39 +00:00
|
|
|
|
bytes = Encoding.UTF8.GetBytes(text);
|
2014-06-11 19:31:33 +00:00
|
|
|
|
|
2016-10-06 18:55:01 +00:00
|
|
|
|
return _memoryStreamProvider.CreateNew(bytes);
|
2014-06-11 19:31:33 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-13 21:32:02 +00:00
|
|
|
|
return _fileSystem.OpenRead(path);
|
2014-06-11 19:31:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-31 16:24:16 +00:00
|
|
|
|
private async Task<Tuple<string, MediaProtocol, string, bool>> GetReadableFile(string mediaPath,
|
2014-06-11 14:42:03 +00:00
|
|
|
|
string[] inputFiles,
|
2014-06-17 01:56:23 +00:00
|
|
|
|
MediaProtocol protocol,
|
2014-06-11 14:42:03 +00:00
|
|
|
|
MediaStream subtitleStream,
|
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
if (!subtitleStream.IsExternal)
|
|
|
|
|
{
|
2014-08-30 15:12:28 +00:00
|
|
|
|
string outputFormat;
|
|
|
|
|
string outputCodec;
|
|
|
|
|
|
2016-04-02 23:39:08 +00:00
|
|
|
|
if (string.Equals(subtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) ||
|
|
|
|
|
string.Equals(subtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase) ||
|
|
|
|
|
string.Equals(subtitleStream.Codec, "srt", StringComparison.OrdinalIgnoreCase))
|
2014-08-30 15:12:28 +00:00
|
|
|
|
{
|
|
|
|
|
// Extract
|
|
|
|
|
outputCodec = "copy";
|
2016-04-02 23:39:08 +00:00
|
|
|
|
outputFormat = subtitleStream.Codec;
|
2014-08-30 15:12:28 +00:00
|
|
|
|
}
|
2016-04-02 23:39:08 +00:00
|
|
|
|
else if (string.Equals(subtitleStream.Codec, "subrip", StringComparison.OrdinalIgnoreCase))
|
2014-08-30 15:12:28 +00:00
|
|
|
|
{
|
|
|
|
|
// Extract
|
|
|
|
|
outputCodec = "copy";
|
|
|
|
|
outputFormat = "srt";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Extract
|
|
|
|
|
outputCodec = "srt";
|
|
|
|
|
outputFormat = "srt";
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-11 14:42:03 +00:00
|
|
|
|
// Extract
|
2015-03-31 16:24:16 +00:00
|
|
|
|
var outputPath = GetSubtitleCachePath(mediaPath, protocol, subtitleStream.Index, "." + outputFormat);
|
2014-06-11 14:42:03 +00:00
|
|
|
|
|
2014-08-30 15:12:28 +00:00
|
|
|
|
await ExtractTextSubtitle(inputFiles, protocol, subtitleStream.Index, outputCodec, outputPath, cancellationToken)
|
2014-06-11 14:42:03 +00:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
2015-03-31 16:24:16 +00:00
|
|
|
|
return new Tuple<string, MediaProtocol, string, bool>(outputPath, MediaProtocol.File, outputFormat, false);
|
2014-06-11 14:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var currentFormat = (Path.GetExtension(subtitleStream.Path) ?? subtitleStream.Codec)
|
|
|
|
|
.TrimStart('.');
|
|
|
|
|
|
|
|
|
|
if (GetReader(currentFormat, false) == null)
|
|
|
|
|
{
|
|
|
|
|
// Convert
|
2015-03-31 16:24:16 +00:00
|
|
|
|
var outputPath = GetSubtitleCachePath(mediaPath, protocol, subtitleStream.Index, ".srt");
|
2014-06-11 14:42:03 +00:00
|
|
|
|
|
2016-04-03 01:19:27 +00:00
|
|
|
|
await ConvertTextSubtitleToSrt(subtitleStream.Path, subtitleStream.Language, protocol, outputPath, cancellationToken).ConfigureAwait(false);
|
2014-06-11 14:42:03 +00:00
|
|
|
|
|
2015-03-31 16:24:16 +00:00
|
|
|
|
return new Tuple<string, MediaProtocol, string, bool>(outputPath, MediaProtocol.File, "srt", true);
|
2014-06-11 14:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-31 16:24:16 +00:00
|
|
|
|
return new Tuple<string, MediaProtocol, string, bool>(subtitleStream.Path, protocol, currentFormat, true);
|
2014-06-11 14:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ISubtitleParser GetReader(string format, bool throwIfMissing)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(format))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("format");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.Equals(format, SubtitleFormat.SRT, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
2015-01-21 19:21:19 +00:00
|
|
|
|
return new SrtParser(_logger);
|
2014-06-11 14:42:03 +00:00
|
|
|
|
}
|
2014-08-22 02:24:38 +00:00
|
|
|
|
if (string.Equals(format, SubtitleFormat.SSA, StringComparison.OrdinalIgnoreCase))
|
2014-06-11 14:42:03 +00:00
|
|
|
|
{
|
|
|
|
|
return new SsaParser();
|
|
|
|
|
}
|
2014-08-22 02:24:38 +00:00
|
|
|
|
if (string.Equals(format, SubtitleFormat.ASS, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return new AssParser();
|
|
|
|
|
}
|
2014-06-11 14:42:03 +00:00
|
|
|
|
|
|
|
|
|
if (throwIfMissing)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Unsupported format: " + format);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-27 21:07:28 +00:00
|
|
|
|
private ISubtitleWriter TryGetWriter(string format)
|
2014-06-11 14:42:03 +00:00
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(format))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("format");
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-22 15:47:44 +00:00
|
|
|
|
if (string.Equals(format, "json", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return new JsonWriter(_json);
|
|
|
|
|
}
|
2014-06-11 20:57:18 +00:00
|
|
|
|
if (string.Equals(format, SubtitleFormat.SRT, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return new SrtWriter();
|
|
|
|
|
}
|
2014-06-11 14:42:03 +00:00
|
|
|
|
if (string.Equals(format, SubtitleFormat.VTT, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return new VttWriter();
|
|
|
|
|
}
|
2014-08-05 23:59:24 +00:00
|
|
|
|
if (string.Equals(format, SubtitleFormat.TTML, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return new TtmlWriter();
|
|
|
|
|
}
|
2014-06-11 14:42:03 +00:00
|
|
|
|
|
2015-12-27 21:07:28 +00:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ISubtitleWriter GetWriter(string format)
|
|
|
|
|
{
|
|
|
|
|
var writer = TryGetWriter(format);
|
|
|
|
|
|
|
|
|
|
if (writer != null)
|
|
|
|
|
{
|
|
|
|
|
return writer;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-11 14:42:03 +00:00
|
|
|
|
throw new ArgumentException("Unsupported format: " + format);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _semaphoreLocks
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ConcurrentDictionary<string, SemaphoreSlim> _semaphoreLocks =
|
|
|
|
|
new ConcurrentDictionary<string, SemaphoreSlim>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the lock.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">The filename.</param>
|
|
|
|
|
/// <returns>System.Object.</returns>
|
|
|
|
|
private SemaphoreSlim GetLock(string filename)
|
|
|
|
|
{
|
|
|
|
|
return _semaphoreLocks.GetOrAdd(filename, key => new SemaphoreSlim(1, 1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2014-08-24 21:07:04 +00:00
|
|
|
|
/// Converts the text subtitle to SRT.
|
2014-06-11 14:42:03 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="inputPath">The input path.</param>
|
2015-03-31 16:24:16 +00:00
|
|
|
|
/// <param name="inputProtocol">The input protocol.</param>
|
2014-06-11 14:42:03 +00:00
|
|
|
|
/// <param name="outputPath">The output path.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
2016-04-03 01:19:27 +00:00
|
|
|
|
private async Task ConvertTextSubtitleToSrt(string inputPath, string language, MediaProtocol inputProtocol, string outputPath, CancellationToken cancellationToken)
|
2014-06-11 14:42:03 +00:00
|
|
|
|
{
|
|
|
|
|
var semaphore = GetLock(outputPath);
|
|
|
|
|
|
|
|
|
|
await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-04-03 01:19:27 +00:00
|
|
|
|
if (!_fileSystem.FileExists(outputPath))
|
2014-06-11 14:42:03 +00:00
|
|
|
|
{
|
2016-04-03 01:19:27 +00:00
|
|
|
|
await ConvertTextSubtitleToSrtInternal(inputPath, language, inputProtocol, outputPath, cancellationToken).ConfigureAwait(false);
|
2014-06-11 14:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
semaphore.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2014-08-24 21:07:04 +00:00
|
|
|
|
/// Converts the text subtitle to SRT internal.
|
2014-06-11 14:42:03 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="inputPath">The input path.</param>
|
2015-03-31 16:24:16 +00:00
|
|
|
|
/// <param name="inputProtocol">The input protocol.</param>
|
2014-06-11 14:42:03 +00:00
|
|
|
|
/// <param name="outputPath">The output path.</param>
|
2015-03-31 16:24:16 +00:00
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
2014-06-11 14:42:03 +00:00
|
|
|
|
/// <returns>Task.</returns>
|
2015-03-31 16:24:16 +00:00
|
|
|
|
/// <exception cref="System.ArgumentNullException">
|
|
|
|
|
/// inputPath
|
2014-06-11 14:42:03 +00:00
|
|
|
|
/// or
|
2015-03-31 16:24:16 +00:00
|
|
|
|
/// outputPath
|
|
|
|
|
/// </exception>
|
2016-04-03 01:19:27 +00:00
|
|
|
|
private async Task ConvertTextSubtitleToSrtInternal(string inputPath, string language, MediaProtocol inputProtocol, string outputPath, CancellationToken cancellationToken)
|
2014-06-11 14:42:03 +00:00
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(inputPath))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("inputPath");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(outputPath))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("outputPath");
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-04 18:14:45 +00:00
|
|
|
|
_fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(outputPath));
|
2014-06-11 14:42:03 +00:00
|
|
|
|
|
2016-04-03 01:19:27 +00:00
|
|
|
|
var encodingParam = await GetSubtitleFileCharacterSet(inputPath, language, inputProtocol, cancellationToken).ConfigureAwait(false);
|
2014-06-11 14:42:03 +00:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(encodingParam))
|
|
|
|
|
{
|
|
|
|
|
encodingParam = " -sub_charenc " + encodingParam;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-01 04:07:12 +00:00
|
|
|
|
var process = _processFactory.Create(new ProcessOptions
|
2014-06-11 14:42:03 +00:00
|
|
|
|
{
|
2016-11-01 04:07:12 +00:00
|
|
|
|
CreateNoWindow = true,
|
2016-12-10 01:58:52 +00:00
|
|
|
|
UseShellExecute = false,
|
2016-11-01 04:07:12 +00:00
|
|
|
|
FileName = _mediaEncoder.EncoderPath,
|
|
|
|
|
Arguments = string.Format("{0} -i \"{1}\" -c:s srt \"{2}\"", encodingParam, inputPath, outputPath),
|
2014-06-11 14:42:03 +00:00
|
|
|
|
|
2016-11-01 04:07:12 +00:00
|
|
|
|
IsHidden = true,
|
|
|
|
|
ErrorDialog = false
|
|
|
|
|
});
|
2014-06-11 14:42:03 +00:00
|
|
|
|
|
2015-10-04 22:04:56 +00:00
|
|
|
|
_logger.Info("{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments);
|
2014-06-11 14:42:03 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
process.Start();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error starting ffmpeg", ex);
|
|
|
|
|
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var ranToCompletion = process.WaitForExit(60000);
|
|
|
|
|
|
|
|
|
|
if (!ranToCompletion)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_logger.Info("Killing ffmpeg subtitle conversion process");
|
|
|
|
|
|
2016-11-14 06:44:21 +00:00
|
|
|
|
process.Kill();
|
2014-06-11 14:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error killing subtitle conversion process", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var exitCode = ranToCompletion ? process.ExitCode : -1;
|
|
|
|
|
|
|
|
|
|
process.Dispose();
|
|
|
|
|
|
|
|
|
|
var failed = false;
|
|
|
|
|
|
|
|
|
|
if (exitCode == -1)
|
|
|
|
|
{
|
|
|
|
|
failed = true;
|
|
|
|
|
|
2016-04-03 01:19:27 +00:00
|
|
|
|
if (_fileSystem.FileExists(outputPath))
|
2014-06-11 14:42:03 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_logger.Info("Deleting converted subtitle due to failure: ", outputPath);
|
2015-01-13 03:46:44 +00:00
|
|
|
|
_fileSystem.DeleteFile(outputPath);
|
2014-06-11 14:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
catch (IOException ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error deleting converted subtitle {0}", ex, outputPath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-04-03 01:19:27 +00:00
|
|
|
|
else if (!_fileSystem.FileExists(outputPath))
|
2014-06-11 14:42:03 +00:00
|
|
|
|
{
|
|
|
|
|
failed = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (failed)
|
|
|
|
|
{
|
2016-11-14 06:44:21 +00:00
|
|
|
|
var msg = string.Format("ffmpeg subtitle conversion failed for {0}", inputPath);
|
2014-06-11 14:42:03 +00:00
|
|
|
|
|
|
|
|
|
_logger.Error(msg);
|
|
|
|
|
|
2016-11-01 04:07:12 +00:00
|
|
|
|
throw new Exception(msg);
|
2014-06-11 14:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
await SetAssFont(outputPath).ConfigureAwait(false);
|
2016-11-14 06:44:21 +00:00
|
|
|
|
|
|
|
|
|
_logger.Info("ffmpeg subtitle conversion succeeded for {0}", inputPath);
|
2014-06-11 14:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Extracts the text subtitle.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="inputFiles">The input files.</param>
|
2014-06-17 01:56:23 +00:00
|
|
|
|
/// <param name="protocol">The protocol.</param>
|
2014-06-11 14:42:03 +00:00
|
|
|
|
/// <param name="subtitleStreamIndex">Index of the subtitle stream.</param>
|
2014-08-30 14:26:29 +00:00
|
|
|
|
/// <param name="outputCodec">The output codec.</param>
|
2014-06-11 14:42:03 +00:00
|
|
|
|
/// <param name="outputPath">The output path.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
/// <exception cref="System.ArgumentException">Must use inputPath list overload</exception>
|
2014-06-17 01:56:23 +00:00
|
|
|
|
private async Task ExtractTextSubtitle(string[] inputFiles, MediaProtocol protocol, int subtitleStreamIndex,
|
2014-08-30 14:26:29 +00:00
|
|
|
|
string outputCodec, string outputPath, CancellationToken cancellationToken)
|
2014-06-11 14:42:03 +00:00
|
|
|
|
{
|
|
|
|
|
var semaphore = GetLock(outputPath);
|
|
|
|
|
|
|
|
|
|
await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-04-03 01:19:27 +00:00
|
|
|
|
if (!_fileSystem.FileExists(outputPath))
|
2014-06-11 14:42:03 +00:00
|
|
|
|
{
|
2017-04-21 20:03:07 +00:00
|
|
|
|
await ExtractTextSubtitleInternal(_mediaEncoder.GetInputArgument(inputFiles, protocol), subtitleStreamIndex, outputCodec, outputPath, cancellationToken).ConfigureAwait(false);
|
2014-06-11 14:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
semaphore.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task ExtractTextSubtitleInternal(string inputPath, int subtitleStreamIndex,
|
2014-08-30 14:26:29 +00:00
|
|
|
|
string outputCodec, string outputPath, CancellationToken cancellationToken)
|
2014-06-11 14:42:03 +00:00
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(inputPath))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("inputPath");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(outputPath))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("outputPath");
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-04 18:14:45 +00:00
|
|
|
|
_fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(outputPath));
|
2014-06-11 19:31:33 +00:00
|
|
|
|
|
2014-08-30 14:26:29 +00:00
|
|
|
|
var processArgs = string.Format("-i {0} -map 0:{1} -an -vn -c:s {2} \"{3}\"", inputPath,
|
|
|
|
|
subtitleStreamIndex, outputCodec, outputPath);
|
2014-06-11 14:42:03 +00:00
|
|
|
|
|
2016-11-01 04:07:12 +00:00
|
|
|
|
var process = _processFactory.Create(new ProcessOptions
|
2014-06-11 14:42:03 +00:00
|
|
|
|
{
|
2016-11-01 04:07:12 +00:00
|
|
|
|
CreateNoWindow = true,
|
2016-12-10 01:58:52 +00:00
|
|
|
|
UseShellExecute = false,
|
2014-06-11 14:42:03 +00:00
|
|
|
|
|
2016-11-01 04:07:12 +00:00
|
|
|
|
FileName = _mediaEncoder.EncoderPath,
|
|
|
|
|
Arguments = processArgs,
|
|
|
|
|
IsHidden = true,
|
|
|
|
|
ErrorDialog = false
|
|
|
|
|
});
|
2014-06-11 14:42:03 +00:00
|
|
|
|
|
2014-12-17 05:30:31 +00:00
|
|
|
|
_logger.Info("{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments);
|
2014-06-11 14:42:03 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
process.Start();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error starting ffmpeg", ex);
|
|
|
|
|
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-22 04:15:12 +00:00
|
|
|
|
var ranToCompletion = process.WaitForExit(300000);
|
2014-06-11 14:42:03 +00:00
|
|
|
|
|
|
|
|
|
if (!ranToCompletion)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_logger.Info("Killing ffmpeg subtitle extraction process");
|
|
|
|
|
|
2016-11-14 06:44:21 +00:00
|
|
|
|
process.Kill();
|
2014-06-11 14:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error killing subtitle extraction process", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var exitCode = ranToCompletion ? process.ExitCode : -1;
|
|
|
|
|
|
|
|
|
|
process.Dispose();
|
|
|
|
|
|
|
|
|
|
var failed = false;
|
|
|
|
|
|
|
|
|
|
if (exitCode == -1)
|
|
|
|
|
{
|
|
|
|
|
failed = true;
|
|
|
|
|
|
2014-12-17 05:30:31 +00:00
|
|
|
|
try
|
2014-06-11 14:42:03 +00:00
|
|
|
|
{
|
2014-12-17 05:30:31 +00:00
|
|
|
|
_logger.Info("Deleting extracted subtitle due to failure: {0}", outputPath);
|
2015-01-13 03:46:44 +00:00
|
|
|
|
_fileSystem.DeleteFile(outputPath);
|
2014-12-17 05:30:31 +00:00
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
2015-01-10 19:42:14 +00:00
|
|
|
|
|
2014-12-17 05:30:31 +00:00
|
|
|
|
}
|
|
|
|
|
catch (IOException ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error deleting extracted subtitle {0}", ex, outputPath);
|
2014-06-11 14:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-04-03 01:19:27 +00:00
|
|
|
|
else if (!_fileSystem.FileExists(outputPath))
|
2014-06-11 14:42:03 +00:00
|
|
|
|
{
|
|
|
|
|
failed = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (failed)
|
|
|
|
|
{
|
|
|
|
|
var msg = string.Format("ffmpeg subtitle extraction failed for {0} to {1}", inputPath, outputPath);
|
|
|
|
|
|
|
|
|
|
_logger.Error(msg);
|
|
|
|
|
|
2016-11-01 04:07:12 +00:00
|
|
|
|
throw new Exception(msg);
|
2014-06-11 14:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var msg = string.Format("ffmpeg subtitle extraction completed for {0} to {1}", inputPath, outputPath);
|
|
|
|
|
|
|
|
|
|
_logger.Info(msg);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-06 04:21:23 +00:00
|
|
|
|
if (string.Equals(outputCodec, "ass", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
await SetAssFont(outputPath).ConfigureAwait(false);
|
|
|
|
|
}
|
2014-06-11 14:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the ass font.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="file">The file.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
private async Task SetAssFont(string file)
|
|
|
|
|
{
|
|
|
|
|
_logger.Info("Setting ass font within {0}", file);
|
|
|
|
|
|
|
|
|
|
string text;
|
|
|
|
|
Encoding encoding;
|
|
|
|
|
|
2016-11-01 04:07:12 +00:00
|
|
|
|
using (var fileStream = _fileSystem.OpenRead(file))
|
2014-06-11 14:42:03 +00:00
|
|
|
|
{
|
2016-11-01 04:07:12 +00:00
|
|
|
|
using (var reader = new StreamReader(fileStream, true))
|
|
|
|
|
{
|
|
|
|
|
encoding = reader.CurrentEncoding;
|
2014-06-11 14:42:03 +00:00
|
|
|
|
|
2016-11-01 04:07:12 +00:00
|
|
|
|
text = await reader.ReadToEndAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
2014-06-11 14:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var newText = text.Replace(",Arial,", ",Arial Unicode MS,");
|
|
|
|
|
|
|
|
|
|
if (!string.Equals(text, newText))
|
|
|
|
|
{
|
2016-11-01 04:07:12 +00:00
|
|
|
|
using (var fileStream = _fileSystem.GetFileStream(file, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read))
|
2014-06-11 14:42:03 +00:00
|
|
|
|
{
|
2016-11-01 04:07:12 +00:00
|
|
|
|
using (var writer = new StreamWriter(fileStream, encoding))
|
|
|
|
|
{
|
|
|
|
|
writer.Write(newText);
|
|
|
|
|
}
|
2014-06-11 14:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-31 16:24:16 +00:00
|
|
|
|
private string GetSubtitleCachePath(string mediaPath, MediaProtocol protocol, int subtitleStreamIndex, string outputSubtitleExtension)
|
2014-06-11 14:42:03 +00:00
|
|
|
|
{
|
2015-03-31 16:24:16 +00:00
|
|
|
|
if (protocol == MediaProtocol.File)
|
|
|
|
|
{
|
|
|
|
|
var ticksParam = string.Empty;
|
|
|
|
|
|
|
|
|
|
var date = _fileSystem.GetLastWriteTimeUtc(mediaPath);
|
2014-06-11 14:42:03 +00:00
|
|
|
|
|
2015-03-31 16:24:16 +00:00
|
|
|
|
var filename = (mediaPath + "_" + subtitleStreamIndex.ToString(CultureInfo.InvariantCulture) + "_" + date.Ticks.ToString(CultureInfo.InvariantCulture) + ticksParam).GetMD5() + outputSubtitleExtension;
|
2014-06-11 14:42:03 +00:00
|
|
|
|
|
2015-03-31 16:24:16 +00:00
|
|
|
|
var prefix = filename.Substring(0, 1);
|
|
|
|
|
|
|
|
|
|
return Path.Combine(SubtitleCachePath, prefix, filename);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var filename = (mediaPath + "_" + subtitleStreamIndex.ToString(CultureInfo.InvariantCulture)).GetMD5() + outputSubtitleExtension;
|
2014-06-11 14:42:03 +00:00
|
|
|
|
|
2015-03-31 16:24:16 +00:00
|
|
|
|
var prefix = filename.Substring(0, 1);
|
2014-06-11 14:42:03 +00:00
|
|
|
|
|
2015-03-31 16:24:16 +00:00
|
|
|
|
return Path.Combine(SubtitleCachePath, prefix, filename);
|
|
|
|
|
}
|
2014-06-11 14:42:03 +00:00
|
|
|
|
}
|
2014-06-11 19:31:33 +00:00
|
|
|
|
|
2016-04-03 01:19:27 +00:00
|
|
|
|
public async Task<string> GetSubtitleFileCharacterSet(string path, string language, MediaProtocol protocol, CancellationToken cancellationToken)
|
2014-06-11 19:31:33 +00:00
|
|
|
|
{
|
2017-04-18 05:53:39 +00:00
|
|
|
|
var bytes = await GetBytes(path, protocol, cancellationToken).ConfigureAwait(false);
|
2015-01-10 19:42:14 +00:00
|
|
|
|
|
2017-04-18 05:53:39 +00:00
|
|
|
|
var charset = _textEncoding.GetDetectedEncodingName(bytes, language);
|
2017-03-15 19:57:18 +00:00
|
|
|
|
|
2017-04-18 05:53:39 +00:00
|
|
|
|
_logger.Debug("charset {0} detected for {1}", charset ?? "null", path);
|
2014-06-11 19:31:33 +00:00
|
|
|
|
|
2017-04-18 05:53:39 +00:00
|
|
|
|
return charset;
|
2014-06-11 19:31:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-18 05:53:39 +00:00
|
|
|
|
private async Task<byte[]> GetBytes(string path, MediaProtocol protocol, CancellationToken cancellationToken)
|
2015-01-10 19:42:14 +00:00
|
|
|
|
{
|
2017-04-18 05:53:39 +00:00
|
|
|
|
if (protocol == MediaProtocol.Http)
|
2015-01-10 19:42:14 +00:00
|
|
|
|
{
|
2017-04-18 05:53:39 +00:00
|
|
|
|
using (var file = await _httpClient.Get(path, cancellationToken).ConfigureAwait(false))
|
2015-01-10 19:42:14 +00:00
|
|
|
|
{
|
2017-04-18 05:53:39 +00:00
|
|
|
|
using (var memoryStream = new MemoryStream())
|
2015-01-10 19:42:14 +00:00
|
|
|
|
{
|
2017-04-18 05:53:39 +00:00
|
|
|
|
await file.CopyToAsync(memoryStream).ConfigureAwait(false);
|
|
|
|
|
memoryStream.Position = 0;
|
2015-01-10 19:42:14 +00:00
|
|
|
|
|
2017-04-18 05:53:39 +00:00
|
|
|
|
return memoryStream.ToArray();
|
2016-04-03 01:19:27 +00:00
|
|
|
|
}
|
2015-01-10 19:42:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-03-31 16:24:16 +00:00
|
|
|
|
if (protocol == MediaProtocol.File)
|
|
|
|
|
{
|
2017-04-18 05:53:39 +00:00
|
|
|
|
return _fileSystem.ReadAllBytes(path);
|
2015-03-31 16:24:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new ArgumentOutOfRangeException("protocol");
|
|
|
|
|
}
|
2014-06-11 14:42:03 +00:00
|
|
|
|
}
|
2017-03-15 19:57:18 +00:00
|
|
|
|
}
|