StreamInfo.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. using MediaBrowser.Model.Drawing;
  2. using MediaBrowser.Model.Dto;
  3. using MediaBrowser.Model.Entities;
  4. using MediaBrowser.Model.Extensions;
  5. using MediaBrowser.Model.MediaInfo;
  6. using MediaBrowser.Model.Session;
  7. using System;
  8. using System.Collections.Generic;
  9. namespace MediaBrowser.Model.Dlna
  10. {
  11. /// <summary>
  12. /// Class StreamInfo.
  13. /// </summary>
  14. public class StreamInfo
  15. {
  16. public StreamInfo()
  17. {
  18. AudioCodecs = new string[] { };
  19. }
  20. public string ItemId { get; set; }
  21. public PlayMethod PlayMethod { get; set; }
  22. public EncodingContext Context { get; set; }
  23. public DlnaProfileType MediaType { get; set; }
  24. public string Container { get; set; }
  25. public string SubProtocol { get; set; }
  26. public long StartPositionTicks { get; set; }
  27. public string VideoCodec { get; set; }
  28. public string VideoProfile { get; set; }
  29. public bool CopyTimestamps { get; set; }
  30. public bool EnableSubtitlesInManifest { get; set; }
  31. public string[] AudioCodecs { get; set; }
  32. public int? AudioStreamIndex { get; set; }
  33. public int? SubtitleStreamIndex { get; set; }
  34. public int? TranscodingMaxAudioChannels { get; set; }
  35. public int? MaxAudioChannels { get; set; }
  36. public int? AudioBitrate { get; set; }
  37. public int? VideoBitrate { get; set; }
  38. public int? VideoLevel { get; set; }
  39. public int? MaxWidth { get; set; }
  40. public int? MaxHeight { get; set; }
  41. public int? MaxVideoBitDepth { get; set; }
  42. public int? MaxRefFrames { get; set; }
  43. public float? MaxFramerate { get; set; }
  44. public DeviceProfile DeviceProfile { get; set; }
  45. public string DeviceProfileId { get; set; }
  46. public string DeviceId { get; set; }
  47. public long? RunTimeTicks { get; set; }
  48. public TranscodeSeekInfo TranscodeSeekInfo { get; set; }
  49. public bool EstimateContentLength { get; set; }
  50. public MediaSourceInfo MediaSource { get; set; }
  51. public SubtitleDeliveryMethod SubtitleDeliveryMethod { get; set; }
  52. public string SubtitleFormat { get; set; }
  53. public string PlaySessionId { get; set; }
  54. public List<MediaSourceInfo> AllMediaSources { get; set; }
  55. public string MediaSourceId
  56. {
  57. get
  58. {
  59. return MediaSource == null ? null : MediaSource.Id;
  60. }
  61. }
  62. public bool IsDirectStream
  63. {
  64. get
  65. {
  66. return PlayMethod == PlayMethod.DirectStream ||
  67. PlayMethod == PlayMethod.DirectPlay;
  68. }
  69. }
  70. public string ToUrl(string baseUrl, string accessToken)
  71. {
  72. if (PlayMethod == PlayMethod.DirectPlay)
  73. {
  74. return MediaSource.Path;
  75. }
  76. if (string.IsNullOrEmpty(baseUrl))
  77. {
  78. throw new ArgumentNullException(baseUrl);
  79. }
  80. List<string> list = new List<string>();
  81. foreach (NameValuePair pair in BuildParams(this, accessToken, false))
  82. {
  83. if (string.IsNullOrEmpty(pair.Value))
  84. {
  85. continue;
  86. }
  87. // Try to keep the url clean by omitting defaults
  88. if (StringHelper.EqualsIgnoreCase(pair.Name, "StartTimeTicks") &&
  89. StringHelper.EqualsIgnoreCase(pair.Value, "0"))
  90. {
  91. continue;
  92. }
  93. if (StringHelper.EqualsIgnoreCase(pair.Name, "SubtitleStreamIndex") &&
  94. StringHelper.EqualsIgnoreCase(pair.Value, "-1"))
  95. {
  96. continue;
  97. }
  98. if (StringHelper.EqualsIgnoreCase(pair.Name, "Static") &&
  99. StringHelper.EqualsIgnoreCase(pair.Value, "false"))
  100. {
  101. continue;
  102. }
  103. list.Add(string.Format("{0}={1}", pair.Name, pair.Value));
  104. }
  105. string queryString = string.Join("&", list.ToArray());
  106. return GetUrl(baseUrl, queryString);
  107. }
  108. public string ToDlnaUrl(string baseUrl, string accessToken)
  109. {
  110. if (PlayMethod == PlayMethod.DirectPlay)
  111. {
  112. return MediaSource.Path;
  113. }
  114. string dlnaCommand = BuildDlnaParam(this, accessToken);
  115. return GetUrl(baseUrl, dlnaCommand);
  116. }
  117. private string GetUrl(string baseUrl, string queryString)
  118. {
  119. if (string.IsNullOrEmpty(baseUrl))
  120. {
  121. throw new ArgumentNullException(baseUrl);
  122. }
  123. string extension = string.IsNullOrEmpty(Container) ? string.Empty : "." + Container;
  124. baseUrl = baseUrl.TrimEnd('/');
  125. if (MediaType == DlnaProfileType.Audio)
  126. {
  127. if (StringHelper.EqualsIgnoreCase(SubProtocol, "hls"))
  128. {
  129. return string.Format("{0}/audio/{1}/master.m3u8?{2}", baseUrl, ItemId, queryString);
  130. }
  131. return string.Format("{0}/audio/{1}/stream{2}?{3}", baseUrl, ItemId, extension, queryString);
  132. }
  133. if (StringHelper.EqualsIgnoreCase(SubProtocol, "hls"))
  134. {
  135. return string.Format("{0}/videos/{1}/master.m3u8?{2}", baseUrl, ItemId, queryString);
  136. }
  137. return string.Format("{0}/videos/{1}/stream{2}?{3}", baseUrl, ItemId, extension, queryString);
  138. }
  139. private static string BuildDlnaParam(StreamInfo item, string accessToken)
  140. {
  141. List<string> list = new List<string>();
  142. foreach (NameValuePair pair in BuildParams(item, accessToken, true))
  143. {
  144. list.Add(pair.Value);
  145. }
  146. return string.Format("Params={0}", string.Join(";", list.ToArray()));
  147. }
  148. private static List<NameValuePair> BuildParams(StreamInfo item, string accessToken, bool isDlna)
  149. {
  150. List<NameValuePair> list = new List<NameValuePair>();
  151. string audioCodecs = item.AudioCodecs.Length == 0 ?
  152. string.Empty :
  153. string.Join(",", item.AudioCodecs);
  154. list.Add(new NameValuePair("DeviceProfileId", item.DeviceProfileId ?? string.Empty));
  155. list.Add(new NameValuePair("DeviceId", item.DeviceId ?? string.Empty));
  156. list.Add(new NameValuePair("MediaSourceId", item.MediaSourceId ?? string.Empty));
  157. list.Add(new NameValuePair("Static", item.IsDirectStream.ToString().ToLower()));
  158. list.Add(new NameValuePair("VideoCodec", item.VideoCodec ?? string.Empty));
  159. list.Add(new NameValuePair("AudioCodec", audioCodecs));
  160. list.Add(new NameValuePair("AudioStreamIndex", item.AudioStreamIndex.HasValue ? StringHelper.ToStringCultureInvariant(item.AudioStreamIndex.Value) : string.Empty));
  161. list.Add(new NameValuePair("SubtitleStreamIndex", item.SubtitleStreamIndex.HasValue && item.SubtitleDeliveryMethod != SubtitleDeliveryMethod.External ? StringHelper.ToStringCultureInvariant(item.SubtitleStreamIndex.Value) : string.Empty));
  162. list.Add(new NameValuePair("VideoBitrate", item.VideoBitrate.HasValue ? StringHelper.ToStringCultureInvariant(item.VideoBitrate.Value) : string.Empty));
  163. list.Add(new NameValuePair("AudioBitrate", item.AudioBitrate.HasValue ? StringHelper.ToStringCultureInvariant(item.AudioBitrate.Value) : string.Empty));
  164. list.Add(new NameValuePair("MaxAudioChannels", item.MaxAudioChannels.HasValue ? StringHelper.ToStringCultureInvariant(item.MaxAudioChannels.Value) : string.Empty));
  165. list.Add(new NameValuePair("MaxFramerate", item.MaxFramerate.HasValue ? StringHelper.ToStringCultureInvariant(item.MaxFramerate.Value) : string.Empty));
  166. list.Add(new NameValuePair("MaxWidth", item.MaxWidth.HasValue ? StringHelper.ToStringCultureInvariant(item.MaxWidth.Value) : string.Empty));
  167. list.Add(new NameValuePair("MaxHeight", item.MaxHeight.HasValue ? StringHelper.ToStringCultureInvariant(item.MaxHeight.Value) : string.Empty));
  168. var forceStartPosition = false;
  169. long startPositionTicks = item.StartPositionTicks;
  170. //if (item.MediaSource.DateLiveStreamOpened.HasValue && startPositionTicks == 0)
  171. //{
  172. // var elapsed = DateTime.UtcNow - item.MediaSource.DateLiveStreamOpened.Value;
  173. // elapsed -= TimeSpan.FromSeconds(20);
  174. // if (elapsed.TotalSeconds >= 0)
  175. // {
  176. // startPositionTicks = elapsed.Ticks + startPositionTicks;
  177. // forceStartPosition = true;
  178. // }
  179. //}
  180. if (StringHelper.EqualsIgnoreCase(item.SubProtocol, "hls") && !forceStartPosition)
  181. {
  182. list.Add(new NameValuePair("StartTimeTicks", string.Empty));
  183. }
  184. else
  185. {
  186. list.Add(new NameValuePair("StartTimeTicks", StringHelper.ToStringCultureInvariant(startPositionTicks)));
  187. }
  188. list.Add(new NameValuePair("Level", item.VideoLevel.HasValue ? StringHelper.ToStringCultureInvariant(item.VideoLevel.Value) : string.Empty));
  189. list.Add(new NameValuePair("MaxRefFrames", item.MaxRefFrames.HasValue ? StringHelper.ToStringCultureInvariant(item.MaxRefFrames.Value) : string.Empty));
  190. list.Add(new NameValuePair("MaxVideoBitDepth", item.MaxVideoBitDepth.HasValue ? StringHelper.ToStringCultureInvariant(item.MaxVideoBitDepth.Value) : string.Empty));
  191. list.Add(new NameValuePair("Profile", item.VideoProfile ?? string.Empty));
  192. // no longer used
  193. list.Add(new NameValuePair("Cabac", string.Empty));
  194. list.Add(new NameValuePair("PlaySessionId", item.PlaySessionId ?? string.Empty));
  195. list.Add(new NameValuePair("api_key", accessToken ?? string.Empty));
  196. string liveStreamId = item.MediaSource == null ? null : item.MediaSource.LiveStreamId;
  197. list.Add(new NameValuePair("LiveStreamId", liveStreamId ?? string.Empty));
  198. if (isDlna)
  199. {
  200. list.Add(new NameValuePair("ItemId", item.ItemId));
  201. }
  202. list.Add(new NameValuePair("CopyTimestamps", item.CopyTimestamps.ToString().ToLower()));
  203. list.Add(new NameValuePair("SubtitleMethod", item.SubtitleStreamIndex.HasValue && item.SubtitleDeliveryMethod != SubtitleDeliveryMethod.External ? item.SubtitleDeliveryMethod.ToString() : string.Empty));
  204. list.Add(new NameValuePair("TranscodingMaxAudioChannels", item.TranscodingMaxAudioChannels.HasValue ? StringHelper.ToStringCultureInvariant(item.TranscodingMaxAudioChannels.Value) : string.Empty));
  205. list.Add(new NameValuePair("EnableSubtitlesInManifest", item.EnableSubtitlesInManifest.ToString().ToLower()));
  206. list.Add(new NameValuePair("Tag", item.MediaSource.ETag ?? string.Empty));
  207. return list;
  208. }
  209. public List<SubtitleStreamInfo> GetExternalSubtitles(bool includeSelectedTrackOnly, string baseUrl, string accessToken)
  210. {
  211. return GetExternalSubtitles(includeSelectedTrackOnly, false, baseUrl, accessToken);
  212. }
  213. public List<SubtitleStreamInfo> GetExternalSubtitles(bool includeSelectedTrackOnly, bool enableAllProfiles, string baseUrl, string accessToken)
  214. {
  215. List<SubtitleStreamInfo> list = GetSubtitleProfiles(includeSelectedTrackOnly, enableAllProfiles, baseUrl, accessToken);
  216. List<SubtitleStreamInfo> newList = new List<SubtitleStreamInfo>();
  217. // First add the selected track
  218. foreach (SubtitleStreamInfo stream in list)
  219. {
  220. if (stream.DeliveryMethod == SubtitleDeliveryMethod.External)
  221. {
  222. newList.Add(stream);
  223. }
  224. }
  225. return newList;
  226. }
  227. public List<SubtitleStreamInfo> GetSubtitleProfiles(bool includeSelectedTrackOnly, string baseUrl, string accessToken)
  228. {
  229. return GetSubtitleProfiles(includeSelectedTrackOnly, false, baseUrl, accessToken);
  230. }
  231. public List<SubtitleStreamInfo> GetSubtitleProfiles(bool includeSelectedTrackOnly, bool enableAllProfiles, string baseUrl, string accessToken)
  232. {
  233. List<SubtitleStreamInfo> list = new List<SubtitleStreamInfo>();
  234. // HLS will preserve timestamps so we can just grab the full subtitle stream
  235. long startPositionTicks = StringHelper.EqualsIgnoreCase(SubProtocol, "hls")
  236. ? 0
  237. : (PlayMethod == PlayMethod.Transcode && !CopyTimestamps ? StartPositionTicks : 0);
  238. // First add the selected track
  239. if (SubtitleStreamIndex.HasValue)
  240. {
  241. foreach (MediaStream stream in MediaSource.MediaStreams)
  242. {
  243. if (stream.Type == MediaStreamType.Subtitle && stream.Index == SubtitleStreamIndex.Value)
  244. {
  245. AddSubtitleProfiles(list, stream, enableAllProfiles, baseUrl, accessToken, startPositionTicks);
  246. }
  247. }
  248. }
  249. if (!includeSelectedTrackOnly)
  250. {
  251. foreach (MediaStream stream in MediaSource.MediaStreams)
  252. {
  253. if (stream.Type == MediaStreamType.Subtitle && (!SubtitleStreamIndex.HasValue || stream.Index != SubtitleStreamIndex.Value))
  254. {
  255. AddSubtitleProfiles(list, stream, enableAllProfiles, baseUrl, accessToken, startPositionTicks);
  256. }
  257. }
  258. }
  259. return list;
  260. }
  261. private void AddSubtitleProfiles(List<SubtitleStreamInfo> list, MediaStream stream, bool enableAllProfiles, string baseUrl, string accessToken, long startPositionTicks)
  262. {
  263. if (enableAllProfiles)
  264. {
  265. foreach (SubtitleProfile profile in DeviceProfile.SubtitleProfiles)
  266. {
  267. SubtitleStreamInfo info = GetSubtitleStreamInfo(stream, baseUrl, accessToken, startPositionTicks, new[] { profile });
  268. list.Add(info);
  269. }
  270. }
  271. else
  272. {
  273. SubtitleStreamInfo info = GetSubtitleStreamInfo(stream, baseUrl, accessToken, startPositionTicks, DeviceProfile.SubtitleProfiles);
  274. list.Add(info);
  275. }
  276. }
  277. private SubtitleStreamInfo GetSubtitleStreamInfo(MediaStream stream, string baseUrl, string accessToken, long startPositionTicks, SubtitleProfile[] subtitleProfiles)
  278. {
  279. SubtitleProfile subtitleProfile = StreamBuilder.GetSubtitleProfile(stream, subtitleProfiles, PlayMethod);
  280. SubtitleStreamInfo info = new SubtitleStreamInfo
  281. {
  282. IsForced = stream.IsForced,
  283. Language = stream.Language,
  284. Name = stream.Language ?? "Unknown",
  285. Format = subtitleProfile.Format,
  286. Index = stream.Index,
  287. DeliveryMethod = subtitleProfile.Method,
  288. DisplayTitle = stream.DisplayTitle
  289. };
  290. if (info.DeliveryMethod == SubtitleDeliveryMethod.External)
  291. {
  292. if (MediaSource.Protocol == MediaProtocol.File || !StringHelper.EqualsIgnoreCase(stream.Codec, subtitleProfile.Format))
  293. {
  294. info.Url = string.Format("{0}/Videos/{1}/{2}/Subtitles/{3}/{4}/Stream.{5}",
  295. baseUrl,
  296. ItemId,
  297. MediaSourceId,
  298. StringHelper.ToStringCultureInvariant(stream.Index),
  299. StringHelper.ToStringCultureInvariant(startPositionTicks),
  300. subtitleProfile.Format);
  301. if (!string.IsNullOrEmpty(accessToken))
  302. {
  303. info.Url += "?api_key=" + accessToken;
  304. }
  305. info.IsExternalUrl = false;
  306. }
  307. else
  308. {
  309. info.Url = stream.Path;
  310. info.IsExternalUrl = true;
  311. }
  312. }
  313. return info;
  314. }
  315. /// <summary>
  316. /// Returns the audio stream that will be used
  317. /// </summary>
  318. public MediaStream TargetAudioStream
  319. {
  320. get
  321. {
  322. if (MediaSource != null)
  323. {
  324. return MediaSource.GetDefaultAudioStream(AudioStreamIndex);
  325. }
  326. return null;
  327. }
  328. }
  329. /// <summary>
  330. /// Returns the video stream that will be used
  331. /// </summary>
  332. public MediaStream TargetVideoStream
  333. {
  334. get
  335. {
  336. if (MediaSource != null)
  337. {
  338. return MediaSource.VideoStream;
  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 int? TargetAudioSampleRate
  347. {
  348. get
  349. {
  350. MediaStream stream = TargetAudioStream;
  351. return stream == null ? null : stream.SampleRate;
  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. MediaStream stream = TargetVideoStream;
  362. return stream == null || !IsDirectStream ? null : stream.BitDepth;
  363. }
  364. }
  365. /// <summary>
  366. /// Gets the target reference frames.
  367. /// </summary>
  368. /// <value>The target reference frames.</value>
  369. public int? TargetRefFrames
  370. {
  371. get
  372. {
  373. MediaStream stream = TargetVideoStream;
  374. return stream == null || !IsDirectStream ? null : stream.RefFrames;
  375. }
  376. }
  377. /// <summary>
  378. /// Predicts the audio sample rate that will be in the output stream
  379. /// </summary>
  380. public float? TargetFramerate
  381. {
  382. get
  383. {
  384. MediaStream stream = TargetVideoStream;
  385. return MaxFramerate.HasValue && !IsDirectStream
  386. ? MaxFramerate
  387. : stream == null ? null : stream.AverageFrameRate ?? stream.RealFrameRate;
  388. }
  389. }
  390. /// <summary>
  391. /// Predicts the audio sample rate that will be in the output stream
  392. /// </summary>
  393. public double? TargetVideoLevel
  394. {
  395. get
  396. {
  397. MediaStream stream = TargetVideoStream;
  398. return VideoLevel.HasValue && !IsDirectStream
  399. ? VideoLevel
  400. : stream == null ? null : stream.Level;
  401. }
  402. }
  403. /// <summary>
  404. /// Predicts the audio sample rate that will be in the output stream
  405. /// </summary>
  406. public int? TargetPacketLength
  407. {
  408. get
  409. {
  410. MediaStream stream = TargetVideoStream;
  411. return !IsDirectStream
  412. ? null
  413. : stream == null ? null : stream.PacketLength;
  414. }
  415. }
  416. /// <summary>
  417. /// Predicts the audio sample rate that will be in the output stream
  418. /// </summary>
  419. public string TargetVideoProfile
  420. {
  421. get
  422. {
  423. MediaStream stream = TargetVideoStream;
  424. return !string.IsNullOrEmpty(VideoProfile) && !IsDirectStream
  425. ? VideoProfile
  426. : stream == null ? null : stream.Profile;
  427. }
  428. }
  429. /// <summary>
  430. /// Gets the target video codec tag.
  431. /// </summary>
  432. /// <value>The target video codec tag.</value>
  433. public string TargetVideoCodecTag
  434. {
  435. get
  436. {
  437. MediaStream stream = TargetVideoStream;
  438. return !IsDirectStream
  439. ? null
  440. : stream == null ? null : stream.CodecTag;
  441. }
  442. }
  443. /// <summary>
  444. /// Predicts the audio bitrate that will be in the output stream
  445. /// </summary>
  446. public int? TargetAudioBitrate
  447. {
  448. get
  449. {
  450. MediaStream stream = TargetAudioStream;
  451. return AudioBitrate.HasValue && !IsDirectStream
  452. ? AudioBitrate
  453. : stream == null ? null : stream.BitRate;
  454. }
  455. }
  456. /// <summary>
  457. /// Predicts the audio channels that will be in the output stream
  458. /// </summary>
  459. public int? TargetAudioChannels
  460. {
  461. get
  462. {
  463. MediaStream stream = TargetAudioStream;
  464. int? streamChannels = stream == null ? null : stream.Channels;
  465. if (MaxAudioChannels.HasValue && !IsDirectStream)
  466. {
  467. if (streamChannels.HasValue)
  468. {
  469. return Math.Min(MaxAudioChannels.Value, streamChannels.Value);
  470. }
  471. return MaxAudioChannels.Value;
  472. }
  473. return streamChannels;
  474. }
  475. }
  476. /// <summary>
  477. /// Predicts the audio codec that will be in the output stream
  478. /// </summary>
  479. public string TargetAudioCodec
  480. {
  481. get
  482. {
  483. MediaStream stream = TargetAudioStream;
  484. string inputCodec = stream == null ? null : stream.Codec;
  485. if (IsDirectStream)
  486. {
  487. return inputCodec;
  488. }
  489. foreach (string codec in AudioCodecs)
  490. {
  491. if (StringHelper.EqualsIgnoreCase(codec, inputCodec))
  492. {
  493. return codec;
  494. }
  495. }
  496. return AudioCodecs.Length == 0 ? null : AudioCodecs[0];
  497. }
  498. }
  499. /// <summary>
  500. /// Predicts the audio channels that will be in the output stream
  501. /// </summary>
  502. public long? TargetSize
  503. {
  504. get
  505. {
  506. if (IsDirectStream)
  507. {
  508. return MediaSource.Size;
  509. }
  510. if (RunTimeTicks.HasValue)
  511. {
  512. int? totalBitrate = TargetTotalBitrate;
  513. double totalSeconds = RunTimeTicks.Value;
  514. // Convert to ms
  515. totalSeconds /= 10000;
  516. // Convert to seconds
  517. totalSeconds /= 1000;
  518. return totalBitrate.HasValue ?
  519. Convert.ToInt64(totalBitrate.Value * totalSeconds) :
  520. (long?)null;
  521. }
  522. return null;
  523. }
  524. }
  525. public int? TargetVideoBitrate
  526. {
  527. get
  528. {
  529. MediaStream stream = TargetVideoStream;
  530. return VideoBitrate.HasValue && !IsDirectStream
  531. ? VideoBitrate
  532. : stream == null ? null : stream.BitRate;
  533. }
  534. }
  535. public TransportStreamTimestamp TargetTimestamp
  536. {
  537. get
  538. {
  539. TransportStreamTimestamp defaultValue = StringHelper.EqualsIgnoreCase(Container, "m2ts")
  540. ? TransportStreamTimestamp.Valid
  541. : TransportStreamTimestamp.None;
  542. return !IsDirectStream
  543. ? defaultValue
  544. : MediaSource == null ? defaultValue : MediaSource.Timestamp ?? TransportStreamTimestamp.None;
  545. }
  546. }
  547. public int? TargetTotalBitrate
  548. {
  549. get
  550. {
  551. return (TargetAudioBitrate ?? 0) + (TargetVideoBitrate ?? 0);
  552. }
  553. }
  554. public bool? IsTargetAnamorphic
  555. {
  556. get
  557. {
  558. if (IsDirectStream)
  559. {
  560. return TargetVideoStream == null ? null : TargetVideoStream.IsAnamorphic;
  561. }
  562. return false;
  563. }
  564. }
  565. public int? TargetWidth
  566. {
  567. get
  568. {
  569. MediaStream videoStream = TargetVideoStream;
  570. if (videoStream != null && videoStream.Width.HasValue && videoStream.Height.HasValue)
  571. {
  572. ImageSize size = new ImageSize
  573. {
  574. Width = videoStream.Width.Value,
  575. Height = videoStream.Height.Value
  576. };
  577. double? maxWidth = MaxWidth.HasValue ? (double)MaxWidth.Value : (double?)null;
  578. double? maxHeight = MaxHeight.HasValue ? (double)MaxHeight.Value : (double?)null;
  579. ImageSize newSize = DrawingUtils.Resize(size,
  580. null,
  581. null,
  582. maxWidth,
  583. maxHeight);
  584. return Convert.ToInt32(newSize.Width);
  585. }
  586. return MaxWidth;
  587. }
  588. }
  589. public int? TargetHeight
  590. {
  591. get
  592. {
  593. MediaStream videoStream = TargetVideoStream;
  594. if (videoStream != null && videoStream.Width.HasValue && videoStream.Height.HasValue)
  595. {
  596. ImageSize size = new ImageSize
  597. {
  598. Width = videoStream.Width.Value,
  599. Height = videoStream.Height.Value
  600. };
  601. double? maxWidth = MaxWidth.HasValue ? (double)MaxWidth.Value : (double?)null;
  602. double? maxHeight = MaxHeight.HasValue ? (double)MaxHeight.Value : (double?)null;
  603. ImageSize newSize = DrawingUtils.Resize(size,
  604. null,
  605. null,
  606. maxWidth,
  607. maxHeight);
  608. return Convert.ToInt32(newSize.Height);
  609. }
  610. return MaxHeight;
  611. }
  612. }
  613. public int? TargetVideoStreamCount
  614. {
  615. get
  616. {
  617. if (IsDirectStream)
  618. {
  619. return GetMediaStreamCount(MediaStreamType.Video, int.MaxValue);
  620. }
  621. return GetMediaStreamCount(MediaStreamType.Video, 1);
  622. }
  623. }
  624. public int? TargetAudioStreamCount
  625. {
  626. get
  627. {
  628. if (IsDirectStream)
  629. {
  630. return GetMediaStreamCount(MediaStreamType.Audio, int.MaxValue);
  631. }
  632. return GetMediaStreamCount(MediaStreamType.Audio, 1);
  633. }
  634. }
  635. private int? GetMediaStreamCount(MediaStreamType type, int limit)
  636. {
  637. var count = MediaSource.GetStreamCount(type);
  638. if (count.HasValue)
  639. {
  640. count = Math.Min(count.Value, limit);
  641. }
  642. return count;
  643. }
  644. public List<MediaStream> GetSelectableAudioStreams()
  645. {
  646. return GetSelectableStreams(MediaStreamType.Audio);
  647. }
  648. public List<MediaStream> GetSelectableSubtitleStreams()
  649. {
  650. return GetSelectableStreams(MediaStreamType.Subtitle);
  651. }
  652. public List<MediaStream> GetSelectableStreams(MediaStreamType type)
  653. {
  654. List<MediaStream> list = new List<MediaStream>();
  655. foreach (MediaStream stream in MediaSource.MediaStreams)
  656. {
  657. if (type == stream.Type)
  658. {
  659. list.Add(stream);
  660. }
  661. }
  662. return list;
  663. }
  664. }
  665. }