2
0

EncodingJobInfo.cs 23 KB

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