| | | 1 | | #pragma warning disable CS1591 |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.Globalization; |
| | | 8 | | using System.IO; |
| | | 9 | | using System.Linq; |
| | | 10 | | using System.Net.Http; |
| | | 11 | | using System.Text; |
| | | 12 | | using System.Threading; |
| | | 13 | | using System.Threading.Tasks; |
| | | 14 | | using AsyncKeyedLock; |
| | | 15 | | using MediaBrowser.Common; |
| | | 16 | | using MediaBrowser.Common.Configuration; |
| | | 17 | | using MediaBrowser.Common.Extensions; |
| | | 18 | | using MediaBrowser.Common.Net; |
| | | 19 | | using MediaBrowser.Controller.Configuration; |
| | | 20 | | using MediaBrowser.Controller.Entities; |
| | | 21 | | using MediaBrowser.Controller.IO; |
| | | 22 | | using MediaBrowser.Controller.Library; |
| | | 23 | | using MediaBrowser.Controller.MediaEncoding; |
| | | 24 | | using MediaBrowser.Model.Dto; |
| | | 25 | | using MediaBrowser.Model.Entities; |
| | | 26 | | using MediaBrowser.Model.IO; |
| | | 27 | | using MediaBrowser.Model.MediaInfo; |
| | | 28 | | using Microsoft.Extensions.Logging; |
| | | 29 | | using Nikse.SubtitleEdit.Core.Common; |
| | | 30 | | using Nikse.SubtitleEdit.Core.SubtitleFormats; |
| | | 31 | | using UtfUnknown; |
| | | 32 | | using SubtitleFormat = MediaBrowser.Model.MediaInfo.SubtitleFormat; |
| | | 33 | | |
| | | 34 | | namespace MediaBrowser.MediaEncoding.Subtitles |
| | | 35 | | { |
| | | 36 | | public sealed class SubtitleEncoder : ISubtitleEncoder, IDisposable |
| | | 37 | | { |
| | | 38 | | private readonly ILogger<SubtitleEncoder> _logger; |
| | | 39 | | private readonly IFileSystem _fileSystem; |
| | | 40 | | private readonly IMediaEncoder _mediaEncoder; |
| | | 41 | | private readonly IHttpClientFactory _httpClientFactory; |
| | | 42 | | private readonly IMediaSourceManager _mediaSourceManager; |
| | | 43 | | private readonly ISubtitleParser _subtitleParser; |
| | | 44 | | private readonly IPathManager _pathManager; |
| | | 45 | | private readonly IServerConfigurationManager _serverConfigurationManager; |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// The _semaphoreLocks. |
| | | 49 | | /// </summary> |
| | 25 | 50 | | private readonly AsyncKeyedLocker<string> _semaphoreLocks = new(o => |
| | 25 | 51 | | { |
| | 25 | 52 | | o.PoolSize = 20; |
| | 25 | 53 | | o.PoolInitialFill = 1; |
| | 25 | 54 | | }); |
| | | 55 | | |
| | | 56 | | public SubtitleEncoder( |
| | | 57 | | ILogger<SubtitleEncoder> logger, |
| | | 58 | | IFileSystem fileSystem, |
| | | 59 | | IMediaEncoder mediaEncoder, |
| | | 60 | | IHttpClientFactory httpClientFactory, |
| | | 61 | | IMediaSourceManager mediaSourceManager, |
| | | 62 | | ISubtitleParser subtitleParser, |
| | | 63 | | IPathManager pathManager, |
| | | 64 | | IServerConfigurationManager serverConfigurationManager) |
| | | 65 | | { |
| | 25 | 66 | | _logger = logger; |
| | 25 | 67 | | _fileSystem = fileSystem; |
| | 25 | 68 | | _mediaEncoder = mediaEncoder; |
| | 25 | 69 | | _httpClientFactory = httpClientFactory; |
| | 25 | 70 | | _mediaSourceManager = mediaSourceManager; |
| | 25 | 71 | | _subtitleParser = subtitleParser; |
| | 25 | 72 | | _pathManager = pathManager; |
| | 25 | 73 | | _serverConfigurationManager = serverConfigurationManager; |
| | 25 | 74 | | } |
| | | 75 | | |
| | | 76 | | private MemoryStream ConvertSubtitles( |
| | | 77 | | Stream stream, |
| | | 78 | | SubtitleInfo inputInfo, |
| | | 79 | | string outputFormat, |
| | | 80 | | long startTimeTicks, |
| | | 81 | | long endTimeTicks, |
| | | 82 | | bool preserveOriginalTimestamps) |
| | | 83 | | { |
| | 0 | 84 | | var subtitle = Subtitle.Parse(stream, Path.GetExtension(inputInfo.Path)); |
| | | 85 | | |
| | 0 | 86 | | FilterEvents(subtitle, startTimeTicks, endTimeTicks, preserveOriginalTimestamps); |
| | | 87 | | |
| | 0 | 88 | | var formatter = GetWriter(outputFormat); |
| | | 89 | | |
| | 0 | 90 | | var text = formatter.ToText(subtitle, "untitled"); |
| | 0 | 91 | | var bytes = Encoding.UTF8.GetBytes(text); |
| | | 92 | | |
| | 0 | 93 | | return new MemoryStream(bytes, 0, bytes.Length, false, true); |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | internal void FilterEvents(Subtitle track, long startPositionTicks, long endTimeTicks, bool preserveTimestamps) |
| | | 97 | | { |
| | | 98 | | // Drop subs that have fully elapsed before the requested start position |
| | 0 | 99 | | track.Paragraphs |
| | 0 | 100 | | .RemoveAll(i => (i.StartTime.TimeSpan.Ticks - startPositionTicks) < 0 && (i.EndTime.TimeSpan.Ticks - sta |
| | | 101 | | |
| | 0 | 102 | | if (endTimeTicks > 0) |
| | | 103 | | { |
| | 0 | 104 | | track.Paragraphs |
| | 0 | 105 | | .RemoveAll(i => i.StartTime.TimeSpan.Ticks > endTimeTicks); |
| | | 106 | | } |
| | | 107 | | |
| | 0 | 108 | | if (!preserveTimestamps) |
| | | 109 | | { |
| | 0 | 110 | | foreach (var trackEvent in track.Paragraphs) |
| | | 111 | | { |
| | 0 | 112 | | trackEvent.StartTime = new TimeCode(TimeSpan.FromTicks(Math.Max(0, trackEvent.StartTime.TimeSpan.Tic |
| | 0 | 113 | | trackEvent.EndTime = new TimeCode(TimeSpan.FromTicks(Math.Max(0, trackEvent.EndTime.TimeSpan.Ticks - |
| | | 114 | | } |
| | | 115 | | } |
| | 0 | 116 | | } |
| | | 117 | | |
| | | 118 | | async Task<Stream> ISubtitleEncoder.GetSubtitles(BaseItem item, string mediaSourceId, int subtitleStreamIndex, s |
| | | 119 | | { |
| | 0 | 120 | | ArgumentNullException.ThrowIfNull(item); |
| | | 121 | | |
| | 0 | 122 | | if (string.IsNullOrWhiteSpace(mediaSourceId)) |
| | | 123 | | { |
| | 0 | 124 | | throw new ArgumentNullException(nameof(mediaSourceId)); |
| | | 125 | | } |
| | | 126 | | |
| | 0 | 127 | | var mediaSources = await _mediaSourceManager.GetPlaybackMediaSources(item, null, true, false, cancellationTo |
| | | 128 | | |
| | 0 | 129 | | var mediaSource = mediaSources |
| | 0 | 130 | | .First(i => string.Equals(i.Id, mediaSourceId, StringComparison.OrdinalIgnoreCase)); |
| | | 131 | | |
| | 0 | 132 | | var subtitleStream = mediaSource.MediaStreams |
| | 0 | 133 | | .First(i => i.Type == MediaStreamType.Subtitle && i.Index == subtitleStreamIndex); |
| | | 134 | | |
| | 0 | 135 | | var (stream, info) = await GetSubtitleStream(mediaSource, subtitleStream, cancellationToken) |
| | 0 | 136 | | .ConfigureAwait(false); |
| | | 137 | | |
| | | 138 | | // Return the original if the same format is being requested |
| | | 139 | | // Character encoding was already handled in GetSubtitleStream |
| | | 140 | | // ASS is a superset of SSA, skipping the conversion and preserving the styles |
| | 0 | 141 | | if (string.Equals(info.Format, outputFormat, StringComparison.OrdinalIgnoreCase) |
| | 0 | 142 | | || (string.Equals(info.Format, SubtitleFormat.SSA, StringComparison.OrdinalIgnoreCase) |
| | 0 | 143 | | && string.Equals(outputFormat, SubtitleFormat.ASS, StringComparison.OrdinalIgnoreCase))) |
| | | 144 | | { |
| | 0 | 145 | | return stream; |
| | | 146 | | } |
| | | 147 | | |
| | 0 | 148 | | using (stream) |
| | | 149 | | { |
| | 0 | 150 | | return ConvertSubtitles(stream, info, outputFormat, startTimeTicks, endTimeTicks, preserveOriginalTimest |
| | | 151 | | } |
| | 0 | 152 | | } |
| | | 153 | | |
| | | 154 | | private async Task<(Stream Stream, SubtitleInfo Info)> GetSubtitleStream( |
| | | 155 | | MediaSourceInfo mediaSource, |
| | | 156 | | MediaStream subtitleStream, |
| | | 157 | | CancellationToken cancellationToken) |
| | | 158 | | { |
| | 0 | 159 | | var fileInfo = await GetReadableFile(mediaSource, subtitleStream, cancellationToken).ConfigureAwait(false); |
| | | 160 | | |
| | 0 | 161 | | var stream = await GetSubtitleStream(fileInfo, cancellationToken).ConfigureAwait(false); |
| | | 162 | | |
| | 0 | 163 | | return (stream, fileInfo); |
| | 0 | 164 | | } |
| | | 165 | | |
| | | 166 | | private async Task<Stream> GetSubtitleStream(SubtitleInfo fileInfo, CancellationToken cancellationToken) |
| | | 167 | | { |
| | 0 | 168 | | if (fileInfo.Protocol == MediaProtocol.Http) |
| | | 169 | | { |
| | 0 | 170 | | var result = await DetectCharset(fileInfo.Path, fileInfo.Protocol, cancellationToken).ConfigureAwait(fal |
| | 0 | 171 | | var detected = result.Detected; |
| | | 172 | | |
| | 0 | 173 | | if (detected is not null) |
| | | 174 | | { |
| | 0 | 175 | | _logger.LogDebug("charset {CharSet} detected for {Path}", detected.EncodingName, fileInfo.Path); |
| | | 176 | | |
| | 0 | 177 | | using var stream = await _httpClientFactory.CreateClient(NamedClient.Default) |
| | 0 | 178 | | .GetStreamAsync(new Uri(fileInfo.Path), cancellationToken) |
| | 0 | 179 | | .ConfigureAwait(false); |
| | | 180 | | |
| | 0 | 181 | | await using (stream.ConfigureAwait(false)) |
| | | 182 | | { |
| | 0 | 183 | | using var reader = new StreamReader(stream, detected.Encoding); |
| | 0 | 184 | | var text = await reader.ReadToEndAsync(cancellationToken).ConfigureAwait(false); |
| | | 185 | | |
| | 0 | 186 | | return new MemoryStream(Encoding.UTF8.GetBytes(text)); |
| | | 187 | | } |
| | 0 | 188 | | } |
| | 0 | 189 | | } |
| | | 190 | | |
| | 0 | 191 | | return AsyncFile.OpenRead(fileInfo.Path); |
| | 0 | 192 | | } |
| | | 193 | | |
| | | 194 | | internal async Task<SubtitleInfo> GetReadableFile( |
| | | 195 | | MediaSourceInfo mediaSource, |
| | | 196 | | MediaStream subtitleStream, |
| | | 197 | | CancellationToken cancellationToken) |
| | | 198 | | { |
| | 4 | 199 | | if (!subtitleStream.IsExternal || subtitleStream.Path.EndsWith(".mks", StringComparison.OrdinalIgnoreCase)) |
| | | 200 | | { |
| | 0 | 201 | | await ExtractAllExtractableSubtitles(mediaSource, cancellationToken).ConfigureAwait(false); |
| | | 202 | | |
| | 0 | 203 | | var outputFileExtension = GetExtractableSubtitleFileExtension(subtitleStream); |
| | 0 | 204 | | var outputFormat = GetExtractableSubtitleFormat(subtitleStream); |
| | 0 | 205 | | var outputPath = GetSubtitleCachePath(mediaSource, subtitleStream.Index, "." + outputFileExtension) |
| | 0 | 206 | | ?? throw new ResourceNotFoundException($"MediaSource {mediaSource.Id} has no subtitle cache (non-GUI |
| | | 207 | | |
| | 0 | 208 | | return new SubtitleInfo() |
| | 0 | 209 | | { |
| | 0 | 210 | | Path = outputPath, |
| | 0 | 211 | | Protocol = MediaProtocol.File, |
| | 0 | 212 | | Format = outputFormat, |
| | 0 | 213 | | IsExternal = MediaStream.IsVobSubFormat(outputFormat) |
| | 0 | 214 | | }; |
| | | 215 | | } |
| | | 216 | | |
| | | 217 | | // Normalize ffmpeg codec names to the file extensions the parser is keyed on |
| | 4 | 218 | | var currentFormat = NormalizeCodecToParserExtension((Path.GetExtension(subtitleStream.Path) ?? subtitleStrea |
| | | 219 | | |
| | | 220 | | // Handle PGS subtitles as raw streams for the client to render |
| | 4 | 221 | | if (MediaStream.IsPgsFormat(currentFormat)) |
| | | 222 | | { |
| | 0 | 223 | | return new SubtitleInfo() |
| | 0 | 224 | | { |
| | 0 | 225 | | Path = subtitleStream.Path, |
| | 0 | 226 | | Protocol = _mediaSourceManager.GetPathProtocol(subtitleStream.Path), |
| | 0 | 227 | | Format = "pgssub", |
| | 0 | 228 | | IsExternal = true |
| | 0 | 229 | | }; |
| | | 230 | | } |
| | | 231 | | |
| | | 232 | | // Fallback to ffmpeg conversion |
| | 4 | 233 | | if (!_subtitleParser.SupportsFileExtension(currentFormat)) |
| | | 234 | | { |
| | | 235 | | // Convert |
| | 0 | 236 | | var outputPath = GetSubtitleCachePath(mediaSource, subtitleStream.Index, ".srt") |
| | 0 | 237 | | ?? throw new ResourceNotFoundException($"MediaSource {mediaSource.Id} has no subtitle cache (non-GUI |
| | | 238 | | |
| | 0 | 239 | | await ConvertTextSubtitleToSrt(subtitleStream, mediaSource, outputPath, cancellationToken).ConfigureAwai |
| | | 240 | | |
| | 0 | 241 | | return new SubtitleInfo() |
| | 0 | 242 | | { |
| | 0 | 243 | | Path = outputPath, |
| | 0 | 244 | | Protocol = MediaProtocol.File, |
| | 0 | 245 | | Format = "srt", |
| | 0 | 246 | | IsExternal = true |
| | 0 | 247 | | }; |
| | | 248 | | } |
| | | 249 | | |
| | | 250 | | // It's possible that the subtitleStream and mediaSource don't share the same protocol (e.g. .STRM file with |
| | 4 | 251 | | return new SubtitleInfo() |
| | 4 | 252 | | { |
| | 4 | 253 | | Path = subtitleStream.Path, |
| | 4 | 254 | | Protocol = _mediaSourceManager.GetPathProtocol(subtitleStream.Path), |
| | 4 | 255 | | Format = currentFormat, |
| | 4 | 256 | | IsExternal = true |
| | 4 | 257 | | }; |
| | 4 | 258 | | } |
| | | 259 | | |
| | | 260 | | private bool TryGetWriter(string format, [NotNullWhen(true)] out Nikse.SubtitleEdit.Core.SubtitleFormats.Subtitl |
| | | 261 | | { |
| | 0 | 262 | | ArgumentException.ThrowIfNullOrEmpty(format); |
| | | 263 | | |
| | 0 | 264 | | if (string.Equals(format, SubtitleFormat.ASS, StringComparison.OrdinalIgnoreCase)) |
| | | 265 | | { |
| | 0 | 266 | | value = new AdvancedSubStationAlpha(); |
| | 0 | 267 | | return true; |
| | | 268 | | } |
| | | 269 | | |
| | 0 | 270 | | if (string.Equals(format, "json", StringComparison.OrdinalIgnoreCase)) |
| | | 271 | | { |
| | 0 | 272 | | value = new JsonWriter(); |
| | 0 | 273 | | return true; |
| | | 274 | | } |
| | | 275 | | |
| | 0 | 276 | | if (string.Equals(format, SubtitleFormat.SRT, StringComparison.OrdinalIgnoreCase) |
| | 0 | 277 | | || string.Equals(format, SubtitleFormat.SUBRIP, StringComparison.OrdinalIgnoreCase)) |
| | | 278 | | { |
| | 0 | 279 | | value = new SubRip(); |
| | 0 | 280 | | return true; |
| | | 281 | | } |
| | | 282 | | |
| | 0 | 283 | | if (string.Equals(format, SubtitleFormat.SSA, StringComparison.OrdinalIgnoreCase)) |
| | | 284 | | { |
| | 0 | 285 | | value = new SubStationAlpha(); |
| | 0 | 286 | | return true; |
| | | 287 | | } |
| | | 288 | | |
| | 0 | 289 | | if (string.Equals(format, SubtitleFormat.VTT, StringComparison.OrdinalIgnoreCase) |
| | 0 | 290 | | || string.Equals(format, SubtitleFormat.WEBVTT, StringComparison.OrdinalIgnoreCase)) |
| | | 291 | | { |
| | 0 | 292 | | value = new WebVTT(); |
| | 0 | 293 | | return true; |
| | | 294 | | } |
| | | 295 | | |
| | 0 | 296 | | if (string.Equals(format, SubtitleFormat.TTML, StringComparison.OrdinalIgnoreCase)) |
| | | 297 | | { |
| | 0 | 298 | | value = new TimedText10(); |
| | 0 | 299 | | return true; |
| | | 300 | | } |
| | | 301 | | |
| | 0 | 302 | | value = null; |
| | 0 | 303 | | return false; |
| | | 304 | | } |
| | | 305 | | |
| | | 306 | | private Nikse.SubtitleEdit.Core.SubtitleFormats.SubtitleFormat GetWriter(string format) |
| | | 307 | | { |
| | 0 | 308 | | if (TryGetWriter(format, out var writer)) |
| | | 309 | | { |
| | 0 | 310 | | return writer; |
| | | 311 | | } |
| | | 312 | | |
| | 0 | 313 | | throw new ArgumentException("Unsupported format: " + format); |
| | | 314 | | } |
| | | 315 | | |
| | | 316 | | /// <summary> |
| | | 317 | | /// Converts the text subtitle to SRT. |
| | | 318 | | /// </summary> |
| | | 319 | | /// <param name="subtitleStream">The subtitle stream.</param> |
| | | 320 | | /// <param name="mediaSource">The input mediaSource.</param> |
| | | 321 | | /// <param name="outputPath">The output path.</param> |
| | | 322 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 323 | | /// <returns>Task.</returns> |
| | | 324 | | private async Task ConvertTextSubtitleToSrt(MediaStream subtitleStream, MediaSourceInfo mediaSource, string outp |
| | | 325 | | { |
| | 0 | 326 | | using (await _semaphoreLocks.LockAsync(outputPath, cancellationToken).ConfigureAwait(false)) |
| | | 327 | | { |
| | 0 | 328 | | if (!IsCachedSubtitleFresh(outputPath, subtitleStream.Path)) |
| | | 329 | | { |
| | 0 | 330 | | await ConvertTextSubtitleToSrtInternal(subtitleStream, mediaSource, outputPath, cancellationToken).C |
| | | 331 | | } |
| | 0 | 332 | | } |
| | 0 | 333 | | } |
| | | 334 | | |
| | | 335 | | // ffmpeg codec names don't always match the file extensions the subtitle parser is keyed on. |
| | | 336 | | private static string NormalizeCodecToParserExtension(string codecOrExtension) |
| | | 337 | | { |
| | 4 | 338 | | return codecOrExtension switch |
| | 4 | 339 | | { |
| | 0 | 340 | | "subrip" => "srt", |
| | 0 | 341 | | "webvtt" => "vtt", |
| | 4 | 342 | | _ => codecOrExtension |
| | 4 | 343 | | }; |
| | | 344 | | } |
| | | 345 | | |
| | | 346 | | // Records "this cache was built from this exact source revision" in a sidecar file next to the cache: "<sizeByt |
| | 0 | 347 | | private static string GetCacheMetaPath(string cachePath) => cachePath + ".meta"; |
| | | 348 | | |
| | | 349 | | private static string FormatCacheMeta(long length, DateTime lastWriteUtc) |
| | 0 | 350 | | => string.Create(CultureInfo.InvariantCulture, $"{length}:{lastWriteUtc.Ticks}"); |
| | | 351 | | |
| | | 352 | | private bool IsCachedSubtitleFresh(string cachePath, string? sourcePath) |
| | | 353 | | { |
| | 0 | 354 | | if (!File.Exists(cachePath)) |
| | | 355 | | { |
| | 0 | 356 | | return false; |
| | | 357 | | } |
| | | 358 | | |
| | 0 | 359 | | var cacheInfo = _fileSystem.GetFileInfo(cachePath); |
| | 0 | 360 | | if (cacheInfo.Length == 0) |
| | | 361 | | { |
| | 0 | 362 | | return false; |
| | | 363 | | } |
| | | 364 | | |
| | 0 | 365 | | if (string.IsNullOrEmpty(sourcePath) || !File.Exists(sourcePath)) |
| | | 366 | | { |
| | 0 | 367 | | return true; |
| | | 368 | | } |
| | | 369 | | |
| | 0 | 370 | | var metaPath = GetCacheMetaPath(cachePath); |
| | 0 | 371 | | if (!File.Exists(metaPath)) |
| | | 372 | | { |
| | | 373 | | // Pre-existing cache from before metadata tracking - regenerate so we can record the source state. |
| | 0 | 374 | | return false; |
| | | 375 | | } |
| | | 376 | | |
| | | 377 | | try |
| | | 378 | | { |
| | 0 | 379 | | var sourceInfo = _fileSystem.GetFileInfo(sourcePath); |
| | 0 | 380 | | var expected = FormatCacheMeta(sourceInfo.Length, sourceInfo.LastWriteTimeUtc); |
| | 0 | 381 | | var actual = File.ReadAllText(metaPath); |
| | 0 | 382 | | return string.Equals(expected, actual, StringComparison.Ordinal); |
| | | 383 | | } |
| | 0 | 384 | | catch (IOException) |
| | | 385 | | { |
| | 0 | 386 | | return false; |
| | | 387 | | } |
| | 0 | 388 | | } |
| | | 389 | | |
| | | 390 | | private void WriteCacheMeta(string cachePath, string? sourcePath) |
| | | 391 | | { |
| | 0 | 392 | | if (string.IsNullOrEmpty(sourcePath)) |
| | | 393 | | { |
| | 0 | 394 | | return; |
| | | 395 | | } |
| | | 396 | | |
| | | 397 | | try |
| | | 398 | | { |
| | 0 | 399 | | var sourceInfo = _fileSystem.GetFileInfo(sourcePath); |
| | 0 | 400 | | if (!sourceInfo.Exists) |
| | | 401 | | { |
| | 0 | 402 | | return; |
| | | 403 | | } |
| | | 404 | | |
| | 0 | 405 | | File.WriteAllText(GetCacheMetaPath(cachePath), FormatCacheMeta(sourceInfo.Length, sourceInfo.LastWriteTi |
| | 0 | 406 | | } |
| | 0 | 407 | | catch (IOException ex) |
| | | 408 | | { |
| | 0 | 409 | | _logger.LogWarning(ex, "Failed to record subtitle cache metadata for {CachePath}", cachePath); |
| | 0 | 410 | | } |
| | 0 | 411 | | } |
| | | 412 | | |
| | | 413 | | /// <summary> |
| | | 414 | | /// Converts the text subtitle to SRT internal. |
| | | 415 | | /// </summary> |
| | | 416 | | /// <param name="subtitleStream">The subtitle stream.</param> |
| | | 417 | | /// <param name="mediaSource">The input mediaSource.</param> |
| | | 418 | | /// <param name="outputPath">The output path.</param> |
| | | 419 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 420 | | /// <returns>Task.</returns> |
| | | 421 | | /// <exception cref="ArgumentNullException"> |
| | | 422 | | /// The <c>inputPath</c> or <c>outputPath</c> is <c>null</c>. |
| | | 423 | | /// </exception> |
| | | 424 | | private async Task ConvertTextSubtitleToSrtInternal(MediaStream subtitleStream, MediaSourceInfo mediaSource, str |
| | | 425 | | { |
| | 0 | 426 | | var inputPath = subtitleStream.Path; |
| | 0 | 427 | | ArgumentException.ThrowIfNullOrEmpty(inputPath); |
| | | 428 | | |
| | 0 | 429 | | ArgumentException.ThrowIfNullOrEmpty(outputPath); |
| | | 430 | | |
| | 0 | 431 | | Directory.CreateDirectory(Path.GetDirectoryName(outputPath) ?? throw new ArgumentException($"Provided path ( |
| | | 432 | | |
| | 0 | 433 | | var encodingParam = await GetSubtitleFileCharacterSet(subtitleStream, subtitleStream.Language, mediaSource, |
| | | 434 | | |
| | | 435 | | // FFmpeg automatically convert character encoding when it is UTF-16 |
| | | 436 | | // If we specify character encoding, it rejects with "do not specify a character encoding" and "Unable to re |
| | 0 | 437 | | if ((inputPath.EndsWith(".smi", StringComparison.Ordinal) || inputPath.EndsWith(".sami", StringComparison.Or |
| | 0 | 438 | | (encodingParam.Equals("UTF-16BE", StringComparison.OrdinalIgnoreCase) || |
| | 0 | 439 | | encodingParam.Equals("UTF-16LE", StringComparison.OrdinalIgnoreCase))) |
| | | 440 | | { |
| | 0 | 441 | | encodingParam = string.Empty; |
| | | 442 | | } |
| | 0 | 443 | | else if (!string.IsNullOrEmpty(encodingParam)) |
| | | 444 | | { |
| | 0 | 445 | | encodingParam = " -sub_charenc " + encodingParam; |
| | | 446 | | } |
| | | 447 | | |
| | | 448 | | int exitCode; |
| | | 449 | | |
| | 0 | 450 | | using (var process = new Process |
| | 0 | 451 | | { |
| | 0 | 452 | | StartInfo = new ProcessStartInfo |
| | 0 | 453 | | { |
| | 0 | 454 | | CreateNoWindow = true, |
| | 0 | 455 | | UseShellExecute = false, |
| | 0 | 456 | | FileName = _mediaEncoder.EncoderPath, |
| | 0 | 457 | | Arguments = string.Format(CultureInfo.InvariantCulture, "-y {0} -i \"{1}\" -c:s srt \"{2}\"", encodi |
| | 0 | 458 | | WindowStyle = ProcessWindowStyle.Hidden, |
| | 0 | 459 | | ErrorDialog = false |
| | 0 | 460 | | }, |
| | 0 | 461 | | EnableRaisingEvents = true |
| | 0 | 462 | | }) |
| | | 463 | | { |
| | 0 | 464 | | _logger.LogInformation("{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments); |
| | | 465 | | |
| | | 466 | | try |
| | | 467 | | { |
| | 0 | 468 | | process.Start(); |
| | 0 | 469 | | } |
| | 0 | 470 | | catch (Exception ex) |
| | | 471 | | { |
| | 0 | 472 | | _logger.LogError(ex, "Error starting ffmpeg"); |
| | | 473 | | |
| | 0 | 474 | | throw; |
| | | 475 | | } |
| | | 476 | | |
| | | 477 | | try |
| | | 478 | | { |
| | 0 | 479 | | var timeoutMinutes = _serverConfigurationManager.GetEncodingOptions().SubtitleExtractionTimeoutMinut |
| | 0 | 480 | | await process.WaitForExitAsync(TimeSpan.FromMinutes(timeoutMinutes)).ConfigureAwait(false); |
| | 0 | 481 | | exitCode = process.ExitCode; |
| | 0 | 482 | | } |
| | 0 | 483 | | catch (OperationCanceledException) |
| | | 484 | | { |
| | 0 | 485 | | process.Kill(true); |
| | 0 | 486 | | exitCode = -1; |
| | 0 | 487 | | } |
| | 0 | 488 | | } |
| | | 489 | | |
| | 0 | 490 | | var failed = false; |
| | | 491 | | |
| | 0 | 492 | | if (exitCode == -1) |
| | | 493 | | { |
| | 0 | 494 | | failed = true; |
| | | 495 | | |
| | 0 | 496 | | if (File.Exists(outputPath)) |
| | | 497 | | { |
| | | 498 | | try |
| | | 499 | | { |
| | 0 | 500 | | _logger.LogInformation("Deleting converted subtitle due to failure: {Path}", outputPath); |
| | 0 | 501 | | _fileSystem.DeleteFile(outputPath); |
| | 0 | 502 | | } |
| | 0 | 503 | | catch (IOException ex) |
| | | 504 | | { |
| | 0 | 505 | | _logger.LogError(ex, "Error deleting converted subtitle {Path}", outputPath); |
| | 0 | 506 | | } |
| | | 507 | | } |
| | | 508 | | } |
| | 0 | 509 | | else if (!File.Exists(outputPath) || _fileSystem.GetFileInfo(outputPath).Length == 0) |
| | | 510 | | { |
| | 0 | 511 | | failed = true; |
| | | 512 | | |
| | | 513 | | try |
| | | 514 | | { |
| | 0 | 515 | | _logger.LogWarning("Deleting converted subtitle due to failure: {Path}", outputPath); |
| | 0 | 516 | | _fileSystem.DeleteFile(outputPath); |
| | 0 | 517 | | } |
| | 0 | 518 | | catch (FileNotFoundException) |
| | | 519 | | { |
| | 0 | 520 | | } |
| | 0 | 521 | | catch (IOException ex) |
| | | 522 | | { |
| | 0 | 523 | | _logger.LogError(ex, "Error deleting converted subtitle {Path}", outputPath); |
| | 0 | 524 | | } |
| | | 525 | | } |
| | | 526 | | |
| | 0 | 527 | | if (failed) |
| | | 528 | | { |
| | 0 | 529 | | _logger.LogError("ffmpeg subtitle conversion failed for {Path}", inputPath); |
| | | 530 | | |
| | 0 | 531 | | throw new FfmpegException( |
| | 0 | 532 | | string.Format(CultureInfo.InvariantCulture, "ffmpeg subtitle conversion failed for {0}", inputPath)) |
| | | 533 | | } |
| | | 534 | | |
| | 0 | 535 | | await SetAssFont(outputPath, cancellationToken).ConfigureAwait(false); |
| | | 536 | | |
| | 0 | 537 | | WriteCacheMeta(outputPath, inputPath); |
| | | 538 | | |
| | 0 | 539 | | _logger.LogInformation("ffmpeg subtitle conversion succeeded for {Path}", inputPath); |
| | 0 | 540 | | } |
| | | 541 | | |
| | | 542 | | private string GetExtractableSubtitleFormat(MediaStream subtitleStream) |
| | | 543 | | { |
| | 0 | 544 | | if (string.Equals(subtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) |
| | 0 | 545 | | || string.Equals(subtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase) |
| | 0 | 546 | | || string.Equals(subtitleStream.Codec, "pgssub", StringComparison.OrdinalIgnoreCase)) |
| | | 547 | | { |
| | 0 | 548 | | return subtitleStream.Codec; |
| | | 549 | | } |
| | 0 | 550 | | else if (MediaStream.IsVobSubFormat(subtitleStream.Codec)) |
| | | 551 | | { |
| | 0 | 552 | | return "mks"; |
| | | 553 | | } |
| | | 554 | | else |
| | | 555 | | { |
| | 0 | 556 | | return "srt"; |
| | | 557 | | } |
| | | 558 | | } |
| | | 559 | | |
| | | 560 | | private string GetExtractableSubtitleFileExtension(MediaStream subtitleStream) |
| | | 561 | | { |
| | | 562 | | // Using .pgssub as file extension is not allowed by ffmpeg. The file extension for pgs subtitles is .sup. |
| | 0 | 563 | | if (string.Equals(subtitleStream.Codec, "pgssub", StringComparison.OrdinalIgnoreCase)) |
| | | 564 | | { |
| | 0 | 565 | | return "sup"; |
| | | 566 | | } |
| | 0 | 567 | | else if (MediaStream.IsVobSubFormat(subtitleStream.Codec)) |
| | | 568 | | { |
| | | 569 | | // FFmpeg cannot mux VobSub subtitle streams back into the .idx/.sub pair, so we use .mks container inst |
| | 0 | 570 | | return "mks"; |
| | | 571 | | } |
| | | 572 | | else |
| | | 573 | | { |
| | 0 | 574 | | return GetExtractableSubtitleFormat(subtitleStream); |
| | | 575 | | } |
| | | 576 | | } |
| | | 577 | | |
| | | 578 | | private bool IsCodecCopyable(string codec) |
| | | 579 | | { |
| | 0 | 580 | | return string.Equals(codec, "ass", StringComparison.OrdinalIgnoreCase) |
| | 0 | 581 | | || string.Equals(codec, "ssa", StringComparison.OrdinalIgnoreCase) |
| | 0 | 582 | | || string.Equals(codec, "srt", StringComparison.OrdinalIgnoreCase) |
| | 0 | 583 | | || string.Equals(codec, "subrip", StringComparison.OrdinalIgnoreCase) |
| | 0 | 584 | | || string.Equals(codec, "pgssub", StringComparison.OrdinalIgnoreCase) |
| | 0 | 585 | | || MediaStream.IsVobSubFormat(codec); |
| | | 586 | | } |
| | | 587 | | |
| | | 588 | | /// <inheritdoc /> |
| | | 589 | | public async Task ExtractAllExtractableSubtitles(MediaSourceInfo mediaSource, CancellationToken cancellationToke |
| | | 590 | | { |
| | 0 | 591 | | var locks = new List<IDisposable>(); |
| | 0 | 592 | | var extractableStreams = new List<MediaStream>(); |
| | | 593 | | |
| | | 594 | | try |
| | | 595 | | { |
| | 0 | 596 | | var subtitleStreams = mediaSource.MediaStreams |
| | 0 | 597 | | .Where(stream => stream is { IsExtractableSubtitleStream: true, SupportsExternalStream: true }); |
| | | 598 | | |
| | 0 | 599 | | foreach (var subtitleStream in subtitleStreams) |
| | | 600 | | { |
| | 0 | 601 | | if (subtitleStream.IsExternal |
| | 0 | 602 | | && !subtitleStream.Path.EndsWith(".mks", StringComparison.OrdinalIgnoreCase)) |
| | | 603 | | { |
| | | 604 | | continue; |
| | | 605 | | } |
| | | 606 | | |
| | 0 | 607 | | var outputPath = GetSubtitleCachePath(mediaSource, subtitleStream.Index, "." + GetExtractableSubtitl |
| | 0 | 608 | | if (outputPath is null) |
| | | 609 | | { |
| | | 610 | | continue; |
| | | 611 | | } |
| | | 612 | | |
| | 0 | 613 | | var releaser = await _semaphoreLocks.LockAsync(outputPath, cancellationToken).ConfigureAwait(false); |
| | | 614 | | |
| | 0 | 615 | | var sourcePath = string.IsNullOrEmpty(subtitleStream.Path) ? mediaSource.Path : subtitleStream.Path; |
| | 0 | 616 | | if (IsCachedSubtitleFresh(outputPath, sourcePath)) |
| | | 617 | | { |
| | 0 | 618 | | releaser.Dispose(); |
| | 0 | 619 | | continue; |
| | | 620 | | } |
| | | 621 | | |
| | 0 | 622 | | locks.Add(releaser); |
| | 0 | 623 | | extractableStreams.Add(subtitleStream); |
| | 0 | 624 | | } |
| | | 625 | | |
| | 0 | 626 | | if (extractableStreams.Count > 0) |
| | | 627 | | { |
| | 0 | 628 | | await ExtractAllExtractableSubtitlesInternal(mediaSource, extractableStreams, cancellationToken).Con |
| | 0 | 629 | | await ExtractAllExtractableSubtitlesMKS(mediaSource, extractableStreams, cancellationToken).Configur |
| | | 630 | | } |
| | 0 | 631 | | } |
| | 0 | 632 | | catch (Exception ex) |
| | | 633 | | { |
| | 0 | 634 | | _logger.LogWarning(ex, "Unable to get streams for File:{File}", mediaSource.Path); |
| | 0 | 635 | | } |
| | | 636 | | finally |
| | | 637 | | { |
| | 0 | 638 | | locks.ForEach(x => x.Dispose()); |
| | | 639 | | } |
| | 0 | 640 | | } |
| | | 641 | | |
| | | 642 | | private async Task ExtractAllExtractableSubtitlesMKS( |
| | | 643 | | MediaSourceInfo mediaSource, |
| | | 644 | | List<MediaStream> subtitleStreams, |
| | | 645 | | CancellationToken cancellationToken) |
| | | 646 | | { |
| | 0 | 647 | | var mksFiles = new List<string>(); |
| | | 648 | | |
| | 0 | 649 | | foreach (var subtitleStream in subtitleStreams) |
| | | 650 | | { |
| | 0 | 651 | | if (string.IsNullOrEmpty(subtitleStream.Path) || !subtitleStream.Path.EndsWith(".mks", StringComparison. |
| | | 652 | | { |
| | | 653 | | continue; |
| | | 654 | | } |
| | | 655 | | |
| | 0 | 656 | | if (!mksFiles.Contains(subtitleStream.Path)) |
| | | 657 | | { |
| | 0 | 658 | | mksFiles.Add(subtitleStream.Path); |
| | | 659 | | } |
| | | 660 | | } |
| | | 661 | | |
| | 0 | 662 | | if (mksFiles.Count == 0) |
| | | 663 | | { |
| | 0 | 664 | | return; |
| | | 665 | | } |
| | | 666 | | |
| | 0 | 667 | | foreach (string mksFile in mksFiles) |
| | | 668 | | { |
| | 0 | 669 | | var inputPath = _mediaEncoder.GetInputArgument(mksFile, mediaSource); |
| | 0 | 670 | | var outputPaths = new List<string>(); |
| | 0 | 671 | | var args = string.Format( |
| | 0 | 672 | | CultureInfo.InvariantCulture, |
| | 0 | 673 | | "-y -i {0}", |
| | 0 | 674 | | inputPath); |
| | | 675 | | |
| | 0 | 676 | | foreach (var subtitleStream in subtitleStreams) |
| | | 677 | | { |
| | 0 | 678 | | if (!subtitleStream.Path.Equals(mksFile, StringComparison.OrdinalIgnoreCase)) |
| | | 679 | | { |
| | | 680 | | continue; |
| | | 681 | | } |
| | | 682 | | |
| | 0 | 683 | | var outputPath = GetSubtitleCachePath(mediaSource, subtitleStream.Index, "." + GetExtractableSubtitl |
| | 0 | 684 | | if (outputPath is null) |
| | | 685 | | { |
| | | 686 | | continue; |
| | | 687 | | } |
| | | 688 | | |
| | 0 | 689 | | var outputCodec = IsCodecCopyable(subtitleStream.Codec) ? "copy" : "srt"; |
| | | 690 | | // FFmpeg does not provide an .idx/.sub muxer, so VobSub streams must be written as MKS files. |
| | 0 | 691 | | var outputFormatOption = MediaStream.IsVobSubFormat(subtitleStream.Codec) ? " -f matroska" : string. |
| | 0 | 692 | | var streamIndex = EncodingHelper.FindIndex(mediaSource.MediaStreams, subtitleStream); |
| | | 693 | | |
| | 0 | 694 | | if (streamIndex == -1) |
| | | 695 | | { |
| | 0 | 696 | | _logger.LogError("Cannot find subtitle stream index for {InputPath} ({Index}), skipping this str |
| | 0 | 697 | | continue; |
| | | 698 | | } |
| | | 699 | | |
| | 0 | 700 | | Directory.CreateDirectory(Path.GetDirectoryName(outputPath) ?? throw new FileNotFoundException($"Cal |
| | | 701 | | |
| | 0 | 702 | | outputPaths.Add(outputPath); |
| | 0 | 703 | | args += string.Format( |
| | 0 | 704 | | CultureInfo.InvariantCulture, |
| | 0 | 705 | | " -map 0:{0} -an -vn -c:s {1}{2} -flush_packets 1 \"{3}\"", |
| | 0 | 706 | | streamIndex, |
| | 0 | 707 | | outputCodec, |
| | 0 | 708 | | outputFormatOption, |
| | 0 | 709 | | outputPath); |
| | | 710 | | } |
| | | 711 | | |
| | 0 | 712 | | await ExtractSubtitlesForFile(inputPath, args, outputPaths, cancellationToken).ConfigureAwait(false); |
| | | 713 | | |
| | 0 | 714 | | foreach (var outputPath in outputPaths) |
| | | 715 | | { |
| | 0 | 716 | | WriteCacheMeta(outputPath, mksFile); |
| | | 717 | | } |
| | 0 | 718 | | } |
| | 0 | 719 | | } |
| | | 720 | | |
| | | 721 | | private async Task ExtractAllExtractableSubtitlesInternal( |
| | | 722 | | MediaSourceInfo mediaSource, |
| | | 723 | | List<MediaStream> subtitleStreams, |
| | | 724 | | CancellationToken cancellationToken) |
| | | 725 | | { |
| | 0 | 726 | | var inputPath = _mediaEncoder.GetInputArgument(mediaSource.Path, mediaSource); |
| | 0 | 727 | | var outputPaths = new List<string>(); |
| | 0 | 728 | | var args = string.Format( |
| | 0 | 729 | | CultureInfo.InvariantCulture, |
| | 0 | 730 | | "-i {0}", |
| | 0 | 731 | | inputPath); |
| | | 732 | | |
| | 0 | 733 | | foreach (var subtitleStream in subtitleStreams) |
| | | 734 | | { |
| | 0 | 735 | | if (!string.IsNullOrEmpty(subtitleStream.Path) && subtitleStream.Path.EndsWith(".mks", StringComparison. |
| | | 736 | | { |
| | 0 | 737 | | _logger.LogDebug("Subtitle {Index} for file {InputPath} is part in an MKS file. Skipping", inputPath |
| | 0 | 738 | | continue; |
| | | 739 | | } |
| | | 740 | | |
| | 0 | 741 | | var outputPath = GetSubtitleCachePath(mediaSource, subtitleStream.Index, "." + GetExtractableSubtitleFil |
| | 0 | 742 | | if (outputPath is null) |
| | | 743 | | { |
| | | 744 | | continue; |
| | | 745 | | } |
| | | 746 | | |
| | 0 | 747 | | var outputCodec = IsCodecCopyable(subtitleStream.Codec) ? "copy" : "srt"; |
| | | 748 | | // FFmpeg does not provide an .idx/.sub muxer, so VobSub streams must be written as MKS files. |
| | 0 | 749 | | var outputFormatOption = MediaStream.IsVobSubFormat(subtitleStream.Codec) ? " -f matroska" : string.Empt |
| | 0 | 750 | | var streamIndex = EncodingHelper.FindIndex(mediaSource.MediaStreams, subtitleStream); |
| | | 751 | | |
| | 0 | 752 | | if (streamIndex == -1) |
| | | 753 | | { |
| | 0 | 754 | | _logger.LogError("Cannot find subtitle stream index for {InputPath} ({Index}), skipping this stream" |
| | 0 | 755 | | continue; |
| | | 756 | | } |
| | | 757 | | |
| | 0 | 758 | | Directory.CreateDirectory(Path.GetDirectoryName(outputPath) ?? throw new FileNotFoundException($"Calcula |
| | | 759 | | |
| | 0 | 760 | | outputPaths.Add(outputPath); |
| | 0 | 761 | | args += string.Format( |
| | 0 | 762 | | CultureInfo.InvariantCulture, |
| | 0 | 763 | | " -map 0:{0} -an -vn -c:s {1}{2} -flush_packets 1 \"{3}\"", |
| | 0 | 764 | | streamIndex, |
| | 0 | 765 | | outputCodec, |
| | 0 | 766 | | outputFormatOption, |
| | 0 | 767 | | outputPath); |
| | | 768 | | } |
| | | 769 | | |
| | 0 | 770 | | if (outputPaths.Count > 0) |
| | | 771 | | { |
| | 0 | 772 | | await ExtractSubtitlesForFile(inputPath, args, outputPaths, cancellationToken).ConfigureAwait(false); |
| | | 773 | | |
| | 0 | 774 | | foreach (var outputPath in outputPaths) |
| | | 775 | | { |
| | 0 | 776 | | WriteCacheMeta(outputPath, mediaSource.Path); |
| | | 777 | | } |
| | | 778 | | } |
| | 0 | 779 | | } |
| | | 780 | | |
| | | 781 | | private async Task ExtractSubtitlesForFile( |
| | | 782 | | string inputPath, |
| | | 783 | | string args, |
| | | 784 | | List<string> outputPaths, |
| | | 785 | | CancellationToken cancellationToken) |
| | | 786 | | { |
| | | 787 | | int exitCode; |
| | | 788 | | |
| | 0 | 789 | | using (var process = new Process |
| | 0 | 790 | | { |
| | 0 | 791 | | StartInfo = new ProcessStartInfo |
| | 0 | 792 | | { |
| | 0 | 793 | | CreateNoWindow = true, |
| | 0 | 794 | | UseShellExecute = false, |
| | 0 | 795 | | FileName = _mediaEncoder.EncoderPath, |
| | 0 | 796 | | Arguments = args, |
| | 0 | 797 | | WindowStyle = ProcessWindowStyle.Hidden, |
| | 0 | 798 | | ErrorDialog = false |
| | 0 | 799 | | }, |
| | 0 | 800 | | EnableRaisingEvents = true |
| | 0 | 801 | | }) |
| | | 802 | | { |
| | 0 | 803 | | _logger.LogInformation("{File} {Arguments}", process.StartInfo.FileName, process.StartInfo.Arguments); |
| | | 804 | | |
| | | 805 | | try |
| | | 806 | | { |
| | 0 | 807 | | process.Start(); |
| | 0 | 808 | | } |
| | 0 | 809 | | catch (Exception ex) |
| | | 810 | | { |
| | 0 | 811 | | _logger.LogError(ex, "Error starting ffmpeg"); |
| | | 812 | | |
| | 0 | 813 | | throw; |
| | | 814 | | } |
| | | 815 | | |
| | | 816 | | try |
| | | 817 | | { |
| | 0 | 818 | | var timeoutMinutes = _serverConfigurationManager.GetEncodingOptions().SubtitleExtractionTimeoutMinut |
| | 0 | 819 | | await process.WaitForExitAsync(TimeSpan.FromMinutes(timeoutMinutes)).ConfigureAwait(false); |
| | 0 | 820 | | exitCode = process.ExitCode; |
| | 0 | 821 | | } |
| | 0 | 822 | | catch (OperationCanceledException) |
| | | 823 | | { |
| | 0 | 824 | | process.Kill(true); |
| | 0 | 825 | | exitCode = -1; |
| | 0 | 826 | | } |
| | 0 | 827 | | } |
| | | 828 | | |
| | 0 | 829 | | var failed = false; |
| | | 830 | | |
| | 0 | 831 | | if (exitCode == -1) |
| | | 832 | | { |
| | 0 | 833 | | failed = true; |
| | | 834 | | |
| | 0 | 835 | | foreach (var outputPath in outputPaths) |
| | | 836 | | { |
| | | 837 | | try |
| | | 838 | | { |
| | 0 | 839 | | _logger.LogWarning("Deleting extracted subtitle due to failure: {Path}", outputPath); |
| | 0 | 840 | | _fileSystem.DeleteFile(outputPath); |
| | 0 | 841 | | } |
| | 0 | 842 | | catch (FileNotFoundException) |
| | | 843 | | { |
| | 0 | 844 | | } |
| | 0 | 845 | | catch (IOException ex) |
| | | 846 | | { |
| | 0 | 847 | | _logger.LogError(ex, "Error deleting extracted subtitle {Path}", outputPath); |
| | 0 | 848 | | } |
| | | 849 | | } |
| | | 850 | | } |
| | | 851 | | else |
| | | 852 | | { |
| | 0 | 853 | | foreach (var outputPath in outputPaths) |
| | | 854 | | { |
| | 0 | 855 | | if (!File.Exists(outputPath) || _fileSystem.GetFileInfo(outputPath).Length == 0) |
| | | 856 | | { |
| | 0 | 857 | | _logger.LogError("ffmpeg subtitle extraction failed for {InputPath} to {OutputPath}", inputPath, |
| | 0 | 858 | | failed = true; |
| | | 859 | | |
| | | 860 | | try |
| | | 861 | | { |
| | 0 | 862 | | _logger.LogWarning("Deleting extracted subtitle due to failure: {Path}", outputPath); |
| | 0 | 863 | | _fileSystem.DeleteFile(outputPath); |
| | 0 | 864 | | } |
| | 0 | 865 | | catch (FileNotFoundException) |
| | | 866 | | { |
| | 0 | 867 | | } |
| | 0 | 868 | | catch (IOException ex) |
| | | 869 | | { |
| | 0 | 870 | | _logger.LogError(ex, "Error deleting extracted subtitle {Path}", outputPath); |
| | 0 | 871 | | } |
| | | 872 | | |
| | | 873 | | continue; |
| | | 874 | | } |
| | | 875 | | |
| | 0 | 876 | | if (outputPath.EndsWith("ass", StringComparison.OrdinalIgnoreCase)) |
| | | 877 | | { |
| | 0 | 878 | | await SetAssFont(outputPath, cancellationToken).ConfigureAwait(false); |
| | | 879 | | } |
| | | 880 | | |
| | 0 | 881 | | _logger.LogInformation("ffmpeg subtitle extraction completed for {InputPath} to {OutputPath}", input |
| | 0 | 882 | | } |
| | | 883 | | } |
| | | 884 | | |
| | 0 | 885 | | if (failed) |
| | | 886 | | { |
| | 0 | 887 | | throw new FfmpegException( |
| | 0 | 888 | | string.Format(CultureInfo.InvariantCulture, "ffmpeg subtitle extraction failed for {0}", inputPath)) |
| | | 889 | | } |
| | 0 | 890 | | } |
| | | 891 | | |
| | | 892 | | /// <summary> |
| | | 893 | | /// Extracts the text subtitle. |
| | | 894 | | /// </summary> |
| | | 895 | | /// <param name="mediaSource">The mediaSource.</param> |
| | | 896 | | /// <param name="subtitleStream">The subtitle stream.</param> |
| | | 897 | | /// <param name="outputCodec">The output codec.</param> |
| | | 898 | | /// <param name="outputPath">The output path.</param> |
| | | 899 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 900 | | /// <returns>Task.</returns> |
| | | 901 | | /// <exception cref="ArgumentException">Must use inputPath list overload.</exception> |
| | | 902 | | private async Task ExtractTextSubtitle( |
| | | 903 | | MediaSourceInfo mediaSource, |
| | | 904 | | MediaStream subtitleStream, |
| | | 905 | | string outputCodec, |
| | | 906 | | string outputPath, |
| | | 907 | | CancellationToken cancellationToken) |
| | | 908 | | { |
| | 0 | 909 | | using (await _semaphoreLocks.LockAsync(outputPath, cancellationToken).ConfigureAwait(false)) |
| | | 910 | | { |
| | 0 | 911 | | if (!File.Exists(outputPath) || _fileSystem.GetFileInfo(outputPath).Length == 0) |
| | | 912 | | { |
| | 0 | 913 | | var subtitleStreamIndex = EncodingHelper.FindIndex(mediaSource.MediaStreams, subtitleStream); |
| | | 914 | | |
| | 0 | 915 | | var args = _mediaEncoder.GetInputArgument(mediaSource.Path, mediaSource); |
| | | 916 | | |
| | 0 | 917 | | if (subtitleStream.IsExternal) |
| | | 918 | | { |
| | 0 | 919 | | args = _mediaEncoder.GetExternalSubtitleInputArgument(subtitleStream.Path); |
| | | 920 | | } |
| | | 921 | | |
| | 0 | 922 | | await ExtractTextSubtitleInternal( |
| | 0 | 923 | | args, |
| | 0 | 924 | | subtitleStreamIndex, |
| | 0 | 925 | | outputCodec, |
| | 0 | 926 | | outputPath, |
| | 0 | 927 | | cancellationToken).ConfigureAwait(false); |
| | | 928 | | } |
| | 0 | 929 | | } |
| | 0 | 930 | | } |
| | | 931 | | |
| | | 932 | | private async Task ExtractTextSubtitleInternal( |
| | | 933 | | string inputPath, |
| | | 934 | | int subtitleStreamIndex, |
| | | 935 | | string outputCodec, |
| | | 936 | | string outputPath, |
| | | 937 | | CancellationToken cancellationToken) |
| | | 938 | | { |
| | 0 | 939 | | ArgumentException.ThrowIfNullOrEmpty(inputPath); |
| | | 940 | | |
| | 0 | 941 | | ArgumentException.ThrowIfNullOrEmpty(outputPath); |
| | | 942 | | |
| | 0 | 943 | | Directory.CreateDirectory(Path.GetDirectoryName(outputPath) ?? throw new ArgumentException($"Provided path ( |
| | | 944 | | |
| | 0 | 945 | | var processArgs = string.Format( |
| | 0 | 946 | | CultureInfo.InvariantCulture, |
| | 0 | 947 | | "-i {0} -copyts -map 0:{1} -an -vn -c:s {2} \"{3}\"", |
| | 0 | 948 | | inputPath, |
| | 0 | 949 | | subtitleStreamIndex, |
| | 0 | 950 | | outputCodec, |
| | 0 | 951 | | outputPath); |
| | | 952 | | |
| | | 953 | | int exitCode; |
| | | 954 | | |
| | 0 | 955 | | using (var process = new Process |
| | 0 | 956 | | { |
| | 0 | 957 | | StartInfo = new ProcessStartInfo |
| | 0 | 958 | | { |
| | 0 | 959 | | CreateNoWindow = true, |
| | 0 | 960 | | UseShellExecute = false, |
| | 0 | 961 | | FileName = _mediaEncoder.EncoderPath, |
| | 0 | 962 | | Arguments = processArgs, |
| | 0 | 963 | | WindowStyle = ProcessWindowStyle.Hidden, |
| | 0 | 964 | | ErrorDialog = false |
| | 0 | 965 | | }, |
| | 0 | 966 | | EnableRaisingEvents = true |
| | 0 | 967 | | }) |
| | | 968 | | { |
| | 0 | 969 | | _logger.LogInformation("{File} {Arguments}", process.StartInfo.FileName, process.StartInfo.Arguments); |
| | | 970 | | |
| | | 971 | | try |
| | | 972 | | { |
| | 0 | 973 | | process.Start(); |
| | 0 | 974 | | } |
| | 0 | 975 | | catch (Exception ex) |
| | | 976 | | { |
| | 0 | 977 | | _logger.LogError(ex, "Error starting ffmpeg"); |
| | | 978 | | |
| | 0 | 979 | | throw; |
| | | 980 | | } |
| | | 981 | | |
| | | 982 | | try |
| | | 983 | | { |
| | 0 | 984 | | var timeoutMinutes = _serverConfigurationManager.GetEncodingOptions().SubtitleExtractionTimeoutMinut |
| | 0 | 985 | | await process.WaitForExitAsync(TimeSpan.FromMinutes(timeoutMinutes)).ConfigureAwait(false); |
| | 0 | 986 | | exitCode = process.ExitCode; |
| | 0 | 987 | | } |
| | 0 | 988 | | catch (OperationCanceledException) |
| | | 989 | | { |
| | 0 | 990 | | process.Kill(true); |
| | 0 | 991 | | exitCode = -1; |
| | 0 | 992 | | } |
| | 0 | 993 | | } |
| | | 994 | | |
| | 0 | 995 | | var failed = false; |
| | | 996 | | |
| | 0 | 997 | | if (exitCode == -1) |
| | | 998 | | { |
| | 0 | 999 | | failed = true; |
| | | 1000 | | |
| | | 1001 | | try |
| | | 1002 | | { |
| | 0 | 1003 | | _logger.LogWarning("Deleting extracted subtitle due to failure: {Path}", outputPath); |
| | 0 | 1004 | | _fileSystem.DeleteFile(outputPath); |
| | 0 | 1005 | | } |
| | 0 | 1006 | | catch (FileNotFoundException) |
| | | 1007 | | { |
| | 0 | 1008 | | } |
| | 0 | 1009 | | catch (IOException ex) |
| | | 1010 | | { |
| | 0 | 1011 | | _logger.LogError(ex, "Error deleting extracted subtitle {Path}", outputPath); |
| | 0 | 1012 | | } |
| | | 1013 | | } |
| | 0 | 1014 | | else if (!File.Exists(outputPath) || _fileSystem.GetFileInfo(outputPath).Length == 0) |
| | | 1015 | | { |
| | 0 | 1016 | | failed = true; |
| | | 1017 | | |
| | | 1018 | | try |
| | | 1019 | | { |
| | 0 | 1020 | | _logger.LogWarning("Deleting extracted subtitle due to failure: {Path}", outputPath); |
| | 0 | 1021 | | _fileSystem.DeleteFile(outputPath); |
| | 0 | 1022 | | } |
| | 0 | 1023 | | catch (FileNotFoundException) |
| | | 1024 | | { |
| | 0 | 1025 | | } |
| | 0 | 1026 | | catch (IOException ex) |
| | | 1027 | | { |
| | 0 | 1028 | | _logger.LogError(ex, "Error deleting extracted subtitle {Path}", outputPath); |
| | 0 | 1029 | | } |
| | | 1030 | | } |
| | | 1031 | | |
| | 0 | 1032 | | if (failed) |
| | | 1033 | | { |
| | 0 | 1034 | | _logger.LogError("ffmpeg subtitle extraction failed for {InputPath} to {OutputPath}", inputPath, outputP |
| | | 1035 | | |
| | 0 | 1036 | | throw new FfmpegException( |
| | 0 | 1037 | | string.Format(CultureInfo.InvariantCulture, "ffmpeg subtitle extraction failed for {0} to {1}", inpu |
| | | 1038 | | } |
| | | 1039 | | |
| | 0 | 1040 | | _logger.LogInformation("ffmpeg subtitle extraction completed for {InputPath} to {OutputPath}", inputPath, ou |
| | | 1041 | | |
| | 0 | 1042 | | if (string.Equals(outputCodec, "ass", StringComparison.OrdinalIgnoreCase)) |
| | | 1043 | | { |
| | 0 | 1044 | | await SetAssFont(outputPath, cancellationToken).ConfigureAwait(false); |
| | | 1045 | | } |
| | 0 | 1046 | | } |
| | | 1047 | | |
| | | 1048 | | /// <summary> |
| | | 1049 | | /// Sets the ass font. |
| | | 1050 | | /// </summary> |
| | | 1051 | | /// <param name="file">The file.</param> |
| | | 1052 | | /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <c>Syst |
| | | 1053 | | /// <returns>Task.</returns> |
| | | 1054 | | private async Task SetAssFont(string file, CancellationToken cancellationToken = default) |
| | | 1055 | | { |
| | 0 | 1056 | | _logger.LogInformation("Setting ass font within {File}", file); |
| | | 1057 | | |
| | | 1058 | | string text; |
| | | 1059 | | Encoding encoding; |
| | | 1060 | | |
| | 0 | 1061 | | using (var fileStream = AsyncFile.OpenRead(file)) |
| | 0 | 1062 | | using (var reader = new StreamReader(fileStream, true)) |
| | | 1063 | | { |
| | 0 | 1064 | | encoding = reader.CurrentEncoding; |
| | | 1065 | | |
| | 0 | 1066 | | text = await reader.ReadToEndAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 1067 | | } |
| | | 1068 | | |
| | 0 | 1069 | | var newText = text.Replace(",Arial,", ",Arial Unicode MS,", StringComparison.Ordinal); |
| | | 1070 | | |
| | 0 | 1071 | | if (!string.Equals(text, newText, StringComparison.Ordinal)) |
| | | 1072 | | { |
| | 0 | 1073 | | var fileStream = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None, IODefaults.File |
| | 0 | 1074 | | await using (fileStream.ConfigureAwait(false)) |
| | | 1075 | | { |
| | 0 | 1076 | | var writer = new StreamWriter(fileStream, encoding); |
| | 0 | 1077 | | await using (writer.ConfigureAwait(false)) |
| | | 1078 | | { |
| | 0 | 1079 | | await writer.WriteAsync(newText.AsMemory(), cancellationToken).ConfigureAwait(false); |
| | | 1080 | | } |
| | | 1081 | | } |
| | | 1082 | | } |
| | 0 | 1083 | | } |
| | | 1084 | | |
| | | 1085 | | private string? GetSubtitleCachePath(MediaSourceInfo mediaSource, int subtitleStreamIndex, string outputSubtitle |
| | | 1086 | | { |
| | 0 | 1087 | | return _pathManager.GetSubtitlePath(mediaSource.Id, subtitleStreamIndex, outputSubtitleExtension); |
| | | 1088 | | } |
| | | 1089 | | |
| | | 1090 | | /// <inheritdoc /> |
| | | 1091 | | public async Task<string> GetSubtitleFileCharacterSet(MediaStream subtitleStream, string language, MediaSourceIn |
| | | 1092 | | { |
| | 0 | 1093 | | var subtitleCodec = subtitleStream.Codec; |
| | 0 | 1094 | | var path = subtitleStream.Path; |
| | | 1095 | | |
| | 0 | 1096 | | if (path.EndsWith(".mks", StringComparison.OrdinalIgnoreCase)) |
| | | 1097 | | { |
| | 0 | 1098 | | var cachePath = GetSubtitleCachePath(mediaSource, subtitleStream.Index, "." + subtitleCodec); |
| | 0 | 1099 | | if (cachePath is not null) |
| | | 1100 | | { |
| | 0 | 1101 | | path = cachePath; |
| | 0 | 1102 | | await ExtractTextSubtitle(mediaSource, subtitleStream, subtitleCodec, path, cancellationToken) |
| | 0 | 1103 | | .ConfigureAwait(false); |
| | | 1104 | | } |
| | | 1105 | | } |
| | | 1106 | | |
| | 0 | 1107 | | var result = await DetectCharset(path, mediaSource.Protocol, cancellationToken).ConfigureAwait(false); |
| | 0 | 1108 | | var charset = result.Detected?.EncodingName ?? string.Empty; |
| | | 1109 | | |
| | | 1110 | | // UTF16 is automatically converted to UTF8 by FFmpeg, do not specify a character encoding |
| | 0 | 1111 | | if ((path.EndsWith(".ass", StringComparison.Ordinal) || path.EndsWith(".ssa", StringComparison.Ordinal) || p |
| | 0 | 1112 | | && (string.Equals(charset, "utf-16le", StringComparison.OrdinalIgnoreCase) |
| | 0 | 1113 | | || string.Equals(charset, "utf-16be", StringComparison.OrdinalIgnoreCase))) |
| | | 1114 | | { |
| | 0 | 1115 | | charset = string.Empty; |
| | | 1116 | | } |
| | | 1117 | | |
| | 0 | 1118 | | _logger.LogDebug("charset {0} detected for {Path}", charset, path); |
| | | 1119 | | |
| | 0 | 1120 | | return charset; |
| | 0 | 1121 | | } |
| | | 1122 | | |
| | | 1123 | | private async Task<DetectionResult> DetectCharset(string path, MediaProtocol protocol, CancellationToken cancell |
| | | 1124 | | { |
| | | 1125 | | switch (protocol) |
| | | 1126 | | { |
| | | 1127 | | case MediaProtocol.Http: |
| | | 1128 | | { |
| | 0 | 1129 | | using var stream = await _httpClientFactory |
| | 0 | 1130 | | .CreateClient(NamedClient.Default) |
| | 0 | 1131 | | .GetStreamAsync(new Uri(path), cancellationToken) |
| | 0 | 1132 | | .ConfigureAwait(false); |
| | | 1133 | | |
| | 0 | 1134 | | return await CharsetDetector.DetectFromStreamAsync(stream, cancellationToken).ConfigureAwait(fal |
| | | 1135 | | } |
| | | 1136 | | |
| | | 1137 | | case MediaProtocol.File: |
| | | 1138 | | { |
| | 0 | 1139 | | return await CharsetDetector.DetectFromFileAsync(path, cancellationToken) |
| | 0 | 1140 | | .ConfigureAwait(false); |
| | | 1141 | | } |
| | | 1142 | | |
| | | 1143 | | default: |
| | 0 | 1144 | | throw new ArgumentOutOfRangeException(nameof(protocol), protocol, "Unsupported protocol"); |
| | | 1145 | | } |
| | 0 | 1146 | | } |
| | | 1147 | | |
| | | 1148 | | public async Task<string> GetSubtitleFilePath(MediaStream subtitleStream, MediaSourceInfo mediaSource, Cancellat |
| | | 1149 | | { |
| | 0 | 1150 | | var info = await GetReadableFile(mediaSource, subtitleStream, cancellationToken) |
| | 0 | 1151 | | .ConfigureAwait(false); |
| | 0 | 1152 | | return info.Path; |
| | 0 | 1153 | | } |
| | | 1154 | | |
| | | 1155 | | /// <inheritdoc /> |
| | | 1156 | | public void Dispose() |
| | | 1157 | | { |
| | 21 | 1158 | | _semaphoreLocks.Dispose(); |
| | 21 | 1159 | | } |
| | | 1160 | | |
| | | 1161 | | #pragma warning disable CA1034 // Nested types should not be visible |
| | | 1162 | | // Only public for the unit tests |
| | | 1163 | | public readonly record struct SubtitleInfo |
| | | 1164 | | { |
| | | 1165 | | public string Path { get; init; } |
| | | 1166 | | |
| | | 1167 | | public MediaProtocol Protocol { get; init; } |
| | | 1168 | | |
| | | 1169 | | public string Format { get; init; } |
| | | 1170 | | |
| | | 1171 | | public bool IsExternal { get; init; } |
| | | 1172 | | } |
| | | 1173 | | } |
| | | 1174 | | } |