BdInfoExaminer.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using BDInfo;
  5. using MediaBrowser.Model.Entities;
  6. using MediaBrowser.Model.IO;
  7. using MediaBrowser.Model.MediaInfo;
  8. namespace MediaBrowser.MediaEncoding.BdInfo
  9. {
  10. /// <summary>
  11. /// Class BdInfoExaminer.
  12. /// </summary>
  13. public class BdInfoExaminer : IBlurayExaminer
  14. {
  15. private readonly IFileSystem _fileSystem;
  16. /// <summary>
  17. /// Initializes a new instance of the <see cref="BdInfoExaminer" /> class.
  18. /// </summary>
  19. /// <param name="fileSystem">The filesystem.</param>
  20. public BdInfoExaminer(IFileSystem fileSystem)
  21. {
  22. _fileSystem = fileSystem;
  23. }
  24. /// <summary>
  25. /// Gets the disc info.
  26. /// </summary>
  27. /// <param name="path">The path.</param>
  28. /// <returns>BlurayDiscInfo.</returns>
  29. public BlurayDiscInfo GetDiscInfo(string path)
  30. {
  31. if (string.IsNullOrWhiteSpace(path))
  32. {
  33. throw new ArgumentNullException(nameof(path));
  34. }
  35. var bdrom = new BDROM(BdInfoDirectoryInfo.FromFileSystemPath(_fileSystem, path));
  36. bdrom.Scan();
  37. // Get the longest playlist
  38. var playlist = bdrom.PlaylistFiles.Values.OrderByDescending(p => p.TotalLength).FirstOrDefault(p => p.IsValid);
  39. var outputStream = new BlurayDiscInfo
  40. {
  41. MediaStreams = Array.Empty<MediaStream>()
  42. };
  43. if (playlist == null)
  44. {
  45. return outputStream;
  46. }
  47. outputStream.Chapters = playlist.Chapters.ToArray();
  48. outputStream.RunTimeTicks = TimeSpan.FromSeconds(playlist.TotalLength).Ticks;
  49. var mediaStreams = new List<MediaStream>();
  50. foreach (var stream in playlist.SortedStreams)
  51. {
  52. if (stream is TSVideoStream videoStream)
  53. {
  54. AddVideoStream(mediaStreams, videoStream);
  55. continue;
  56. }
  57. if (stream is TSAudioStream audioStream)
  58. {
  59. AddAudioStream(mediaStreams, audioStream);
  60. continue;
  61. }
  62. if (stream is TSTextStream textStream)
  63. {
  64. AddSubtitleStream(mediaStreams, textStream);
  65. continue;
  66. }
  67. if (stream is TSGraphicsStream graphicsStream)
  68. {
  69. AddSubtitleStream(mediaStreams, graphicsStream);
  70. }
  71. }
  72. outputStream.MediaStreams = mediaStreams.ToArray();
  73. outputStream.PlaylistName = playlist.Name;
  74. if (playlist.StreamClips != null && playlist.StreamClips.Any())
  75. {
  76. // Get the files in the playlist
  77. outputStream.Files = playlist.StreamClips.Select(i => i.StreamFile.Name).ToArray();
  78. }
  79. return outputStream;
  80. }
  81. /// <summary>
  82. /// Adds the video stream.
  83. /// </summary>
  84. /// <param name="streams">The streams.</param>
  85. /// <param name="videoStream">The video stream.</param>
  86. private void AddVideoStream(List<MediaStream> streams, TSVideoStream videoStream)
  87. {
  88. var mediaStream = new MediaStream
  89. {
  90. BitRate = Convert.ToInt32(videoStream.BitRate),
  91. Width = videoStream.Width,
  92. Height = videoStream.Height,
  93. Codec = videoStream.CodecShortName,
  94. IsInterlaced = videoStream.IsInterlaced,
  95. Type = MediaStreamType.Video,
  96. Index = streams.Count
  97. };
  98. if (videoStream.FrameRateDenominator > 0)
  99. {
  100. float frameRateEnumerator = videoStream.FrameRateEnumerator;
  101. float frameRateDenominator = videoStream.FrameRateDenominator;
  102. mediaStream.AverageFrameRate = mediaStream.RealFrameRate = frameRateEnumerator / frameRateDenominator;
  103. }
  104. streams.Add(mediaStream);
  105. }
  106. /// <summary>
  107. /// Adds the audio stream.
  108. /// </summary>
  109. /// <param name="streams">The streams.</param>
  110. /// <param name="audioStream">The audio stream.</param>
  111. private void AddAudioStream(List<MediaStream> streams, TSAudioStream audioStream)
  112. {
  113. var stream = new MediaStream
  114. {
  115. Codec = audioStream.CodecShortName,
  116. Language = audioStream.LanguageCode,
  117. Channels = audioStream.ChannelCount,
  118. SampleRate = audioStream.SampleRate,
  119. Type = MediaStreamType.Audio,
  120. Index = streams.Count
  121. };
  122. var bitrate = Convert.ToInt32(audioStream.BitRate);
  123. if (bitrate > 0)
  124. {
  125. stream.BitRate = bitrate;
  126. }
  127. if (audioStream.LFE > 0)
  128. {
  129. stream.Channels = audioStream.ChannelCount + 1;
  130. }
  131. streams.Add(stream);
  132. }
  133. /// <summary>
  134. /// Adds the subtitle stream.
  135. /// </summary>
  136. /// <param name="streams">The streams.</param>
  137. /// <param name="textStream">The text stream.</param>
  138. private void AddSubtitleStream(List<MediaStream> streams, TSTextStream textStream)
  139. {
  140. streams.Add(new MediaStream
  141. {
  142. Language = textStream.LanguageCode,
  143. Codec = textStream.CodecShortName,
  144. Type = MediaStreamType.Subtitle,
  145. Index = streams.Count
  146. });
  147. }
  148. /// <summary>
  149. /// Adds the subtitle stream.
  150. /// </summary>
  151. /// <param name="streams">The streams.</param>
  152. /// <param name="textStream">The text stream.</param>
  153. private void AddSubtitleStream(List<MediaStream> streams, TSGraphicsStream textStream)
  154. {
  155. streams.Add(new MediaStream
  156. {
  157. Language = textStream.LanguageCode,
  158. Codec = textStream.CodecShortName,
  159. Type = MediaStreamType.Subtitle,
  160. Index = streams.Count
  161. });
  162. }
  163. }
  164. }