MediaEncoder.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Common.MediaInfo;
  4. using MediaBrowser.Common.Net;
  5. using MediaBrowser.Model.Entities;
  6. using MediaBrowser.Model.IO;
  7. using MediaBrowser.Model.Logging;
  8. using MediaBrowser.Model.Serialization;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.ComponentModel;
  12. using System.Diagnostics;
  13. using System.Globalization;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Reflection;
  17. using System.Runtime.InteropServices;
  18. using System.Text;
  19. using System.Threading;
  20. using System.Threading.Tasks;
  21. namespace MediaBrowser.Server.Implementations.MediaEncoder
  22. {
  23. /// <summary>
  24. /// Class MediaEncoder
  25. /// </summary>
  26. public class MediaEncoder : IMediaEncoder, IDisposable
  27. {
  28. /// <summary>
  29. /// Gets or sets the zip client.
  30. /// </summary>
  31. /// <value>The zip client.</value>
  32. private readonly IZipClient _zipClient;
  33. /// <summary>
  34. /// The _logger
  35. /// </summary>
  36. private readonly ILogger _logger;
  37. /// <summary>
  38. /// The _app paths
  39. /// </summary>
  40. private readonly IApplicationPaths _appPaths;
  41. /// <summary>
  42. /// Gets the json serializer.
  43. /// </summary>
  44. /// <value>The json serializer.</value>
  45. private readonly IJsonSerializer _jsonSerializer;
  46. private readonly IHttpClient _httpClient;
  47. /// <summary>
  48. /// The video image resource pool
  49. /// </summary>
  50. private readonly SemaphoreSlim _videoImageResourcePool = new SemaphoreSlim(1, 1);
  51. /// <summary>
  52. /// The audio image resource pool
  53. /// </summary>
  54. private readonly SemaphoreSlim _audioImageResourcePool = new SemaphoreSlim(1, 1);
  55. /// <summary>
  56. /// The _subtitle extraction resource pool
  57. /// </summary>
  58. private readonly SemaphoreSlim _subtitleExtractionResourcePool = new SemaphoreSlim(2, 2);
  59. /// <summary>
  60. /// The FF probe resource pool
  61. /// </summary>
  62. private readonly SemaphoreSlim _ffProbeResourcePool = new SemaphoreSlim(2, 2);
  63. /// <summary>
  64. /// Gets or sets the versioned directory path.
  65. /// </summary>
  66. /// <value>The versioned directory path.</value>
  67. private string VersionedDirectoryPath { get; set; }
  68. /// <summary>
  69. /// Initializes a new instance of the <see cref="MediaEncoder" /> class.
  70. /// </summary>
  71. /// <param name="logger">The logger.</param>
  72. /// <param name="zipClient">The zip client.</param>
  73. /// <param name="appPaths">The app paths.</param>
  74. /// <param name="jsonSerializer">The json serializer.</param>
  75. public MediaEncoder(ILogger logger, IZipClient zipClient, IApplicationPaths appPaths,
  76. IJsonSerializer jsonSerializer, IHttpClient httpClient)
  77. {
  78. _logger = logger;
  79. _zipClient = zipClient;
  80. _appPaths = appPaths;
  81. _jsonSerializer = jsonSerializer;
  82. _httpClient = httpClient;
  83. // Not crazy about this but it's the only way to suppress ffmpeg crash dialog boxes
  84. SetErrorMode(ErrorModes.SEM_FAILCRITICALERRORS | ErrorModes.SEM_NOALIGNMENTFAULTEXCEPT |
  85. ErrorModes.SEM_NOGPFAULTERRORBOX | ErrorModes.SEM_NOOPENFILEERRORBOX);
  86. Task.Run(() => VersionedDirectoryPath = GetVersionedDirectoryPath());
  87. }
  88. /// <summary>
  89. /// Gets the media tools path.
  90. /// </summary>
  91. /// <param name="create">if set to <c>true</c> [create].</param>
  92. /// <returns>System.String.</returns>
  93. private string GetMediaToolsPath(bool create)
  94. {
  95. var path = Path.Combine(_appPaths.ProgramDataPath, "ffmpeg");
  96. if (create && !Directory.Exists(path))
  97. {
  98. Directory.CreateDirectory(path);
  99. }
  100. return path;
  101. }
  102. /// <summary>
  103. /// Gets the encoder path.
  104. /// </summary>
  105. /// <value>The encoder path.</value>
  106. public string EncoderPath
  107. {
  108. get { return FFMpegPath; }
  109. }
  110. /// <summary>
  111. /// The _ FF MPEG path
  112. /// </summary>
  113. private string _FFMpegPath;
  114. /// <summary>
  115. /// Gets the path to ffmpeg.exe
  116. /// </summary>
  117. /// <value>The FF MPEG path.</value>
  118. public string FFMpegPath
  119. {
  120. get { return _FFMpegPath ?? (_FFMpegPath = Path.Combine(VersionedDirectoryPath, "ffmpeg.exe")); }
  121. }
  122. /// <summary>
  123. /// The _ FF probe path
  124. /// </summary>
  125. private string _FFProbePath;
  126. /// <summary>
  127. /// Gets the path to ffprobe.exe
  128. /// </summary>
  129. /// <value>The FF probe path.</value>
  130. private string FFProbePath
  131. {
  132. get { return _FFProbePath ?? (_FFProbePath = Path.Combine(VersionedDirectoryPath, "ffprobe.exe")); }
  133. }
  134. /// <summary>
  135. /// Gets the version.
  136. /// </summary>
  137. /// <value>The version.</value>
  138. public string Version
  139. {
  140. get { return Path.GetFileNameWithoutExtension(VersionedDirectoryPath); }
  141. }
  142. /// <summary>
  143. /// Gets the versioned directory path.
  144. /// </summary>
  145. /// <returns>System.String.</returns>
  146. private string GetVersionedDirectoryPath()
  147. {
  148. var assembly = GetType().Assembly;
  149. var prefix = GetType().Namespace + ".";
  150. var srch = prefix + "ffmpeg";
  151. var resource = assembly.GetManifestResourceNames().First(r => r.StartsWith(srch));
  152. var filename =
  153. resource.Substring(resource.IndexOf(prefix, StringComparison.OrdinalIgnoreCase) + prefix.Length);
  154. var versionedDirectoryPath = Path.Combine(GetMediaToolsPath(true),
  155. Path.GetFileNameWithoutExtension(filename));
  156. if (!Directory.Exists(versionedDirectoryPath))
  157. {
  158. Directory.CreateDirectory(versionedDirectoryPath);
  159. }
  160. ExtractTools(assembly, resource, versionedDirectoryPath);
  161. return versionedDirectoryPath;
  162. }
  163. /// <summary>
  164. /// Extracts the tools.
  165. /// </summary>
  166. /// <param name="assembly">The assembly.</param>
  167. /// <param name="zipFileResourcePath">The zip file resource path.</param>
  168. /// <param name="targetPath">The target path.</param>
  169. private void ExtractTools(Assembly assembly, string zipFileResourcePath, string targetPath)
  170. {
  171. using (var resourceStream = assembly.GetManifestResourceStream(zipFileResourcePath))
  172. {
  173. _zipClient.ExtractAll(resourceStream, targetPath, false);
  174. }
  175. ExtractFonts(targetPath);
  176. }
  177. private const string FontUrl = "https://www.dropbox.com/s/9nb76tybcsw5xrk/ARIALUNI.zip?dl=1";
  178. /// <summary>
  179. /// Extracts the fonts.
  180. /// </summary>
  181. /// <param name="targetPath">The target path.</param>
  182. private async void ExtractFonts(string targetPath)
  183. {
  184. var fontsDirectory = Path.Combine(targetPath, "fonts");
  185. if (!Directory.Exists(fontsDirectory))
  186. {
  187. Directory.CreateDirectory(fontsDirectory);
  188. }
  189. const string fontFilename = "ARIALUNI.TTF";
  190. var fontFile = Path.Combine(fontsDirectory, fontFilename);
  191. if (!File.Exists(fontFile))
  192. {
  193. var tempFile = await _httpClient.GetTempFile(new HttpRequestOptions
  194. {
  195. Url = FontUrl,
  196. Progress = new Progress<double>()
  197. });
  198. _zipClient.ExtractAll(tempFile, fontsDirectory, true);
  199. try
  200. {
  201. File.Delete(tempFile);
  202. }
  203. catch (IOException ex)
  204. {
  205. // Log this, but don't let it fail the operation
  206. _logger.ErrorException("Error deleting temp file {0}", ex, tempFile);
  207. }
  208. }
  209. await WriteFontConfigFile(fontsDirectory).ConfigureAwait(false);
  210. }
  211. private async Task WriteFontConfigFile(string fontsDirectory)
  212. {
  213. const string fontConfigFilename = "fonts.conf";
  214. var fontConfigFile = Path.Combine(fontsDirectory, fontConfigFilename);
  215. if (!File.Exists(fontConfigFile))
  216. {
  217. var contents = string.Format("<?xml version=\"1.0\"?><fontconfig><dir>{0}</dir><alias><family>Arial</family><prefer>Arial Unicode MS</prefer></alias></fontconfig>", fontsDirectory);
  218. var bytes = Encoding.UTF8.GetBytes(contents);
  219. using (var fileStream = new FileStream(fontConfigFile, FileMode.Create, FileAccess.Write,
  220. FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize,
  221. FileOptions.Asynchronous))
  222. {
  223. await fileStream.WriteAsync(bytes, 0, bytes.Length);
  224. }
  225. }
  226. }
  227. /// <summary>
  228. /// Gets the media info.
  229. /// </summary>
  230. /// <param name="inputFiles">The input files.</param>
  231. /// <param name="type">The type.</param>
  232. /// <param name="cancellationToken">The cancellation token.</param>
  233. /// <returns>Task.</returns>
  234. public Task<MediaInfoResult> GetMediaInfo(string[] inputFiles, InputType type,
  235. CancellationToken cancellationToken)
  236. {
  237. return GetMediaInfoInternal(GetInputArgument(inputFiles, type), type != InputType.AudioFile,
  238. GetProbeSizeArgument(type), cancellationToken);
  239. }
  240. /// <summary>
  241. /// Gets the input argument.
  242. /// </summary>
  243. /// <param name="inputFiles">The input files.</param>
  244. /// <param name="type">The type.</param>
  245. /// <returns>System.String.</returns>
  246. /// <exception cref="System.ArgumentException">Unrecognized InputType</exception>
  247. public string GetInputArgument(string[] inputFiles, InputType type)
  248. {
  249. string inputPath;
  250. switch (type)
  251. {
  252. case InputType.Dvd:
  253. case InputType.VideoFile:
  254. case InputType.AudioFile:
  255. inputPath = GetConcatInputArgument(inputFiles);
  256. break;
  257. case InputType.Bluray:
  258. inputPath = GetBlurayInputArgument(inputFiles[0]);
  259. break;
  260. case InputType.Url:
  261. inputPath = GetHttpInputArgument(inputFiles);
  262. break;
  263. default:
  264. throw new ArgumentException("Unrecognized InputType");
  265. }
  266. return inputPath;
  267. }
  268. /// <summary>
  269. /// Gets the HTTP input argument.
  270. /// </summary>
  271. /// <param name="inputFiles">The input files.</param>
  272. /// <returns>System.String.</returns>
  273. private string GetHttpInputArgument(string[] inputFiles)
  274. {
  275. var url = inputFiles[0];
  276. return string.Format("\"{0}\"", url);
  277. }
  278. /// <summary>
  279. /// Gets the probe size argument.
  280. /// </summary>
  281. /// <param name="type">The type.</param>
  282. /// <returns>System.String.</returns>
  283. public string GetProbeSizeArgument(InputType type)
  284. {
  285. return type == InputType.Dvd ? "-probesize 1G -analyzeduration 200M" : string.Empty;
  286. }
  287. /// <summary>
  288. /// Gets the media info internal.
  289. /// </summary>
  290. /// <param name="inputPath">The input path.</param>
  291. /// <param name="extractChapters">if set to <c>true</c> [extract chapters].</param>
  292. /// <param name="probeSizeArgument">The probe size argument.</param>
  293. /// <param name="cancellationToken">The cancellation token.</param>
  294. /// <returns>Task{MediaInfoResult}.</returns>
  295. /// <exception cref="System.ApplicationException"></exception>
  296. private async Task<MediaInfoResult> GetMediaInfoInternal(string inputPath, bool extractChapters,
  297. string probeSizeArgument,
  298. CancellationToken cancellationToken)
  299. {
  300. var process = new Process
  301. {
  302. StartInfo = new ProcessStartInfo
  303. {
  304. CreateNoWindow = true,
  305. UseShellExecute = false,
  306. // Must consume both or ffmpeg may hang due to deadlocks. See comments below.
  307. RedirectStandardOutput = true,
  308. RedirectStandardError = true,
  309. FileName = FFProbePath,
  310. Arguments =
  311. string.Format(
  312. "{0} -i {1} -threads 0 -v info -print_format json -show_streams -show_format",
  313. probeSizeArgument, inputPath).Trim(),
  314. WindowStyle = ProcessWindowStyle.Hidden,
  315. ErrorDialog = false
  316. },
  317. EnableRaisingEvents = true
  318. };
  319. _logger.Debug("{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments);
  320. process.Exited += ProcessExited;
  321. await _ffProbeResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
  322. MediaInfoResult result;
  323. string standardError = null;
  324. try
  325. {
  326. process.Start();
  327. }
  328. catch (Exception ex)
  329. {
  330. _ffProbeResourcePool.Release();
  331. _logger.ErrorException("Error starting ffprobe", ex);
  332. throw;
  333. }
  334. try
  335. {
  336. Task<string> standardErrorReadTask = null;
  337. // MUST read both stdout and stderr asynchronously or a deadlock may occurr
  338. if (extractChapters)
  339. {
  340. standardErrorReadTask = process.StandardError.ReadToEndAsync();
  341. }
  342. else
  343. {
  344. process.BeginErrorReadLine();
  345. }
  346. result = _jsonSerializer.DeserializeFromStream<MediaInfoResult>(process.StandardOutput.BaseStream);
  347. if (extractChapters)
  348. {
  349. standardError = await standardErrorReadTask.ConfigureAwait(false);
  350. }
  351. }
  352. catch
  353. {
  354. // Hate having to do this
  355. try
  356. {
  357. process.Kill();
  358. }
  359. catch (InvalidOperationException ex1)
  360. {
  361. _logger.ErrorException("Error killing ffprobe", ex1);
  362. }
  363. catch (Win32Exception ex1)
  364. {
  365. _logger.ErrorException("Error killing ffprobe", ex1);
  366. }
  367. throw;
  368. }
  369. finally
  370. {
  371. _ffProbeResourcePool.Release();
  372. }
  373. if (result == null)
  374. {
  375. throw new ApplicationException(string.Format("FFProbe failed for {0}", inputPath));
  376. }
  377. cancellationToken.ThrowIfCancellationRequested();
  378. if (result.streams != null)
  379. {
  380. // Normalize aspect ratio if invalid
  381. foreach (var stream in result.streams)
  382. {
  383. if (string.Equals(stream.display_aspect_ratio, "0:1", StringComparison.OrdinalIgnoreCase))
  384. {
  385. stream.display_aspect_ratio = string.Empty;
  386. }
  387. if (string.Equals(stream.sample_aspect_ratio, "0:1", StringComparison.OrdinalIgnoreCase))
  388. {
  389. stream.sample_aspect_ratio = string.Empty;
  390. }
  391. }
  392. }
  393. if (extractChapters && !string.IsNullOrEmpty(standardError))
  394. {
  395. AddChapters(result, standardError);
  396. }
  397. return result;
  398. }
  399. /// <summary>
  400. /// The us culture
  401. /// </summary>
  402. protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
  403. /// <summary>
  404. /// Adds the chapters.
  405. /// </summary>
  406. /// <param name="result">The result.</param>
  407. /// <param name="standardError">The standard error.</param>
  408. private void AddChapters(MediaInfoResult result, string standardError)
  409. {
  410. var lines = standardError.Split('\n').Select(l => l.TrimStart());
  411. var chapters = new List<ChapterInfo>();
  412. ChapterInfo lastChapter = null;
  413. foreach (var line in lines)
  414. {
  415. if (line.StartsWith("Chapter", StringComparison.OrdinalIgnoreCase))
  416. {
  417. // Example:
  418. // Chapter #0.2: start 400.534, end 4565.435
  419. const string srch = "start ";
  420. var start = line.IndexOf(srch, StringComparison.OrdinalIgnoreCase);
  421. if (start == -1)
  422. {
  423. continue;
  424. }
  425. var subString = line.Substring(start + srch.Length);
  426. subString = subString.Substring(0, subString.IndexOf(','));
  427. double seconds;
  428. if (double.TryParse(subString, NumberStyles.Any, UsCulture, out seconds))
  429. {
  430. lastChapter = new ChapterInfo
  431. {
  432. StartPositionTicks = TimeSpan.FromSeconds(seconds).Ticks
  433. };
  434. chapters.Add(lastChapter);
  435. }
  436. }
  437. else if (line.StartsWith("title", StringComparison.OrdinalIgnoreCase))
  438. {
  439. if (lastChapter != null && string.IsNullOrEmpty(lastChapter.Name))
  440. {
  441. var index = line.IndexOf(':');
  442. if (index != -1)
  443. {
  444. lastChapter.Name = line.Substring(index + 1).Trim().TrimEnd('\r');
  445. }
  446. }
  447. }
  448. }
  449. result.Chapters = chapters;
  450. }
  451. /// <summary>
  452. /// Processes the exited.
  453. /// </summary>
  454. /// <param name="sender">The sender.</param>
  455. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  456. private void ProcessExited(object sender, EventArgs e)
  457. {
  458. ((Process)sender).Dispose();
  459. }
  460. /// <summary>
  461. /// Converts the text subtitle to ass.
  462. /// </summary>
  463. /// <param name="inputPath">The input path.</param>
  464. /// <param name="outputPath">The output path.</param>
  465. /// <param name="language">The language.</param>
  466. /// <param name="offset">The offset.</param>
  467. /// <param name="cancellationToken">The cancellation token.</param>
  468. /// <returns>Task.</returns>
  469. /// <exception cref="System.ArgumentNullException">inputPath
  470. /// or
  471. /// outputPath</exception>
  472. /// <exception cref="System.ApplicationException"></exception>
  473. public async Task ConvertTextSubtitleToAss(string inputPath, string outputPath, string language, TimeSpan offset,
  474. CancellationToken cancellationToken)
  475. {
  476. if (string.IsNullOrEmpty(inputPath))
  477. {
  478. throw new ArgumentNullException("inputPath");
  479. }
  480. if (string.IsNullOrEmpty(outputPath))
  481. {
  482. throw new ArgumentNullException("outputPath");
  483. }
  484. var fastSeekSeconds = offset.TotalSeconds >= 1 ? offset.TotalSeconds - 1 : 0;
  485. var slowSeekSeconds = offset.TotalSeconds >= 1 ? 1 : 0;
  486. var fastSeekParam = fastSeekSeconds > 0 ? "-ss " + fastSeekSeconds.ToString(UsCulture) + " " : string.Empty;
  487. var slowSeekParam = slowSeekSeconds > 0 ? " -ss " + slowSeekSeconds.ToString(UsCulture) : string.Empty;
  488. var encodingParam = string.IsNullOrEmpty(language) ? string.Empty :
  489. GetSubtitleLanguageEncodingParam(language) + " ";
  490. var process = new Process
  491. {
  492. StartInfo = new ProcessStartInfo
  493. {
  494. RedirectStandardOutput = false,
  495. RedirectStandardError = true,
  496. CreateNoWindow = true,
  497. UseShellExecute = false,
  498. FileName = FFMpegPath,
  499. Arguments =
  500. string.Format("{0}{1}-i \"{2}\"{3} \"{4}\"", encodingParam, fastSeekParam, inputPath, slowSeekParam,
  501. outputPath),
  502. WindowStyle = ProcessWindowStyle.Hidden,
  503. ErrorDialog = false
  504. }
  505. };
  506. _logger.Debug("{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments);
  507. await _subtitleExtractionResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
  508. var logFilePath = Path.Combine(_appPaths.LogDirectoryPath, "ffmpeg-sub-convert-" + Guid.NewGuid() + ".txt");
  509. var logFileStream = new FileStream(logFilePath, FileMode.Create, FileAccess.Write, FileShare.Read,
  510. StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous);
  511. try
  512. {
  513. process.Start();
  514. }
  515. catch (Exception ex)
  516. {
  517. _subtitleExtractionResourcePool.Release();
  518. logFileStream.Dispose();
  519. _logger.ErrorException("Error starting ffmpeg", ex);
  520. throw;
  521. }
  522. var logTask = process.StandardError.BaseStream.CopyToAsync(logFileStream);
  523. var ranToCompletion = process.WaitForExit(60000);
  524. if (!ranToCompletion)
  525. {
  526. try
  527. {
  528. _logger.Info("Killing ffmpeg process");
  529. process.Kill();
  530. process.WaitForExit(1000);
  531. await logTask.ConfigureAwait(false);
  532. }
  533. catch (Win32Exception ex)
  534. {
  535. _logger.ErrorException("Error killing process", ex);
  536. }
  537. catch (InvalidOperationException ex)
  538. {
  539. _logger.ErrorException("Error killing process", ex);
  540. }
  541. catch (NotSupportedException ex)
  542. {
  543. _logger.ErrorException("Error killing process", ex);
  544. }
  545. finally
  546. {
  547. logFileStream.Dispose();
  548. _subtitleExtractionResourcePool.Release();
  549. }
  550. }
  551. var exitCode = ranToCompletion ? process.ExitCode : -1;
  552. process.Dispose();
  553. var failed = false;
  554. if (exitCode == -1)
  555. {
  556. failed = true;
  557. if (File.Exists(outputPath))
  558. {
  559. try
  560. {
  561. _logger.Info("Deleting converted subtitle due to failure: ", outputPath);
  562. File.Delete(outputPath);
  563. }
  564. catch (IOException ex)
  565. {
  566. _logger.ErrorException("Error deleting converted subtitle {0}", ex, outputPath);
  567. }
  568. }
  569. }
  570. else if (!File.Exists(outputPath))
  571. {
  572. failed = true;
  573. }
  574. if (failed)
  575. {
  576. var msg = string.Format("ffmpeg subtitle converted failed for {0}", inputPath);
  577. _logger.Error(msg);
  578. throw new ApplicationException(msg);
  579. }
  580. await SetAssFont(outputPath).ConfigureAwait(false);
  581. }
  582. /// <summary>
  583. /// Gets the subtitle language encoding param.
  584. /// </summary>
  585. /// <param name="language">The language.</param>
  586. /// <returns>System.String.</returns>
  587. private string GetSubtitleLanguageEncodingParam(string language)
  588. {
  589. switch (language.ToLower())
  590. {
  591. case "pol":
  592. case "cze":
  593. case "ces":
  594. case "slo":
  595. case "slk":
  596. case "hun":
  597. case "slv":
  598. case "srp":
  599. case "hrv":
  600. case "rum":
  601. case "ron":
  602. case "rup":
  603. case "alb":
  604. case "sqi":
  605. return "-sub_charenc windows-1250";
  606. case "ara":
  607. return "-sub_charenc windows-1256";
  608. case "heb":
  609. return "-sub_charenc windows-1255";
  610. case "grc":
  611. case "gre":
  612. return "-sub_charenc windows-1253";
  613. case "crh":
  614. case "ota":
  615. case "tur":
  616. return "-sub_charenc windows-1254";
  617. case "rus":
  618. return "-sub_charenc windows-1251";
  619. case "vie":
  620. return "-sub_charenc windows-1258";
  621. default:
  622. return "-sub_charenc windows-1252";
  623. }
  624. }
  625. /// <summary>
  626. /// Extracts the text subtitle.
  627. /// </summary>
  628. /// <param name="inputFiles">The input files.</param>
  629. /// <param name="type">The type.</param>
  630. /// <param name="subtitleStreamIndex">Index of the subtitle stream.</param>
  631. /// <param name="offset">The offset.</param>
  632. /// <param name="outputPath">The output path.</param>
  633. /// <param name="cancellationToken">The cancellation token.</param>
  634. /// <returns>Task.</returns>
  635. /// <exception cref="System.ArgumentException">Must use inputPath list overload</exception>
  636. public Task ExtractTextSubtitle(string[] inputFiles, InputType type, int subtitleStreamIndex, TimeSpan offset, string outputPath, CancellationToken cancellationToken)
  637. {
  638. return ExtractTextSubtitleInternal(GetInputArgument(inputFiles, type), subtitleStreamIndex, offset, outputPath, cancellationToken);
  639. }
  640. /// <summary>
  641. /// Extracts the text subtitle.
  642. /// </summary>
  643. /// <param name="inputPath">The input path.</param>
  644. /// <param name="subtitleStreamIndex">Index of the subtitle stream.</param>
  645. /// <param name="offset">The offset.</param>
  646. /// <param name="outputPath">The output path.</param>
  647. /// <param name="cancellationToken">The cancellation token.</param>
  648. /// <returns>Task.</returns>
  649. /// <exception cref="System.ArgumentNullException">inputPath
  650. /// or
  651. /// outputPath
  652. /// or
  653. /// cancellationToken</exception>
  654. /// <exception cref="System.ApplicationException"></exception>
  655. private async Task ExtractTextSubtitleInternal(string inputPath, int subtitleStreamIndex, TimeSpan offset, string outputPath, CancellationToken cancellationToken)
  656. {
  657. if (string.IsNullOrEmpty(inputPath))
  658. {
  659. throw new ArgumentNullException("inputPath");
  660. }
  661. if (string.IsNullOrEmpty(outputPath))
  662. {
  663. throw new ArgumentNullException("outputPath");
  664. }
  665. if (cancellationToken == null)
  666. {
  667. throw new ArgumentNullException("cancellationToken");
  668. }
  669. var fastSeekSeconds = offset.TotalSeconds >= 1 ? offset.TotalSeconds - 1 : 0;
  670. var slowSeekSeconds = offset.TotalSeconds >= 1 ? 1 : 0;
  671. var fastSeekParam = fastSeekSeconds > 0 ? "-ss " + fastSeekSeconds.ToString(UsCulture) + " " : string.Empty;
  672. var slowSeekParam = slowSeekSeconds > 0 ? " -ss " + slowSeekSeconds.ToString(UsCulture) : string.Empty;
  673. var process = new Process
  674. {
  675. StartInfo = new ProcessStartInfo
  676. {
  677. CreateNoWindow = true,
  678. UseShellExecute = false,
  679. RedirectStandardOutput = false,
  680. RedirectStandardError = true,
  681. FileName = FFMpegPath,
  682. Arguments = string.Format("{0}-i {1}{2} -map 0:{3} -an -vn -c:s ass \"{4}\"", fastSeekParam, inputPath, slowSeekParam, subtitleStreamIndex, outputPath),
  683. WindowStyle = ProcessWindowStyle.Hidden,
  684. ErrorDialog = false
  685. }
  686. };
  687. _logger.Debug("{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments);
  688. await _subtitleExtractionResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
  689. var logFilePath = Path.Combine(_appPaths.LogDirectoryPath, "ffmpeg-sub-extract-" + Guid.NewGuid() + ".txt");
  690. var logFileStream = new FileStream(logFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous);
  691. try
  692. {
  693. process.Start();
  694. }
  695. catch (Exception ex)
  696. {
  697. _subtitleExtractionResourcePool.Release();
  698. logFileStream.Dispose();
  699. _logger.ErrorException("Error starting ffmpeg", ex);
  700. throw;
  701. }
  702. process.StandardError.BaseStream.CopyToAsync(logFileStream);
  703. var ranToCompletion = process.WaitForExit(60000);
  704. if (!ranToCompletion)
  705. {
  706. try
  707. {
  708. _logger.Info("Killing ffmpeg process");
  709. process.Kill();
  710. process.WaitForExit(1000);
  711. }
  712. catch (Win32Exception ex)
  713. {
  714. _logger.ErrorException("Error killing process", ex);
  715. }
  716. catch (InvalidOperationException ex)
  717. {
  718. _logger.ErrorException("Error killing process", ex);
  719. }
  720. catch (NotSupportedException ex)
  721. {
  722. _logger.ErrorException("Error killing process", ex);
  723. }
  724. finally
  725. {
  726. logFileStream.Dispose();
  727. _subtitleExtractionResourcePool.Release();
  728. }
  729. }
  730. var exitCode = ranToCompletion ? process.ExitCode : -1;
  731. process.Dispose();
  732. var failed = false;
  733. if (exitCode == -1)
  734. {
  735. failed = true;
  736. if (File.Exists(outputPath))
  737. {
  738. try
  739. {
  740. _logger.Info("Deleting extracted subtitle due to failure: ", outputPath);
  741. File.Delete(outputPath);
  742. }
  743. catch (IOException ex)
  744. {
  745. _logger.ErrorException("Error deleting extracted subtitle {0}", ex, outputPath);
  746. }
  747. }
  748. }
  749. else if (!File.Exists(outputPath))
  750. {
  751. failed = true;
  752. }
  753. if (failed)
  754. {
  755. var msg = string.Format("ffmpeg subtitle extraction failed for {0}", inputPath);
  756. _logger.Error(msg);
  757. throw new ApplicationException(msg);
  758. }
  759. await SetAssFont(outputPath).ConfigureAwait(false);
  760. }
  761. /// <summary>
  762. /// Sets the ass font.
  763. /// </summary>
  764. /// <param name="file">The file.</param>
  765. /// <returns>Task.</returns>
  766. private async Task SetAssFont(string file)
  767. {
  768. string text;
  769. Encoding encoding;
  770. using (var reader = new StreamReader(file, detectEncodingFromByteOrderMarks: true))
  771. {
  772. encoding = reader.CurrentEncoding;
  773. text = await reader.ReadToEndAsync().ConfigureAwait(false);
  774. }
  775. var newText = text.Replace(",Arial,", ",Arial Unicode MS,");
  776. if (!string.Equals(text, newText))
  777. {
  778. using (var writer = new StreamWriter(file, false, encoding))
  779. {
  780. writer.Write(newText);
  781. }
  782. }
  783. }
  784. /// <summary>
  785. /// Extracts the image.
  786. /// </summary>
  787. /// <param name="inputFiles">The input files.</param>
  788. /// <param name="type">The type.</param>
  789. /// <param name="threedFormat">The threed format.</param>
  790. /// <param name="offset">The offset.</param>
  791. /// <param name="outputPath">The output path.</param>
  792. /// <param name="cancellationToken">The cancellation token.</param>
  793. /// <returns>Task.</returns>
  794. /// <exception cref="System.ArgumentException">Must use inputPath list overload</exception>
  795. public async Task ExtractImage(string[] inputFiles, InputType type, Video3DFormat? threedFormat, TimeSpan? offset, string outputPath, CancellationToken cancellationToken)
  796. {
  797. var resourcePool = type == InputType.AudioFile ? _audioImageResourcePool : _videoImageResourcePool;
  798. var inputArgument = GetInputArgument(inputFiles, type);
  799. if (type != InputType.AudioFile)
  800. {
  801. try
  802. {
  803. await ExtractImageInternal(inputArgument, type, threedFormat, offset, outputPath, true, resourcePool, cancellationToken).ConfigureAwait(false);
  804. return;
  805. }
  806. catch
  807. {
  808. _logger.Error("I-frame image extraction failed, will attempt standard way. Input: {0}", inputArgument);
  809. }
  810. }
  811. await ExtractImageInternal(inputArgument, type, threedFormat, offset, outputPath, false, resourcePool, cancellationToken).ConfigureAwait(false);
  812. }
  813. /// <summary>
  814. /// Extracts the image.
  815. /// </summary>
  816. /// <param name="inputPath">The input path.</param>
  817. /// <param name="type">The type.</param>
  818. /// <param name="threedFormat">The threed format.</param>
  819. /// <param name="offset">The offset.</param>
  820. /// <param name="outputPath">The output path.</param>
  821. /// <param name="useIFrame">if set to <c>true</c> [use I frame].</param>
  822. /// <param name="resourcePool">The resource pool.</param>
  823. /// <param name="cancellationToken">The cancellation token.</param>
  824. /// <returns>Task.</returns>
  825. /// <exception cref="System.ArgumentNullException">inputPath
  826. /// or
  827. /// outputPath</exception>
  828. /// <exception cref="System.ApplicationException"></exception>
  829. private async Task ExtractImageInternal(string inputPath, InputType type, Video3DFormat? threedFormat, TimeSpan? offset, string outputPath, bool useIFrame, SemaphoreSlim resourcePool, CancellationToken cancellationToken)
  830. {
  831. if (string.IsNullOrEmpty(inputPath))
  832. {
  833. throw new ArgumentNullException("inputPath");
  834. }
  835. if (string.IsNullOrEmpty(outputPath))
  836. {
  837. throw new ArgumentNullException("outputPath");
  838. }
  839. var vf = "scale=iw*sar:ih, scale=600:-1";
  840. if (threedFormat.HasValue)
  841. {
  842. switch (threedFormat.Value)
  843. {
  844. case Video3DFormat.HalfSideBySide:
  845. case Video3DFormat.FullSideBySide:
  846. vf = "crop=iw/2:ih:0:0,scale=(iw*2):ih,scale=600:-1";
  847. break;
  848. case Video3DFormat.HalfTopAndBottom:
  849. case Video3DFormat.FullTopAndBottom:
  850. vf = "crop=iw:ih/2:0:0,scale=iw:(ih*2),scale=600:-1";
  851. break;
  852. }
  853. }
  854. var args = useIFrame ? string.Format("-i {0} -threads 0 -v quiet -vframes 1 -filter:v select=\"eq(pict_type\\,I)\" -vf \"{2}\" -f image2 \"{1}\"", inputPath, outputPath, vf) :
  855. string.Format("-i {0} -threads 0 -v quiet -vframes 1 -vf \"{2}\" -f image2 \"{1}\"", inputPath, outputPath, vf);
  856. var probeSize = GetProbeSizeArgument(type);
  857. if (!string.IsNullOrEmpty(probeSize))
  858. {
  859. args = probeSize + " " + args;
  860. }
  861. if (offset.HasValue)
  862. {
  863. args = string.Format("-ss {0} ", Convert.ToInt32(offset.Value.TotalSeconds)).ToString(UsCulture) + args;
  864. }
  865. var process = new Process
  866. {
  867. StartInfo = new ProcessStartInfo
  868. {
  869. CreateNoWindow = true,
  870. UseShellExecute = false,
  871. FileName = FFMpegPath,
  872. Arguments = args,
  873. WindowStyle = ProcessWindowStyle.Hidden,
  874. ErrorDialog = false
  875. }
  876. };
  877. await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
  878. var ranToCompletion = StartAndWaitForProcess(process);
  879. resourcePool.Release();
  880. var exitCode = ranToCompletion ? process.ExitCode : -1;
  881. process.Dispose();
  882. var failed = false;
  883. if (exitCode == -1)
  884. {
  885. failed = true;
  886. if (File.Exists(outputPath))
  887. {
  888. try
  889. {
  890. _logger.Info("Deleting extracted image due to failure: ", outputPath);
  891. File.Delete(outputPath);
  892. }
  893. catch (IOException ex)
  894. {
  895. _logger.ErrorException("Error deleting extracted image {0}", ex, outputPath);
  896. }
  897. }
  898. }
  899. else if (!File.Exists(outputPath))
  900. {
  901. failed = true;
  902. }
  903. if (failed)
  904. {
  905. var msg = string.Format("ffmpeg image extraction failed for {0}", inputPath);
  906. _logger.Error(msg);
  907. throw new ApplicationException(msg);
  908. }
  909. }
  910. /// <summary>
  911. /// Starts the and wait for process.
  912. /// </summary>
  913. /// <param name="process">The process.</param>
  914. /// <param name="timeout">The timeout.</param>
  915. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  916. private bool StartAndWaitForProcess(Process process, int timeout = 10000)
  917. {
  918. process.Start();
  919. var ranToCompletion = process.WaitForExit(timeout);
  920. if (!ranToCompletion)
  921. {
  922. try
  923. {
  924. _logger.Info("Killing ffmpeg process");
  925. process.Kill();
  926. process.WaitForExit(1000);
  927. }
  928. catch (Win32Exception ex)
  929. {
  930. _logger.ErrorException("Error killing process", ex);
  931. }
  932. catch (InvalidOperationException ex)
  933. {
  934. _logger.ErrorException("Error killing process", ex);
  935. }
  936. catch (NotSupportedException ex)
  937. {
  938. _logger.ErrorException("Error killing process", ex);
  939. }
  940. }
  941. return ranToCompletion;
  942. }
  943. /// <summary>
  944. /// Gets the file input argument.
  945. /// </summary>
  946. /// <param name="path">The path.</param>
  947. /// <returns>System.String.</returns>
  948. private string GetFileInputArgument(string path)
  949. {
  950. return string.Format("file:\"{0}\"", path);
  951. }
  952. /// <summary>
  953. /// Gets the concat input argument.
  954. /// </summary>
  955. /// <param name="playableStreamFiles">The playable stream files.</param>
  956. /// <returns>System.String.</returns>
  957. private string GetConcatInputArgument(string[] playableStreamFiles)
  958. {
  959. // Get all streams
  960. // If there's more than one we'll need to use the concat command
  961. if (playableStreamFiles.Length > 1)
  962. {
  963. var files = string.Join("|", playableStreamFiles);
  964. return string.Format("concat:\"{0}\"", files);
  965. }
  966. // Determine the input path for video files
  967. return GetFileInputArgument(playableStreamFiles[0]);
  968. }
  969. /// <summary>
  970. /// Gets the bluray input argument.
  971. /// </summary>
  972. /// <param name="blurayRoot">The bluray root.</param>
  973. /// <returns>System.String.</returns>
  974. private string GetBlurayInputArgument(string blurayRoot)
  975. {
  976. return string.Format("bluray:\"{0}\"", blurayRoot);
  977. }
  978. /// <summary>
  979. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  980. /// </summary>
  981. public void Dispose()
  982. {
  983. Dispose(true);
  984. }
  985. /// <summary>
  986. /// Releases unmanaged and - optionally - managed resources.
  987. /// </summary>
  988. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  989. protected virtual void Dispose(bool dispose)
  990. {
  991. if (dispose)
  992. {
  993. _videoImageResourcePool.Dispose();
  994. }
  995. SetErrorMode(ErrorModes.SYSTEM_DEFAULT);
  996. }
  997. /// <summary>
  998. /// Sets the error mode.
  999. /// </summary>
  1000. /// <param name="uMode">The u mode.</param>
  1001. /// <returns>ErrorModes.</returns>
  1002. [DllImport("kernel32.dll")]
  1003. static extern ErrorModes SetErrorMode(ErrorModes uMode);
  1004. /// <summary>
  1005. /// Enum ErrorModes
  1006. /// </summary>
  1007. [Flags]
  1008. public enum ErrorModes : uint
  1009. {
  1010. /// <summary>
  1011. /// The SYSTE m_ DEFAULT
  1012. /// </summary>
  1013. SYSTEM_DEFAULT = 0x0,
  1014. /// <summary>
  1015. /// The SE m_ FAILCRITICALERRORS
  1016. /// </summary>
  1017. SEM_FAILCRITICALERRORS = 0x0001,
  1018. /// <summary>
  1019. /// The SE m_ NOALIGNMENTFAULTEXCEPT
  1020. /// </summary>
  1021. SEM_NOALIGNMENTFAULTEXCEPT = 0x0004,
  1022. /// <summary>
  1023. /// The SE m_ NOGPFAULTERRORBOX
  1024. /// </summary>
  1025. SEM_NOGPFAULTERRORBOX = 0x0002,
  1026. /// <summary>
  1027. /// The SE m_ NOOPENFILEERRORBOX
  1028. /// </summary>
  1029. SEM_NOOPENFILEERRORBOX = 0x8000
  1030. }
  1031. }
  1032. }