MediaEncoder.cs 37 KB

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