From 888b8d619aec031f57cfd03410ccda52edcca958 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Thu, 20 Feb 2014 11:37:41 -0500 Subject: added encoding manager interface --- .../MediaEncoding/ChapterImageRefreshOptions.cs | 17 + .../MediaEncoding/IEncodingManager.cs | 33 ++ .../MediaEncoding/IMediaEncoder.cs | 108 ++++++ .../MediaEncoding/InternalMediaInfoResult.cs | 311 ++++++++++++++++++ .../MediaEncoding/MediaEncoderHelpers.cs | 365 +++++++++++++++++++++ 5 files changed, 834 insertions(+) create mode 100644 MediaBrowser.Controller/MediaEncoding/ChapterImageRefreshOptions.cs create mode 100644 MediaBrowser.Controller/MediaEncoding/IEncodingManager.cs create mode 100644 MediaBrowser.Controller/MediaEncoding/IMediaEncoder.cs create mode 100644 MediaBrowser.Controller/MediaEncoding/InternalMediaInfoResult.cs create mode 100644 MediaBrowser.Controller/MediaEncoding/MediaEncoderHelpers.cs (limited to 'MediaBrowser.Controller/MediaEncoding') diff --git a/MediaBrowser.Controller/MediaEncoding/ChapterImageRefreshOptions.cs b/MediaBrowser.Controller/MediaEncoding/ChapterImageRefreshOptions.cs new file mode 100644 index 000000000..e11bd6cdf --- /dev/null +++ b/MediaBrowser.Controller/MediaEncoding/ChapterImageRefreshOptions.cs @@ -0,0 +1,17 @@ +using MediaBrowser.Controller.Entities; +using MediaBrowser.Model.Entities; +using System.Collections.Generic; + +namespace MediaBrowser.Controller.MediaEncoding +{ + public class ChapterImageRefreshOptions + { + public Video Video { get; set; } + + public List Chapters { get; set; } + + public bool SaveChapters { get; set; } + + public bool ExtractImages { get; set; } + } +} diff --git a/MediaBrowser.Controller/MediaEncoding/IEncodingManager.cs b/MediaBrowser.Controller/MediaEncoding/IEncodingManager.cs new file mode 100644 index 000000000..d1e40e3f0 --- /dev/null +++ b/MediaBrowser.Controller/MediaEncoding/IEncodingManager.cs @@ -0,0 +1,33 @@ +using System.Threading; +using System.Threading.Tasks; + +namespace MediaBrowser.Controller.MediaEncoding +{ + public interface IEncodingManager + { + /// + /// Gets the subtitle cache path. + /// + /// The original subtitle path. + /// The output subtitle extension. + /// System.String. + string GetSubtitleCachePath(string originalSubtitlePath, string outputSubtitleExtension); + + /// + /// Gets the subtitle cache path. + /// + /// The media path. + /// Index of the subtitle stream. + /// The output subtitle extension. + /// System.String. + string GetSubtitleCachePath(string mediaPath, int subtitleStreamIndex, string outputSubtitleExtension); + + /// + /// Refreshes the chapter images. + /// + /// The options. + /// The cancellation token. + /// Task{System.Boolean}. + Task RefreshChapterImages(ChapterImageRefreshOptions options, CancellationToken cancellationToken); + } +} diff --git a/MediaBrowser.Controller/MediaEncoding/IMediaEncoder.cs b/MediaBrowser.Controller/MediaEncoding/IMediaEncoder.cs new file mode 100644 index 000000000..119688fa7 --- /dev/null +++ b/MediaBrowser.Controller/MediaEncoding/IMediaEncoder.cs @@ -0,0 +1,108 @@ +using MediaBrowser.Model.Entities; +using System; +using System.IO; +using System.Threading; +using System.Threading.Tasks; + +namespace MediaBrowser.Controller.MediaEncoding +{ + /// + /// Interface IMediaEncoder + /// + public interface IMediaEncoder + { + /// + /// Gets the encoder path. + /// + /// The encoder path. + string EncoderPath { get; } + + /// + /// Gets the version. + /// + /// The version. + string Version { get; } + + /// + /// Extracts the image. + /// + /// The input files. + /// The type. + /// if set to true [is audio]. + /// The threed format. + /// The offset. + /// The cancellation token. + /// Task{Stream}. + Task ExtractImage(string[] inputFiles, InputType type, bool isAudio, Video3DFormat? threedFormat, TimeSpan? offset, CancellationToken cancellationToken); + + /// + /// Extracts the text subtitle. + /// + /// The input files. + /// The type. + /// Index of the subtitle stream. + /// if set to true, copy stream instead of converting. + /// The output path. + /// The cancellation token. + /// Task. + Task ExtractTextSubtitle(string[] inputFiles, InputType type, int subtitleStreamIndex, bool copySubtitleStream, string outputPath, CancellationToken cancellationToken); + + /// + /// Converts the text subtitle to ass. + /// + /// The input path. + /// The output path. + /// The language. + /// The cancellation token. + /// Task. + Task ConvertTextSubtitleToAss(string inputPath, string outputPath, string language, CancellationToken cancellationToken); + + /// + /// Gets the media info. + /// + /// The input files. + /// The type. + /// if set to true [is audio]. + /// The cancellation token. + /// Task. + Task GetMediaInfo(string[] inputFiles, InputType type, bool isAudio, CancellationToken cancellationToken); + + /// + /// Gets the probe size argument. + /// + /// The type. + /// System.String. + string GetProbeSizeArgument(InputType type); + + /// + /// Gets the input argument. + /// + /// The input files. + /// The type. + /// System.String. + string GetInputArgument(string[] inputFiles, InputType type); + } + + /// + /// Enum InputType + /// + public enum InputType + { + /// + /// The file + /// + File, + /// + /// The bluray + /// + Bluray, + /// + /// The DVD + /// + Dvd, + /// + /// The URL + /// + Url + } +} diff --git a/MediaBrowser.Controller/MediaEncoding/InternalMediaInfoResult.cs b/MediaBrowser.Controller/MediaEncoding/InternalMediaInfoResult.cs new file mode 100644 index 000000000..e113521ec --- /dev/null +++ b/MediaBrowser.Controller/MediaEncoding/InternalMediaInfoResult.cs @@ -0,0 +1,311 @@ +using MediaBrowser.Model.Entities; +using System.Collections.Generic; + +namespace MediaBrowser.Controller.MediaEncoding +{ + /// + /// Class MediaInfoResult + /// + public class InternalMediaInfoResult + { + /// + /// Gets or sets the streams. + /// + /// The streams. + public MediaStreamInfo[] streams { get; set; } + + /// + /// Gets or sets the format. + /// + /// The format. + public MediaFormatInfo format { get; set; } + + /// + /// Gets or sets the chapters. + /// + /// The chapters. + public List Chapters { get; set; } + } + + /// + /// Represents a stream within the output + /// + public class MediaStreamInfo + { + /// + /// Gets or sets the index. + /// + /// The index. + public int index { get; set; } + + /// + /// Gets or sets the profile. + /// + /// The profile. + public string profile { get; set; } + + /// + /// Gets or sets the codec_name. + /// + /// The codec_name. + public string codec_name { get; set; } + + /// + /// Gets or sets the codec_long_name. + /// + /// The codec_long_name. + public string codec_long_name { get; set; } + + /// + /// Gets or sets the codec_type. + /// + /// The codec_type. + public string codec_type { get; set; } + + /// + /// Gets or sets the sample_rate. + /// + /// The sample_rate. + public string sample_rate { get; set; } + + /// + /// Gets or sets the channels. + /// + /// The channels. + public int channels { get; set; } + + /// + /// Gets or sets the channel_layout. + /// + /// The channel_layout. + public string channel_layout { get; set; } + + /// + /// Gets or sets the avg_frame_rate. + /// + /// The avg_frame_rate. + public string avg_frame_rate { get; set; } + + /// + /// Gets or sets the duration. + /// + /// The duration. + public string duration { get; set; } + + /// + /// Gets or sets the bit_rate. + /// + /// The bit_rate. + public string bit_rate { get; set; } + + /// + /// Gets or sets the width. + /// + /// The width. + public int width { get; set; } + + /// + /// Gets or sets the height. + /// + /// The height. + public int height { get; set; } + + /// + /// Gets or sets the display_aspect_ratio. + /// + /// The display_aspect_ratio. + public string display_aspect_ratio { get; set; } + + /// + /// Gets or sets the tags. + /// + /// The tags. + public Dictionary tags { get; set; } + + /// + /// Gets or sets the bits_per_sample. + /// + /// The bits_per_sample. + public int bits_per_sample { get; set; } + + /// + /// Gets or sets the r_frame_rate. + /// + /// The r_frame_rate. + public string r_frame_rate { get; set; } + + /// + /// Gets or sets the has_b_frames. + /// + /// The has_b_frames. + public int has_b_frames { get; set; } + + /// + /// Gets or sets the sample_aspect_ratio. + /// + /// The sample_aspect_ratio. + public string sample_aspect_ratio { get; set; } + + /// + /// Gets or sets the pix_fmt. + /// + /// The pix_fmt. + public string pix_fmt { get; set; } + + /// + /// Gets or sets the level. + /// + /// The level. + public int level { get; set; } + + /// + /// Gets or sets the time_base. + /// + /// The time_base. + public string time_base { get; set; } + + /// + /// Gets or sets the start_time. + /// + /// The start_time. + public string start_time { get; set; } + + /// + /// Gets or sets the codec_time_base. + /// + /// The codec_time_base. + public string codec_time_base { get; set; } + + /// + /// Gets or sets the codec_tag. + /// + /// The codec_tag. + public string codec_tag { get; set; } + + /// + /// Gets or sets the codec_tag_string. + /// + /// The codec_tag_string. + public string codec_tag_string { get; set; } + + /// + /// Gets or sets the sample_fmt. + /// + /// The sample_fmt. + public string sample_fmt { get; set; } + + /// + /// Gets or sets the dmix_mode. + /// + /// The dmix_mode. + public string dmix_mode { get; set; } + + /// + /// Gets or sets the start_pts. + /// + /// The start_pts. + public string start_pts { get; set; } + + /// + /// Gets or sets the is_avc. + /// + /// The is_avc. + public string is_avc { get; set; } + + /// + /// Gets or sets the nal_length_size. + /// + /// The nal_length_size. + public string nal_length_size { get; set; } + + /// + /// Gets or sets the ltrt_cmixlev. + /// + /// The ltrt_cmixlev. + public string ltrt_cmixlev { get; set; } + + /// + /// Gets or sets the ltrt_surmixlev. + /// + /// The ltrt_surmixlev. + public string ltrt_surmixlev { get; set; } + + /// + /// Gets or sets the loro_cmixlev. + /// + /// The loro_cmixlev. + public string loro_cmixlev { get; set; } + + /// + /// Gets or sets the loro_surmixlev. + /// + /// The loro_surmixlev. + public string loro_surmixlev { get; set; } + + /// + /// Gets or sets the disposition. + /// + /// The disposition. + public Dictionary disposition { get; set; } + } + + /// + /// Class MediaFormat + /// + public class MediaFormatInfo + { + /// + /// Gets or sets the filename. + /// + /// The filename. + public string filename { get; set; } + + /// + /// Gets or sets the nb_streams. + /// + /// The nb_streams. + public int nb_streams { get; set; } + + /// + /// Gets or sets the format_name. + /// + /// The format_name. + public string format_name { get; set; } + + /// + /// Gets or sets the format_long_name. + /// + /// The format_long_name. + public string format_long_name { get; set; } + + /// + /// Gets or sets the start_time. + /// + /// The start_time. + public string start_time { get; set; } + + /// + /// Gets or sets the duration. + /// + /// The duration. + public string duration { get; set; } + + /// + /// Gets or sets the size. + /// + /// The size. + public string size { get; set; } + + /// + /// Gets or sets the bit_rate. + /// + /// The bit_rate. + public string bit_rate { get; set; } + + /// + /// Gets or sets the tags. + /// + /// The tags. + public Dictionary tags { get; set; } + } +} diff --git a/MediaBrowser.Controller/MediaEncoding/MediaEncoderHelpers.cs b/MediaBrowser.Controller/MediaEncoding/MediaEncoderHelpers.cs new file mode 100644 index 000000000..b2b9e2af3 --- /dev/null +++ b/MediaBrowser.Controller/MediaEncoding/MediaEncoderHelpers.cs @@ -0,0 +1,365 @@ +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.IO; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; + +namespace MediaBrowser.Controller.MediaEncoding +{ + /// + /// Class MediaEncoderHelpers + /// + public static class MediaEncoderHelpers + { + /// + /// Gets the input argument. + /// + /// The video path. + /// if set to true [is remote]. + /// Type of the video. + /// Type of the iso. + /// The iso mount. + /// The playable stream file names. + /// The type. + /// System.String[][]. + public static string[] GetInputArgument(string videoPath, bool isRemote, VideoType videoType, IsoType? isoType, IIsoMount isoMount, IEnumerable playableStreamFileNames, out InputType type) + { + var inputPath = isoMount == null ? new[] { videoPath } : new[] { isoMount.MountedPath }; + + type = InputType.File; + + switch (videoType) + { + case VideoType.BluRay: + type = InputType.Bluray; + break; + case VideoType.Dvd: + type = InputType.Dvd; + inputPath = GetPlayableStreamFiles(inputPath[0], playableStreamFileNames).ToArray(); + break; + case VideoType.Iso: + if (isoType.HasValue) + { + switch (isoType.Value) + { + case IsoType.BluRay: + type = InputType.Bluray; + break; + case IsoType.Dvd: + type = InputType.Dvd; + inputPath = GetPlayableStreamFiles(inputPath[0], playableStreamFileNames).ToArray(); + break; + } + } + break; + case VideoType.VideoFile: + { + if (isRemote) + { + type = InputType.Url; + } + break; + } + } + + return inputPath; + } + + public static List GetPlayableStreamFiles(string rootPath, IEnumerable filenames) + { + var allFiles = Directory + .EnumerateFiles(rootPath, "*", SearchOption.AllDirectories) + .ToList(); + + return filenames.Select(name => allFiles.FirstOrDefault(f => string.Equals(Path.GetFileName(f), name, StringComparison.OrdinalIgnoreCase))) + .Where(f => !string.IsNullOrEmpty(f)) + .ToList(); + } + + /// + /// Gets the type of the input. + /// + /// Type of the video. + /// Type of the iso. + /// InputType. + public static InputType GetInputType(VideoType? videoType, IsoType? isoType) + { + var type = InputType.File; + + if (videoType.HasValue) + { + switch (videoType.Value) + { + case VideoType.BluRay: + type = InputType.Bluray; + break; + case VideoType.Dvd: + type = InputType.Dvd; + break; + case VideoType.Iso: + if (isoType.HasValue) + { + switch (isoType.Value) + { + case IsoType.BluRay: + type = InputType.Bluray; + break; + case IsoType.Dvd: + type = InputType.Dvd; + break; + } + } + break; + } + } + + return type; + } + + public static Model.Entities.MediaInfo GetMediaInfo(InternalMediaInfoResult data) + { + var internalStreams = data.streams ?? new MediaStreamInfo[] { }; + + var info = new Model.Entities.MediaInfo(); + + info.MediaStreams = internalStreams.Select(s => GetMediaStream(s, data.format)) + .Where(i => i != null) + .ToList(); + + if (data.format != null) + { + info.Format = data.format.format_name; + } + + return info; + } + + private static readonly CultureInfo UsCulture = new CultureInfo("en-US"); + + /// + /// Converts ffprobe stream info to our MediaStream class + /// + /// The stream info. + /// The format info. + /// MediaStream. + private static MediaStream GetMediaStream(MediaStreamInfo streamInfo, MediaFormatInfo formatInfo) + { + var stream = new MediaStream + { + Codec = streamInfo.codec_name, + Profile = streamInfo.profile, + Level = streamInfo.level, + Index = streamInfo.index + }; + + if (streamInfo.tags != null) + { + stream.Language = GetDictionaryValue(streamInfo.tags, "language"); + } + + if (string.Equals(streamInfo.codec_type, "audio", StringComparison.OrdinalIgnoreCase)) + { + stream.Type = MediaStreamType.Audio; + + stream.Channels = streamInfo.channels; + + if (!string.IsNullOrEmpty(streamInfo.sample_rate)) + { + stream.SampleRate = int.Parse(streamInfo.sample_rate, UsCulture); + } + + stream.ChannelLayout = ParseChannelLayout(streamInfo.channel_layout); + } + else if (string.Equals(streamInfo.codec_type, "subtitle", StringComparison.OrdinalIgnoreCase)) + { + stream.Type = MediaStreamType.Subtitle; + } + else if (string.Equals(streamInfo.codec_type, "video", StringComparison.OrdinalIgnoreCase)) + { + stream.Type = MediaStreamType.Video; + + stream.Width = streamInfo.width; + stream.Height = streamInfo.height; + stream.AspectRatio = GetAspectRatio(streamInfo); + + stream.AverageFrameRate = GetFrameRate(streamInfo.avg_frame_rate); + stream.RealFrameRate = GetFrameRate(streamInfo.r_frame_rate); + } + else + { + return null; + } + + // Get stream bitrate + if (stream.Type != MediaStreamType.Subtitle) + { + var bitrate = 0; + + if (!string.IsNullOrEmpty(streamInfo.bit_rate)) + { + bitrate = int.Parse(streamInfo.bit_rate, UsCulture); + } + else if (formatInfo != null && !string.IsNullOrEmpty(formatInfo.bit_rate)) + { + // If the stream info doesn't have a bitrate get the value from the media format info + bitrate = int.Parse(formatInfo.bit_rate, UsCulture); + } + + if (bitrate > 0) + { + stream.BitRate = bitrate; + } + } + + if (streamInfo.disposition != null) + { + var isDefault = GetDictionaryValue(streamInfo.disposition, "default"); + var isForced = GetDictionaryValue(streamInfo.disposition, "forced"); + + stream.IsDefault = string.Equals(isDefault, "1", StringComparison.OrdinalIgnoreCase); + + stream.IsForced = string.Equals(isForced, "1", StringComparison.OrdinalIgnoreCase); + } + + return stream; + } + + /// + /// Gets a string from an FFProbeResult tags dictionary + /// + /// The tags. + /// The key. + /// System.String. + private static string GetDictionaryValue(Dictionary tags, string key) + { + if (tags == null) + { + return null; + } + + string val; + + tags.TryGetValue(key, out val); + return val; + } + + private static string ParseChannelLayout(string input) + { + if (string.IsNullOrEmpty(input)) + { + return input; + } + + return input.Split('(').FirstOrDefault(); + } + + private static string GetAspectRatio(MediaStreamInfo info) + { + var original = info.display_aspect_ratio; + + int height; + int width; + + var parts = (original ?? string.Empty).Split(':'); + if (!(parts.Length == 2 && + int.TryParse(parts[0], NumberStyles.Any, UsCulture, out width) && + int.TryParse(parts[1], NumberStyles.Any, UsCulture, out height) && + width > 0 && + height > 0)) + { + width = info.width; + height = info.height; + } + + if (width > 0 && height > 0) + { + double ratio = width; + ratio /= height; + + if (IsClose(ratio, 1.777777778, .03)) + { + return "16:9"; + } + + if (IsClose(ratio, 1.3333333333, .05)) + { + return "4:3"; + } + + if (IsClose(ratio, 1.41)) + { + return "1.41:1"; + } + + if (IsClose(ratio, 1.5)) + { + return "1.5:1"; + } + + if (IsClose(ratio, 1.6)) + { + return "1.6:1"; + } + + if (IsClose(ratio, 1.66666666667)) + { + return "5:3"; + } + + if (IsClose(ratio, 1.85, .02)) + { + return "1.85:1"; + } + + if (IsClose(ratio, 2.35, .025)) + { + return "2.35:1"; + } + + if (IsClose(ratio, 2.4, .025)) + { + return "2.40:1"; + } + } + + return original; + } + + private static bool IsClose(double d1, double d2, double variance = .005) + { + return Math.Abs(d1 - d2) <= variance; + } + + /// + /// Gets a frame rate from a string value in ffprobe output + /// This could be a number or in the format of 2997/125. + /// + /// The value. + /// System.Nullable{System.Single}. + private static float? GetFrameRate(string value) + { + if (!string.IsNullOrEmpty(value)) + { + var parts = value.Split('/'); + + float result; + + if (parts.Length == 2) + { + result = float.Parse(parts[0], UsCulture) / float.Parse(parts[1], UsCulture); + } + else + { + result = float.Parse(parts[0], UsCulture); + } + + return float.IsNaN(result) ? (float?)null : result; + } + + return null; + } + + } +} -- cgit v1.2.3