jellyfin/MediaBrowser.MediaEncoding/BdInfo/BdInfoExaminer.cs

188 lines
5.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using BDInfo;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.MediaInfo;
2023-02-18 13:42:35 +00:00
namespace MediaBrowser.MediaEncoding.BdInfo;
/// <summary>
/// Class BdInfoExaminer.
/// </summary>
public class BdInfoExaminer : IBlurayExaminer
{
2023-02-18 13:42:35 +00:00
private readonly IFileSystem _fileSystem;
/// <summary>
2023-02-18 13:42:35 +00:00
/// Initializes a new instance of the <see cref="BdInfoExaminer" /> class.
/// </summary>
2023-02-18 13:42:35 +00:00
/// <param name="fileSystem">The filesystem.</param>
public BdInfoExaminer(IFileSystem fileSystem)
{
2023-02-18 13:42:35 +00:00
_fileSystem = fileSystem;
}
2023-02-18 13:42:35 +00:00
/// <summary>
/// Gets the disc info.
/// </summary>
/// <param name="path">The path.</param>
/// <returns>BlurayDiscInfo.</returns>
public BlurayDiscInfo GetDiscInfo(string path)
{
if (string.IsNullOrWhiteSpace(path))
{
2023-02-18 13:42:35 +00:00
throw new ArgumentNullException(nameof(path));
}
2023-02-18 13:42:35 +00:00
var bdrom = new BDROM(BdInfoDirectoryInfo.FromFileSystemPath(_fileSystem, path));
2023-02-18 13:42:35 +00:00
bdrom.Scan();
2023-02-18 13:42:35 +00:00
// Get the longest playlist
var playlist = bdrom.PlaylistFiles.Values.OrderByDescending(p => p.TotalLength).FirstOrDefault(p => p.IsValid);
2023-02-18 13:42:35 +00:00
var outputStream = new BlurayDiscInfo
{
MediaStreams = Array.Empty<MediaStream>()
};
2023-02-18 13:42:35 +00:00
if (playlist is null)
{
return outputStream;
}
2023-02-18 13:42:35 +00:00
outputStream.Chapters = playlist.Chapters.ToArray();
2023-02-18 13:42:35 +00:00
outputStream.RunTimeTicks = TimeSpan.FromSeconds(playlist.TotalLength).Ticks;
2023-02-18 13:42:35 +00:00
var sortedStreams = playlist.SortedStreams;
var mediaStreams = new List<MediaStream>(sortedStreams.Count);
2023-02-18 13:42:35 +00:00
foreach (var stream in sortedStreams)
{
switch (stream)
{
2023-02-18 13:42:35 +00:00
case TSVideoStream videoStream:
AddVideoStream(mediaStreams, videoStream);
2023-02-18 13:42:35 +00:00
break;
case TSAudioStream audioStream:
AddAudioStream(mediaStreams, audioStream);
2023-02-18 13:42:35 +00:00
break;
case TSTextStream textStream:
AddSubtitleStream(mediaStreams, textStream);
2023-02-18 13:42:35 +00:00
break;
case TSGraphicsStream graphicStream:
AddSubtitleStream(mediaStreams, graphicStream);
break;
}
2023-02-18 13:42:35 +00:00
}
2023-02-18 13:42:35 +00:00
outputStream.MediaStreams = mediaStreams.ToArray();
2023-02-18 13:42:35 +00:00
outputStream.PlaylistName = playlist.Name;
2023-02-18 13:42:35 +00:00
if (playlist.StreamClips is not null && playlist.StreamClips.Count > 0)
{
// Get the files in the playlist
outputStream.Files = playlist.StreamClips.Select(i => i.StreamFile.Name).ToArray();
}
2023-02-18 13:42:35 +00:00
return outputStream;
}
2023-02-18 13:42:35 +00:00
/// <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
{
BitRate = Convert.ToInt32(videoStream.BitRate),
Width = videoStream.Width,
Height = videoStream.Height,
Codec = videoStream.CodecShortName,
IsInterlaced = videoStream.IsInterlaced,
Type = MediaStreamType.Video,
Index = streams.Count
};
if (videoStream.FrameRateDenominator > 0)
{
float frameRateEnumerator = videoStream.FrameRateEnumerator;
float frameRateDenominator = videoStream.FrameRateDenominator;
2023-02-18 13:42:35 +00:00
mediaStream.AverageFrameRate = mediaStream.RealFrameRate = frameRateEnumerator / frameRateDenominator;
}
2023-02-18 13:42:35 +00:00
streams.Add(mediaStream);
}
2023-02-18 13:42:35 +00:00
/// <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)
{
var stream = new MediaStream
{
Codec = audioStream.CodecShortName,
Language = audioStream.LanguageCode,
Channels = audioStream.ChannelCount,
SampleRate = audioStream.SampleRate,
Type = MediaStreamType.Audio,
Index = streams.Count
};
2023-02-18 13:42:35 +00:00
var bitrate = Convert.ToInt32(audioStream.BitRate);
2023-02-18 13:42:35 +00:00
if (bitrate > 0)
{
stream.BitRate = bitrate;
}
2023-02-18 13:42:35 +00:00
if (audioStream.LFE > 0)
{
2023-02-18 13:42:35 +00:00
stream.Channels = audioStream.ChannelCount + 1;
}
2023-02-18 13:42:35 +00:00
streams.Add(stream);
}
/// <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
{
Language = textStream.LanguageCode,
Codec = textStream.CodecShortName,
Type = MediaStreamType.Subtitle,
Index = streams.Count
});
}
/// <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
{
2023-02-18 13:42:35 +00:00
Language = textStream.LanguageCode,
Codec = textStream.CodecShortName,
Type = MediaStreamType.Subtitle,
Index = streams.Count
});
}
}