MediaEncoder.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Controller.Channels;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Controller.Library;
  5. using MediaBrowser.Controller.LiveTv;
  6. using MediaBrowser.Controller.MediaEncoding;
  7. using MediaBrowser.Controller.Session;
  8. using MediaBrowser.MediaEncoding.Probing;
  9. using MediaBrowser.Model.Dlna;
  10. using MediaBrowser.Model.Dto;
  11. using MediaBrowser.Model.Entities;
  12. using MediaBrowser.Model.Extensions;
  13. using MediaBrowser.Model.IO;
  14. using MediaBrowser.Model.Logging;
  15. using MediaBrowser.Model.MediaInfo;
  16. using MediaBrowser.Model.Serialization;
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Diagnostics;
  20. using System.Globalization;
  21. using System.IO;
  22. using System.Linq;
  23. using System.Threading;
  24. using System.Threading.Tasks;
  25. namespace MediaBrowser.MediaEncoding.Encoder
  26. {
  27. /// <summary>
  28. /// Class MediaEncoder
  29. /// </summary>
  30. public class MediaEncoder : IMediaEncoder, IDisposable
  31. {
  32. /// <summary>
  33. /// The _logger
  34. /// </summary>
  35. private readonly ILogger _logger;
  36. /// <summary>
  37. /// Gets the json serializer.
  38. /// </summary>
  39. /// <value>The json serializer.</value>
  40. private readonly IJsonSerializer _jsonSerializer;
  41. /// <summary>
  42. /// The _thumbnail resource pool
  43. /// </summary>
  44. private readonly SemaphoreSlim _thumbnailResourcePool = new SemaphoreSlim(1, 1);
  45. /// <summary>
  46. /// The video image resource pool
  47. /// </summary>
  48. private readonly SemaphoreSlim _videoImageResourcePool = new SemaphoreSlim(1, 1);
  49. /// <summary>
  50. /// The audio image resource pool
  51. /// </summary>
  52. private readonly SemaphoreSlim _audioImageResourcePool = new SemaphoreSlim(2, 2);
  53. /// <summary>
  54. /// The FF probe resource pool
  55. /// </summary>
  56. private readonly SemaphoreSlim _ffProbeResourcePool = new SemaphoreSlim(2, 2);
  57. public string FFMpegPath { get; private set; }
  58. public string FFProbePath { get; private set; }
  59. public string Version { get; private set; }
  60. protected readonly IServerConfigurationManager ConfigurationManager;
  61. protected readonly IFileSystem FileSystem;
  62. protected readonly ILiveTvManager LiveTvManager;
  63. protected readonly IIsoManager IsoManager;
  64. protected readonly ILibraryManager LibraryManager;
  65. protected readonly IChannelManager ChannelManager;
  66. protected readonly ISessionManager SessionManager;
  67. protected readonly Func<ISubtitleEncoder> SubtitleEncoder;
  68. protected readonly Func<IMediaSourceManager> MediaSourceManager;
  69. private readonly List<ProcessWrapper> _runningProcesses = new List<ProcessWrapper>();
  70. public MediaEncoder(ILogger logger, IJsonSerializer jsonSerializer, string ffMpegPath, string ffProbePath, string version, IServerConfigurationManager configurationManager, IFileSystem fileSystem, ILiveTvManager liveTvManager, IIsoManager isoManager, ILibraryManager libraryManager, IChannelManager channelManager, ISessionManager sessionManager, Func<ISubtitleEncoder> subtitleEncoder, Func<IMediaSourceManager> mediaSourceManager)
  71. {
  72. _logger = logger;
  73. _jsonSerializer = jsonSerializer;
  74. Version = version;
  75. ConfigurationManager = configurationManager;
  76. FileSystem = fileSystem;
  77. LiveTvManager = liveTvManager;
  78. IsoManager = isoManager;
  79. LibraryManager = libraryManager;
  80. ChannelManager = channelManager;
  81. SessionManager = sessionManager;
  82. SubtitleEncoder = subtitleEncoder;
  83. MediaSourceManager = mediaSourceManager;
  84. FFProbePath = ffProbePath;
  85. FFMpegPath = ffMpegPath;
  86. }
  87. public void SetAvailableEncoders(List<string> list)
  88. {
  89. }
  90. private List<string> _decoders = new List<string>();
  91. public void SetAvailableDecoders(List<string> list)
  92. {
  93. _decoders = list.ToList();
  94. }
  95. public bool SupportsDecoder(string decoder)
  96. {
  97. return _decoders.Contains(decoder, StringComparer.OrdinalIgnoreCase);
  98. }
  99. /// <summary>
  100. /// Gets the encoder path.
  101. /// </summary>
  102. /// <value>The encoder path.</value>
  103. public string EncoderPath
  104. {
  105. get { return FFMpegPath; }
  106. }
  107. /// <summary>
  108. /// Gets the media info.
  109. /// </summary>
  110. /// <param name="request">The request.</param>
  111. /// <param name="cancellationToken">The cancellation token.</param>
  112. /// <returns>Task.</returns>
  113. public Task<Model.MediaInfo.MediaInfo> GetMediaInfo(MediaInfoRequest request, CancellationToken cancellationToken)
  114. {
  115. var extractChapters = request.MediaType == DlnaProfileType.Video && request.ExtractChapters;
  116. var inputFiles = MediaEncoderHelpers.GetInputArgument(FileSystem, request.InputPath, request.Protocol, request.MountedIso, request.PlayableStreamFileNames);
  117. var extractKeyFrameInterval = request.ExtractKeyFrameInterval && request.Protocol == MediaProtocol.File && request.VideoType == VideoType.VideoFile;
  118. return GetMediaInfoInternal(GetInputArgument(inputFiles, request.Protocol), request.InputPath, request.Protocol, extractChapters, extractKeyFrameInterval,
  119. GetProbeSizeArgument(inputFiles, request.Protocol), request.MediaType == DlnaProfileType.Audio, request.VideoType, cancellationToken);
  120. }
  121. /// <summary>
  122. /// Gets the input argument.
  123. /// </summary>
  124. /// <param name="inputFiles">The input files.</param>
  125. /// <param name="protocol">The protocol.</param>
  126. /// <returns>System.String.</returns>
  127. /// <exception cref="System.ArgumentException">Unrecognized InputType</exception>
  128. public string GetInputArgument(string[] inputFiles, MediaProtocol protocol)
  129. {
  130. return EncodingUtils.GetInputArgument(inputFiles.ToList(), protocol);
  131. }
  132. /// <summary>
  133. /// Gets the probe size argument.
  134. /// </summary>
  135. /// <param name="inputFiles">The input files.</param>
  136. /// <param name="protocol">The protocol.</param>
  137. /// <returns>System.String.</returns>
  138. public string GetProbeSizeArgument(string[] inputFiles, MediaProtocol protocol)
  139. {
  140. return EncodingUtils.GetProbeSizeArgument(inputFiles.Length > 1);
  141. }
  142. /// <summary>
  143. /// Gets the media info internal.
  144. /// </summary>
  145. /// <param name="inputPath">The input path.</param>
  146. /// <param name="primaryPath">The primary path.</param>
  147. /// <param name="protocol">The protocol.</param>
  148. /// <param name="extractChapters">if set to <c>true</c> [extract chapters].</param>
  149. /// <param name="extractKeyFrameInterval">if set to <c>true</c> [extract key frame interval].</param>
  150. /// <param name="probeSizeArgument">The probe size argument.</param>
  151. /// <param name="isAudio">if set to <c>true</c> [is audio].</param>
  152. /// <param name="videoType">Type of the video.</param>
  153. /// <param name="cancellationToken">The cancellation token.</param>
  154. /// <returns>Task{MediaInfoResult}.</returns>
  155. /// <exception cref="System.ApplicationException"></exception>
  156. private async Task<Model.MediaInfo.MediaInfo> GetMediaInfoInternal(string inputPath,
  157. string primaryPath,
  158. MediaProtocol protocol,
  159. bool extractChapters,
  160. bool extractKeyFrameInterval,
  161. string probeSizeArgument,
  162. bool isAudio,
  163. VideoType videoType,
  164. CancellationToken cancellationToken)
  165. {
  166. var args = extractChapters
  167. ? "{0} -i {1} -threads 0 -v info -print_format json -show_streams -show_chapters -show_format"
  168. : "{0} -i {1} -threads 0 -v info -print_format json -show_streams -show_format";
  169. var process = new Process
  170. {
  171. StartInfo = new ProcessStartInfo
  172. {
  173. CreateNoWindow = true,
  174. UseShellExecute = false,
  175. // Must consume both or ffmpeg may hang due to deadlocks. See comments below.
  176. RedirectStandardOutput = true,
  177. RedirectStandardError = true,
  178. RedirectStandardInput = true,
  179. FileName = FFProbePath,
  180. Arguments = string.Format(args,
  181. probeSizeArgument, inputPath).Trim(),
  182. WindowStyle = ProcessWindowStyle.Hidden,
  183. ErrorDialog = false
  184. },
  185. EnableRaisingEvents = true
  186. };
  187. _logger.Debug("{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments);
  188. await _ffProbeResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
  189. using (var processWrapper = new ProcessWrapper(process, this, _logger))
  190. {
  191. try
  192. {
  193. StartProcess(processWrapper);
  194. }
  195. catch (Exception ex)
  196. {
  197. _ffProbeResourcePool.Release();
  198. _logger.ErrorException("Error starting ffprobe", ex);
  199. throw;
  200. }
  201. try
  202. {
  203. process.BeginErrorReadLine();
  204. var result = _jsonSerializer.DeserializeFromStream<InternalMediaInfoResult>(process.StandardOutput.BaseStream);
  205. if (result != null)
  206. {
  207. if (result.streams != null)
  208. {
  209. // Normalize aspect ratio if invalid
  210. foreach (var stream in result.streams)
  211. {
  212. if (string.Equals(stream.display_aspect_ratio, "0:1", StringComparison.OrdinalIgnoreCase))
  213. {
  214. stream.display_aspect_ratio = string.Empty;
  215. }
  216. if (string.Equals(stream.sample_aspect_ratio, "0:1", StringComparison.OrdinalIgnoreCase))
  217. {
  218. stream.sample_aspect_ratio = string.Empty;
  219. }
  220. }
  221. }
  222. var mediaInfo = new ProbeResultNormalizer(_logger, FileSystem).GetMediaInfo(result, videoType, isAudio, primaryPath, protocol);
  223. if (extractKeyFrameInterval && mediaInfo.RunTimeTicks.HasValue)
  224. {
  225. if (ConfigurationManager.Configuration.EnableVideoFrameByFrameAnalysis && mediaInfo.Size.HasValue)
  226. {
  227. foreach (var stream in mediaInfo.MediaStreams)
  228. {
  229. if (EnableKeyframeExtraction(mediaInfo, stream))
  230. {
  231. try
  232. {
  233. stream.KeyFrames = await GetKeyFrames(inputPath, stream.Index, cancellationToken).ConfigureAwait(false);
  234. }
  235. catch (OperationCanceledException)
  236. {
  237. }
  238. catch (Exception ex)
  239. {
  240. _logger.ErrorException("Error getting key frame interval", ex);
  241. }
  242. }
  243. }
  244. }
  245. }
  246. if (!process.WaitForExit(5000))
  247. {
  248. StopProcess(processWrapper, 100, true);
  249. }
  250. return mediaInfo;
  251. }
  252. }
  253. catch
  254. {
  255. StopProcess(processWrapper, 100, true);
  256. throw;
  257. }
  258. finally
  259. {
  260. _ffProbeResourcePool.Release();
  261. }
  262. }
  263. throw new ApplicationException(string.Format("FFProbe failed for {0}", inputPath));
  264. }
  265. private bool EnableKeyframeExtraction(MediaSourceInfo mediaSource, MediaStream videoStream)
  266. {
  267. if (videoStream.Type == MediaStreamType.Video && string.Equals(videoStream.Codec, "h264", StringComparison.OrdinalIgnoreCase) &&
  268. !videoStream.IsInterlaced &&
  269. !(videoStream.IsAnamorphic ?? false))
  270. {
  271. var audioStreams = mediaSource.MediaStreams.Where(i => i.Type == MediaStreamType.Audio).ToList();
  272. // If it has aac audio then it will probably direct stream anyway, so don't bother with this
  273. if (audioStreams.Count == 1 && string.Equals(audioStreams[0].Codec, "aac", StringComparison.OrdinalIgnoreCase))
  274. {
  275. return false;
  276. }
  277. return true;
  278. }
  279. return false;
  280. }
  281. private async Task<List<int>> GetKeyFrames(string inputPath, int videoStreamIndex, CancellationToken cancellationToken)
  282. {
  283. inputPath = inputPath.Split(new[] { ':' }, 2).Last().Trim('"');
  284. const string args = "-show_packets -print_format compact -select_streams v:{1} -show_entries packet=flags -show_entries packet=pts_time \"{0}\"";
  285. var process = new Process
  286. {
  287. StartInfo = new ProcessStartInfo
  288. {
  289. CreateNoWindow = true,
  290. UseShellExecute = false,
  291. // Must consume both or ffmpeg may hang due to deadlocks. See comments below.
  292. RedirectStandardOutput = true,
  293. RedirectStandardError = true,
  294. FileName = FFProbePath,
  295. Arguments = string.Format(args, inputPath, videoStreamIndex.ToString(CultureInfo.InvariantCulture)).Trim(),
  296. WindowStyle = ProcessWindowStyle.Hidden,
  297. ErrorDialog = false
  298. },
  299. EnableRaisingEvents = true
  300. };
  301. _logger.Info("{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments);
  302. using (process)
  303. {
  304. var start = DateTime.UtcNow;
  305. process.Start();
  306. var lines = new List<int>();
  307. try
  308. {
  309. process.BeginErrorReadLine();
  310. await StartReadingOutput(process.StandardOutput.BaseStream, lines, cancellationToken).ConfigureAwait(false);
  311. }
  312. catch (OperationCanceledException)
  313. {
  314. if (cancellationToken.IsCancellationRequested)
  315. {
  316. throw;
  317. }
  318. }
  319. process.WaitForExit();
  320. _logger.Info("Keyframe extraction took {0} seconds", (DateTime.UtcNow - start).TotalSeconds);
  321. //_logger.Debug("Found keyframes {0}", string.Join(",", lines.ToArray()));
  322. return lines;
  323. }
  324. }
  325. private async Task StartReadingOutput(Stream source, List<int> keyframes, CancellationToken cancellationToken)
  326. {
  327. try
  328. {
  329. using (var reader = new StreamReader(source))
  330. {
  331. var text = await reader.ReadToEndAsync().ConfigureAwait(false);
  332. var lines = StringHelper.RegexSplit(text, "[\r\n]+");
  333. foreach (var line in lines)
  334. {
  335. if (string.IsNullOrWhiteSpace(line))
  336. {
  337. continue;
  338. }
  339. var values = line.Split('|')
  340. .Where(i => !string.IsNullOrWhiteSpace(i))
  341. .Select(i => i.Split('='))
  342. .Where(i => i.Length == 2)
  343. .ToDictionary(i => i[0], i => i[1]);
  344. string flags;
  345. if (values.TryGetValue("flags", out flags) && string.Equals(flags, "k", StringComparison.OrdinalIgnoreCase))
  346. {
  347. string pts_time;
  348. double frameSeconds;
  349. if (values.TryGetValue("pts_time", out pts_time) && double.TryParse(pts_time, NumberStyles.Any, CultureInfo.InvariantCulture, out frameSeconds))
  350. {
  351. var ms = frameSeconds * 1000;
  352. keyframes.Add(Convert.ToInt32(ms));
  353. }
  354. }
  355. }
  356. }
  357. }
  358. catch (OperationCanceledException)
  359. {
  360. throw;
  361. }
  362. catch (Exception ex)
  363. {
  364. _logger.ErrorException("Error reading ffprobe output", ex);
  365. }
  366. }
  367. /// <summary>
  368. /// The us culture
  369. /// </summary>
  370. protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
  371. public Task<Stream> ExtractAudioImage(string path, CancellationToken cancellationToken)
  372. {
  373. return ExtractImage(new[] { path }, MediaProtocol.File, true, null, null, cancellationToken);
  374. }
  375. public Task<Stream> ExtractVideoImage(string[] inputFiles, MediaProtocol protocol, Video3DFormat? threedFormat,
  376. TimeSpan? offset, CancellationToken cancellationToken)
  377. {
  378. return ExtractImage(inputFiles, protocol, false, threedFormat, offset, cancellationToken);
  379. }
  380. private async Task<Stream> ExtractImage(string[] inputFiles, MediaProtocol protocol, bool isAudio,
  381. Video3DFormat? threedFormat, TimeSpan? offset, CancellationToken cancellationToken)
  382. {
  383. var resourcePool = isAudio ? _audioImageResourcePool : _videoImageResourcePool;
  384. var inputArgument = GetInputArgument(inputFiles, protocol);
  385. if (!isAudio)
  386. {
  387. try
  388. {
  389. return await ExtractImageInternal(inputArgument, protocol, threedFormat, offset, true, resourcePool, cancellationToken).ConfigureAwait(false);
  390. }
  391. catch (ArgumentException)
  392. {
  393. throw;
  394. }
  395. catch
  396. {
  397. _logger.Error("I-frame image extraction failed, will attempt standard way. Input: {0}", inputArgument);
  398. }
  399. }
  400. return await ExtractImageInternal(inputArgument, protocol, threedFormat, offset, false, resourcePool, cancellationToken).ConfigureAwait(false);
  401. }
  402. private async Task<Stream> ExtractImageInternal(string inputPath, MediaProtocol protocol, Video3DFormat? threedFormat, TimeSpan? offset, bool useIFrame, SemaphoreSlim resourcePool, CancellationToken cancellationToken)
  403. {
  404. if (string.IsNullOrEmpty(inputPath))
  405. {
  406. throw new ArgumentNullException("inputPath");
  407. }
  408. // 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.
  409. // This filter chain may have adverse effects on recorded tv thumbnails if ar changes during presentation ex. commercials @ diff ar
  410. var vf = "scale=600:trunc(600/dar/2)*2";
  411. if (threedFormat.HasValue)
  412. {
  413. switch (threedFormat.Value)
  414. {
  415. case Video3DFormat.HalfSideBySide:
  416. 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,setsar=sar=1,scale=600:trunc(600/dar/2)*2";
  417. // 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.
  418. break;
  419. case Video3DFormat.FullSideBySide:
  420. 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,setsar=sar=1,scale=600:trunc(600/dar/2)*2";
  421. //fsbs crop width in half,set the display aspect,crop out any black bars we may have made the scale width to 600.
  422. break;
  423. case Video3DFormat.HalfTopAndBottom:
  424. 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,setsar=sar=1,scale=600:trunc(600/dar/2)*2";
  425. //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
  426. break;
  427. case Video3DFormat.FullTopAndBottom:
  428. 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,setsar=sar=1,scale=600:trunc(600/dar/2)*2";
  429. // ftab crop heigt in half, set the display aspect,crop out any black bars we may have made the scale width to 600
  430. break;
  431. }
  432. }
  433. // 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 just in case.
  434. var args = useIFrame ? string.Format("-i {0} -threads 1 -v quiet -vframes 1 -vf \"{2},thumbnail=30\" -f image2 \"{1}\"", inputPath, "-", vf) :
  435. string.Format("-i {0} -threads 1 -v quiet -vframes 1 -vf \"{2}\" -f image2 \"{1}\"", inputPath, "-", vf);
  436. var probeSize = GetProbeSizeArgument(new[] { inputPath }, protocol);
  437. if (!string.IsNullOrEmpty(probeSize))
  438. {
  439. args = probeSize + " " + args;
  440. }
  441. if (offset.HasValue)
  442. {
  443. args = string.Format("-ss {0} ", GetTimeParameter(offset.Value)) + args;
  444. }
  445. var process = new Process
  446. {
  447. StartInfo = new ProcessStartInfo
  448. {
  449. CreateNoWindow = true,
  450. UseShellExecute = false,
  451. FileName = FFMpegPath,
  452. Arguments = args,
  453. WindowStyle = ProcessWindowStyle.Hidden,
  454. ErrorDialog = false,
  455. RedirectStandardOutput = true,
  456. RedirectStandardError = true,
  457. RedirectStandardInput = true
  458. }
  459. };
  460. _logger.Debug("{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments);
  461. await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
  462. using (var processWrapper = new ProcessWrapper(process, this, _logger))
  463. {
  464. bool ranToCompletion;
  465. var memoryStream = new MemoryStream();
  466. try
  467. {
  468. StartProcess(processWrapper);
  469. #pragma warning disable 4014
  470. // Important - don't await the log task or we won't be able to kill ffmpeg when the user stops playback
  471. process.StandardOutput.BaseStream.CopyToAsync(memoryStream);
  472. #pragma warning restore 4014
  473. // MUST read both stdout and stderr asynchronously or a deadlock may occurr
  474. process.BeginErrorReadLine();
  475. ranToCompletion = process.WaitForExit(10000);
  476. if (!ranToCompletion)
  477. {
  478. StopProcess(processWrapper, 1000, false);
  479. }
  480. }
  481. finally
  482. {
  483. resourcePool.Release();
  484. }
  485. var exitCode = ranToCompletion ? processWrapper.ExitCode ?? 0 : -1;
  486. if (exitCode == -1 || memoryStream.Length == 0)
  487. {
  488. memoryStream.Dispose();
  489. var msg = string.Format("ffmpeg image extraction failed for {0}", inputPath);
  490. _logger.Error(msg);
  491. throw new ApplicationException(msg);
  492. }
  493. memoryStream.Position = 0;
  494. return memoryStream;
  495. }
  496. }
  497. public string GetTimeParameter(long ticks)
  498. {
  499. var time = TimeSpan.FromTicks(ticks);
  500. return GetTimeParameter(time);
  501. }
  502. public string GetTimeParameter(TimeSpan time)
  503. {
  504. return time.ToString(@"hh\:mm\:ss\.fff", UsCulture);
  505. }
  506. public async Task ExtractVideoImagesOnInterval(string[] inputFiles,
  507. MediaProtocol protocol,
  508. Video3DFormat? threedFormat,
  509. TimeSpan interval,
  510. string targetDirectory,
  511. string filenamePrefix,
  512. int? maxWidth,
  513. CancellationToken cancellationToken)
  514. {
  515. var resourcePool = _thumbnailResourcePool;
  516. var inputArgument = GetInputArgument(inputFiles, protocol);
  517. var vf = "fps=fps=1/" + interval.TotalSeconds.ToString(UsCulture);
  518. if (maxWidth.HasValue)
  519. {
  520. var maxWidthParam = maxWidth.Value.ToString(UsCulture);
  521. vf += string.Format(",scale=min(iw\\,{0}):trunc(ow/dar/2)*2", maxWidthParam);
  522. }
  523. FileSystem.CreateDirectory(targetDirectory);
  524. var outputPath = Path.Combine(targetDirectory, filenamePrefix + "%05d.jpg");
  525. var args = string.Format("-i {0} -threads 1 -v quiet -vf \"{2}\" -f image2 \"{1}\"", inputArgument, outputPath, vf);
  526. var probeSize = GetProbeSizeArgument(new[] { inputArgument }, protocol);
  527. if (!string.IsNullOrEmpty(probeSize))
  528. {
  529. args = probeSize + " " + args;
  530. }
  531. var process = new Process
  532. {
  533. StartInfo = new ProcessStartInfo
  534. {
  535. CreateNoWindow = true,
  536. UseShellExecute = false,
  537. FileName = FFMpegPath,
  538. Arguments = args,
  539. WindowStyle = ProcessWindowStyle.Hidden,
  540. ErrorDialog = false,
  541. RedirectStandardInput = true
  542. }
  543. };
  544. _logger.Info(process.StartInfo.FileName + " " + process.StartInfo.Arguments);
  545. await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
  546. bool ranToCompletion = false;
  547. using (var processWrapper = new ProcessWrapper(process, this, _logger))
  548. {
  549. try
  550. {
  551. StartProcess(processWrapper);
  552. // Need to give ffmpeg enough time to make all the thumbnails, which could be a while,
  553. // but we still need to detect if the process hangs.
  554. // Making the assumption that as long as new jpegs are showing up, everything is good.
  555. bool isResponsive = true;
  556. int lastCount = 0;
  557. while (isResponsive)
  558. {
  559. if (process.WaitForExit(30000))
  560. {
  561. ranToCompletion = true;
  562. break;
  563. }
  564. cancellationToken.ThrowIfCancellationRequested();
  565. var jpegCount = Directory.GetFiles(targetDirectory)
  566. .Count(i => string.Equals(Path.GetExtension(i), ".jpg", StringComparison.OrdinalIgnoreCase));
  567. isResponsive = (jpegCount > lastCount);
  568. lastCount = jpegCount;
  569. }
  570. if (!ranToCompletion)
  571. {
  572. StopProcess(processWrapper, 1000, false);
  573. }
  574. }
  575. finally
  576. {
  577. resourcePool.Release();
  578. }
  579. var exitCode = ranToCompletion ? processWrapper.ExitCode ?? 0 : -1;
  580. if (exitCode == -1)
  581. {
  582. var msg = string.Format("ffmpeg image extraction failed for {0}", inputArgument);
  583. _logger.Error(msg);
  584. throw new ApplicationException(msg);
  585. }
  586. }
  587. }
  588. public async Task<string> EncodeAudio(EncodingJobOptions options,
  589. IProgress<double> progress,
  590. CancellationToken cancellationToken)
  591. {
  592. var job = await new AudioEncoder(this,
  593. _logger,
  594. ConfigurationManager,
  595. FileSystem,
  596. IsoManager,
  597. LibraryManager,
  598. SessionManager,
  599. SubtitleEncoder(),
  600. MediaSourceManager())
  601. .Start(options, progress, cancellationToken).ConfigureAwait(false);
  602. await job.TaskCompletionSource.Task.ConfigureAwait(false);
  603. return job.OutputFilePath;
  604. }
  605. public async Task<string> EncodeVideo(EncodingJobOptions options,
  606. IProgress<double> progress,
  607. CancellationToken cancellationToken)
  608. {
  609. var job = await new VideoEncoder(this,
  610. _logger,
  611. ConfigurationManager,
  612. FileSystem,
  613. IsoManager,
  614. LibraryManager,
  615. SessionManager,
  616. SubtitleEncoder(),
  617. MediaSourceManager())
  618. .Start(options, progress, cancellationToken).ConfigureAwait(false);
  619. await job.TaskCompletionSource.Task.ConfigureAwait(false);
  620. return job.OutputFilePath;
  621. }
  622. private void StartProcess(ProcessWrapper process)
  623. {
  624. process.Process.Start();
  625. lock (_runningProcesses)
  626. {
  627. _runningProcesses.Add(process);
  628. }
  629. }
  630. private void StopProcess(ProcessWrapper process, int waitTimeMs, bool enableForceKill)
  631. {
  632. try
  633. {
  634. _logger.Info("Killing ffmpeg process");
  635. try
  636. {
  637. process.Process.StandardInput.WriteLine("q");
  638. }
  639. catch (Exception)
  640. {
  641. _logger.Error("Error sending q command to process");
  642. }
  643. try
  644. {
  645. if (process.Process.WaitForExit(waitTimeMs))
  646. {
  647. return;
  648. }
  649. }
  650. catch (Exception ex)
  651. {
  652. _logger.Error("Error in WaitForExit", ex);
  653. }
  654. if (enableForceKill)
  655. {
  656. process.Process.Kill();
  657. }
  658. }
  659. catch (Exception ex)
  660. {
  661. _logger.ErrorException("Error killing process", ex);
  662. }
  663. }
  664. private void StopProcesses()
  665. {
  666. List<ProcessWrapper> proceses;
  667. lock (_runningProcesses)
  668. {
  669. proceses = _runningProcesses.ToList();
  670. }
  671. _runningProcesses.Clear();
  672. foreach (var process in proceses)
  673. {
  674. if (!process.HasExited)
  675. {
  676. StopProcess(process, 500, true);
  677. }
  678. }
  679. }
  680. public string EscapeSubtitleFilterPath(string path)
  681. {
  682. return path.Replace('\\', '/').Replace(":/", "\\:/").Replace("'", "'\\\\\\''");
  683. }
  684. /// <summary>
  685. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  686. /// </summary>
  687. public void Dispose()
  688. {
  689. Dispose(true);
  690. }
  691. /// <summary>
  692. /// Releases unmanaged and - optionally - managed resources.
  693. /// </summary>
  694. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  695. protected virtual void Dispose(bool dispose)
  696. {
  697. if (dispose)
  698. {
  699. _videoImageResourcePool.Dispose();
  700. StopProcesses();
  701. }
  702. }
  703. private class ProcessWrapper : IDisposable
  704. {
  705. public readonly Process Process;
  706. public bool HasExited;
  707. public int? ExitCode;
  708. private readonly MediaEncoder _mediaEncoder;
  709. private readonly ILogger _logger;
  710. public ProcessWrapper(Process process, MediaEncoder mediaEncoder, ILogger logger)
  711. {
  712. Process = process;
  713. _mediaEncoder = mediaEncoder;
  714. _logger = logger;
  715. Process.Exited += Process_Exited;
  716. }
  717. void Process_Exited(object sender, EventArgs e)
  718. {
  719. var process = (Process)sender;
  720. HasExited = true;
  721. try
  722. {
  723. ExitCode = process.ExitCode;
  724. }
  725. catch (Exception ex)
  726. {
  727. }
  728. lock (_mediaEncoder._runningProcesses)
  729. {
  730. _mediaEncoder._runningProcesses.Remove(this);
  731. }
  732. process.Dispose();
  733. }
  734. private bool _disposed;
  735. private readonly object _syncLock = new object();
  736. public void Dispose()
  737. {
  738. lock (_syncLock)
  739. {
  740. if (!_disposed)
  741. {
  742. if (Process != null)
  743. {
  744. Process.Exited -= Process_Exited;
  745. Process.Dispose();
  746. }
  747. }
  748. _disposed = true;
  749. }
  750. }
  751. }
  752. }
  753. }