MediaEncoder.cs 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text.Json;
  7. using System.Text.RegularExpressions;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using MediaBrowser.Common.Configuration;
  11. using MediaBrowser.Common.Extensions;
  12. using MediaBrowser.Controller.Configuration;
  13. using MediaBrowser.Controller.MediaEncoding;
  14. using MediaBrowser.MediaEncoding.Probing;
  15. using MediaBrowser.Model.Configuration;
  16. using MediaBrowser.Model.Dlna;
  17. using MediaBrowser.Model.Entities;
  18. using MediaBrowser.Model.Globalization;
  19. using MediaBrowser.Model.IO;
  20. using MediaBrowser.Model.MediaInfo;
  21. using MediaBrowser.Model.System;
  22. using Microsoft.Extensions.Logging;
  23. using Microsoft.Extensions.Configuration;
  24. using System.Diagnostics;
  25. namespace MediaBrowser.MediaEncoding.Encoder
  26. {
  27. /// <summary>
  28. /// Class MediaEncoder
  29. /// </summary>
  30. public class MediaEncoder : IMediaEncoder, IDisposable
  31. {
  32. /// <summary>
  33. /// The default image extraction timeout in milliseconds.
  34. /// </summary>
  35. internal const int DefaultImageExtractionTimeout = 5000;
  36. private readonly ILogger _logger;
  37. private readonly IServerConfigurationManager _configurationManager;
  38. private readonly IFileSystem _fileSystem;
  39. private readonly ILocalizationManager _localization;
  40. private readonly Func<ISubtitleEncoder> _subtitleEncoder;
  41. private readonly IConfiguration _configuration;
  42. private readonly string _startupOptionFFmpegPath;
  43. private readonly SemaphoreSlim _thumbnailResourcePool = new SemaphoreSlim(2, 2);
  44. private readonly object _runningProcessesLock = new object();
  45. private readonly List<ProcessWrapper> _runningProcesses = new List<ProcessWrapper>();
  46. private EncodingHelper _encodingHelper;
  47. private string _ffmpegPath;
  48. private string _ffprobePath;
  49. public MediaEncoder(
  50. ILogger<MediaEncoder> logger,
  51. IServerConfigurationManager configurationManager,
  52. IFileSystem fileSystem,
  53. ILocalizationManager localization,
  54. Func<ISubtitleEncoder> subtitleEncoder,
  55. IConfiguration configuration,
  56. string startupOptionsFFmpegPath)
  57. {
  58. _logger = logger;
  59. _configurationManager = configurationManager;
  60. _fileSystem = fileSystem;
  61. _localization = localization;
  62. _startupOptionFFmpegPath = startupOptionsFFmpegPath;
  63. _subtitleEncoder = subtitleEncoder;
  64. _configuration = configuration;
  65. }
  66. private EncodingHelper EncodingHelper
  67. => LazyInitializer.EnsureInitialized(
  68. ref _encodingHelper,
  69. () => new EncodingHelper(this, _fileSystem, _subtitleEncoder(), _configuration));
  70. /// <inheritdoc />
  71. public string EncoderPath => _ffmpegPath;
  72. /// <inheritdoc />
  73. public FFmpegLocation EncoderLocation { get; private set; }
  74. /// <summary>
  75. /// Run at startup or if the user removes a Custom path from transcode page.
  76. /// Sets global variables FFmpegPath.
  77. /// Precedence is: Config > CLI > $PATH
  78. /// </summary>
  79. public void SetFFmpegPath()
  80. {
  81. // 1) Custom path stored in config/encoding xml file under tag <EncoderAppPath> takes precedence
  82. if (!ValidatePath(_configurationManager.GetConfiguration<EncodingOptions>("encoding").EncoderAppPath, FFmpegLocation.Custom))
  83. {
  84. // 2) Check if the --ffmpeg CLI switch has been given
  85. if (!ValidatePath(_startupOptionFFmpegPath, FFmpegLocation.SetByArgument))
  86. {
  87. // 3) Search system $PATH environment variable for valid FFmpeg
  88. if (!ValidatePath(ExistsOnSystemPath("ffmpeg"), FFmpegLocation.System))
  89. {
  90. EncoderLocation = FFmpegLocation.NotFound;
  91. _ffmpegPath = null;
  92. }
  93. }
  94. }
  95. // Write the FFmpeg path to the config/encoding.xml file as <EncoderAppPathDisplay> so it appears in UI
  96. var config = _configurationManager.GetConfiguration<EncodingOptions>("encoding");
  97. config.EncoderAppPathDisplay = _ffmpegPath ?? string.Empty;
  98. _configurationManager.SaveConfiguration("encoding", config);
  99. // Only if mpeg path is set, try and set path to probe
  100. if (_ffmpegPath != null)
  101. {
  102. // Determine a probe path from the mpeg path
  103. _ffprobePath = Regex.Replace(_ffmpegPath, @"[^\/\\]+?(\.[^\/\\\n.]+)?$", @"ffprobe$1");
  104. // Interrogate to understand what coders are supported
  105. var validator = new EncoderValidator(_logger, _ffmpegPath);
  106. SetAvailableDecoders(validator.GetDecoders());
  107. SetAvailableEncoders(validator.GetEncoders());
  108. }
  109. _logger.LogInformation("FFmpeg: {0}: {1}", EncoderLocation, _ffmpegPath ?? string.Empty);
  110. }
  111. /// <summary>
  112. /// Triggered from the Settings > Transcoding UI page when users submits Custom FFmpeg path to use.
  113. /// Only write the new path to xml if it exists. Do not perform validation checks on ffmpeg here.
  114. /// </summary>
  115. /// <param name="path"></param>
  116. /// <param name="pathType"></param>
  117. public void UpdateEncoderPath(string path, string pathType)
  118. {
  119. string newPath;
  120. _logger.LogInformation("Attempting to update encoder path to {0}. pathType: {1}", path ?? string.Empty, pathType ?? string.Empty);
  121. if (!string.Equals(pathType, "custom", StringComparison.OrdinalIgnoreCase))
  122. {
  123. throw new ArgumentException("Unexpected pathType value");
  124. }
  125. else if (string.IsNullOrWhiteSpace(path))
  126. {
  127. // User had cleared the custom path in UI
  128. newPath = string.Empty;
  129. }
  130. else if (File.Exists(path))
  131. {
  132. newPath = path;
  133. }
  134. else if (Directory.Exists(path))
  135. {
  136. // Given path is directory, so resolve down to filename
  137. newPath = GetEncoderPathFromDirectory(path, "ffmpeg");
  138. }
  139. else
  140. {
  141. throw new ResourceNotFoundException();
  142. }
  143. // Write the new ffmpeg path to the xml as <EncoderAppPath>
  144. // This ensures its not lost on next startup
  145. var config = _configurationManager.GetConfiguration<EncodingOptions>("encoding");
  146. config.EncoderAppPath = newPath;
  147. _configurationManager.SaveConfiguration("encoding", config);
  148. // Trigger SetFFmpegPath so we validate the new path and setup probe path
  149. SetFFmpegPath();
  150. }
  151. /// <summary>
  152. /// Validates the supplied FQPN to ensure it is a ffmpeg utility.
  153. /// If checks pass, global variable FFmpegPath and EncoderLocation are updated.
  154. /// </summary>
  155. /// <param name="path">FQPN to test</param>
  156. /// <param name="location">Location (External, Custom, System) of tool</param>
  157. /// <returns></returns>
  158. private bool ValidatePath(string path, FFmpegLocation location)
  159. {
  160. bool rc = false;
  161. if (!string.IsNullOrEmpty(path))
  162. {
  163. if (File.Exists(path))
  164. {
  165. rc = new EncoderValidator(_logger, path).ValidateVersion();
  166. if (!rc)
  167. {
  168. _logger.LogWarning("FFmpeg: {0}: Failed version check: {1}", location, path);
  169. }
  170. // ToDo - Enable the ffmpeg validator. At the moment any version can be used.
  171. rc = true;
  172. _ffmpegPath = path;
  173. EncoderLocation = location;
  174. }
  175. else
  176. {
  177. _logger.LogWarning("FFmpeg: {0}: File not found: {1}", location, path);
  178. }
  179. }
  180. return rc;
  181. }
  182. private string GetEncoderPathFromDirectory(string path, string filename)
  183. {
  184. try
  185. {
  186. var files = _fileSystem.GetFilePaths(path);
  187. var excludeExtensions = new[] { ".c" };
  188. return files.FirstOrDefault(i => string.Equals(Path.GetFileNameWithoutExtension(i), filename, StringComparison.OrdinalIgnoreCase)
  189. && !excludeExtensions.Contains(Path.GetExtension(i) ?? string.Empty));
  190. }
  191. catch (Exception)
  192. {
  193. // Trap all exceptions, like DirNotExists, and return null
  194. return null;
  195. }
  196. }
  197. /// <summary>
  198. /// Search the system $PATH environment variable looking for given filename.
  199. /// </summary>
  200. /// <param name="fileName"></param>
  201. /// <returns></returns>
  202. private string ExistsOnSystemPath(string fileName)
  203. {
  204. string inJellyfinPath = GetEncoderPathFromDirectory(System.AppContext.BaseDirectory, fileName);
  205. if (!string.IsNullOrEmpty(inJellyfinPath))
  206. {
  207. return inJellyfinPath;
  208. }
  209. var values = Environment.GetEnvironmentVariable("PATH");
  210. foreach (var path in values.Split(Path.PathSeparator))
  211. {
  212. var candidatePath = GetEncoderPathFromDirectory(path, fileName);
  213. if (!string.IsNullOrEmpty(candidatePath))
  214. {
  215. return candidatePath;
  216. }
  217. }
  218. return null;
  219. }
  220. private List<string> _encoders = new List<string>();
  221. public void SetAvailableEncoders(IEnumerable<string> list)
  222. {
  223. _encoders = list.ToList();
  224. //_logger.Info("Supported encoders: {0}", string.Join(",", list.ToArray()));
  225. }
  226. private List<string> _decoders = new List<string>();
  227. public void SetAvailableDecoders(IEnumerable<string> list)
  228. {
  229. _decoders = list.ToList();
  230. //_logger.Info("Supported decoders: {0}", string.Join(",", list.ToArray()));
  231. }
  232. public bool SupportsEncoder(string encoder)
  233. {
  234. return _encoders.Contains(encoder, StringComparer.OrdinalIgnoreCase);
  235. }
  236. public bool SupportsDecoder(string decoder)
  237. {
  238. return _decoders.Contains(decoder, StringComparer.OrdinalIgnoreCase);
  239. }
  240. public bool CanEncodeToAudioCodec(string codec)
  241. {
  242. if (string.Equals(codec, "opus", StringComparison.OrdinalIgnoreCase))
  243. {
  244. codec = "libopus";
  245. }
  246. else if (string.Equals(codec, "mp3", StringComparison.OrdinalIgnoreCase))
  247. {
  248. codec = "libmp3lame";
  249. }
  250. return SupportsEncoder(codec);
  251. }
  252. public bool CanEncodeToSubtitleCodec(string codec)
  253. {
  254. // TODO
  255. return true;
  256. }
  257. /// <summary>
  258. /// Gets the media info.
  259. /// </summary>
  260. /// <param name="request">The request.</param>
  261. /// <param name="cancellationToken">The cancellation token.</param>
  262. /// <returns>Task.</returns>
  263. public Task<MediaInfo> GetMediaInfo(MediaInfoRequest request, CancellationToken cancellationToken)
  264. {
  265. var extractChapters = request.MediaType == DlnaProfileType.Video && request.ExtractChapters;
  266. var inputFiles = MediaEncoderHelpers.GetInputArgument(_fileSystem, request.MediaSource.Path, request.MountedIso, request.PlayableStreamFileNames);
  267. var probeSize = EncodingHelper.GetProbeSizeArgument(inputFiles.Length);
  268. string analyzeDuration;
  269. if (request.MediaSource.AnalyzeDurationMs > 0)
  270. {
  271. analyzeDuration = "-analyzeduration " +
  272. (request.MediaSource.AnalyzeDurationMs * 1000).ToString();
  273. }
  274. else
  275. {
  276. analyzeDuration = EncodingHelper.GetAnalyzeDurationArgument(inputFiles.Length);
  277. }
  278. probeSize = probeSize + " " + analyzeDuration;
  279. probeSize = probeSize.Trim();
  280. var forceEnableLogging = request.MediaSource.Protocol != MediaProtocol.File;
  281. return GetMediaInfoInternal(GetInputArgument(inputFiles, request.MediaSource.Protocol), request.MediaSource.Path, request.MediaSource.Protocol, extractChapters,
  282. probeSize, request.MediaType == DlnaProfileType.Audio, request.MediaSource.VideoType, forceEnableLogging, cancellationToken);
  283. }
  284. /// <summary>
  285. /// Gets the input argument.
  286. /// </summary>
  287. /// <param name="inputFiles">The input files.</param>
  288. /// <param name="protocol">The protocol.</param>
  289. /// <returns>System.String.</returns>
  290. /// <exception cref="ArgumentException">Unrecognized InputType</exception>
  291. public string GetInputArgument(IReadOnlyList<string> inputFiles, MediaProtocol protocol)
  292. => EncodingUtils.GetInputArgument(inputFiles, protocol);
  293. /// <summary>
  294. /// Gets the media info internal.
  295. /// </summary>
  296. /// <returns>Task{MediaInfoResult}.</returns>
  297. private async Task<MediaInfo> GetMediaInfoInternal(string inputPath,
  298. string primaryPath,
  299. MediaProtocol protocol,
  300. bool extractChapters,
  301. string probeSizeArgument,
  302. bool isAudio,
  303. VideoType? videoType,
  304. bool forceEnableLogging,
  305. CancellationToken cancellationToken)
  306. {
  307. var args = extractChapters
  308. ? "{0} -i {1} -threads 0 -v warning -print_format json -show_streams -show_chapters -show_format"
  309. : "{0} -i {1} -threads 0 -v warning -print_format json -show_streams -show_format";
  310. args = string.Format(args, probeSizeArgument, inputPath).Trim();
  311. var processStartInfo = new ProcessStartInfo
  312. {
  313. CreateNoWindow = true,
  314. UseShellExecute = false,
  315. // Must consume both or ffmpeg may hang due to deadlocks. See comments below.
  316. RedirectStandardOutput = true,
  317. FileName = _ffprobePath,
  318. Arguments = args,
  319. WindowStyle = ProcessWindowStyle.Hidden,
  320. ErrorDialog = false,
  321. };
  322. var process = new Process { StartInfo = processStartInfo, EnableRaisingEvents = true };
  323. if (forceEnableLogging)
  324. {
  325. _logger.LogInformation("{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments);
  326. }
  327. else
  328. {
  329. _logger.LogDebug("{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments);
  330. }
  331. using (var processWrapper = new ProcessWrapper(process, this))
  332. {
  333. _logger.LogDebug("Starting ffprobe with args {Args}", args);
  334. StartProcess(processWrapper);
  335. InternalMediaInfoResult result;
  336. try
  337. {
  338. result = await JsonSerializer.DeserializeAsync<InternalMediaInfoResult>(
  339. process.StandardOutput.BaseStream,
  340. cancellationToken: cancellationToken).ConfigureAwait(false);
  341. }
  342. catch
  343. {
  344. StopProcess(processWrapper, 100);
  345. throw;
  346. }
  347. if (result == null || (result.Streams == null && result.Format == null))
  348. {
  349. throw new Exception("ffprobe failed - streams and format are both null.");
  350. }
  351. if (result.Streams != null)
  352. {
  353. // Normalize aspect ratio if invalid
  354. foreach (var stream in result.Streams)
  355. {
  356. if (string.Equals(stream.DisplayAspectRatio, "0:1", StringComparison.OrdinalIgnoreCase))
  357. {
  358. stream.DisplayAspectRatio = string.Empty;
  359. }
  360. if (string.Equals(stream.SampleAspectRatio, "0:1", StringComparison.OrdinalIgnoreCase))
  361. {
  362. stream.SampleAspectRatio = string.Empty;
  363. }
  364. }
  365. }
  366. return new ProbeResultNormalizer(_logger, _fileSystem, _localization).GetMediaInfo(result, videoType, isAudio, primaryPath, protocol);
  367. }
  368. }
  369. /// <summary>
  370. /// The us culture
  371. /// </summary>
  372. protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
  373. public Task<string> ExtractAudioImage(string path, int? imageStreamIndex, CancellationToken cancellationToken)
  374. {
  375. return ExtractImage(new[] { path }, null, null, imageStreamIndex, MediaProtocol.File, true, null, null, cancellationToken);
  376. }
  377. public Task<string> ExtractVideoImage(string[] inputFiles, string container, MediaProtocol protocol, MediaStream videoStream, Video3DFormat? threedFormat, TimeSpan? offset, CancellationToken cancellationToken)
  378. {
  379. return ExtractImage(inputFiles, container, videoStream, null, protocol, false, threedFormat, offset, cancellationToken);
  380. }
  381. public Task<string> ExtractVideoImage(string[] inputFiles, string container, MediaProtocol protocol, MediaStream imageStream, int? imageStreamIndex, CancellationToken cancellationToken)
  382. {
  383. return ExtractImage(inputFiles, container, imageStream, imageStreamIndex, protocol, false, null, null, cancellationToken);
  384. }
  385. private async Task<string> ExtractImage(string[] inputFiles, string container, MediaStream videoStream, int? imageStreamIndex, MediaProtocol protocol, bool isAudio,
  386. Video3DFormat? threedFormat, TimeSpan? offset, CancellationToken cancellationToken)
  387. {
  388. var inputArgument = GetInputArgument(inputFiles, protocol);
  389. if (isAudio)
  390. {
  391. if (imageStreamIndex.HasValue && imageStreamIndex.Value > 0)
  392. {
  393. // It seems for audio files we need to subtract 1 (for the audio stream??)
  394. imageStreamIndex = imageStreamIndex.Value - 1;
  395. }
  396. }
  397. else
  398. {
  399. try
  400. {
  401. return await ExtractImageInternal(inputArgument, container, videoStream, imageStreamIndex, threedFormat, offset, true, cancellationToken).ConfigureAwait(false);
  402. }
  403. catch (ArgumentException)
  404. {
  405. throw;
  406. }
  407. catch (Exception ex)
  408. {
  409. _logger.LogError(ex, "I-frame image extraction failed, will attempt standard way. Input: {arguments}", inputArgument);
  410. }
  411. }
  412. return await ExtractImageInternal(inputArgument, container, videoStream, imageStreamIndex, threedFormat, offset, false, cancellationToken).ConfigureAwait(false);
  413. }
  414. private async Task<string> ExtractImageInternal(string inputPath, string container, MediaStream videoStream, int? imageStreamIndex, Video3DFormat? threedFormat, TimeSpan? offset, bool useIFrame, CancellationToken cancellationToken)
  415. {
  416. if (string.IsNullOrEmpty(inputPath))
  417. {
  418. throw new ArgumentNullException(nameof(inputPath));
  419. }
  420. var tempExtractPath = Path.Combine(_configurationManager.ApplicationPaths.TempDirectory, Guid.NewGuid() + ".jpg");
  421. Directory.CreateDirectory(Path.GetDirectoryName(tempExtractPath));
  422. // 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.
  423. // This filter chain may have adverse effects on recorded tv thumbnails if ar changes during presentation ex. commercials @ diff ar
  424. var vf = "scale=600:trunc(600/dar/2)*2";
  425. if (threedFormat.HasValue)
  426. {
  427. switch (threedFormat.Value)
  428. {
  429. case Video3DFormat.HalfSideBySide:
  430. 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";
  431. // 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.
  432. break;
  433. case Video3DFormat.FullSideBySide:
  434. 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";
  435. //fsbs crop width in half,set the display aspect,crop out any black bars we may have made the scale width to 600.
  436. break;
  437. case Video3DFormat.HalfTopAndBottom:
  438. 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";
  439. //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
  440. break;
  441. case Video3DFormat.FullTopAndBottom:
  442. 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";
  443. // ftab crop heigt in half, set the display aspect,crop out any black bars we may have made the scale width to 600
  444. break;
  445. default:
  446. break;
  447. }
  448. }
  449. var mapArg = imageStreamIndex.HasValue ? (" -map 0:v:" + imageStreamIndex.Value.ToString(CultureInfo.InvariantCulture)) : string.Empty;
  450. var enableThumbnail = !new List<string> { "wtv" }.Contains(container ?? string.Empty, StringComparer.OrdinalIgnoreCase);
  451. // 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.
  452. var thumbnail = enableThumbnail ? ",thumbnail=24" : string.Empty;
  453. var args = useIFrame ? string.Format("-i {0}{3} -threads 0 -v quiet -vframes 1 -vf \"{2}{4}\" -f image2 \"{1}\"", inputPath, tempExtractPath, vf, mapArg, thumbnail) :
  454. string.Format("-i {0}{3} -threads 0 -v quiet -vframes 1 -vf \"{2}\" -f image2 \"{1}\"", inputPath, tempExtractPath, vf, mapArg);
  455. var probeSizeArgument = EncodingHelper.GetProbeSizeArgument(1);
  456. var analyzeDurationArgument = EncodingHelper.GetAnalyzeDurationArgument(1);
  457. if (!string.IsNullOrWhiteSpace(probeSizeArgument))
  458. {
  459. args = probeSizeArgument + " " + args;
  460. }
  461. if (!string.IsNullOrWhiteSpace(analyzeDurationArgument))
  462. {
  463. args = analyzeDurationArgument + " " + args;
  464. }
  465. if (offset.HasValue)
  466. {
  467. args = string.Format("-ss {0} ", GetTimeParameter(offset.Value)) + args;
  468. }
  469. if (videoStream != null)
  470. {
  471. /* fix
  472. var decoder = encodinghelper.GetHardwareAcceleratedVideoDecoder(VideoType.VideoFile, videoStream, GetEncodingOptions());
  473. if (!string.IsNullOrWhiteSpace(decoder))
  474. {
  475. args = decoder + " " + args;
  476. }
  477. */
  478. }
  479. if (!string.IsNullOrWhiteSpace(container))
  480. {
  481. var inputFormat = EncodingHelper.GetInputFormat(container);
  482. if (!string.IsNullOrWhiteSpace(inputFormat))
  483. {
  484. args = "-f " + inputFormat + " " + args;
  485. }
  486. }
  487. var processStartInfo = new ProcessStartInfo
  488. {
  489. CreateNoWindow = true,
  490. UseShellExecute = false,
  491. FileName = _ffmpegPath,
  492. Arguments = args,
  493. WindowStyle = ProcessWindowStyle.Hidden,
  494. ErrorDialog = false,
  495. };
  496. _logger.LogDebug("{0} {1}", processStartInfo.FileName, processStartInfo.Arguments);
  497. var process = new Process { StartInfo = processStartInfo, EnableRaisingEvents = true };
  498. using (var processWrapper = new ProcessWrapper(process, this))
  499. {
  500. bool ranToCompletion;
  501. await _thumbnailResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
  502. try
  503. {
  504. StartProcess(processWrapper);
  505. var timeoutMs = _configurationManager.Configuration.ImageExtractionTimeoutMs;
  506. if (timeoutMs <= 0)
  507. {
  508. timeoutMs = DefaultImageExtractionTimeout;
  509. }
  510. ranToCompletion = await process.WaitForExitAsync(TimeSpan.FromMilliseconds(timeoutMs)).ConfigureAwait(false);
  511. if (!ranToCompletion)
  512. {
  513. StopProcess(processWrapper, 1000);
  514. }
  515. }
  516. finally
  517. {
  518. _thumbnailResourcePool.Release();
  519. }
  520. var exitCode = ranToCompletion ? processWrapper.ExitCode ?? 0 : -1;
  521. var file = _fileSystem.GetFileInfo(tempExtractPath);
  522. if (exitCode == -1 || !file.Exists || file.Length == 0)
  523. {
  524. var msg = string.Format("ffmpeg image extraction failed for {0}", inputPath);
  525. _logger.LogError(msg);
  526. throw new Exception(msg);
  527. }
  528. return tempExtractPath;
  529. }
  530. }
  531. public string GetTimeParameter(long ticks)
  532. {
  533. var time = TimeSpan.FromTicks(ticks);
  534. return GetTimeParameter(time);
  535. }
  536. public string GetTimeParameter(TimeSpan time)
  537. {
  538. return time.ToString(@"hh\:mm\:ss\.fff", UsCulture);
  539. }
  540. public async Task ExtractVideoImagesOnInterval(
  541. string[] inputFiles,
  542. string container,
  543. MediaStream videoStream,
  544. MediaProtocol protocol,
  545. Video3DFormat? threedFormat,
  546. TimeSpan interval,
  547. string targetDirectory,
  548. string filenamePrefix,
  549. int? maxWidth,
  550. CancellationToken cancellationToken)
  551. {
  552. var inputArgument = GetInputArgument(inputFiles, protocol);
  553. var vf = "fps=fps=1/" + interval.TotalSeconds.ToString(UsCulture);
  554. if (maxWidth.HasValue)
  555. {
  556. var maxWidthParam = maxWidth.Value.ToString(UsCulture);
  557. vf += string.Format(",scale=min(iw\\,{0}):trunc(ow/dar/2)*2", maxWidthParam);
  558. }
  559. Directory.CreateDirectory(targetDirectory);
  560. var outputPath = Path.Combine(targetDirectory, filenamePrefix + "%05d.jpg");
  561. var args = string.Format("-i {0} -threads 0 -v quiet -vf \"{2}\" -f image2 \"{1}\"", inputArgument, outputPath, vf);
  562. var probeSizeArgument = EncodingHelper.GetProbeSizeArgument(1);
  563. var analyzeDurationArgument = EncodingHelper.GetAnalyzeDurationArgument(1);
  564. if (!string.IsNullOrWhiteSpace(probeSizeArgument))
  565. {
  566. args = probeSizeArgument + " " + args;
  567. }
  568. if (!string.IsNullOrWhiteSpace(analyzeDurationArgument))
  569. {
  570. args = analyzeDurationArgument + " " + args;
  571. }
  572. if (videoStream != null)
  573. {
  574. /* fix
  575. var decoder = encodinghelper.GetHardwareAcceleratedVideoDecoder(VideoType.VideoFile, videoStream, GetEncodingOptions());
  576. if (!string.IsNullOrWhiteSpace(decoder))
  577. {
  578. args = decoder + " " + args;
  579. }
  580. */
  581. }
  582. if (!string.IsNullOrWhiteSpace(container))
  583. {
  584. var inputFormat = EncodingHelper.GetInputFormat(container);
  585. if (!string.IsNullOrWhiteSpace(inputFormat))
  586. {
  587. args = "-f " + inputFormat + " " + args;
  588. }
  589. }
  590. var processStartInfo = new ProcessStartInfo
  591. {
  592. CreateNoWindow = true,
  593. UseShellExecute = false,
  594. FileName = _ffmpegPath,
  595. Arguments = args,
  596. WindowStyle = ProcessWindowStyle.Hidden,
  597. ErrorDialog = false
  598. };
  599. _logger.LogInformation(processStartInfo.FileName + " " + processStartInfo.Arguments);
  600. await _thumbnailResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
  601. bool ranToCompletion = false;
  602. var process = new Process { StartInfo = processStartInfo, EnableRaisingEvents = true };
  603. using (var processWrapper = new ProcessWrapper(process, this))
  604. {
  605. try
  606. {
  607. StartProcess(processWrapper);
  608. // Need to give ffmpeg enough time to make all the thumbnails, which could be a while,
  609. // but we still need to detect if the process hangs.
  610. // Making the assumption that as long as new jpegs are showing up, everything is good.
  611. bool isResponsive = true;
  612. int lastCount = 0;
  613. while (isResponsive)
  614. {
  615. if (await process.WaitForExitAsync(TimeSpan.FromSeconds(30)).ConfigureAwait(false))
  616. {
  617. ranToCompletion = true;
  618. break;
  619. }
  620. cancellationToken.ThrowIfCancellationRequested();
  621. var jpegCount = _fileSystem.GetFilePaths(targetDirectory)
  622. .Count(i => string.Equals(Path.GetExtension(i), ".jpg", StringComparison.OrdinalIgnoreCase));
  623. isResponsive = jpegCount > lastCount;
  624. lastCount = jpegCount;
  625. }
  626. if (!ranToCompletion)
  627. {
  628. StopProcess(processWrapper, 1000);
  629. }
  630. }
  631. finally
  632. {
  633. _thumbnailResourcePool.Release();
  634. }
  635. var exitCode = ranToCompletion ? processWrapper.ExitCode ?? 0 : -1;
  636. if (exitCode == -1)
  637. {
  638. var msg = string.Format("ffmpeg image extraction failed for {0}", inputArgument);
  639. _logger.LogError(msg);
  640. throw new Exception(msg);
  641. }
  642. }
  643. }
  644. private void StartProcess(ProcessWrapper process)
  645. {
  646. process.Process.Start();
  647. lock (_runningProcessesLock)
  648. {
  649. _runningProcesses.Add(process);
  650. }
  651. }
  652. private void StopProcess(ProcessWrapper process, int waitTimeMs)
  653. {
  654. try
  655. {
  656. if (process.Process.WaitForExit(waitTimeMs))
  657. {
  658. return;
  659. }
  660. _logger.LogInformation("Killing ffmpeg process");
  661. process.Process.Kill();
  662. }
  663. catch (InvalidOperationException)
  664. {
  665. // The process has already exited or
  666. // there is no process associated with this Process object.
  667. }
  668. catch (Exception ex)
  669. {
  670. _logger.LogError(ex, "Error killing process");
  671. }
  672. }
  673. private void StopProcesses()
  674. {
  675. List<ProcessWrapper> proceses;
  676. lock (_runningProcessesLock)
  677. {
  678. proceses = _runningProcesses.ToList();
  679. _runningProcesses.Clear();
  680. }
  681. foreach (var process in proceses)
  682. {
  683. if (!process.HasExited)
  684. {
  685. StopProcess(process, 500);
  686. }
  687. }
  688. }
  689. public string EscapeSubtitleFilterPath(string path)
  690. {
  691. // https://ffmpeg.org/ffmpeg-filters.html#Notes-on-filtergraph-escaping
  692. // We need to double escape
  693. return path.Replace('\\', '/').Replace(":", "\\:").Replace("'", "'\\\\\\''");
  694. }
  695. /// <inheritdoc />
  696. public void Dispose()
  697. {
  698. Dispose(true);
  699. GC.SuppressFinalize(this);
  700. }
  701. /// <summary>
  702. /// Releases unmanaged and - optionally - managed resources.
  703. /// </summary>
  704. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  705. protected virtual void Dispose(bool dispose)
  706. {
  707. if (dispose)
  708. {
  709. StopProcesses();
  710. }
  711. }
  712. /// <inheritdoc />
  713. public Task ConvertImage(string inputPath, string outputPath)
  714. {
  715. throw new NotImplementedException();
  716. }
  717. /// <inheritdoc />
  718. public IEnumerable<string> GetPrimaryPlaylistVobFiles(string path, IIsoMount isoMount, uint? titleNumber)
  719. {
  720. // min size 300 mb
  721. const long MinPlayableSize = 314572800;
  722. var root = isoMount != null ? isoMount.MountedPath : path;
  723. // Try to eliminate menus and intros by skipping all files at the front of the list that are less than the minimum size
  724. // Once we reach a file that is at least the minimum, return all subsequent ones
  725. var allVobs = _fileSystem.GetFiles(root, true)
  726. .Where(file => string.Equals(file.Extension, ".vob", StringComparison.OrdinalIgnoreCase))
  727. .OrderBy(i => i.FullName)
  728. .ToList();
  729. // If we didn't find any satisfying the min length, just take them all
  730. if (allVobs.Count == 0)
  731. {
  732. _logger.LogWarning("No vobs found in dvd structure.");
  733. return Enumerable.Empty<string>();
  734. }
  735. if (titleNumber.HasValue)
  736. {
  737. var prefix = string.Format(
  738. CultureInfo.InvariantCulture,
  739. titleNumber.Value >= 10 ? "VTS_{0}_" : "VTS_0{0}_",
  740. titleNumber.Value);
  741. var vobs = allVobs.Where(i => i.Name.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)).ToList();
  742. if (vobs.Count > 0)
  743. {
  744. var minSizeVobs = vobs
  745. .SkipWhile(f => f.Length < MinPlayableSize)
  746. .ToList();
  747. return minSizeVobs.Count == 0 ? vobs.Select(i => i.FullName) : minSizeVobs.Select(i => i.FullName);
  748. }
  749. _logger.LogWarning("Could not determine vob file list for {0} using DvdLib. Will scan using file sizes.", path);
  750. }
  751. var files = allVobs
  752. .SkipWhile(f => f.Length < MinPlayableSize)
  753. .ToList();
  754. // If we didn't find any satisfying the min length, just take them all
  755. if (files.Count == 0)
  756. {
  757. _logger.LogWarning("Vob size filter resulted in zero matches. Taking all vobs.");
  758. files = allVobs;
  759. }
  760. // Assuming they're named "vts_05_01", take all files whose second part matches that of the first file
  761. if (files.Count > 0)
  762. {
  763. var parts = _fileSystem.GetFileNameWithoutExtension(files[0]).Split('_');
  764. if (parts.Length == 3)
  765. {
  766. var title = parts[1];
  767. files = files.TakeWhile(f =>
  768. {
  769. var fileParts = _fileSystem.GetFileNameWithoutExtension(f).Split('_');
  770. return fileParts.Length == 3 && string.Equals(title, fileParts[1], StringComparison.OrdinalIgnoreCase);
  771. }).ToList();
  772. // If this resulted in not getting any vobs, just take them all
  773. if (files.Count == 0)
  774. {
  775. _logger.LogWarning("Vob filename filter resulted in zero matches. Taking all vobs.");
  776. files = allVobs;
  777. }
  778. }
  779. }
  780. return files.Select(i => i.FullName);
  781. }
  782. public bool CanExtractSubtitles(string codec)
  783. {
  784. // TODO is there ever a case when a subtitle can't be extracted??
  785. return true;
  786. }
  787. private class ProcessWrapper : IDisposable
  788. {
  789. private readonly MediaEncoder _mediaEncoder;
  790. private bool _disposed = false;
  791. public ProcessWrapper(Process process, MediaEncoder mediaEncoder)
  792. {
  793. Process = process;
  794. _mediaEncoder = mediaEncoder;
  795. Process.Exited += OnProcessExited;
  796. }
  797. public Process Process { get; }
  798. public bool HasExited { get; private set; }
  799. public int? ExitCode { get; private set; }
  800. void OnProcessExited(object sender, EventArgs e)
  801. {
  802. var process = (Process)sender;
  803. HasExited = true;
  804. try
  805. {
  806. ExitCode = process.ExitCode;
  807. }
  808. catch
  809. {
  810. }
  811. DisposeProcess(process);
  812. }
  813. private void DisposeProcess(Process process)
  814. {
  815. lock (_mediaEncoder._runningProcessesLock)
  816. {
  817. _mediaEncoder._runningProcesses.Remove(this);
  818. }
  819. try
  820. {
  821. process.Dispose();
  822. }
  823. catch
  824. {
  825. }
  826. }
  827. public void Dispose()
  828. {
  829. if (!_disposed)
  830. {
  831. if (Process != null)
  832. {
  833. Process.Exited -= OnProcessExited;
  834. DisposeProcess(Process);
  835. }
  836. }
  837. _disposed = true;
  838. }
  839. }
  840. }
  841. }