EncodingJobInfo.cs 21 KB

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