BdInfoExaminer.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. var videoStream = stream as TSVideoStream;
  53. if (videoStream != null)
  54. {
  55. AddVideoStream(mediaStreams, videoStream);
  56. continue;
  57. }
  58. var audioStream = stream as TSAudioStream;
  59. if (audioStream != null)
  60. {
  61. AddAudioStream(mediaStreams, audioStream);
  62. continue;
  63. }
  64. var textStream = stream as TSTextStream;
  65. if (textStream != null)
  66. {
  67. AddSubtitleStream(mediaStreams, textStream);
  68. continue;
  69. }
  70. var graphicsStream = stream as TSGraphicsStream;
  71. if (graphicsStream != null)
  72. {
  73. AddSubtitleStream(mediaStreams, graphicsStream);
  74. }
  75. }
  76. outputStream.MediaStreams = mediaStreams.ToArray();
  77. outputStream.PlaylistName = playlist.Name;
  78. if (playlist.StreamClips != null && playlist.StreamClips.Any())
  79. {
  80. // Get the files in the playlist
  81. outputStream.Files = playlist.StreamClips.Select(i => i.StreamFile.Name).ToArray();
  82. }
  83. return outputStream;
  84. }
  85. /// <summary>
  86. /// Adds the video stream.
  87. /// </summary>
  88. /// <param name="streams">The streams.</param>
  89. /// <param name="videoStream">The video stream.</param>
  90. private void AddVideoStream(List<MediaStream> streams, TSVideoStream videoStream)
  91. {
  92. var mediaStream = new MediaStream
  93. {
  94. BitRate = Convert.ToInt32(videoStream.BitRate),
  95. Width = videoStream.Width,
  96. Height = videoStream.Height,
  97. Codec = videoStream.CodecShortName,
  98. IsInterlaced = videoStream.IsInterlaced,
  99. Type = MediaStreamType.Video,
  100. Index = streams.Count
  101. };
  102. if (videoStream.FrameRateDenominator > 0)
  103. {
  104. float frameRateEnumerator = videoStream.FrameRateEnumerator;
  105. float frameRateDenominator = videoStream.FrameRateDenominator;
  106. mediaStream.AverageFrameRate = mediaStream.RealFrameRate = frameRateEnumerator / frameRateDenominator;
  107. }
  108. streams.Add(mediaStream);
  109. }
  110. /// <summary>
  111. /// Adds the audio stream.
  112. /// </summary>
  113. /// <param name="streams">The streams.</param>
  114. /// <param name="audioStream">The audio stream.</param>
  115. private void AddAudioStream(List<MediaStream> streams, TSAudioStream audioStream)
  116. {
  117. var stream = new MediaStream
  118. {
  119. Codec = audioStream.CodecShortName,
  120. Language = audioStream.LanguageCode,
  121. Channels = audioStream.ChannelCount,
  122. SampleRate = audioStream.SampleRate,
  123. Type = MediaStreamType.Audio,
  124. Index = streams.Count
  125. };
  126. var bitrate = Convert.ToInt32(audioStream.BitRate);
  127. if (bitrate > 0)
  128. {
  129. stream.BitRate = bitrate;
  130. }
  131. if (audioStream.LFE > 0)
  132. {
  133. stream.Channels = audioStream.ChannelCount + 1;
  134. }
  135. streams.Add(stream);
  136. }
  137. /// <summary>
  138. /// Adds the subtitle stream.
  139. /// </summary>
  140. /// <param name="streams">The streams.</param>
  141. /// <param name="textStream">The text stream.</param>
  142. private void AddSubtitleStream(List<MediaStream> streams, TSTextStream textStream)
  143. {
  144. streams.Add(new MediaStream
  145. {
  146. Language = textStream.LanguageCode,
  147. Codec = textStream.CodecShortName,
  148. Type = MediaStreamType.Subtitle,
  149. Index = streams.Count
  150. });
  151. }
  152. /// <summary>
  153. /// Adds the subtitle stream.
  154. /// </summary>
  155. /// <param name="streams">The streams.</param>
  156. /// <param name="textStream">The text stream.</param>
  157. private void AddSubtitleStream(List<MediaStream> streams, TSGraphicsStream textStream)
  158. {
  159. streams.Add(new MediaStream
  160. {
  161. Language = textStream.LanguageCode,
  162. Codec = textStream.CodecShortName,
  163. Type = MediaStreamType.Subtitle,
  164. Index = streams.Count
  165. });
  166. }
  167. }
  168. }