EncodingJobInfo.cs 20 KB

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