2
0

MediaEncoder.cs 37 KB

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