SubtitleEncoder.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Extensions;
  3. using MediaBrowser.Common.Net;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Controller.MediaEncoding;
  7. using MediaBrowser.Model.Entities;
  8. using MediaBrowser.Model.Logging;
  9. using MediaBrowser.Model.MediaInfo;
  10. using MediaBrowser.Model.Serialization;
  11. using System;
  12. using System.Collections.Concurrent;
  13. using System.Diagnostics;
  14. using System.Globalization;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading;
  19. using System.Threading.Tasks;
  20. using CommonIO;
  21. using UniversalDetector;
  22. namespace MediaBrowser.MediaEncoding.Subtitles
  23. {
  24. public class SubtitleEncoder : ISubtitleEncoder
  25. {
  26. private readonly ILibraryManager _libraryManager;
  27. private readonly ILogger _logger;
  28. private readonly IApplicationPaths _appPaths;
  29. private readonly IFileSystem _fileSystem;
  30. private readonly IMediaEncoder _mediaEncoder;
  31. private readonly IJsonSerializer _json;
  32. private readonly IHttpClient _httpClient;
  33. private readonly IMediaSourceManager _mediaSourceManager;
  34. public SubtitleEncoder(ILibraryManager libraryManager, ILogger logger, IApplicationPaths appPaths, IFileSystem fileSystem, IMediaEncoder mediaEncoder, IJsonSerializer json, IHttpClient httpClient, IMediaSourceManager mediaSourceManager)
  35. {
  36. _libraryManager = libraryManager;
  37. _logger = logger;
  38. _appPaths = appPaths;
  39. _fileSystem = fileSystem;
  40. _mediaEncoder = mediaEncoder;
  41. _json = json;
  42. _httpClient = httpClient;
  43. _mediaSourceManager = mediaSourceManager;
  44. }
  45. private string SubtitleCachePath
  46. {
  47. get
  48. {
  49. return Path.Combine(_appPaths.DataPath, "subtitles");
  50. }
  51. }
  52. private async Task<Stream> ConvertSubtitles(Stream stream,
  53. string inputFormat,
  54. string outputFormat,
  55. long startTimeTicks,
  56. long? endTimeTicks,
  57. bool preserveOriginalTimestamps,
  58. CancellationToken cancellationToken)
  59. {
  60. var ms = new MemoryStream();
  61. try
  62. {
  63. var reader = GetReader(inputFormat, true);
  64. var trackInfo = reader.Parse(stream, cancellationToken);
  65. FilterEvents(trackInfo, startTimeTicks, endTimeTicks, preserveOriginalTimestamps);
  66. var writer = GetWriter(outputFormat);
  67. writer.Write(trackInfo, ms, cancellationToken);
  68. ms.Position = 0;
  69. }
  70. catch
  71. {
  72. ms.Dispose();
  73. throw;
  74. }
  75. return ms;
  76. }
  77. private void FilterEvents(SubtitleTrackInfo track, long startPositionTicks, long? endTimeTicks, bool preserveTimestamps)
  78. {
  79. // Drop subs that are earlier than what we're looking for
  80. track.TrackEvents = track.TrackEvents
  81. .SkipWhile(i => (i.StartPositionTicks - startPositionTicks) < 0 || (i.EndPositionTicks - startPositionTicks) < 0)
  82. .ToList();
  83. if (endTimeTicks.HasValue)
  84. {
  85. var endTime = endTimeTicks.Value;
  86. track.TrackEvents = track.TrackEvents
  87. .TakeWhile(i => i.StartPositionTicks <= endTime)
  88. .ToList();
  89. }
  90. if (!preserveTimestamps)
  91. {
  92. foreach (var trackEvent in track.TrackEvents)
  93. {
  94. trackEvent.EndPositionTicks -= startPositionTicks;
  95. trackEvent.StartPositionTicks -= startPositionTicks;
  96. }
  97. }
  98. }
  99. public async Task<Stream> GetSubtitles(string itemId,
  100. string mediaSourceId,
  101. int subtitleStreamIndex,
  102. string outputFormat,
  103. long startTimeTicks,
  104. long? endTimeTicks,
  105. bool preserveOriginalTimestamps,
  106. CancellationToken cancellationToken)
  107. {
  108. var subtitle = await GetSubtitleStream(itemId, mediaSourceId, subtitleStreamIndex, cancellationToken)
  109. .ConfigureAwait(false);
  110. var inputFormat = subtitle.Item2;
  111. if (string.Equals(inputFormat, outputFormat, StringComparison.OrdinalIgnoreCase) && TryGetWriter(outputFormat) == null)
  112. {
  113. return subtitle.Item1;
  114. }
  115. using (var stream = subtitle.Item1)
  116. {
  117. return await ConvertSubtitles(stream, inputFormat, outputFormat, startTimeTicks, endTimeTicks, preserveOriginalTimestamps, cancellationToken).ConfigureAwait(false);
  118. }
  119. }
  120. private async Task<Tuple<Stream, string>> GetSubtitleStream(string itemId,
  121. string mediaSourceId,
  122. int subtitleStreamIndex,
  123. CancellationToken cancellationToken)
  124. {
  125. var mediaSources = await _mediaSourceManager.GetPlayackMediaSources(itemId, null, false, new[] { MediaType.Audio, MediaType.Video }, cancellationToken).ConfigureAwait(false);
  126. var mediaSource = mediaSources
  127. .First(i => string.Equals(i.Id, mediaSourceId));
  128. var subtitleStream = mediaSource.MediaStreams
  129. .First(i => i.Type == MediaStreamType.Subtitle && i.Index == subtitleStreamIndex);
  130. var inputFiles = new[] { mediaSource.Path };
  131. if (mediaSource.VideoType.HasValue)
  132. {
  133. if (mediaSource.VideoType.Value == VideoType.BluRay ||
  134. mediaSource.VideoType.Value == VideoType.Dvd)
  135. {
  136. var mediaSourceItem = (Video)_libraryManager.GetItemById(new Guid(mediaSourceId));
  137. inputFiles = mediaSourceItem.GetPlayableStreamFiles().ToArray();
  138. }
  139. }
  140. var fileInfo = await GetReadableFile(mediaSource.Path, inputFiles, mediaSource.Protocol, subtitleStream, cancellationToken).ConfigureAwait(false);
  141. var stream = await GetSubtitleStream(fileInfo.Item1, subtitleStream.Language, fileInfo.Item2, fileInfo.Item4, cancellationToken).ConfigureAwait(false);
  142. return new Tuple<Stream, string>(stream, fileInfo.Item3);
  143. }
  144. private async Task<Stream> GetSubtitleStream(string path, string language, MediaProtocol protocol, bool requiresCharset, CancellationToken cancellationToken)
  145. {
  146. if (requiresCharset)
  147. {
  148. var charset = await GetSubtitleFileCharacterSet(path, language, protocol, cancellationToken).ConfigureAwait(false);
  149. if (!string.IsNullOrEmpty(charset))
  150. {
  151. using (var fs = await GetStream(path, protocol, cancellationToken).ConfigureAwait(false))
  152. {
  153. using (var reader = new StreamReader(fs, GetEncoding(charset)))
  154. {
  155. var text = await reader.ReadToEndAsync().ConfigureAwait(false);
  156. var bytes = Encoding.UTF8.GetBytes(text);
  157. return new MemoryStream(bytes);
  158. }
  159. }
  160. }
  161. }
  162. return _fileSystem.OpenRead(path);
  163. }
  164. private Encoding GetEncoding(string charset)
  165. {
  166. if (string.IsNullOrWhiteSpace(charset))
  167. {
  168. throw new ArgumentNullException("charset");
  169. }
  170. _logger.Debug("Getting encoding object for character set: {0}", charset);
  171. try
  172. {
  173. return Encoding.GetEncoding(charset);
  174. }
  175. catch (ArgumentException)
  176. {
  177. charset = charset.Replace("-", string.Empty);
  178. _logger.Debug("Getting encoding object for character set: {0}", charset);
  179. return Encoding.GetEncoding(charset);
  180. }
  181. }
  182. private async Task<Tuple<string, MediaProtocol, string, bool>> GetReadableFile(string mediaPath,
  183. string[] inputFiles,
  184. MediaProtocol protocol,
  185. MediaStream subtitleStream,
  186. CancellationToken cancellationToken)
  187. {
  188. if (!subtitleStream.IsExternal)
  189. {
  190. string outputFormat;
  191. string outputCodec;
  192. if (string.Equals(subtitleStream.Codec, "ass", StringComparison.OrdinalIgnoreCase) ||
  193. string.Equals(subtitleStream.Codec, "ssa", StringComparison.OrdinalIgnoreCase) ||
  194. string.Equals(subtitleStream.Codec, "srt", StringComparison.OrdinalIgnoreCase))
  195. {
  196. // Extract
  197. outputCodec = "copy";
  198. outputFormat = subtitleStream.Codec;
  199. }
  200. else if (string.Equals(subtitleStream.Codec, "subrip", StringComparison.OrdinalIgnoreCase))
  201. {
  202. // Extract
  203. outputCodec = "copy";
  204. outputFormat = "srt";
  205. }
  206. else
  207. {
  208. // Extract
  209. outputCodec = "srt";
  210. outputFormat = "srt";
  211. }
  212. // Extract
  213. var outputPath = GetSubtitleCachePath(mediaPath, protocol, subtitleStream.Index, "." + outputFormat);
  214. await ExtractTextSubtitle(inputFiles, protocol, subtitleStream.Index, outputCodec, outputPath, cancellationToken)
  215. .ConfigureAwait(false);
  216. return new Tuple<string, MediaProtocol, string, bool>(outputPath, MediaProtocol.File, outputFormat, false);
  217. }
  218. var currentFormat = (Path.GetExtension(subtitleStream.Path) ?? subtitleStream.Codec)
  219. .TrimStart('.');
  220. if (GetReader(currentFormat, false) == null)
  221. {
  222. // Convert
  223. var outputPath = GetSubtitleCachePath(mediaPath, protocol, subtitleStream.Index, ".srt");
  224. await ConvertTextSubtitleToSrt(subtitleStream.Path, subtitleStream.Language, protocol, outputPath, cancellationToken).ConfigureAwait(false);
  225. return new Tuple<string, MediaProtocol, string, bool>(outputPath, MediaProtocol.File, "srt", true);
  226. }
  227. return new Tuple<string, MediaProtocol, string, bool>(subtitleStream.Path, protocol, currentFormat, true);
  228. }
  229. private ISubtitleParser GetReader(string format, bool throwIfMissing)
  230. {
  231. if (string.IsNullOrEmpty(format))
  232. {
  233. throw new ArgumentNullException("format");
  234. }
  235. if (string.Equals(format, SubtitleFormat.SRT, StringComparison.OrdinalIgnoreCase))
  236. {
  237. return new SrtParser(_logger);
  238. }
  239. if (string.Equals(format, SubtitleFormat.SSA, StringComparison.OrdinalIgnoreCase))
  240. {
  241. return new SsaParser();
  242. }
  243. if (string.Equals(format, SubtitleFormat.ASS, StringComparison.OrdinalIgnoreCase))
  244. {
  245. return new AssParser();
  246. }
  247. if (throwIfMissing)
  248. {
  249. throw new ArgumentException("Unsupported format: " + format);
  250. }
  251. return null;
  252. }
  253. private ISubtitleWriter TryGetWriter(string format)
  254. {
  255. if (string.IsNullOrEmpty(format))
  256. {
  257. throw new ArgumentNullException("format");
  258. }
  259. if (string.Equals(format, "json", StringComparison.OrdinalIgnoreCase))
  260. {
  261. return new JsonWriter(_json);
  262. }
  263. if (string.Equals(format, SubtitleFormat.SRT, StringComparison.OrdinalIgnoreCase))
  264. {
  265. return new SrtWriter();
  266. }
  267. if (string.Equals(format, SubtitleFormat.VTT, StringComparison.OrdinalIgnoreCase))
  268. {
  269. return new VttWriter();
  270. }
  271. if (string.Equals(format, SubtitleFormat.TTML, StringComparison.OrdinalIgnoreCase))
  272. {
  273. return new TtmlWriter();
  274. }
  275. return null;
  276. }
  277. private ISubtitleWriter GetWriter(string format)
  278. {
  279. var writer = TryGetWriter(format);
  280. if (writer != null)
  281. {
  282. return writer;
  283. }
  284. throw new ArgumentException("Unsupported format: " + format);
  285. }
  286. /// <summary>
  287. /// The _semaphoreLocks
  288. /// </summary>
  289. private readonly ConcurrentDictionary<string, SemaphoreSlim> _semaphoreLocks =
  290. new ConcurrentDictionary<string, SemaphoreSlim>();
  291. /// <summary>
  292. /// Gets the lock.
  293. /// </summary>
  294. /// <param name="filename">The filename.</param>
  295. /// <returns>System.Object.</returns>
  296. private SemaphoreSlim GetLock(string filename)
  297. {
  298. return _semaphoreLocks.GetOrAdd(filename, key => new SemaphoreSlim(1, 1));
  299. }
  300. /// <summary>
  301. /// Converts the text subtitle to SRT.
  302. /// </summary>
  303. /// <param name="inputPath">The input path.</param>
  304. /// <param name="inputProtocol">The input protocol.</param>
  305. /// <param name="outputPath">The output path.</param>
  306. /// <param name="cancellationToken">The cancellation token.</param>
  307. /// <returns>Task.</returns>
  308. private async Task ConvertTextSubtitleToSrt(string inputPath, string language, MediaProtocol inputProtocol, string outputPath, CancellationToken cancellationToken)
  309. {
  310. var semaphore = GetLock(outputPath);
  311. await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
  312. try
  313. {
  314. if (!_fileSystem.FileExists(outputPath))
  315. {
  316. await ConvertTextSubtitleToSrtInternal(inputPath, language, inputProtocol, outputPath, cancellationToken).ConfigureAwait(false);
  317. }
  318. }
  319. finally
  320. {
  321. semaphore.Release();
  322. }
  323. }
  324. /// <summary>
  325. /// Converts the text subtitle to SRT internal.
  326. /// </summary>
  327. /// <param name="inputPath">The input path.</param>
  328. /// <param name="inputProtocol">The input protocol.</param>
  329. /// <param name="outputPath">The output path.</param>
  330. /// <param name="cancellationToken">The cancellation token.</param>
  331. /// <returns>Task.</returns>
  332. /// <exception cref="System.ArgumentNullException">
  333. /// inputPath
  334. /// or
  335. /// outputPath
  336. /// </exception>
  337. /// <exception cref="System.ApplicationException"></exception>
  338. private async Task ConvertTextSubtitleToSrtInternal(string inputPath, string language, MediaProtocol inputProtocol, string outputPath, CancellationToken cancellationToken)
  339. {
  340. if (string.IsNullOrEmpty(inputPath))
  341. {
  342. throw new ArgumentNullException("inputPath");
  343. }
  344. if (string.IsNullOrEmpty(outputPath))
  345. {
  346. throw new ArgumentNullException("outputPath");
  347. }
  348. _fileSystem.CreateDirectory(Path.GetDirectoryName(outputPath));
  349. var encodingParam = await GetSubtitleFileCharacterSet(inputPath, language, inputProtocol, cancellationToken).ConfigureAwait(false);
  350. if (!string.IsNullOrEmpty(encodingParam))
  351. {
  352. encodingParam = " -sub_charenc " + encodingParam;
  353. }
  354. var process = new Process
  355. {
  356. StartInfo = new ProcessStartInfo
  357. {
  358. RedirectStandardOutput = false,
  359. RedirectStandardError = true,
  360. RedirectStandardInput = true,
  361. CreateNoWindow = true,
  362. UseShellExecute = false,
  363. FileName = _mediaEncoder.EncoderPath,
  364. Arguments = string.Format("{0} -i \"{1}\" -c:s srt \"{2}\"", encodingParam, inputPath, outputPath),
  365. WindowStyle = ProcessWindowStyle.Hidden,
  366. ErrorDialog = false
  367. }
  368. };
  369. _logger.Info("{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments);
  370. var logFilePath = Path.Combine(_appPaths.LogDirectoryPath, "ffmpeg-sub-convert-" + Guid.NewGuid() + ".txt");
  371. _fileSystem.CreateDirectory(Path.GetDirectoryName(logFilePath));
  372. var logFileStream = _fileSystem.GetFileStream(logFilePath, FileMode.Create, FileAccess.Write, FileShare.Read,
  373. true);
  374. try
  375. {
  376. process.Start();
  377. }
  378. catch (Exception ex)
  379. {
  380. logFileStream.Dispose();
  381. _logger.ErrorException("Error starting ffmpeg", ex);
  382. throw;
  383. }
  384. var logTask = process.StandardError.BaseStream.CopyToAsync(logFileStream);
  385. var ranToCompletion = process.WaitForExit(60000);
  386. if (!ranToCompletion)
  387. {
  388. try
  389. {
  390. _logger.Info("Killing ffmpeg subtitle conversion process");
  391. process.StandardInput.WriteLine("q");
  392. process.WaitForExit(1000);
  393. await logTask.ConfigureAwait(false);
  394. }
  395. catch (Exception ex)
  396. {
  397. _logger.ErrorException("Error killing subtitle conversion process", ex);
  398. }
  399. finally
  400. {
  401. logFileStream.Dispose();
  402. }
  403. }
  404. var exitCode = ranToCompletion ? process.ExitCode : -1;
  405. process.Dispose();
  406. var failed = false;
  407. if (exitCode == -1)
  408. {
  409. failed = true;
  410. if (_fileSystem.FileExists(outputPath))
  411. {
  412. try
  413. {
  414. _logger.Info("Deleting converted subtitle due to failure: ", outputPath);
  415. _fileSystem.DeleteFile(outputPath);
  416. }
  417. catch (IOException ex)
  418. {
  419. _logger.ErrorException("Error deleting converted subtitle {0}", ex, outputPath);
  420. }
  421. }
  422. }
  423. else if (!_fileSystem.FileExists(outputPath))
  424. {
  425. failed = true;
  426. }
  427. if (failed)
  428. {
  429. var msg = string.Format("ffmpeg subtitle converted failed for {0}", inputPath);
  430. _logger.Error(msg);
  431. throw new ApplicationException(msg);
  432. }
  433. await SetAssFont(outputPath).ConfigureAwait(false);
  434. }
  435. /// <summary>
  436. /// Extracts the text subtitle.
  437. /// </summary>
  438. /// <param name="inputFiles">The input files.</param>
  439. /// <param name="protocol">The protocol.</param>
  440. /// <param name="subtitleStreamIndex">Index of the subtitle stream.</param>
  441. /// <param name="outputCodec">The output codec.</param>
  442. /// <param name="outputPath">The output path.</param>
  443. /// <param name="cancellationToken">The cancellation token.</param>
  444. /// <returns>Task.</returns>
  445. /// <exception cref="System.ArgumentException">Must use inputPath list overload</exception>
  446. private async Task ExtractTextSubtitle(string[] inputFiles, MediaProtocol protocol, int subtitleStreamIndex,
  447. string outputCodec, string outputPath, CancellationToken cancellationToken)
  448. {
  449. var semaphore = GetLock(outputPath);
  450. await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
  451. try
  452. {
  453. if (!_fileSystem.FileExists(outputPath))
  454. {
  455. await ExtractTextSubtitleInternal(_mediaEncoder.GetInputArgument(inputFiles, protocol), subtitleStreamIndex,
  456. outputCodec, outputPath, cancellationToken).ConfigureAwait(false);
  457. }
  458. }
  459. finally
  460. {
  461. semaphore.Release();
  462. }
  463. }
  464. private async Task ExtractTextSubtitleInternal(string inputPath, int subtitleStreamIndex,
  465. string outputCodec, string outputPath, CancellationToken cancellationToken)
  466. {
  467. if (string.IsNullOrEmpty(inputPath))
  468. {
  469. throw new ArgumentNullException("inputPath");
  470. }
  471. if (string.IsNullOrEmpty(outputPath))
  472. {
  473. throw new ArgumentNullException("outputPath");
  474. }
  475. _fileSystem.CreateDirectory(Path.GetDirectoryName(outputPath));
  476. var processArgs = string.Format("-i {0} -map 0:{1} -an -vn -c:s {2} \"{3}\"", inputPath,
  477. subtitleStreamIndex, outputCodec, outputPath);
  478. var process = new Process
  479. {
  480. StartInfo = new ProcessStartInfo
  481. {
  482. CreateNoWindow = true,
  483. UseShellExecute = false,
  484. RedirectStandardOutput = false,
  485. RedirectStandardError = true,
  486. RedirectStandardInput = true,
  487. FileName = _mediaEncoder.EncoderPath,
  488. Arguments = processArgs,
  489. WindowStyle = ProcessWindowStyle.Hidden,
  490. ErrorDialog = false
  491. }
  492. };
  493. _logger.Info("{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments);
  494. var logFilePath = Path.Combine(_appPaths.LogDirectoryPath, "ffmpeg-sub-extract-" + Guid.NewGuid() + ".txt");
  495. _fileSystem.CreateDirectory(Path.GetDirectoryName(logFilePath));
  496. var logFileStream = _fileSystem.GetFileStream(logFilePath, FileMode.Create, FileAccess.Write, FileShare.Read,
  497. true);
  498. try
  499. {
  500. process.Start();
  501. }
  502. catch (Exception ex)
  503. {
  504. logFileStream.Dispose();
  505. _logger.ErrorException("Error starting ffmpeg", ex);
  506. throw;
  507. }
  508. process.StandardError.BaseStream.CopyToAsync(logFileStream);
  509. var ranToCompletion = process.WaitForExit(300000);
  510. if (!ranToCompletion)
  511. {
  512. try
  513. {
  514. _logger.Info("Killing ffmpeg subtitle extraction process");
  515. process.StandardInput.WriteLine("q");
  516. process.WaitForExit(1000);
  517. }
  518. catch (Exception ex)
  519. {
  520. _logger.ErrorException("Error killing subtitle extraction process", ex);
  521. }
  522. finally
  523. {
  524. logFileStream.Dispose();
  525. }
  526. }
  527. var exitCode = ranToCompletion ? process.ExitCode : -1;
  528. process.Dispose();
  529. var failed = false;
  530. if (exitCode == -1)
  531. {
  532. failed = true;
  533. try
  534. {
  535. _logger.Info("Deleting extracted subtitle due to failure: {0}", outputPath);
  536. _fileSystem.DeleteFile(outputPath);
  537. }
  538. catch (FileNotFoundException)
  539. {
  540. }
  541. catch (DirectoryNotFoundException)
  542. {
  543. }
  544. catch (IOException ex)
  545. {
  546. _logger.ErrorException("Error deleting extracted subtitle {0}", ex, outputPath);
  547. }
  548. }
  549. else if (!_fileSystem.FileExists(outputPath))
  550. {
  551. failed = true;
  552. }
  553. if (failed)
  554. {
  555. var msg = string.Format("ffmpeg subtitle extraction failed for {0} to {1}", inputPath, outputPath);
  556. _logger.Error(msg);
  557. throw new ApplicationException(msg);
  558. }
  559. else
  560. {
  561. var msg = string.Format("ffmpeg subtitle extraction completed for {0} to {1}", inputPath, outputPath);
  562. _logger.Info(msg);
  563. }
  564. if (string.Equals(outputCodec, "ass", StringComparison.OrdinalIgnoreCase))
  565. {
  566. await SetAssFont(outputPath).ConfigureAwait(false);
  567. }
  568. }
  569. /// <summary>
  570. /// Sets the ass font.
  571. /// </summary>
  572. /// <param name="file">The file.</param>
  573. /// <returns>Task.</returns>
  574. private async Task SetAssFont(string file)
  575. {
  576. _logger.Info("Setting ass font within {0}", file);
  577. string text;
  578. Encoding encoding;
  579. using (var reader = new StreamReader(file, true))
  580. {
  581. encoding = reader.CurrentEncoding;
  582. text = await reader.ReadToEndAsync().ConfigureAwait(false);
  583. }
  584. var newText = text.Replace(",Arial,", ",Arial Unicode MS,");
  585. if (!string.Equals(text, newText))
  586. {
  587. using (var writer = new StreamWriter(file, false, encoding))
  588. {
  589. writer.Write(newText);
  590. }
  591. }
  592. }
  593. private string GetSubtitleCachePath(string mediaPath, MediaProtocol protocol, int subtitleStreamIndex, string outputSubtitleExtension)
  594. {
  595. if (protocol == MediaProtocol.File)
  596. {
  597. var ticksParam = string.Empty;
  598. var date = _fileSystem.GetLastWriteTimeUtc(mediaPath);
  599. var filename = (mediaPath + "_" + subtitleStreamIndex.ToString(CultureInfo.InvariantCulture) + "_" + date.Ticks.ToString(CultureInfo.InvariantCulture) + ticksParam).GetMD5() + outputSubtitleExtension;
  600. var prefix = filename.Substring(0, 1);
  601. return Path.Combine(SubtitleCachePath, prefix, filename);
  602. }
  603. else
  604. {
  605. var filename = (mediaPath + "_" + subtitleStreamIndex.ToString(CultureInfo.InvariantCulture)).GetMD5() + outputSubtitleExtension;
  606. var prefix = filename.Substring(0, 1);
  607. return Path.Combine(SubtitleCachePath, prefix, filename);
  608. }
  609. }
  610. public async Task<string> GetSubtitleFileCharacterSet(string path, string language, MediaProtocol protocol, CancellationToken cancellationToken)
  611. {
  612. if (protocol == MediaProtocol.File)
  613. {
  614. if (GetFileEncoding(path).Equals(Encoding.UTF8))
  615. {
  616. return string.Empty;
  617. }
  618. }
  619. var charset = await DetectCharset(path, language, protocol, cancellationToken).ConfigureAwait(false);
  620. if (!string.IsNullOrWhiteSpace(charset))
  621. {
  622. if (string.Equals(charset, "utf-8", StringComparison.OrdinalIgnoreCase))
  623. {
  624. return null;
  625. }
  626. return charset;
  627. }
  628. if (!string.IsNullOrWhiteSpace(language))
  629. {
  630. return GetSubtitleFileCharacterSetFromLanguage(language);
  631. }
  632. return null;
  633. }
  634. public string GetSubtitleFileCharacterSetFromLanguage(string language)
  635. {
  636. switch (language.ToLower())
  637. {
  638. case "pol":
  639. case "cze":
  640. case "ces":
  641. case "slo":
  642. case "slk":
  643. case "hun":
  644. case "slv":
  645. case "srp":
  646. case "hrv":
  647. case "rum":
  648. case "ron":
  649. case "rup":
  650. case "alb":
  651. case "sqi":
  652. return "windows-1250";
  653. case "ara":
  654. return "windows-1256";
  655. case "heb":
  656. return "windows-1255";
  657. case "grc":
  658. case "gre":
  659. return "windows-1253";
  660. case "crh":
  661. case "ota":
  662. case "tur":
  663. return "windows-1254";
  664. case "rus":
  665. return "windows-1251";
  666. case "vie":
  667. return "windows-1258";
  668. case "kor":
  669. return "cp949";
  670. default:
  671. return "windows-1252";
  672. }
  673. }
  674. private async Task<string> DetectCharset(string path, string language, MediaProtocol protocol, CancellationToken cancellationToken)
  675. {
  676. try
  677. {
  678. using (var file = await GetStream(path, protocol, cancellationToken).ConfigureAwait(false))
  679. {
  680. var detector = new CharsetDetector();
  681. detector.Feed(file);
  682. detector.DataEnd();
  683. var charset = detector.Charset;
  684. if (!string.IsNullOrWhiteSpace(charset))
  685. {
  686. _logger.Info("UniversalDetector detected charset {0} for {1}", charset, path);
  687. }
  688. // This is often incorrectly indetected. If this happens, try to use other techniques instead
  689. if (string.Equals("x-mac-cyrillic", charset, StringComparison.OrdinalIgnoreCase))
  690. {
  691. if (!string.IsNullOrWhiteSpace(language))
  692. {
  693. return null;
  694. }
  695. }
  696. return charset;
  697. }
  698. }
  699. catch (IOException ex)
  700. {
  701. _logger.ErrorException("Error attempting to determine subtitle charset from {0}", ex, path);
  702. }
  703. return null;
  704. }
  705. private Encoding GetFileEncoding(string srcFile)
  706. {
  707. // *** Detect byte order mark if any - otherwise assume default
  708. var buffer = new byte[5];
  709. using (var file = _fileSystem.GetFileStream(srcFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
  710. {
  711. file.Read(buffer, 0, 5);
  712. }
  713. if (buffer[0] == 0xef && buffer[1] == 0xbb && buffer[2] == 0xbf)
  714. return Encoding.UTF8;
  715. if (buffer[0] == 0xfe && buffer[1] == 0xff)
  716. return Encoding.Unicode;
  717. if (buffer[0] == 0 && buffer[1] == 0 && buffer[2] == 0xfe && buffer[3] == 0xff)
  718. return Encoding.UTF32;
  719. if (buffer[0] == 0x2b && buffer[1] == 0x2f && buffer[2] == 0x76)
  720. return Encoding.UTF7;
  721. // It's ok - anything aside from utf is ok since that's what we're looking for
  722. return Encoding.Default;
  723. }
  724. private async Task<Stream> GetStream(string path, MediaProtocol protocol, CancellationToken cancellationToken)
  725. {
  726. if (protocol == MediaProtocol.Http)
  727. {
  728. return await _httpClient.Get(path, cancellationToken).ConfigureAwait(false);
  729. }
  730. if (protocol == MediaProtocol.File)
  731. {
  732. return _fileSystem.GetFileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  733. }
  734. throw new ArgumentOutOfRangeException("protocol");
  735. }
  736. }
  737. }