MediaEncoder.cs 44 KB

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