jellyfin/MediaBrowser.MediaEncoding/BdInfo/BdInfoExaminer.cs

197 lines
6.2 KiB
C#
Raw Normal View History

using BDInfo;
using MediaBrowser.Model.Entities;
2013-02-21 06:38:23 +00:00
using MediaBrowser.Model.MediaInfo;
using System;
using System.Collections.Generic;
using System.Linq;
2016-10-30 07:11:37 +00:00
using MediaBrowser.Model.IO;
using MediaBrowser.Model.TextEncoding;
2013-02-21 06:38:23 +00:00
2014-03-27 19:30:21 +00:00
namespace MediaBrowser.MediaEncoding.BdInfo
2013-02-21 06:38:23 +00:00
{
/// <summary>
/// Class BdInfoExaminer
/// </summary>
public class BdInfoExaminer : IBlurayExaminer
{
2016-10-30 07:11:37 +00:00
private readonly IFileSystem _fileSystem;
private readonly IEncoding _textEncoding;
public BdInfoExaminer(IFileSystem fileSystem, IEncoding textEncoding)
{
_fileSystem = fileSystem;
_textEncoding = textEncoding;
}
2013-02-21 06:38:23 +00:00
/// <summary>
/// Gets the disc info.
/// </summary>
/// <param name="path">The path.</param>
/// <returns>BlurayDiscInfo.</returns>
public BlurayDiscInfo GetDiscInfo(string path)
{
2016-10-30 07:11:37 +00:00
var bdrom = new BDROM(path, _fileSystem, _textEncoding);
2013-02-21 06:38:23 +00:00
bdrom.Scan();
// Get the longest playlist
var playlist = bdrom.PlaylistFiles.Values.OrderByDescending(p => p.TotalLength).FirstOrDefault(p => p.IsValid);
var outputStream = new BlurayDiscInfo
{
MediaStreams = new List<MediaStream>()
};
if (playlist == null)
{
return outputStream;
}
outputStream.Chapters = playlist.Chapters;
outputStream.RunTimeTicks = TimeSpan.FromSeconds(playlist.TotalLength).Ticks;
var mediaStreams = new List<MediaStream>();
2013-02-21 06:38:23 +00:00
foreach (var stream in playlist.SortedStreams)
{
var videoStream = stream as TSVideoStream;
if (videoStream != null)
{
AddVideoStream(mediaStreams, videoStream);
continue;
}
var audioStream = stream as TSAudioStream;
if (audioStream != null)
{
AddAudioStream(mediaStreams, audioStream);
continue;
}
var textStream = stream as TSTextStream;
if (textStream != null)
{
AddSubtitleStream(mediaStreams, textStream);
continue;
}
var graphicsStream = stream as TSGraphicsStream;
if (graphicsStream != null)
{
AddSubtitleStream(mediaStreams, graphicsStream);
}
}
outputStream.MediaStreams = mediaStreams;
2013-09-02 01:35:57 +00:00
outputStream.PlaylistName = playlist.Name;
2013-02-21 06:38:23 +00:00
if (playlist.StreamClips != null && playlist.StreamClips.Any())
{
// Get the files in the playlist
outputStream.Files = playlist.StreamClips.Select(i => i.StreamFile.Name).ToList();
}
return outputStream;
}
/// <summary>
/// Adds the video stream.
/// </summary>
/// <param name="streams">The streams.</param>
/// <param name="videoStream">The video stream.</param>
private void AddVideoStream(List<MediaStream> streams, TSVideoStream videoStream)
{
var mediaStream = new MediaStream
2013-05-24 04:02:42 +00:00
{
BitRate = Convert.ToInt32(videoStream.BitRate),
Width = videoStream.Width,
Height = videoStream.Height,
Codec = videoStream.CodecShortName,
2013-12-06 03:39:44 +00:00
IsInterlaced = videoStream.IsInterlaced,
2013-05-24 04:02:42 +00:00
Type = MediaStreamType.Video,
Index = streams.Count
};
2013-02-21 06:38:23 +00:00
if (videoStream.FrameRateDenominator > 0)
{
float frameRateEnumerator = videoStream.FrameRateEnumerator;
float frameRateDenominator = videoStream.FrameRateDenominator;
mediaStream.AverageFrameRate = mediaStream.RealFrameRate = frameRateEnumerator / frameRateDenominator;
}
streams.Add(mediaStream);
}
/// <summary>
/// Adds the audio stream.
/// </summary>
/// <param name="streams">The streams.</param>
/// <param name="audioStream">The audio stream.</param>
private void AddAudioStream(List<MediaStream> streams, TSAudioStream audioStream)
{
2013-05-25 13:06:40 +00:00
var stream = new MediaStream
2013-05-24 04:02:42 +00:00
{
Codec = audioStream.CodecShortName,
Language = audioStream.LanguageCode,
Channels = audioStream.ChannelCount,
SampleRate = audioStream.SampleRate,
Type = MediaStreamType.Audio,
Index = streams.Count
2013-05-25 13:06:40 +00:00
};
var bitrate = Convert.ToInt32(audioStream.BitRate);
if (bitrate > 0)
{
stream.BitRate = bitrate;
}
2013-05-25 13:06:40 +00:00
if (audioStream.LFE > 0)
{
stream.Channels = audioStream.ChannelCount + 1;
}
2013-12-06 03:39:44 +00:00
2013-05-25 13:06:40 +00:00
streams.Add(stream);
2013-02-21 06:38:23 +00:00
}
/// <summary>
/// Adds the subtitle stream.
/// </summary>
/// <param name="streams">The streams.</param>
/// <param name="textStream">The text stream.</param>
private void AddSubtitleStream(List<MediaStream> streams, TSTextStream textStream)
{
streams.Add(new MediaStream
2013-05-24 04:02:42 +00:00
{
Language = textStream.LanguageCode,
Codec = textStream.CodecShortName,
Type = MediaStreamType.Subtitle,
Index = streams.Count
});
2013-02-21 06:38:23 +00:00
}
/// <summary>
/// Adds the subtitle stream.
/// </summary>
/// <param name="streams">The streams.</param>
/// <param name="textStream">The text stream.</param>
private void AddSubtitleStream(List<MediaStream> streams, TSGraphicsStream textStream)
{
streams.Add(new MediaStream
{
Language = textStream.LanguageCode,
Codec = textStream.CodecShortName,
Type = MediaStreamType.Subtitle,
Index = streams.Count
});
}
}
}