EncodingJobInfo.cs 20 KB

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