EncodingJobInfo.cs 20 KB

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