2
0

MediaEncoder.cs 38 KB

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