EncodingJobInfo.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. #nullable disable
  2. #pragma warning disable CS1591, SA1401
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. using System.Linq;
  7. using Jellyfin.Data.Entities;
  8. using MediaBrowser.Model.Dlna;
  9. using MediaBrowser.Model.Drawing;
  10. using MediaBrowser.Model.Dto;
  11. using MediaBrowser.Model.Entities;
  12. using MediaBrowser.Model.MediaInfo;
  13. using MediaBrowser.Model.Net;
  14. using MediaBrowser.Model.Session;
  15. namespace MediaBrowser.Controller.MediaEncoding
  16. {
  17. // For now, a common base class until the API and MediaEncoding classes are unified
  18. public class EncodingJobInfo
  19. {
  20. public int? OutputAudioBitrate;
  21. public int? OutputAudioChannels;
  22. private TranscodeReason[] _transcodeReasons = null;
  23. public EncodingJobInfo(TranscodingJobType jobType)
  24. {
  25. TranscodingType = jobType;
  26. RemoteHttpHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  27. SupportedAudioCodecs = Array.Empty<string>();
  28. SupportedVideoCodecs = Array.Empty<string>();
  29. SupportedSubtitleCodecs = Array.Empty<string>();
  30. }
  31. public TranscodeReason[] TranscodeReasons
  32. {
  33. get
  34. {
  35. if (_transcodeReasons == null)
  36. {
  37. if (BaseRequest.TranscodeReasons == null)
  38. {
  39. return Array.Empty<TranscodeReason>();
  40. }
  41. _transcodeReasons = BaseRequest.TranscodeReasons
  42. .Split(',')
  43. .Where(i => !string.IsNullOrEmpty(i))
  44. .Select(v => (TranscodeReason)Enum.Parse(typeof(TranscodeReason), v, true))
  45. .ToArray();
  46. }
  47. return _transcodeReasons;
  48. }
  49. }
  50. public IProgress<double> Progress { get; set; }
  51. public MediaStream VideoStream { get; set; }
  52. public VideoType VideoType { get; set; }
  53. public Dictionary<string, string> RemoteHttpHeaders { get; set; }
  54. public string OutputVideoCodec { get; set; }
  55. public MediaProtocol InputProtocol { get; set; }
  56. public string MediaPath { get; set; }
  57. public bool IsInputVideo { get; set; }
  58. public string OutputAudioCodec { get; set; }
  59. public int? OutputVideoBitrate { get; set; }
  60. public MediaStream SubtitleStream { get; set; }
  61. public SubtitleDeliveryMethod SubtitleDeliveryMethod { get; set; }
  62. public string[] SupportedSubtitleCodecs { get; set; }
  63. public int InternalSubtitleStreamOffset { get; set; }
  64. public MediaSourceInfo MediaSource { get; set; }
  65. public User User { get; set; }
  66. public long? RunTimeTicks { get; set; }
  67. public bool ReadInputAtNativeFramerate { get; set; }
  68. public string OutputFilePath { get; set; }
  69. public string MimeType { get; set; }
  70. public bool IgnoreInputDts => MediaSource.IgnoreDts;
  71. public bool IgnoreInputIndex => MediaSource.IgnoreIndex;
  72. public bool GenPtsInput => MediaSource.GenPtsInput;
  73. public bool DiscardCorruptFramesInput => false;
  74. public bool EnableFastSeekInput => false;
  75. public bool GenPtsOutput => false;
  76. public string OutputContainer { get; set; }
  77. public string OutputVideoSync
  78. {
  79. get
  80. {
  81. // For live tv + in progress recordings
  82. if (string.Equals(InputContainer, "mpegts", StringComparison.OrdinalIgnoreCase)
  83. || string.Equals(InputContainer, "ts", StringComparison.OrdinalIgnoreCase))
  84. {
  85. if (!MediaSource.RunTimeTicks.HasValue)
  86. {
  87. return "cfr";
  88. }
  89. }
  90. return "-1";
  91. }
  92. }
  93. public string AlbumCoverPath { get; set; }
  94. public string InputAudioSync { get; set; }
  95. public string InputVideoSync { get; set; }
  96. public TransportStreamTimestamp InputTimestamp { get; set; }
  97. public MediaStream AudioStream { get; set; }
  98. public string[] SupportedAudioCodecs { get; set; }
  99. public string[] SupportedVideoCodecs { get; set; }
  100. public string InputContainer { get; set; }
  101. public IsoType? IsoType { get; set; }
  102. public BaseEncodingJobOptions BaseRequest { get; set; }
  103. public bool IsVideoRequest { get; set; }
  104. public TranscodingJobType TranscodingType { get; set; }
  105. public long? StartTimeTicks => BaseRequest.StartTimeTicks;
  106. public bool CopyTimestamps => BaseRequest.CopyTimestamps;
  107. public bool IsSegmentedLiveStream
  108. => TranscodingType != TranscodingJobType.Progressive && !RunTimeTicks.HasValue;
  109. public int? TotalOutputBitrate => (OutputAudioBitrate ?? 0) + (OutputVideoBitrate ?? 0);
  110. public int? OutputWidth
  111. {
  112. get
  113. {
  114. if (VideoStream != null && VideoStream.Width.HasValue && VideoStream.Height.HasValue)
  115. {
  116. var size = new ImageDimensions(VideoStream.Width.Value, VideoStream.Height.Value);
  117. var newSize = DrawingUtils.Resize(
  118. size,
  119. BaseRequest.Width ?? 0,
  120. BaseRequest.Height ?? 0,
  121. BaseRequest.MaxWidth ?? 0,
  122. BaseRequest.MaxHeight ?? 0);
  123. return newSize.Width;
  124. }
  125. if (!IsVideoRequest)
  126. {
  127. return null;
  128. }
  129. return BaseRequest.MaxWidth ?? BaseRequest.Width;
  130. }
  131. }
  132. public int? OutputHeight
  133. {
  134. get
  135. {
  136. if (VideoStream != null && VideoStream.Width.HasValue && VideoStream.Height.HasValue)
  137. {
  138. var size = new ImageDimensions(VideoStream.Width.Value, VideoStream.Height.Value);
  139. var newSize = DrawingUtils.Resize(
  140. size,
  141. BaseRequest.Width ?? 0,
  142. BaseRequest.Height ?? 0,
  143. BaseRequest.MaxWidth ?? 0,
  144. BaseRequest.MaxHeight ?? 0);
  145. return newSize.Height;
  146. }
  147. if (!IsVideoRequest)
  148. {
  149. return null;
  150. }
  151. return BaseRequest.MaxHeight ?? BaseRequest.Height;
  152. }
  153. }
  154. public int? OutputAudioSampleRate
  155. {
  156. get
  157. {
  158. if (BaseRequest.Static
  159. || EncodingHelper.IsCopyCodec(OutputAudioCodec))
  160. {
  161. if (AudioStream != null)
  162. {
  163. return AudioStream.SampleRate;
  164. }
  165. }
  166. else if (BaseRequest.AudioSampleRate.HasValue)
  167. {
  168. // Don't exceed what the encoder supports
  169. // Seeing issues of attempting to encode to 88200
  170. return BaseRequest.AudioSampleRate.Value;
  171. }
  172. return null;
  173. }
  174. }
  175. public int? OutputAudioBitDepth
  176. {
  177. get
  178. {
  179. if (BaseRequest.Static
  180. || EncodingHelper.IsCopyCodec(OutputAudioCodec))
  181. {
  182. if (AudioStream != null)
  183. {
  184. return AudioStream.BitDepth;
  185. }
  186. }
  187. return null;
  188. }
  189. }
  190. /// <summary>
  191. /// Gets the target video level.
  192. /// </summary>
  193. public double? TargetVideoLevel
  194. {
  195. get
  196. {
  197. if (BaseRequest.Static || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  198. {
  199. return VideoStream?.Level;
  200. }
  201. var level = GetRequestedLevel(ActualOutputVideoCodec);
  202. if (!string.IsNullOrEmpty(level)
  203. && double.TryParse(level, NumberStyles.Any, CultureInfo.InvariantCulture, out var result))
  204. {
  205. return result;
  206. }
  207. return null;
  208. }
  209. }
  210. /// <summary>
  211. /// Gets the target video bit depth.
  212. /// </summary>
  213. public int? TargetVideoBitDepth
  214. {
  215. get
  216. {
  217. if (BaseRequest.Static
  218. || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  219. {
  220. return VideoStream?.BitDepth;
  221. }
  222. return null;
  223. }
  224. }
  225. /// <summary>
  226. /// Gets the target reference frames.
  227. /// </summary>
  228. /// <value>The target reference frames.</value>
  229. public int? TargetRefFrames
  230. {
  231. get
  232. {
  233. if (BaseRequest.Static
  234. || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  235. {
  236. return VideoStream?.RefFrames;
  237. }
  238. return null;
  239. }
  240. }
  241. /// <summary>
  242. /// Gets the target framerate.
  243. /// </summary>
  244. public float? TargetFramerate
  245. {
  246. get
  247. {
  248. if (BaseRequest.Static
  249. || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  250. {
  251. return VideoStream == null ? null : (VideoStream.AverageFrameRate ?? VideoStream.RealFrameRate);
  252. }
  253. return BaseRequest.MaxFramerate ?? BaseRequest.Framerate;
  254. }
  255. }
  256. public TransportStreamTimestamp TargetTimestamp
  257. {
  258. get
  259. {
  260. if (BaseRequest.Static)
  261. {
  262. return InputTimestamp;
  263. }
  264. return string.Equals(OutputContainer, "m2ts", StringComparison.OrdinalIgnoreCase) ?
  265. TransportStreamTimestamp.Valid :
  266. TransportStreamTimestamp.None;
  267. }
  268. }
  269. /// <summary>
  270. /// Gets the target packet length.
  271. /// </summary>
  272. public int? TargetPacketLength
  273. {
  274. get
  275. {
  276. if (BaseRequest.Static || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  277. {
  278. return VideoStream?.PacketLength;
  279. }
  280. return null;
  281. }
  282. }
  283. /// <summary>
  284. /// Gets the target video profile.
  285. /// </summary>
  286. public string TargetVideoProfile
  287. {
  288. get
  289. {
  290. if (BaseRequest.Static || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  291. {
  292. return VideoStream?.Profile;
  293. }
  294. var requestedProfile = GetRequestedProfiles(ActualOutputVideoCodec).FirstOrDefault();
  295. if (!string.IsNullOrEmpty(requestedProfile))
  296. {
  297. return requestedProfile;
  298. }
  299. return null;
  300. }
  301. }
  302. public string TargetVideoCodecTag
  303. {
  304. get
  305. {
  306. if (BaseRequest.Static
  307. || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  308. {
  309. return VideoStream?.CodecTag;
  310. }
  311. return null;
  312. }
  313. }
  314. public bool? IsTargetAnamorphic
  315. {
  316. get
  317. {
  318. if (BaseRequest.Static
  319. || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  320. {
  321. return VideoStream?.IsAnamorphic;
  322. }
  323. return false;
  324. }
  325. }
  326. public string ActualOutputVideoCodec
  327. {
  328. get
  329. {
  330. if (VideoStream == null)
  331. {
  332. return null;
  333. }
  334. if (EncodingHelper.IsCopyCodec(OutputVideoCodec))
  335. {
  336. return VideoStream.Codec;
  337. }
  338. return OutputVideoCodec;
  339. }
  340. }
  341. public string ActualOutputAudioCodec
  342. {
  343. get
  344. {
  345. if (AudioStream == null)
  346. {
  347. return null;
  348. }
  349. if (EncodingHelper.IsCopyCodec(OutputAudioCodec))
  350. {
  351. return AudioStream.Codec;
  352. }
  353. return OutputAudioCodec;
  354. }
  355. }
  356. public bool? IsTargetInterlaced
  357. {
  358. get
  359. {
  360. if (BaseRequest.Static
  361. || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  362. {
  363. return VideoStream?.IsInterlaced;
  364. }
  365. if (DeInterlace(ActualOutputVideoCodec, true))
  366. {
  367. return false;
  368. }
  369. return VideoStream?.IsInterlaced;
  370. }
  371. }
  372. public bool? IsTargetAVC
  373. {
  374. get
  375. {
  376. if (BaseRequest.Static || EncodingHelper.IsCopyCodec(OutputVideoCodec))
  377. {
  378. return VideoStream?.IsAVC;
  379. }
  380. return false;
  381. }
  382. }
  383. public int? TargetVideoStreamCount
  384. {
  385. get
  386. {
  387. if (BaseRequest.Static)
  388. {
  389. return GetMediaStreamCount(MediaStreamType.Video, int.MaxValue);
  390. }
  391. return GetMediaStreamCount(MediaStreamType.Video, 1);
  392. }
  393. }
  394. public int? TargetAudioStreamCount
  395. {
  396. get
  397. {
  398. if (BaseRequest.Static)
  399. {
  400. return GetMediaStreamCount(MediaStreamType.Audio, int.MaxValue);
  401. }
  402. return GetMediaStreamCount(MediaStreamType.Audio, 1);
  403. }
  404. }
  405. public int HlsListSize => 0;
  406. public bool EnableBreakOnNonKeyFrames(string videoCodec)
  407. {
  408. if (TranscodingType != TranscodingJobType.Progressive)
  409. {
  410. if (IsSegmentedLiveStream)
  411. {
  412. return false;
  413. }
  414. return BaseRequest.BreakOnNonKeyFrames && EncodingHelper.IsCopyCodec(videoCodec);
  415. }
  416. return false;
  417. }
  418. private int? GetMediaStreamCount(MediaStreamType type, int limit)
  419. {
  420. var count = MediaSource.GetStreamCount(type);
  421. if (count.HasValue)
  422. {
  423. count = Math.Min(count.Value, limit);
  424. }
  425. return count;
  426. }
  427. public string GetMimeType(string outputPath, bool enableStreamDefault = true)
  428. {
  429. if (!string.IsNullOrEmpty(MimeType))
  430. {
  431. return MimeType;
  432. }
  433. if (enableStreamDefault)
  434. {
  435. return MimeTypes.GetMimeType(outputPath);
  436. }
  437. return MimeTypes.GetMimeType(outputPath, null);
  438. }
  439. public bool DeInterlace(string videoCodec, bool forceDeinterlaceIfSourceIsInterlaced)
  440. {
  441. var videoStream = VideoStream;
  442. var isInputInterlaced = videoStream != null && videoStream.IsInterlaced;
  443. if (!isInputInterlaced)
  444. {
  445. return false;
  446. }
  447. // Support general param
  448. if (BaseRequest.DeInterlace)
  449. {
  450. return true;
  451. }
  452. if (!string.IsNullOrEmpty(videoCodec))
  453. {
  454. if (string.Equals(BaseRequest.GetOption(videoCodec, "deinterlace"), "true", StringComparison.OrdinalIgnoreCase))
  455. {
  456. return true;
  457. }
  458. }
  459. return forceDeinterlaceIfSourceIsInterlaced;
  460. }
  461. public string[] GetRequestedProfiles(string codec)
  462. {
  463. if (!string.IsNullOrEmpty(BaseRequest.Profile))
  464. {
  465. return BaseRequest.Profile.Split(new[] { '|', ',' }, StringSplitOptions.RemoveEmptyEntries);
  466. }
  467. if (!string.IsNullOrEmpty(codec))
  468. {
  469. var profile = BaseRequest.GetOption(codec, "profile");
  470. if (!string.IsNullOrEmpty(profile))
  471. {
  472. return profile.Split(new[] { '|', ',' }, StringSplitOptions.RemoveEmptyEntries);
  473. }
  474. }
  475. return Array.Empty<string>();
  476. }
  477. public string GetRequestedLevel(string codec)
  478. {
  479. if (!string.IsNullOrEmpty(BaseRequest.Level))
  480. {
  481. return BaseRequest.Level;
  482. }
  483. if (!string.IsNullOrEmpty(codec))
  484. {
  485. return BaseRequest.GetOption(codec, "level");
  486. }
  487. return null;
  488. }
  489. public int? GetRequestedMaxRefFrames(string codec)
  490. {
  491. if (BaseRequest.MaxRefFrames.HasValue)
  492. {
  493. return BaseRequest.MaxRefFrames;
  494. }
  495. if (!string.IsNullOrEmpty(codec))
  496. {
  497. var value = BaseRequest.GetOption(codec, "maxrefframes");
  498. if (!string.IsNullOrEmpty(value)
  499. && int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var result))
  500. {
  501. return result;
  502. }
  503. }
  504. return null;
  505. }
  506. public int? GetRequestedVideoBitDepth(string codec)
  507. {
  508. if (BaseRequest.MaxVideoBitDepth.HasValue)
  509. {
  510. return BaseRequest.MaxVideoBitDepth;
  511. }
  512. if (!string.IsNullOrEmpty(codec))
  513. {
  514. var value = BaseRequest.GetOption(codec, "videobitdepth");
  515. if (!string.IsNullOrEmpty(value)
  516. && int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var result))
  517. {
  518. return result;
  519. }
  520. }
  521. return null;
  522. }
  523. public int? GetRequestedAudioBitDepth(string codec)
  524. {
  525. if (BaseRequest.MaxAudioBitDepth.HasValue)
  526. {
  527. return BaseRequest.MaxAudioBitDepth;
  528. }
  529. if (!string.IsNullOrEmpty(codec))
  530. {
  531. var value = BaseRequest.GetOption(codec, "audiobitdepth");
  532. if (!string.IsNullOrEmpty(value)
  533. && int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var result))
  534. {
  535. return result;
  536. }
  537. }
  538. return null;
  539. }
  540. public int? GetRequestedAudioChannels(string codec)
  541. {
  542. if (!string.IsNullOrEmpty(codec))
  543. {
  544. var value = BaseRequest.GetOption(codec, "audiochannels");
  545. if (!string.IsNullOrEmpty(value)
  546. && int.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out var result))
  547. {
  548. return result;
  549. }
  550. }
  551. if (BaseRequest.MaxAudioChannels.HasValue)
  552. {
  553. return BaseRequest.MaxAudioChannels;
  554. }
  555. if (BaseRequest.AudioChannels.HasValue)
  556. {
  557. return BaseRequest.AudioChannels;
  558. }
  559. if (BaseRequest.TranscodingMaxAudioChannels.HasValue)
  560. {
  561. return BaseRequest.TranscodingMaxAudioChannels;
  562. }
  563. return null;
  564. }
  565. public virtual void ReportTranscodingProgress(TimeSpan? transcodingPosition, float? framerate, double? percentComplete, long? bytesTranscoded, int? bitRate)
  566. {
  567. Progress.Report(percentComplete.Value);
  568. }
  569. }
  570. }