MediaEncoder.cs 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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 = "scale=iw*sar:ih, scale=600:-1";
  733. if (threedFormat.HasValue)
  734. {
  735. switch (threedFormat.Value)
  736. {
  737. case Video3DFormat.HalfSideBySide:
  738. case Video3DFormat.FullSideBySide:
  739. vf = "crop=iw/2:ih:0:0,scale=(iw*2):ih,scale=600:-1";
  740. break;
  741. case Video3DFormat.HalfTopAndBottom:
  742. case Video3DFormat.FullTopAndBottom:
  743. vf = "crop=iw:ih/2:0:0,scale=iw:(ih*2),scale=600:-1";
  744. break;
  745. }
  746. }
  747. 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) :
  748. string.Format("-i {0} -threads 0 -v quiet -vframes 1 -vf \"{2}\" -f image2 \"{1}\"", inputPath, outputPath, vf);
  749. var probeSize = GetProbeSizeArgument(type);
  750. if (!string.IsNullOrEmpty(probeSize))
  751. {
  752. args = probeSize + " " + args;
  753. }
  754. if (offset.HasValue)
  755. {
  756. args = string.Format("-ss {0} ", Convert.ToInt32(offset.Value.TotalSeconds)).ToString(UsCulture) + args;
  757. }
  758. var process = new Process
  759. {
  760. StartInfo = new ProcessStartInfo
  761. {
  762. CreateNoWindow = true,
  763. UseShellExecute = false,
  764. FileName = FFMpegPath,
  765. Arguments = args,
  766. WindowStyle = ProcessWindowStyle.Hidden,
  767. ErrorDialog = false
  768. }
  769. };
  770. await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
  771. var ranToCompletion = StartAndWaitForProcess(process);
  772. resourcePool.Release();
  773. var exitCode = ranToCompletion ? process.ExitCode : -1;
  774. process.Dispose();
  775. var failed = false;
  776. if (exitCode == -1)
  777. {
  778. failed = true;
  779. if (File.Exists(outputPath))
  780. {
  781. try
  782. {
  783. _logger.Info("Deleting extracted image due to failure: ", outputPath);
  784. File.Delete(outputPath);
  785. }
  786. catch (IOException ex)
  787. {
  788. _logger.ErrorException("Error deleting extracted image {0}", ex, outputPath);
  789. }
  790. }
  791. }
  792. else if (!File.Exists(outputPath))
  793. {
  794. failed = true;
  795. }
  796. if (failed)
  797. {
  798. var msg = string.Format("ffmpeg image extraction failed for {0}", inputPath);
  799. _logger.Error(msg);
  800. throw new ApplicationException(msg);
  801. }
  802. }
  803. /// <summary>
  804. /// Starts the and wait for process.
  805. /// </summary>
  806. /// <param name="process">The process.</param>
  807. /// <param name="timeout">The timeout.</param>
  808. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  809. private bool StartAndWaitForProcess(Process process, int timeout = 10000)
  810. {
  811. process.Start();
  812. var ranToCompletion = process.WaitForExit(timeout);
  813. if (!ranToCompletion)
  814. {
  815. try
  816. {
  817. _logger.Info("Killing ffmpeg process");
  818. process.Kill();
  819. process.WaitForExit(1000);
  820. }
  821. catch (Win32Exception ex)
  822. {
  823. _logger.ErrorException("Error killing process", ex);
  824. }
  825. catch (InvalidOperationException ex)
  826. {
  827. _logger.ErrorException("Error killing process", ex);
  828. }
  829. catch (NotSupportedException ex)
  830. {
  831. _logger.ErrorException("Error killing process", ex);
  832. }
  833. }
  834. return ranToCompletion;
  835. }
  836. /// <summary>
  837. /// Gets the file input argument.
  838. /// </summary>
  839. /// <param name="path">The path.</param>
  840. /// <returns>System.String.</returns>
  841. private string GetFileInputArgument(string path)
  842. {
  843. return string.Format("file:\"{0}\"", path);
  844. }
  845. /// <summary>
  846. /// Gets the concat input argument.
  847. /// </summary>
  848. /// <param name="playableStreamFiles">The playable stream files.</param>
  849. /// <returns>System.String.</returns>
  850. private string GetConcatInputArgument(string[] playableStreamFiles)
  851. {
  852. // Get all streams
  853. // If there's more than one we'll need to use the concat command
  854. if (playableStreamFiles.Length > 1)
  855. {
  856. var files = string.Join("|", playableStreamFiles);
  857. return string.Format("concat:\"{0}\"", files);
  858. }
  859. // Determine the input path for video files
  860. return GetFileInputArgument(playableStreamFiles[0]);
  861. }
  862. /// <summary>
  863. /// Gets the bluray input argument.
  864. /// </summary>
  865. /// <param name="blurayRoot">The bluray root.</param>
  866. /// <returns>System.String.</returns>
  867. private string GetBlurayInputArgument(string blurayRoot)
  868. {
  869. return string.Format("bluray:\"{0}\"", blurayRoot);
  870. }
  871. /// <summary>
  872. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  873. /// </summary>
  874. public void Dispose()
  875. {
  876. Dispose(true);
  877. }
  878. /// <summary>
  879. /// Releases unmanaged and - optionally - managed resources.
  880. /// </summary>
  881. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  882. protected virtual void Dispose(bool dispose)
  883. {
  884. if (dispose)
  885. {
  886. _videoImageResourcePool.Dispose();
  887. }
  888. }
  889. }
  890. }