MediaEncoder.cs 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Globalization;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text.Json;
  10. using System.Text.RegularExpressions;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using AsyncKeyedLock;
  14. using Jellyfin.Data.Enums;
  15. using Jellyfin.Extensions;
  16. using Jellyfin.Extensions.Json;
  17. using Jellyfin.Extensions.Json.Converters;
  18. using MediaBrowser.Common;
  19. using MediaBrowser.Common.Configuration;
  20. using MediaBrowser.Common.Extensions;
  21. using MediaBrowser.Controller.Configuration;
  22. using MediaBrowser.Controller.Extensions;
  23. using MediaBrowser.Controller.MediaEncoding;
  24. using MediaBrowser.MediaEncoding.Probing;
  25. using MediaBrowser.Model.Configuration;
  26. using MediaBrowser.Model.Dlna;
  27. using MediaBrowser.Model.Drawing;
  28. using MediaBrowser.Model.Dto;
  29. using MediaBrowser.Model.Entities;
  30. using MediaBrowser.Model.Globalization;
  31. using MediaBrowser.Model.IO;
  32. using MediaBrowser.Model.MediaInfo;
  33. using Microsoft.Extensions.Configuration;
  34. using Microsoft.Extensions.Logging;
  35. namespace MediaBrowser.MediaEncoding.Encoder
  36. {
  37. /// <summary>
  38. /// Class MediaEncoder.
  39. /// </summary>
  40. public partial class MediaEncoder : IMediaEncoder, IDisposable
  41. {
  42. /// <summary>
  43. /// The default SDR image extraction timeout in milliseconds.
  44. /// </summary>
  45. internal const int DefaultSdrImageExtractionTimeout = 10000;
  46. /// <summary>
  47. /// The default HDR image extraction timeout in milliseconds.
  48. /// </summary>
  49. internal const int DefaultHdrImageExtractionTimeout = 20000;
  50. private readonly ILogger<MediaEncoder> _logger;
  51. private readonly IServerConfigurationManager _configurationManager;
  52. private readonly IFileSystem _fileSystem;
  53. private readonly ILocalizationManager _localization;
  54. private readonly IBlurayExaminer _blurayExaminer;
  55. private readonly IConfiguration _config;
  56. private readonly IServerConfigurationManager _serverConfig;
  57. private readonly string _startupOptionFFmpegPath;
  58. private readonly AsyncNonKeyedLocker _thumbnailResourcePool;
  59. private readonly Lock _runningProcessesLock = new();
  60. private readonly List<ProcessWrapper> _runningProcesses = new List<ProcessWrapper>();
  61. // MediaEncoder is registered as a Singleton
  62. private readonly JsonSerializerOptions _jsonSerializerOptions;
  63. private List<string> _encoders = new List<string>();
  64. private List<string> _decoders = new List<string>();
  65. private List<string> _hwaccels = new List<string>();
  66. private List<string> _filters = new List<string>();
  67. private IDictionary<int, bool> _filtersWithOption = new Dictionary<int, bool>();
  68. private IDictionary<BitStreamFilterOptionType, bool> _bitStreamFiltersWithOption = new Dictionary<BitStreamFilterOptionType, bool>();
  69. private bool _isPkeyPauseSupported = false;
  70. private bool _isLowPriorityHwDecodeSupported = false;
  71. private bool _proberSupportsFirstVideoFrame = false;
  72. private bool _isVaapiDeviceAmd = false;
  73. private bool _isVaapiDeviceInteliHD = false;
  74. private bool _isVaapiDeviceInteli965 = false;
  75. private bool _isVaapiDeviceSupportVulkanDrmModifier = false;
  76. private bool _isVaapiDeviceSupportVulkanDrmInterop = false;
  77. private bool _isVideoToolboxAv1DecodeAvailable = false;
  78. private static string[] _vulkanImageDrmFmtModifierExts =
  79. {
  80. "VK_EXT_image_drm_format_modifier",
  81. };
  82. private static string[] _vulkanExternalMemoryDmaBufExts =
  83. {
  84. "VK_KHR_external_memory_fd",
  85. "VK_EXT_external_memory_dma_buf",
  86. "VK_KHR_external_semaphore_fd",
  87. "VK_EXT_external_memory_host"
  88. };
  89. private Version _ffmpegVersion = null;
  90. private string _ffmpegPath = string.Empty;
  91. private string _ffprobePath;
  92. private int _threads;
  93. public MediaEncoder(
  94. ILogger<MediaEncoder> logger,
  95. IServerConfigurationManager configurationManager,
  96. IFileSystem fileSystem,
  97. IBlurayExaminer blurayExaminer,
  98. ILocalizationManager localization,
  99. IConfiguration config,
  100. IServerConfigurationManager serverConfig)
  101. {
  102. _logger = logger;
  103. _configurationManager = configurationManager;
  104. _fileSystem = fileSystem;
  105. _blurayExaminer = blurayExaminer;
  106. _localization = localization;
  107. _config = config;
  108. _serverConfig = serverConfig;
  109. _startupOptionFFmpegPath = config.GetValue<string>(Controller.Extensions.ConfigurationExtensions.FfmpegPathKey) ?? string.Empty;
  110. _jsonSerializerOptions = new JsonSerializerOptions(JsonDefaults.Options);
  111. _jsonSerializerOptions.Converters.Add(new JsonBoolStringConverter());
  112. // Although the type is not nullable, this might still be null during unit tests
  113. var semaphoreCount = serverConfig.Configuration?.ParallelImageEncodingLimit ?? 0;
  114. if (semaphoreCount < 1)
  115. {
  116. semaphoreCount = Environment.ProcessorCount;
  117. }
  118. _thumbnailResourcePool = new(semaphoreCount);
  119. }
  120. /// <inheritdoc />
  121. public string EncoderPath => _ffmpegPath;
  122. /// <inheritdoc />
  123. public string ProbePath => _ffprobePath;
  124. /// <inheritdoc />
  125. public Version EncoderVersion => _ffmpegVersion;
  126. /// <inheritdoc />
  127. public bool IsPkeyPauseSupported => _isPkeyPauseSupported;
  128. /// <inheritdoc />
  129. public bool IsVaapiDeviceAmd => _isVaapiDeviceAmd;
  130. /// <inheritdoc />
  131. public bool IsVaapiDeviceInteliHD => _isVaapiDeviceInteliHD;
  132. /// <inheritdoc />
  133. public bool IsVaapiDeviceInteli965 => _isVaapiDeviceInteli965;
  134. /// <inheritdoc />
  135. public bool IsVaapiDeviceSupportVulkanDrmModifier => _isVaapiDeviceSupportVulkanDrmModifier;
  136. /// <inheritdoc />
  137. public bool IsVaapiDeviceSupportVulkanDrmInterop => _isVaapiDeviceSupportVulkanDrmInterop;
  138. public bool IsVideoToolboxAv1DecodeAvailable => _isVideoToolboxAv1DecodeAvailable;
  139. [GeneratedRegex(@"[^\/\\]+?(\.[^\/\\\n.]+)?$")]
  140. private static partial Regex FfprobePathRegex();
  141. /// <summary>
  142. /// Run at startup to validate ffmpeg.
  143. /// Sets global variables FFmpegPath.
  144. /// Precedence is: CLI/Env var > Config > $PATH.
  145. /// </summary>
  146. /// <returns>bool indicates whether a valid ffmpeg is found.</returns>
  147. public bool SetFFmpegPath()
  148. {
  149. var skipValidation = _config.GetFFmpegSkipValidation();
  150. if (skipValidation)
  151. {
  152. _logger.LogWarning("FFmpeg: Skipping FFmpeg Validation due to FFmpeg:novalidation set to true");
  153. return true;
  154. }
  155. // 1) Check if the --ffmpeg CLI switch has been given
  156. var ffmpegPath = _startupOptionFFmpegPath;
  157. string ffmpegPathSetMethodText = "command line or environment variable";
  158. if (string.IsNullOrEmpty(ffmpegPath))
  159. {
  160. // 2) Custom path stored in config/encoding xml file under tag <EncoderAppPath> should be used as a fallback
  161. ffmpegPath = _configurationManager.GetEncodingOptions().EncoderAppPath;
  162. ffmpegPathSetMethodText = "encoding.xml config file";
  163. if (string.IsNullOrEmpty(ffmpegPath))
  164. {
  165. // 3) Check "ffmpeg"
  166. ffmpegPath = "ffmpeg";
  167. ffmpegPathSetMethodText = "system $PATH";
  168. }
  169. }
  170. if (!ValidatePath(ffmpegPath))
  171. {
  172. _ffmpegPath = null;
  173. _logger.LogError("FFmpeg: Path set by {FfmpegPathSetMethodText} is invalid", ffmpegPathSetMethodText);
  174. return false;
  175. }
  176. // Write the FFmpeg path to the config/encoding.xml file as <EncoderAppPathDisplay> so it appears in UI
  177. var options = _configurationManager.GetEncodingOptions();
  178. options.EncoderAppPathDisplay = _ffmpegPath ?? string.Empty;
  179. _configurationManager.SaveConfiguration("encoding", options);
  180. // Only if mpeg path is set, try and set path to probe
  181. if (_ffmpegPath is not null)
  182. {
  183. // Determine a probe path from the mpeg path
  184. _ffprobePath = FfprobePathRegex().Replace(_ffmpegPath, "ffprobe$1");
  185. // Interrogate to understand what coders are supported
  186. var validator = new EncoderValidator(_logger, _ffmpegPath);
  187. SetAvailableDecoders(validator.GetDecoders());
  188. SetAvailableEncoders(validator.GetEncoders());
  189. SetAvailableFilters(validator.GetFilters());
  190. SetAvailableFiltersWithOption(validator.GetFiltersWithOption());
  191. SetAvailableBitStreamFiltersWithOption(validator.GetBitStreamFiltersWithOption());
  192. SetAvailableHwaccels(validator.GetHwaccels());
  193. SetMediaEncoderVersion(validator);
  194. _threads = EncodingHelper.GetNumberOfThreads(null, options, null);
  195. _isPkeyPauseSupported = validator.CheckSupportedRuntimeKey("p pause transcoding", _ffmpegVersion);
  196. _isLowPriorityHwDecodeSupported = validator.CheckSupportedHwaccelFlag("low_priority");
  197. _proberSupportsFirstVideoFrame = validator.CheckSupportedProberOption("only_first_vframe", _ffprobePath);
  198. // Check the Vaapi device vendor
  199. if (OperatingSystem.IsLinux()
  200. && SupportsHwaccel("vaapi")
  201. && !string.IsNullOrEmpty(options.VaapiDevice)
  202. && options.HardwareAccelerationType == HardwareAccelerationType.vaapi)
  203. {
  204. _isVaapiDeviceAmd = validator.CheckVaapiDeviceByDriverName("Mesa Gallium driver", options.VaapiDevice);
  205. _isVaapiDeviceInteliHD = validator.CheckVaapiDeviceByDriverName("Intel iHD driver", options.VaapiDevice);
  206. _isVaapiDeviceInteli965 = validator.CheckVaapiDeviceByDriverName("Intel i965 driver", options.VaapiDevice);
  207. _isVaapiDeviceSupportVulkanDrmModifier = validator.CheckVulkanDrmDeviceByExtensionName(options.VaapiDevice, _vulkanImageDrmFmtModifierExts);
  208. _isVaapiDeviceSupportVulkanDrmInterop = validator.CheckVulkanDrmDeviceByExtensionName(options.VaapiDevice, _vulkanExternalMemoryDmaBufExts);
  209. if (_isVaapiDeviceAmd)
  210. {
  211. _logger.LogInformation("VAAPI device {RenderNodePath} is AMD GPU", options.VaapiDevice);
  212. }
  213. else if (_isVaapiDeviceInteliHD)
  214. {
  215. _logger.LogInformation("VAAPI device {RenderNodePath} is Intel GPU (iHD)", options.VaapiDevice);
  216. }
  217. else if (_isVaapiDeviceInteli965)
  218. {
  219. _logger.LogInformation("VAAPI device {RenderNodePath} is Intel GPU (i965)", options.VaapiDevice);
  220. }
  221. if (_isVaapiDeviceSupportVulkanDrmModifier)
  222. {
  223. _logger.LogInformation("VAAPI device {RenderNodePath} supports Vulkan DRM modifier", options.VaapiDevice);
  224. }
  225. if (_isVaapiDeviceSupportVulkanDrmInterop)
  226. {
  227. _logger.LogInformation("VAAPI device {RenderNodePath} supports Vulkan DRM interop", options.VaapiDevice);
  228. }
  229. }
  230. // Check if VideoToolbox supports AV1 decode
  231. if (OperatingSystem.IsMacOS() && SupportsHwaccel("videotoolbox"))
  232. {
  233. _isVideoToolboxAv1DecodeAvailable = validator.CheckIsVideoToolboxAv1DecodeAvailable();
  234. }
  235. }
  236. _logger.LogInformation("FFmpeg: {FfmpegPath}", _ffmpegPath ?? string.Empty);
  237. return !string.IsNullOrWhiteSpace(ffmpegPath);
  238. }
  239. /// <summary>
  240. /// Validates the supplied FQPN to ensure it is a ffmpeg utility.
  241. /// If checks pass, global variable FFmpegPath is updated.
  242. /// </summary>
  243. /// <param name="path">FQPN to test.</param>
  244. /// <returns><c>true</c> if the version validation succeeded; otherwise, <c>false</c>.</returns>
  245. private bool ValidatePath(string path)
  246. {
  247. if (string.IsNullOrEmpty(path))
  248. {
  249. return false;
  250. }
  251. bool rc = new EncoderValidator(_logger, path).ValidateVersion();
  252. if (!rc)
  253. {
  254. _logger.LogError("FFmpeg: Failed version check: {Path}", path);
  255. return false;
  256. }
  257. _ffmpegPath = path;
  258. return true;
  259. }
  260. private string GetEncoderPathFromDirectory(string path, string filename, bool recursive = false)
  261. {
  262. try
  263. {
  264. var files = _fileSystem.GetFilePaths(path, recursive);
  265. return files.FirstOrDefault(i => Path.GetFileNameWithoutExtension(i.AsSpan()).Equals(filename, StringComparison.OrdinalIgnoreCase)
  266. && !Path.GetExtension(i.AsSpan()).Equals(".c", StringComparison.OrdinalIgnoreCase));
  267. }
  268. catch (Exception)
  269. {
  270. // Trap all exceptions, like DirNotExists, and return null
  271. return null;
  272. }
  273. }
  274. public void SetAvailableEncoders(IEnumerable<string> list)
  275. {
  276. _encoders = list.ToList();
  277. }
  278. public void SetAvailableDecoders(IEnumerable<string> list)
  279. {
  280. _decoders = list.ToList();
  281. }
  282. public void SetAvailableHwaccels(IEnumerable<string> list)
  283. {
  284. _hwaccels = list.ToList();
  285. }
  286. public void SetAvailableFilters(IEnumerable<string> list)
  287. {
  288. _filters = list.ToList();
  289. }
  290. public void SetAvailableFiltersWithOption(IDictionary<int, bool> dict)
  291. {
  292. _filtersWithOption = dict;
  293. }
  294. public void SetAvailableBitStreamFiltersWithOption(IDictionary<BitStreamFilterOptionType, bool> dict)
  295. {
  296. _bitStreamFiltersWithOption = dict;
  297. }
  298. public void SetMediaEncoderVersion(EncoderValidator validator)
  299. {
  300. _ffmpegVersion = validator.GetFFmpegVersion();
  301. }
  302. /// <inheritdoc />
  303. public bool SupportsEncoder(string encoder)
  304. {
  305. return _encoders.Contains(encoder, StringComparer.OrdinalIgnoreCase);
  306. }
  307. /// <inheritdoc />
  308. public bool SupportsDecoder(string decoder)
  309. {
  310. return _decoders.Contains(decoder, StringComparer.OrdinalIgnoreCase);
  311. }
  312. /// <inheritdoc />
  313. public bool SupportsHwaccel(string hwaccel)
  314. {
  315. return _hwaccels.Contains(hwaccel, StringComparer.OrdinalIgnoreCase);
  316. }
  317. /// <inheritdoc />
  318. public bool SupportsFilter(string filter)
  319. {
  320. return _filters.Contains(filter, StringComparer.OrdinalIgnoreCase);
  321. }
  322. /// <inheritdoc />
  323. public bool SupportsFilterWithOption(FilterOptionType option)
  324. {
  325. if (_filtersWithOption.TryGetValue((int)option, out var val))
  326. {
  327. return val;
  328. }
  329. return false;
  330. }
  331. public bool SupportsBitStreamFilterWithOption(BitStreamFilterOptionType option)
  332. {
  333. return _bitStreamFiltersWithOption.TryGetValue(option, out var val) && val;
  334. }
  335. public bool CanEncodeToAudioCodec(string codec)
  336. {
  337. if (string.Equals(codec, "opus", StringComparison.OrdinalIgnoreCase))
  338. {
  339. codec = "libopus";
  340. }
  341. else if (string.Equals(codec, "mp3", StringComparison.OrdinalIgnoreCase))
  342. {
  343. codec = "libmp3lame";
  344. }
  345. return SupportsEncoder(codec);
  346. }
  347. public bool CanEncodeToSubtitleCodec(string codec)
  348. {
  349. // TODO
  350. return true;
  351. }
  352. /// <inheritdoc />
  353. public Task<MediaInfo> GetMediaInfo(MediaInfoRequest request, CancellationToken cancellationToken)
  354. {
  355. var extractChapters = request.MediaType == DlnaProfileType.Video && request.ExtractChapters;
  356. var extraArgs = GetExtraArguments(request);
  357. return GetMediaInfoInternal(
  358. GetInputArgument(request.MediaSource.Path, request.MediaSource),
  359. request.MediaSource.Path,
  360. request.MediaSource.Protocol,
  361. extractChapters,
  362. extraArgs,
  363. request.MediaType == DlnaProfileType.Audio,
  364. request.MediaSource.VideoType,
  365. cancellationToken);
  366. }
  367. internal string GetExtraArguments(MediaInfoRequest request)
  368. {
  369. var ffmpegAnalyzeDuration = _config.GetFFmpegAnalyzeDuration() ?? string.Empty;
  370. var ffmpegProbeSize = _config.GetFFmpegProbeSize() ?? string.Empty;
  371. var analyzeDuration = string.Empty;
  372. var extraArgs = string.Empty;
  373. if (request.MediaSource.AnalyzeDurationMs > 0)
  374. {
  375. analyzeDuration = "-analyzeduration " + (request.MediaSource.AnalyzeDurationMs * 1000);
  376. }
  377. else if (!string.IsNullOrEmpty(ffmpegAnalyzeDuration))
  378. {
  379. analyzeDuration = "-analyzeduration " + ffmpegAnalyzeDuration;
  380. }
  381. if (!string.IsNullOrEmpty(analyzeDuration))
  382. {
  383. extraArgs = analyzeDuration;
  384. }
  385. if (!string.IsNullOrEmpty(ffmpegProbeSize))
  386. {
  387. extraArgs += " -probesize " + ffmpegProbeSize;
  388. }
  389. if (request.MediaSource.RequiredHttpHeaders.TryGetValue("User-Agent", out var userAgent))
  390. {
  391. extraArgs += $" -user_agent \"{userAgent}\"";
  392. }
  393. if (request.MediaSource.Protocol == MediaProtocol.Rtsp)
  394. {
  395. extraArgs += " -rtsp_transport tcp+udp -rtsp_flags prefer_tcp";
  396. }
  397. return extraArgs;
  398. }
  399. /// <inheritdoc />
  400. public string GetInputArgument(IReadOnlyList<string> inputFiles, MediaSourceInfo mediaSource)
  401. {
  402. return EncodingUtils.GetInputArgument("file", inputFiles, mediaSource.Protocol);
  403. }
  404. /// <inheritdoc />
  405. public string GetInputArgument(string inputFile, MediaSourceInfo mediaSource)
  406. {
  407. var prefix = "file";
  408. if (mediaSource.IsoType == IsoType.BluRay)
  409. {
  410. prefix = "bluray";
  411. }
  412. return EncodingUtils.GetInputArgument(prefix, new[] { inputFile }, mediaSource.Protocol);
  413. }
  414. /// <inheritdoc />
  415. public string GetExternalSubtitleInputArgument(string inputFile)
  416. {
  417. const string Prefix = "file";
  418. return EncodingUtils.GetInputArgument(Prefix, new[] { inputFile }, MediaProtocol.File);
  419. }
  420. /// <summary>
  421. /// Gets the media info internal.
  422. /// </summary>
  423. /// <returns>Task{MediaInfoResult}.</returns>
  424. private async Task<MediaInfo> GetMediaInfoInternal(
  425. string inputPath,
  426. string primaryPath,
  427. MediaProtocol protocol,
  428. bool extractChapters,
  429. string probeSizeArgument,
  430. bool isAudio,
  431. VideoType? videoType,
  432. CancellationToken cancellationToken)
  433. {
  434. var args = extractChapters
  435. ? "{0} -i {1} -threads {2} -v warning -print_format json -show_streams -show_chapters -show_format"
  436. : "{0} -i {1} -threads {2} -v warning -print_format json -show_streams -show_format";
  437. if (_proberSupportsFirstVideoFrame)
  438. {
  439. args += " -show_frames -only_first_vframe";
  440. }
  441. args = string.Format(CultureInfo.InvariantCulture, args, probeSizeArgument, inputPath, _threads).Trim();
  442. var process = new Process
  443. {
  444. StartInfo = new ProcessStartInfo
  445. {
  446. CreateNoWindow = true,
  447. UseShellExecute = false,
  448. // Must consume both or ffmpeg may hang due to deadlocks.
  449. RedirectStandardOutput = true,
  450. FileName = _ffprobePath,
  451. Arguments = args,
  452. WindowStyle = ProcessWindowStyle.Hidden,
  453. ErrorDialog = false,
  454. },
  455. EnableRaisingEvents = true
  456. };
  457. _logger.LogInformation("Starting {ProcessFileName} with args {ProcessArgs}", _ffprobePath, args);
  458. var memoryStream = new MemoryStream();
  459. await using (memoryStream.ConfigureAwait(false))
  460. using (var processWrapper = new ProcessWrapper(process, this))
  461. {
  462. StartProcess(processWrapper);
  463. using var reader = process.StandardOutput;
  464. await reader.BaseStream.CopyToAsync(memoryStream, cancellationToken).ConfigureAwait(false);
  465. memoryStream.Seek(0, SeekOrigin.Begin);
  466. InternalMediaInfoResult result;
  467. try
  468. {
  469. result = await JsonSerializer.DeserializeAsync<InternalMediaInfoResult>(
  470. memoryStream,
  471. _jsonSerializerOptions,
  472. cancellationToken).ConfigureAwait(false);
  473. }
  474. catch
  475. {
  476. StopProcess(processWrapper, 100);
  477. throw;
  478. }
  479. if (result is null || (result.Streams is null && result.Format is null))
  480. {
  481. throw new FfmpegException("ffprobe failed - streams and format are both null.");
  482. }
  483. if (result.Streams is not null)
  484. {
  485. // Normalize aspect ratio if invalid
  486. foreach (var stream in result.Streams)
  487. {
  488. if (string.Equals(stream.DisplayAspectRatio, "0:1", StringComparison.OrdinalIgnoreCase))
  489. {
  490. stream.DisplayAspectRatio = string.Empty;
  491. }
  492. if (string.Equals(stream.SampleAspectRatio, "0:1", StringComparison.OrdinalIgnoreCase))
  493. {
  494. stream.SampleAspectRatio = string.Empty;
  495. }
  496. }
  497. }
  498. return new ProbeResultNormalizer(_logger, _localization).GetMediaInfo(result, videoType, isAudio, primaryPath, protocol);
  499. }
  500. }
  501. /// <inheritdoc />
  502. public Task<string> ExtractAudioImage(string path, int? imageStreamIndex, CancellationToken cancellationToken)
  503. {
  504. var mediaSource = new MediaSourceInfo
  505. {
  506. Protocol = MediaProtocol.File
  507. };
  508. return ExtractImage(path, null, null, imageStreamIndex, mediaSource, true, null, null, ImageFormat.Jpg, cancellationToken);
  509. }
  510. /// <inheritdoc />
  511. public Task<string> ExtractVideoImage(string inputFile, string container, MediaSourceInfo mediaSource, MediaStream videoStream, Video3DFormat? threedFormat, TimeSpan? offset, CancellationToken cancellationToken)
  512. {
  513. return ExtractImage(inputFile, container, videoStream, null, mediaSource, false, threedFormat, offset, ImageFormat.Jpg, cancellationToken);
  514. }
  515. /// <inheritdoc />
  516. public Task<string> ExtractVideoImage(string inputFile, string container, MediaSourceInfo mediaSource, MediaStream imageStream, int? imageStreamIndex, ImageFormat? targetFormat, CancellationToken cancellationToken)
  517. {
  518. return ExtractImage(inputFile, container, imageStream, imageStreamIndex, mediaSource, false, null, null, targetFormat, cancellationToken);
  519. }
  520. private async Task<string> ExtractImage(
  521. string inputFile,
  522. string container,
  523. MediaStream videoStream,
  524. int? imageStreamIndex,
  525. MediaSourceInfo mediaSource,
  526. bool isAudio,
  527. Video3DFormat? threedFormat,
  528. TimeSpan? offset,
  529. ImageFormat? targetFormat,
  530. CancellationToken cancellationToken)
  531. {
  532. var inputArgument = GetInputPathArgument(inputFile, mediaSource);
  533. if (!isAudio)
  534. {
  535. try
  536. {
  537. return await ExtractImageInternal(inputArgument, container, videoStream, imageStreamIndex, threedFormat, offset, true, targetFormat, false, cancellationToken).ConfigureAwait(false);
  538. }
  539. catch (ArgumentException)
  540. {
  541. throw;
  542. }
  543. catch (Exception ex)
  544. {
  545. _logger.LogError(ex, "I-frame image extraction failed, will attempt standard way. Input: {Arguments}", inputArgument);
  546. }
  547. }
  548. return await ExtractImageInternal(inputArgument, container, videoStream, imageStreamIndex, threedFormat, offset, false, targetFormat, isAudio, cancellationToken).ConfigureAwait(false);
  549. }
  550. private string GetImageResolutionParameter()
  551. {
  552. var imageResolutionParameter = _serverConfig.Configuration.ChapterImageResolution switch
  553. {
  554. ImageResolution.P144 => "256x144",
  555. ImageResolution.P240 => "426x240",
  556. ImageResolution.P360 => "640x360",
  557. ImageResolution.P480 => "854x480",
  558. ImageResolution.P720 => "1280x720",
  559. ImageResolution.P1080 => "1920x1080",
  560. ImageResolution.P1440 => "2560x1440",
  561. ImageResolution.P2160 => "3840x2160",
  562. _ => string.Empty
  563. };
  564. if (!string.IsNullOrEmpty(imageResolutionParameter))
  565. {
  566. imageResolutionParameter = " -s " + imageResolutionParameter;
  567. }
  568. return imageResolutionParameter;
  569. }
  570. private async Task<string> ExtractImageInternal(
  571. string inputPath,
  572. string container,
  573. MediaStream videoStream,
  574. int? imageStreamIndex,
  575. Video3DFormat? threedFormat,
  576. TimeSpan? offset,
  577. bool useIFrame,
  578. ImageFormat? targetFormat,
  579. bool isAudio,
  580. CancellationToken cancellationToken)
  581. {
  582. ArgumentException.ThrowIfNullOrEmpty(inputPath);
  583. var useTradeoff = _config.GetFFmpegImgExtractPerfTradeoff();
  584. var outputExtension = targetFormat?.GetExtension() ?? ".jpg";
  585. var tempExtractPath = Path.Combine(_configurationManager.ApplicationPaths.TempDirectory, Guid.NewGuid() + outputExtension);
  586. Directory.CreateDirectory(Path.GetDirectoryName(tempExtractPath));
  587. // deint -> scale -> thumbnail -> tonemap.
  588. // put the SW tonemap right after the thumbnail to do it only once to reduce cpu usage.
  589. var filters = new List<string>();
  590. // deinterlace using bwdif algorithm for video stream.
  591. if (videoStream is not null && videoStream.IsInterlaced)
  592. {
  593. filters.Add("bwdif=0:-1:0");
  594. }
  595. // apply some filters to thumbnail extracted below (below) crop any black lines that we made and get the correct ar.
  596. // This filter chain may have adverse effects on recorded tv thumbnails if ar changes during presentation ex. commercials @ diff ar
  597. var scaler = threedFormat switch
  598. {
  599. // hsbs crop width in half,scale to correct size, set the display aspect,crop out any black bars we may have made. Work out the correct height based on the display aspect it will maintain the aspect where -1 in this case (3d) may not.
  600. Video3DFormat.HalfSideBySide => @"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",
  601. // fsbs crop width in half,set the display aspect,crop out any black bars we may have made
  602. Video3DFormat.FullSideBySide => @"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",
  603. // htab crop height in half,scale to correct size, set the display aspect,crop out any black bars we may have made
  604. Video3DFormat.HalfTopAndBottom => @"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",
  605. // ftab crop height in half, set the display aspect,crop out any black bars we may have made
  606. Video3DFormat.FullTopAndBottom => @"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",
  607. _ => "scale=round(iw*sar/2)*2:round(ih/2)*2"
  608. };
  609. filters.Add(scaler);
  610. // 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.
  611. // mpegts need larger batch size otherwise the corrupted thumbnail will be created. Larger batch size will lower the processing speed.
  612. var enableThumbnail = !useTradeoff && useIFrame && !string.Equals("wtv", container, StringComparison.OrdinalIgnoreCase);
  613. if (enableThumbnail)
  614. {
  615. var useLargerBatchSize = string.Equals("mpegts", container, StringComparison.OrdinalIgnoreCase);
  616. filters.Add("thumbnail=n=" + (useLargerBatchSize ? "50" : "24"));
  617. }
  618. // Use SW tonemap on HDR video stream only when the zscale or tonemapx filter is available.
  619. // Only enable Dolby Vision tonemap when tonemapx is available
  620. var enableHdrExtraction = false;
  621. if (videoStream?.VideoRange == VideoRange.HDR)
  622. {
  623. if (SupportsFilter("tonemapx"))
  624. {
  625. var peak = videoStream.VideoRangeType == VideoRangeType.DOVI ? "400" : "100";
  626. enableHdrExtraction = true;
  627. filters.Add($"tonemapx=tonemap=bt2390:desat=0:peak={peak}:t=bt709:m=bt709:p=bt709:format=yuv420p");
  628. }
  629. else if (SupportsFilter("zscale") && videoStream.VideoRangeType != VideoRangeType.DOVI)
  630. {
  631. enableHdrExtraction = true;
  632. filters.Add("zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0:peak=100,zscale=t=bt709:m=bt709,format=yuv420p");
  633. }
  634. }
  635. var vf = string.Join(',', filters);
  636. var mapArg = imageStreamIndex.HasValue ? (" -map 0:" + imageStreamIndex.Value.ToString(CultureInfo.InvariantCulture)) : string.Empty;
  637. var args = string.Format(CultureInfo.InvariantCulture, "-i {0}{3} -threads {4} -v quiet -vframes 1 -vf {2}{5} -f image2 \"{1}\"", inputPath, tempExtractPath, vf, mapArg, _threads, isAudio ? string.Empty : GetImageResolutionParameter());
  638. if (offset.HasValue)
  639. {
  640. args = string.Format(CultureInfo.InvariantCulture, "-ss {0} ", GetTimeParameter(offset.Value)) + args;
  641. }
  642. if (useIFrame && useTradeoff)
  643. {
  644. args = "-skip_frame nokey " + args;
  645. }
  646. if (!string.IsNullOrWhiteSpace(container))
  647. {
  648. var inputFormat = EncodingHelper.GetInputFormat(container);
  649. if (!string.IsNullOrWhiteSpace(inputFormat))
  650. {
  651. args = "-f " + inputFormat + " " + args;
  652. }
  653. }
  654. var process = new Process
  655. {
  656. StartInfo = new ProcessStartInfo
  657. {
  658. CreateNoWindow = true,
  659. UseShellExecute = false,
  660. FileName = _ffmpegPath,
  661. Arguments = args,
  662. WindowStyle = ProcessWindowStyle.Hidden,
  663. ErrorDialog = false,
  664. },
  665. EnableRaisingEvents = true
  666. };
  667. _logger.LogDebug("{ProcessFileName} {ProcessArguments}", process.StartInfo.FileName, process.StartInfo.Arguments);
  668. using (var processWrapper = new ProcessWrapper(process, this))
  669. {
  670. using (await _thumbnailResourcePool.LockAsync(cancellationToken).ConfigureAwait(false))
  671. {
  672. StartProcess(processWrapper);
  673. var timeoutMs = _configurationManager.Configuration.ImageExtractionTimeoutMs;
  674. if (timeoutMs <= 0)
  675. {
  676. timeoutMs = enableHdrExtraction ? DefaultHdrImageExtractionTimeout : DefaultSdrImageExtractionTimeout;
  677. }
  678. try
  679. {
  680. await process.WaitForExitAsync(TimeSpan.FromMilliseconds(timeoutMs)).ConfigureAwait(false);
  681. }
  682. catch (OperationCanceledException ex)
  683. {
  684. process.Kill(true);
  685. throw new FfmpegException(string.Format(CultureInfo.InvariantCulture, "ffmpeg image extraction timed out for {0} after {1}ms", inputPath, timeoutMs), ex);
  686. }
  687. }
  688. var file = _fileSystem.GetFileInfo(tempExtractPath);
  689. if (processWrapper.ExitCode > 0 || !file.Exists || file.Length == 0)
  690. {
  691. throw new FfmpegException(string.Format(CultureInfo.InvariantCulture, "ffmpeg image extraction failed for {0}", inputPath));
  692. }
  693. return tempExtractPath;
  694. }
  695. }
  696. /// <inheritdoc />
  697. public Task<string> ExtractVideoImagesOnIntervalAccelerated(
  698. string inputFile,
  699. string container,
  700. MediaSourceInfo mediaSource,
  701. MediaStream imageStream,
  702. int maxWidth,
  703. TimeSpan interval,
  704. bool allowHwAccel,
  705. bool enableHwEncoding,
  706. int? threads,
  707. int? qualityScale,
  708. ProcessPriorityClass? priority,
  709. bool enableKeyFrameOnlyExtraction,
  710. EncodingHelper encodingHelper,
  711. CancellationToken cancellationToken)
  712. {
  713. var options = allowHwAccel ? _configurationManager.GetEncodingOptions() : new EncodingOptions();
  714. threads ??= _threads;
  715. if (allowHwAccel && enableKeyFrameOnlyExtraction)
  716. {
  717. var hardwareAccelerationType = options.HardwareAccelerationType;
  718. var supportsKeyFrameOnly = (hardwareAccelerationType == HardwareAccelerationType.nvenc && options.EnableEnhancedNvdecDecoder)
  719. || (hardwareAccelerationType == HardwareAccelerationType.amf && OperatingSystem.IsWindows())
  720. || (hardwareAccelerationType == HardwareAccelerationType.qsv && options.PreferSystemNativeHwDecoder)
  721. || hardwareAccelerationType == HardwareAccelerationType.vaapi
  722. || hardwareAccelerationType == HardwareAccelerationType.videotoolbox
  723. || hardwareAccelerationType == HardwareAccelerationType.rkmpp;
  724. if (!supportsKeyFrameOnly)
  725. {
  726. // Disable hardware acceleration when the hardware decoder does not support keyframe only mode.
  727. allowHwAccel = false;
  728. options = new EncodingOptions();
  729. }
  730. }
  731. // A new EncodingOptions instance must be used as to not disable HW acceleration for all of Jellyfin.
  732. // Additionally, we must set a few fields without defaults to prevent null pointer exceptions.
  733. if (!allowHwAccel)
  734. {
  735. options.EnableHardwareEncoding = false;
  736. options.HardwareAccelerationType = HardwareAccelerationType.none;
  737. options.EnableTonemapping = false;
  738. }
  739. if (imageStream.Width is not null && imageStream.Height is not null && !string.IsNullOrEmpty(imageStream.AspectRatio))
  740. {
  741. // For hardware trickplay encoders, we need to re-calculate the size because they used fixed scale dimensions
  742. var darParts = imageStream.AspectRatio.Split(':');
  743. var (wa, ha) = (double.Parse(darParts[0], CultureInfo.InvariantCulture), double.Parse(darParts[1], CultureInfo.InvariantCulture));
  744. // When dimension / DAR does not equal to 1:1, then the frames are most likely stored stretched.
  745. // Note: this might be incorrect for 3D videos as the SAR stored might be per eye instead of per video, but we really can do little about it.
  746. var shouldResetHeight = Math.Abs((imageStream.Width.Value * ha) - (imageStream.Height.Value * wa)) > .05;
  747. if (shouldResetHeight)
  748. {
  749. // SAR = DAR * Height / Width
  750. // RealHeight = Height / SAR = Height / (DAR * Height / Width) = Width / DAR
  751. imageStream.Height = Convert.ToInt32(imageStream.Width.Value * ha / wa);
  752. }
  753. }
  754. var baseRequest = new BaseEncodingJobOptions { MaxWidth = maxWidth, MaxFramerate = (float)(1.0 / interval.TotalSeconds) };
  755. var jobState = new EncodingJobInfo(TranscodingJobType.Progressive)
  756. {
  757. IsVideoRequest = true, // must be true for InputVideoHwaccelArgs to return non-empty value
  758. MediaSource = mediaSource,
  759. VideoStream = imageStream,
  760. BaseRequest = baseRequest, // GetVideoProcessingFilterParam errors if null
  761. MediaPath = inputFile,
  762. OutputVideoCodec = "mjpeg"
  763. };
  764. var vidEncoder = enableHwEncoding ? encodingHelper.GetVideoEncoder(jobState, options) : jobState.OutputVideoCodec;
  765. // Get input and filter arguments
  766. var inputArg = encodingHelper.GetInputArgument(jobState, options, container).Trim();
  767. if (string.IsNullOrWhiteSpace(inputArg))
  768. {
  769. throw new InvalidOperationException("EncodingHelper returned empty input arguments.");
  770. }
  771. if (!allowHwAccel)
  772. {
  773. inputArg = "-threads " + threads + " " + inputArg; // HW accel args set a different input thread count, only set if disabled
  774. }
  775. if (options.HardwareAccelerationType == HardwareAccelerationType.videotoolbox && _isLowPriorityHwDecodeSupported)
  776. {
  777. // VideoToolbox supports low priority decoding, which is useful for trickplay
  778. inputArg = "-hwaccel_flags +low_priority " + inputArg;
  779. }
  780. if (enableKeyFrameOnlyExtraction)
  781. {
  782. inputArg = "-skip_frame nokey " + inputArg;
  783. }
  784. var filterParam = encodingHelper.GetVideoProcessingFilterParam(jobState, options, vidEncoder).Trim();
  785. if (string.IsNullOrWhiteSpace(filterParam))
  786. {
  787. throw new InvalidOperationException("EncodingHelper returned empty or invalid filter parameters.");
  788. }
  789. return ExtractVideoImagesOnIntervalInternal(inputArg, filterParam, vidEncoder, threads, qualityScale, priority, cancellationToken);
  790. }
  791. private async Task<string> ExtractVideoImagesOnIntervalInternal(
  792. string inputArg,
  793. string filterParam,
  794. string vidEncoder,
  795. int? outputThreads,
  796. int? qualityScale,
  797. ProcessPriorityClass? priority,
  798. CancellationToken cancellationToken)
  799. {
  800. if (string.IsNullOrWhiteSpace(inputArg))
  801. {
  802. throw new InvalidOperationException("Empty or invalid input argument.");
  803. }
  804. // ffmpeg qscale is a value from 1-31, with 1 being best quality and 31 being worst
  805. // jpeg quality is a value from 0-100, with 0 being worst quality and 100 being best
  806. var encoderQuality = Math.Clamp(qualityScale ?? 4, 1, 31);
  807. var encoderQualityOption = "-qscale:v ";
  808. if (vidEncoder.Contains("vaapi", StringComparison.OrdinalIgnoreCase)
  809. || vidEncoder.Contains("qsv", StringComparison.OrdinalIgnoreCase))
  810. {
  811. // vaapi and qsv's mjpeg encoder use jpeg quality as input, instead of ffmpeg defined qscale
  812. encoderQuality = 100 - ((encoderQuality - 1) * (100 / 30));
  813. encoderQualityOption = "-global_quality:v ";
  814. }
  815. if (vidEncoder.Contains("videotoolbox", StringComparison.OrdinalIgnoreCase))
  816. {
  817. // videotoolbox's mjpeg encoder uses jpeg quality scaled to QP2LAMBDA (118) instead of ffmpeg defined qscale
  818. encoderQuality = 118 - ((encoderQuality - 1) * (118 / 30));
  819. }
  820. if (vidEncoder.Contains("rkmpp", StringComparison.OrdinalIgnoreCase))
  821. {
  822. // rkmpp's mjpeg encoder uses jpeg quality as input (max is 99, not 100), instead of ffmpeg defined qscale
  823. encoderQuality = 99 - ((encoderQuality - 1) * (99 / 30));
  824. encoderQualityOption = "-qp_init:v ";
  825. }
  826. // Output arguments
  827. var targetDirectory = Path.Combine(_configurationManager.ApplicationPaths.TempDirectory, Guid.NewGuid().ToString("N"));
  828. Directory.CreateDirectory(targetDirectory);
  829. var outputPath = Path.Combine(targetDirectory, "%08d.jpg");
  830. // Final command arguments
  831. var args = string.Format(
  832. CultureInfo.InvariantCulture,
  833. "-loglevel error {0} -an -sn {1} -threads {2} -c:v {3} {4}{5}{6}-f {7} \"{8}\"",
  834. inputArg,
  835. filterParam,
  836. outputThreads.GetValueOrDefault(_threads),
  837. vidEncoder,
  838. encoderQualityOption + encoderQuality + " ",
  839. vidEncoder.Contains("videotoolbox", StringComparison.InvariantCultureIgnoreCase) ? "-allow_sw 1 " : string.Empty, // allow_sw fallback for some intel macs
  840. EncodingHelper.GetVideoSyncOption("0", EncoderVersion).Trim() + " ", // passthrough timestamp
  841. "image2",
  842. outputPath);
  843. // Start ffmpeg process
  844. var process = new Process
  845. {
  846. StartInfo = new ProcessStartInfo
  847. {
  848. CreateNoWindow = true,
  849. UseShellExecute = false,
  850. FileName = _ffmpegPath,
  851. Arguments = args,
  852. WindowStyle = ProcessWindowStyle.Hidden,
  853. ErrorDialog = false,
  854. },
  855. EnableRaisingEvents = true
  856. };
  857. var processDescription = string.Format(CultureInfo.InvariantCulture, "{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments);
  858. _logger.LogInformation("Trickplay generation: {ProcessDescription}", processDescription);
  859. using (var processWrapper = new ProcessWrapper(process, this))
  860. {
  861. bool ranToCompletion = false;
  862. using (await _thumbnailResourcePool.LockAsync(cancellationToken).ConfigureAwait(false))
  863. {
  864. StartProcess(processWrapper);
  865. // Set process priority
  866. if (priority.HasValue)
  867. {
  868. try
  869. {
  870. processWrapper.Process.PriorityClass = priority.Value;
  871. }
  872. catch (Exception ex)
  873. {
  874. _logger.LogDebug(ex, "Unable to set process priority to {Priority} for {Description}", priority.Value, processDescription);
  875. }
  876. }
  877. // Need to give ffmpeg enough time to make all the thumbnails, which could be a while,
  878. // but we still need to detect if the process hangs.
  879. // Making the assumption that as long as new jpegs are showing up, everything is good.
  880. bool isResponsive = true;
  881. int lastCount = 0;
  882. var timeoutMs = _configurationManager.Configuration.ImageExtractionTimeoutMs;
  883. timeoutMs = timeoutMs <= 0 ? DefaultHdrImageExtractionTimeout : timeoutMs;
  884. while (isResponsive && !cancellationToken.IsCancellationRequested)
  885. {
  886. try
  887. {
  888. await process.WaitForExitAsync(TimeSpan.FromMilliseconds(timeoutMs)).ConfigureAwait(false);
  889. ranToCompletion = true;
  890. break;
  891. }
  892. catch (OperationCanceledException)
  893. {
  894. // We don't actually expect the process to be finished in one timeout span, just that one image has been generated.
  895. }
  896. var jpegCount = _fileSystem.GetFilePaths(targetDirectory).Count();
  897. isResponsive = jpegCount > lastCount;
  898. lastCount = jpegCount;
  899. }
  900. if (!ranToCompletion)
  901. {
  902. if (!isResponsive)
  903. {
  904. _logger.LogInformation("Trickplay process unresponsive.");
  905. }
  906. _logger.LogInformation("Stopping trickplay extraction.");
  907. StopProcess(processWrapper, 1000);
  908. }
  909. }
  910. var exitCode = ranToCompletion ? processWrapper.ExitCode ?? 0 : -1;
  911. if (exitCode == -1)
  912. {
  913. _logger.LogError("ffmpeg image extraction failed for {ProcessDescription}", processDescription);
  914. // Cleanup temp folder here, because the targetDirectory is not returned and the cleanup for failed ffmpeg process is not possible for caller.
  915. // Ideally the ffmpeg should not write any files if it fails, but it seems like it is not guaranteed.
  916. try
  917. {
  918. Directory.Delete(targetDirectory, true);
  919. }
  920. catch (Exception e)
  921. {
  922. _logger.LogError(e, "Failed to delete ffmpeg temp directory {TargetDirectory}", targetDirectory);
  923. }
  924. throw new FfmpegException(string.Format(CultureInfo.InvariantCulture, "ffmpeg image extraction failed for {0}", processDescription));
  925. }
  926. return targetDirectory;
  927. }
  928. }
  929. public string GetTimeParameter(long ticks)
  930. {
  931. var time = TimeSpan.FromTicks(ticks);
  932. return GetTimeParameter(time);
  933. }
  934. public string GetTimeParameter(TimeSpan time)
  935. {
  936. return time.ToString(@"hh\:mm\:ss\.fff", CultureInfo.InvariantCulture);
  937. }
  938. private void StartProcess(ProcessWrapper process)
  939. {
  940. process.Process.Start();
  941. lock (_runningProcessesLock)
  942. {
  943. _runningProcesses.Add(process);
  944. }
  945. }
  946. private void StopProcess(ProcessWrapper process, int waitTimeMs)
  947. {
  948. try
  949. {
  950. if (process.Process.WaitForExit(waitTimeMs))
  951. {
  952. return;
  953. }
  954. _logger.LogInformation("Killing ffmpeg process");
  955. process.Process.Kill();
  956. }
  957. catch (InvalidOperationException)
  958. {
  959. // The process has already exited or
  960. // there is no process associated with this Process object.
  961. }
  962. catch (Exception ex)
  963. {
  964. _logger.LogError(ex, "Error killing process");
  965. }
  966. }
  967. private void StopProcesses()
  968. {
  969. List<ProcessWrapper> processes;
  970. lock (_runningProcessesLock)
  971. {
  972. processes = _runningProcesses.ToList();
  973. _runningProcesses.Clear();
  974. }
  975. foreach (var process in processes)
  976. {
  977. if (!process.HasExited)
  978. {
  979. StopProcess(process, 500);
  980. }
  981. }
  982. }
  983. public string EscapeSubtitleFilterPath(string path)
  984. {
  985. // https://ffmpeg.org/ffmpeg-filters.html#Notes-on-filtergraph-escaping
  986. // We need to double escape
  987. return path
  988. .Replace('\\', '/')
  989. .Replace(":", "\\:", StringComparison.Ordinal)
  990. .Replace("'", @"'\\\''", StringComparison.Ordinal)
  991. .Replace("\"", "\\\"", StringComparison.Ordinal);
  992. }
  993. /// <inheritdoc />
  994. public void Dispose()
  995. {
  996. Dispose(true);
  997. GC.SuppressFinalize(this);
  998. }
  999. /// <summary>
  1000. /// Releases unmanaged and - optionally - managed resources.
  1001. /// </summary>
  1002. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  1003. protected virtual void Dispose(bool dispose)
  1004. {
  1005. if (dispose)
  1006. {
  1007. StopProcesses();
  1008. _thumbnailResourcePool.Dispose();
  1009. }
  1010. }
  1011. /// <inheritdoc />
  1012. public Task ConvertImage(string inputPath, string outputPath)
  1013. {
  1014. throw new NotImplementedException();
  1015. }
  1016. /// <inheritdoc />
  1017. public IReadOnlyList<string> GetPrimaryPlaylistVobFiles(string path, uint? titleNumber)
  1018. {
  1019. // Eliminate menus and intros by omitting VIDEO_TS.VOB and all subsequent title .vob files ending with _0.VOB
  1020. var allVobs = _fileSystem.GetFiles(path, true)
  1021. .Where(file => string.Equals(file.Extension, ".VOB", StringComparison.OrdinalIgnoreCase))
  1022. .Where(file => !string.Equals(file.Name, "VIDEO_TS.VOB", StringComparison.OrdinalIgnoreCase))
  1023. .Where(file => !file.Name.EndsWith("_0.VOB", StringComparison.OrdinalIgnoreCase))
  1024. .OrderBy(i => i.FullName)
  1025. .ToList();
  1026. if (titleNumber.HasValue)
  1027. {
  1028. var prefix = string.Format(CultureInfo.InvariantCulture, "VTS_{0:D2}_", titleNumber.Value);
  1029. var vobs = allVobs.Where(i => i.Name.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)).ToList();
  1030. if (vobs.Count > 0)
  1031. {
  1032. return vobs.Select(i => i.FullName).ToList();
  1033. }
  1034. _logger.LogWarning("Could not determine .vob files for title {Title} of {Path}.", titleNumber, path);
  1035. }
  1036. // Check for multiple big titles (> 900 MB)
  1037. var titles = allVobs
  1038. .Where(vob => vob.Length >= 900 * 1024 * 1024)
  1039. .Select(vob => _fileSystem.GetFileNameWithoutExtension(vob).AsSpan().RightPart('_').ToString())
  1040. .Distinct()
  1041. .ToList();
  1042. // Fall back to first title if no big title is found
  1043. if (titles.Count == 0)
  1044. {
  1045. titles.Add(_fileSystem.GetFileNameWithoutExtension(allVobs[0]).AsSpan().RightPart('_').ToString());
  1046. }
  1047. // Aggregate all .vob files of the titles
  1048. return allVobs
  1049. .Where(vob => titles.Contains(_fileSystem.GetFileNameWithoutExtension(vob).AsSpan().RightPart('_').ToString()))
  1050. .Select(i => i.FullName)
  1051. .Order()
  1052. .ToList();
  1053. }
  1054. /// <inheritdoc />
  1055. public IReadOnlyList<string> GetPrimaryPlaylistM2tsFiles(string path)
  1056. => _blurayExaminer.GetDiscInfo(path).Files;
  1057. /// <inheritdoc />
  1058. public string GetInputPathArgument(EncodingJobInfo state)
  1059. => GetInputPathArgument(state.MediaPath, state.MediaSource);
  1060. /// <inheritdoc />
  1061. public string GetInputPathArgument(string path, MediaSourceInfo mediaSource)
  1062. {
  1063. return mediaSource.VideoType switch
  1064. {
  1065. VideoType.Dvd => GetInputArgument(GetPrimaryPlaylistVobFiles(path, null), mediaSource),
  1066. VideoType.BluRay => GetInputArgument(GetPrimaryPlaylistM2tsFiles(path), mediaSource),
  1067. _ => GetInputArgument(path, mediaSource)
  1068. };
  1069. }
  1070. /// <inheritdoc />
  1071. public void GenerateConcatConfig(MediaSourceInfo source, string concatFilePath)
  1072. {
  1073. // Get all playable files
  1074. IReadOnlyList<string> files;
  1075. var videoType = source.VideoType;
  1076. if (videoType == VideoType.Dvd)
  1077. {
  1078. files = GetPrimaryPlaylistVobFiles(source.Path, null);
  1079. }
  1080. else if (videoType == VideoType.BluRay)
  1081. {
  1082. files = GetPrimaryPlaylistM2tsFiles(source.Path);
  1083. }
  1084. else
  1085. {
  1086. return;
  1087. }
  1088. // Generate concat configuration entries for each file and write to file
  1089. Directory.CreateDirectory(Path.GetDirectoryName(concatFilePath));
  1090. using var sw = new FormattingStreamWriter(concatFilePath, CultureInfo.InvariantCulture);
  1091. foreach (var path in files)
  1092. {
  1093. var mediaInfoResult = GetMediaInfo(
  1094. new MediaInfoRequest
  1095. {
  1096. MediaType = DlnaProfileType.Video,
  1097. MediaSource = new MediaSourceInfo
  1098. {
  1099. Path = path,
  1100. Protocol = MediaProtocol.File,
  1101. VideoType = videoType
  1102. }
  1103. },
  1104. CancellationToken.None).GetAwaiter().GetResult();
  1105. var duration = TimeSpan.FromTicks(mediaInfoResult.RunTimeTicks.Value).TotalSeconds;
  1106. // Add file path stanza to concat configuration
  1107. sw.WriteLine("file '{0}'", path.Replace("'", @"'\''", StringComparison.Ordinal));
  1108. // Add duration stanza to concat configuration
  1109. sw.WriteLine("duration {0}", duration);
  1110. }
  1111. }
  1112. public bool CanExtractSubtitles(string codec)
  1113. {
  1114. // TODO is there ever a case when a subtitle can't be extracted??
  1115. return true;
  1116. }
  1117. private sealed class ProcessWrapper : IDisposable
  1118. {
  1119. private readonly MediaEncoder _mediaEncoder;
  1120. private bool _disposed = false;
  1121. public ProcessWrapper(Process process, MediaEncoder mediaEncoder)
  1122. {
  1123. Process = process;
  1124. _mediaEncoder = mediaEncoder;
  1125. Process.Exited += OnProcessExited;
  1126. }
  1127. public Process Process { get; }
  1128. public bool HasExited { get; private set; }
  1129. public int? ExitCode { get; private set; }
  1130. private void OnProcessExited(object sender, EventArgs e)
  1131. {
  1132. var process = (Process)sender;
  1133. HasExited = true;
  1134. try
  1135. {
  1136. ExitCode = process.ExitCode;
  1137. }
  1138. catch
  1139. {
  1140. }
  1141. DisposeProcess(process);
  1142. }
  1143. private void DisposeProcess(Process process)
  1144. {
  1145. lock (_mediaEncoder._runningProcessesLock)
  1146. {
  1147. _mediaEncoder._runningProcesses.Remove(this);
  1148. }
  1149. process.Dispose();
  1150. }
  1151. public void Dispose()
  1152. {
  1153. if (!_disposed)
  1154. {
  1155. if (Process is not null)
  1156. {
  1157. Process.Exited -= OnProcessExited;
  1158. DisposeProcess(Process);
  1159. }
  1160. }
  1161. _disposed = true;
  1162. }
  1163. }
  1164. }
  1165. }