jellyfin/MediaBrowser.Model/Dlna/MediaFormatProfileResolver.cs

533 lines
22 KiB
C#
Raw Normal View History

#nullable disable
2020-02-03 17:49:27 -07:00
#pragma warning disable CS1591
using System;
2018-12-27 16:27:57 -07:00
using System.Collections.Generic;
2020-08-07 10:26:28 -07:00
using System.Globalization;
using MediaBrowser.Model.MediaInfo;
2018-12-27 16:27:57 -07:00
namespace MediaBrowser.Model.Dlna
{
2021-04-02 11:06:38 -07:00
public static class MediaFormatProfileResolver
2018-12-27 16:27:57 -07:00
{
2021-04-02 11:06:38 -07:00
public static MediaFormatProfile[] ResolveVideoFormat(string container, string videoCodec, string audioCodec, int? width, int? height, TransportStreamTimestamp timestampType)
2018-12-27 16:27:57 -07:00
{
2020-01-09 09:07:13 -07:00
if (string.Equals(container, "asf", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
MediaFormatProfile? val = ResolveVideoASFFormat(videoCodec, audioCodec, width, height);
return val.HasValue ? new MediaFormatProfile[] { val.Value } : Array.Empty<MediaFormatProfile>();
2018-12-27 16:27:57 -07:00
}
2020-01-09 09:07:13 -07:00
if (string.Equals(container, "mp4", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
MediaFormatProfile? val = ResolveVideoMP4Format(videoCodec, audioCodec, width, height);
return val.HasValue ? new MediaFormatProfile[] { val.Value } : Array.Empty<MediaFormatProfile>();
2018-12-27 16:27:57 -07:00
}
2020-01-09 09:07:13 -07:00
if (string.Equals(container, "avi", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return new MediaFormatProfile[] { MediaFormatProfile.AVI };
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
2020-01-09 09:07:13 -07:00
if (string.Equals(container, "mkv", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return new MediaFormatProfile[] { MediaFormatProfile.MATROSKA };
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
2020-01-09 09:07:13 -07:00
if (string.Equals(container, "mpeg2ps", StringComparison.OrdinalIgnoreCase) ||
string.Equals(container, "ts", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return new MediaFormatProfile[] { MediaFormatProfile.MPEG_PS_NTSC, MediaFormatProfile.MPEG_PS_PAL };
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
2020-01-09 09:07:13 -07:00
if (string.Equals(container, "mpeg1video", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return new MediaFormatProfile[] { MediaFormatProfile.MPEG1 };
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
2020-01-09 09:07:13 -07:00
if (string.Equals(container, "mpeg2ts", StringComparison.OrdinalIgnoreCase) ||
string.Equals(container, "mpegts", StringComparison.OrdinalIgnoreCase) ||
string.Equals(container, "m2ts", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
return ResolveVideoMPEG2TSFormat(videoCodec, audioCodec, width, height, timestampType);
}
2020-01-09 09:07:13 -07:00
if (string.Equals(container, "flv", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return new MediaFormatProfile[] { MediaFormatProfile.FLV };
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
2020-01-09 09:07:13 -07:00
if (string.Equals(container, "wtv", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return new MediaFormatProfile[] { MediaFormatProfile.WTV };
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
2020-01-09 09:07:13 -07:00
if (string.Equals(container, "3gp", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
MediaFormatProfile? val = ResolveVideo3GPFormat(videoCodec, audioCodec);
return val.HasValue ? new MediaFormatProfile[] { val.Value } : Array.Empty<MediaFormatProfile>();
2018-12-27 16:27:57 -07:00
}
2020-01-09 09:07:13 -07:00
if (string.Equals(container, "ogv", StringComparison.OrdinalIgnoreCase) || string.Equals(container, "ogg", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return new MediaFormatProfile[] { MediaFormatProfile.OGV };
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
return Array.Empty<MediaFormatProfile>();
2018-12-27 16:27:57 -07:00
}
2021-04-02 11:06:38 -07:00
private static MediaFormatProfile[] ResolveVideoMPEG2TSFormat(string videoCodec, string audioCodec, int? width, int? height, TransportStreamTimestamp timestampType)
2018-12-27 16:27:57 -07:00
{
string suffix = string.Empty;
2018-12-27 16:27:57 -07:00
switch (timestampType)
{
case TransportStreamTimestamp.None:
suffix = "_ISO";
break;
case TransportStreamTimestamp.Valid:
suffix = "_T";
break;
}
string resolution = "S";
if ((width.HasValue && width.Value > 720) || (height.HasValue && height.Value > 576))
{
resolution = "H";
}
2020-01-09 09:07:13 -07:00
if (string.Equals(videoCodec, "mpeg2video", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
var list = new List<MediaFormatProfile>
{
ValueOf("MPEG_TS_SD_NA" + suffix),
ValueOf("MPEG_TS_SD_EU" + suffix),
ValueOf("MPEG_TS_SD_KO" + suffix)
};
2018-12-27 16:27:57 -07:00
2020-01-09 09:07:13 -07:00
if ((timestampType == TransportStreamTimestamp.Valid) && string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
list.Add(MediaFormatProfile.MPEG_TS_JP_T);
}
2018-12-28 08:48:26 -07:00
return list.ToArray();
2018-12-27 16:27:57 -07:00
}
2020-06-15 14:43:52 -07:00
2020-01-09 09:07:13 -07:00
if (string.Equals(videoCodec, "h264", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
2020-01-09 09:07:13 -07:00
if (string.Equals(audioCodec, "lpcm", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return new MediaFormatProfile[] { MediaFormatProfile.AVC_TS_HD_50_LPCM_T };
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
2020-01-09 09:07:13 -07:00
if (string.Equals(audioCodec, "dts", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
if (timestampType == TransportStreamTimestamp.None)
{
return new MediaFormatProfile[] { MediaFormatProfile.AVC_TS_HD_DTS_ISO };
}
2018-12-27 16:27:57 -07:00
return new MediaFormatProfile[] { MediaFormatProfile.AVC_TS_HD_DTS_T };
}
2020-01-09 09:07:13 -07:00
if (string.Equals(audioCodec, "mp2", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
if (timestampType == TransportStreamTimestamp.None)
{
2020-08-07 10:26:28 -07:00
return new MediaFormatProfile[] { ValueOf(string.Format(CultureInfo.InvariantCulture, "AVC_TS_HP_{0}D_MPEG1_L2_ISO", resolution)) };
2018-12-27 16:27:57 -07:00
}
2020-08-07 10:26:28 -07:00
return new MediaFormatProfile[] { ValueOf(string.Format(CultureInfo.InvariantCulture, "AVC_TS_HP_{0}D_MPEG1_L2_T", resolution)) };
2018-12-27 16:27:57 -07:00
}
2020-01-09 09:07:13 -07:00
if (string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2020-08-07 10:26:28 -07:00
return new MediaFormatProfile[] { ValueOf(string.Format(CultureInfo.InvariantCulture, "AVC_TS_MP_{0}D_AAC_MULT5{1}", resolution, suffix)) };
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
2020-01-09 09:07:13 -07:00
if (string.Equals(audioCodec, "mp3", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2020-08-07 10:26:28 -07:00
return new MediaFormatProfile[] { ValueOf(string.Format(CultureInfo.InvariantCulture, "AVC_TS_MP_{0}D_MPEG1_L3{1}", resolution, suffix)) };
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
if (string.IsNullOrEmpty(audioCodec) ||
2020-01-09 09:07:13 -07:00
string.Equals(audioCodec, "ac3", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2020-08-07 10:26:28 -07:00
return new MediaFormatProfile[] { ValueOf(string.Format(CultureInfo.InvariantCulture, "AVC_TS_MP_{0}D_AC3{1}", resolution, suffix)) };
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
}
2020-01-09 09:07:13 -07:00
else if (string.Equals(videoCodec, "vc1", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
2020-01-09 09:07:13 -07:00
if (string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "ac3", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
if ((width.HasValue && width.Value > 720) || (height.HasValue && height.Value > 576))
{
return new MediaFormatProfile[] { MediaFormatProfile.VC1_TS_AP_L2_AC3_ISO };
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
return new MediaFormatProfile[] { MediaFormatProfile.VC1_TS_AP_L1_AC3_ISO };
}
2020-06-15 14:43:52 -07:00
2020-01-09 09:07:13 -07:00
if (string.Equals(audioCodec, "dts", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
2020-01-09 09:07:13 -07:00
suffix = string.Equals(suffix, "_ISO", StringComparison.OrdinalIgnoreCase) ? suffix : "_T";
2018-12-27 16:27:57 -07:00
2020-08-07 10:26:28 -07:00
return new MediaFormatProfile[] { ValueOf(string.Format(CultureInfo.InvariantCulture, "VC1_TS_HD_DTS{0}", suffix)) };
2018-12-27 16:27:57 -07:00
}
}
2020-01-09 09:07:13 -07:00
else if (string.Equals(videoCodec, "mpeg4", StringComparison.OrdinalIgnoreCase) || string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
2020-01-09 09:07:13 -07:00
if (string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2020-08-07 10:26:28 -07:00
return new MediaFormatProfile[] { ValueOf(string.Format(CultureInfo.InvariantCulture, "MPEG4_P2_TS_ASP_AAC{0}", suffix)) };
2020-06-20 02:12:36 -07:00
}
2020-01-09 09:07:13 -07:00
if (string.Equals(audioCodec, "mp3", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2020-08-07 10:26:28 -07:00
return new MediaFormatProfile[] { ValueOf(string.Format(CultureInfo.InvariantCulture, "MPEG4_P2_TS_ASP_MPEG1_L3{0}", suffix)) };
2020-06-20 02:12:36 -07:00
}
2020-01-09 09:07:13 -07:00
if (string.Equals(audioCodec, "mp2", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2020-08-07 10:26:28 -07:00
return new MediaFormatProfile[] { ValueOf(string.Format(CultureInfo.InvariantCulture, "MPEG4_P2_TS_ASP_MPEG2_L2{0}", suffix)) };
2020-06-20 02:12:36 -07:00
}
2020-01-09 09:07:13 -07:00
if (string.Equals(audioCodec, "ac3", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2020-08-07 10:26:28 -07:00
return new MediaFormatProfile[] { ValueOf(string.Format(CultureInfo.InvariantCulture, "MPEG4_P2_TS_ASP_AC3{0}", suffix)) };
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
}
2020-07-04 08:54:25 -07:00
return Array.Empty<MediaFormatProfile>();
2018-12-27 16:27:57 -07:00
}
2021-04-02 11:06:38 -07:00
private static MediaFormatProfile ValueOf(string value)
2018-12-27 16:27:57 -07:00
{
return (MediaFormatProfile)Enum.Parse(typeof(MediaFormatProfile), value, true);
}
2021-04-02 11:06:38 -07:00
private static MediaFormatProfile? ResolveVideoMP4Format(string videoCodec, string audioCodec, int? width, int? height)
2018-12-27 16:27:57 -07:00
{
2020-01-09 09:07:13 -07:00
if (string.Equals(videoCodec, "h264", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
2020-01-09 09:07:13 -07:00
if (string.Equals(audioCodec, "lpcm", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return MediaFormatProfile.AVC_MP4_LPCM;
2020-06-20 02:12:36 -07:00
}
2020-06-20 02:13:48 -07:00
2018-12-27 16:27:57 -07:00
if (string.IsNullOrEmpty(audioCodec) ||
2020-01-09 09:07:13 -07:00
string.Equals(audioCodec, "ac3", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
return MediaFormatProfile.AVC_MP4_MP_SD_AC3;
}
2020-06-15 14:43:52 -07:00
2020-01-09 09:07:13 -07:00
if (string.Equals(audioCodec, "mp3", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
return MediaFormatProfile.AVC_MP4_MP_SD_MPEG1_L3;
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
if (width.HasValue && height.HasValue)
{
if ((width.Value <= 720) && (height.Value <= 576))
{
2020-01-09 09:07:13 -07:00
if (string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return MediaFormatProfile.AVC_MP4_MP_SD_AAC_MULT5;
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
}
else if ((width.Value <= 1280) && (height.Value <= 720))
{
2020-01-09 09:07:13 -07:00
if (string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return MediaFormatProfile.AVC_MP4_MP_HD_720p_AAC;
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
}
else if ((width.Value <= 1920) && (height.Value <= 1080))
{
2020-01-09 09:07:13 -07:00
if (string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
return MediaFormatProfile.AVC_MP4_MP_HD_1080i_AAC;
}
}
}
}
2020-01-09 09:07:13 -07:00
else if (string.Equals(videoCodec, "mpeg4", StringComparison.OrdinalIgnoreCase) ||
string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
if (width.HasValue && height.HasValue && width.Value <= 720 && height.Value <= 576)
{
2020-01-09 09:07:13 -07:00
if (string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return MediaFormatProfile.MPEG4_P2_MP4_ASP_AAC;
2020-06-20 02:12:36 -07:00
}
2020-06-20 02:13:48 -07:00
2020-01-09 09:07:13 -07:00
if (string.Equals(audioCodec, "ac3", StringComparison.OrdinalIgnoreCase) || string.Equals(audioCodec, "mp3", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
return MediaFormatProfile.MPEG4_P2_MP4_NDSD;
}
}
2020-01-09 09:07:13 -07:00
else if (string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
return MediaFormatProfile.MPEG4_P2_MP4_SP_L6_AAC;
}
}
2020-01-09 09:07:13 -07:00
else if (string.Equals(videoCodec, "h263", StringComparison.OrdinalIgnoreCase) && string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
return MediaFormatProfile.MPEG4_H263_MP4_P0_L10_AAC;
}
return null;
}
2021-04-02 11:06:38 -07:00
private static MediaFormatProfile? ResolveVideo3GPFormat(string videoCodec, string audioCodec)
2018-12-27 16:27:57 -07:00
{
2020-01-09 09:07:13 -07:00
if (string.Equals(videoCodec, "h264", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
2020-01-09 09:07:13 -07:00
if (string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "aac", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return MediaFormatProfile.AVC_3GPP_BL_QCIF15_AAC;
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
}
2020-01-09 09:07:13 -07:00
else if (string.Equals(videoCodec, "mpeg4", StringComparison.OrdinalIgnoreCase) ||
string.Equals(videoCodec, "msmpeg4", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
2020-01-09 09:07:13 -07:00
if (string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "wma", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return MediaFormatProfile.MPEG4_P2_3GPP_SP_L0B_AAC;
2020-06-20 02:12:36 -07:00
}
2020-01-09 09:07:13 -07:00
if (string.Equals(audioCodec, "amrnb", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return MediaFormatProfile.MPEG4_P2_3GPP_SP_L0B_AMR;
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
}
2020-01-09 09:07:13 -07:00
else if (string.Equals(videoCodec, "h263", StringComparison.OrdinalIgnoreCase) && string.Equals(audioCodec, "amrnb", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
return MediaFormatProfile.MPEG4_H263_3GPP_P0_L10_AMR;
}
return null;
}
2021-04-02 11:06:38 -07:00
private static MediaFormatProfile? ResolveVideoASFFormat(string videoCodec, string audioCodec, int? width, int? height)
2018-12-27 16:27:57 -07:00
{
2020-01-09 09:07:13 -07:00
if (string.Equals(videoCodec, "wmv", StringComparison.OrdinalIgnoreCase) &&
(string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "wma", StringComparison.OrdinalIgnoreCase) || string.Equals(videoCodec, "wmapro", StringComparison.OrdinalIgnoreCase)))
2018-12-27 16:27:57 -07:00
{
if (width.HasValue && height.HasValue)
{
if ((width.Value <= 720) && (height.Value <= 576))
{
2020-01-09 09:07:13 -07:00
if (string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "wma", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
return MediaFormatProfile.WMVMED_FULL;
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
return MediaFormatProfile.WMVMED_PRO;
}
}
2020-01-09 09:07:13 -07:00
if (string.IsNullOrEmpty(audioCodec) || string.Equals(audioCodec, "wma", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
return MediaFormatProfile.WMVHIGH_FULL;
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
return MediaFormatProfile.WMVHIGH_PRO;
}
2020-01-09 09:07:13 -07:00
if (string.Equals(videoCodec, "vc1", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
if (width.HasValue && height.HasValue)
{
if ((width.Value <= 720) && (height.Value <= 576))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return MediaFormatProfile.VC1_ASF_AP_L1_WMA;
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
if ((width.Value <= 1280) && (height.Value <= 720))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return MediaFormatProfile.VC1_ASF_AP_L2_WMA;
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
if ((width.Value <= 1920) && (height.Value <= 1080))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return MediaFormatProfile.VC1_ASF_AP_L3_WMA;
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
}
}
2020-01-09 09:07:13 -07:00
else if (string.Equals(videoCodec, "mpeg2video", StringComparison.OrdinalIgnoreCase))
2018-12-27 16:27:57 -07:00
{
return MediaFormatProfile.DVR_MS;
}
return null;
}
2021-04-02 11:06:38 -07:00
public static MediaFormatProfile? ResolveAudioFormat(string container, int? bitrate, int? frequency, int? channels)
2018-12-27 16:27:57 -07:00
{
2020-01-09 09:07:13 -07:00
if (string.Equals(container, "asf", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return ResolveAudioASFFormat(bitrate);
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
2020-01-09 09:07:13 -07:00
if (string.Equals(container, "mp3", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return MediaFormatProfile.MP3;
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
2020-01-09 09:07:13 -07:00
if (string.Equals(container, "lpcm", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return ResolveAudioLPCMFormat(frequency, channels);
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
2020-01-09 09:07:13 -07:00
if (string.Equals(container, "mp4", StringComparison.OrdinalIgnoreCase) ||
string.Equals(container, "aac", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return ResolveAudioMP4Format(bitrate);
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
2020-01-09 09:07:13 -07:00
if (string.Equals(container, "adts", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return ResolveAudioADTSFormat(bitrate);
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
2020-01-09 09:07:13 -07:00
if (string.Equals(container, "flac", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return MediaFormatProfile.FLAC;
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
2020-01-09 09:07:13 -07:00
if (string.Equals(container, "oga", StringComparison.OrdinalIgnoreCase) ||
string.Equals(container, "ogg", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return MediaFormatProfile.OGG;
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
return null;
}
2021-04-02 11:06:38 -07:00
private static MediaFormatProfile ResolveAudioASFFormat(int? bitrate)
2018-12-27 16:27:57 -07:00
{
if (bitrate.HasValue && bitrate.Value <= 193)
{
return MediaFormatProfile.WMA_BASE;
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
return MediaFormatProfile.WMA_FULL;
}
2021-04-02 11:06:38 -07:00
private static MediaFormatProfile? ResolveAudioLPCMFormat(int? frequency, int? channels)
2018-12-27 16:27:57 -07:00
{
if (frequency.HasValue && channels.HasValue)
{
if (frequency.Value == 44100 && channels.Value == 1)
{
return MediaFormatProfile.LPCM16_44_MONO;
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
if (frequency.Value == 44100 && channels.Value == 2)
{
return MediaFormatProfile.LPCM16_44_STEREO;
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
if (frequency.Value == 48000 && channels.Value == 1)
{
return MediaFormatProfile.LPCM16_48_MONO;
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
if (frequency.Value == 48000 && channels.Value == 2)
{
return MediaFormatProfile.LPCM16_48_STEREO;
}
return null;
}
return MediaFormatProfile.LPCM16_48_STEREO;
}
2021-04-02 11:06:38 -07:00
private static MediaFormatProfile ResolveAudioMP4Format(int? bitrate)
2018-12-27 16:27:57 -07:00
{
if (bitrate.HasValue && bitrate.Value <= 320)
{
return MediaFormatProfile.AAC_ISO_320;
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
return MediaFormatProfile.AAC_ISO;
}
2021-04-02 11:06:38 -07:00
private static MediaFormatProfile ResolveAudioADTSFormat(int? bitrate)
2018-12-27 16:27:57 -07:00
{
if (bitrate.HasValue && bitrate.Value <= 320)
{
return MediaFormatProfile.AAC_ADTS_320;
}
2020-06-15 14:43:52 -07:00
2018-12-27 16:27:57 -07:00
return MediaFormatProfile.AAC_ADTS;
}
2021-04-02 11:06:38 -07:00
public static MediaFormatProfile? ResolveImageFormat(string container, int? width, int? height)
2018-12-27 16:27:57 -07:00
{
2020-01-09 09:07:13 -07:00
if (string.Equals(container, "jpeg", StringComparison.OrdinalIgnoreCase) ||
string.Equals(container, "jpg", StringComparison.OrdinalIgnoreCase))
{
2018-12-27 16:27:57 -07:00
return ResolveImageJPGFormat(width, height);
}
2018-12-27 16:27:57 -07:00
2020-01-09 09:07:13 -07:00
if (string.Equals(container, "png", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return ResolveImagePNGFormat(width, height);
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
2020-01-09 09:07:13 -07:00
if (string.Equals(container, "gif", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return MediaFormatProfile.GIF_LRG;
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
2020-01-09 09:07:13 -07:00
if (string.Equals(container, "raw", StringComparison.OrdinalIgnoreCase))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return MediaFormatProfile.RAW;
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
return null;
}
2021-04-02 11:06:38 -07:00
private static MediaFormatProfile ResolveImageJPGFormat(int? width, int? height)
2018-12-27 16:27:57 -07:00
{
if (width.HasValue && height.HasValue)
{
if ((width.Value <= 160) && (height.Value <= 160))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return MediaFormatProfile.JPEG_TN;
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
if ((width.Value <= 640) && (height.Value <= 480))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return MediaFormatProfile.JPEG_SM;
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
if ((width.Value <= 1024) && (height.Value <= 768))
{
return MediaFormatProfile.JPEG_MED;
}
return MediaFormatProfile.JPEG_LRG;
}
return MediaFormatProfile.JPEG_SM;
}
2021-04-02 11:06:38 -07:00
private static MediaFormatProfile ResolveImagePNGFormat(int? width, int? height)
2018-12-27 16:27:57 -07:00
{
if (width.HasValue && height.HasValue)
{
if ((width.Value <= 160) && (height.Value <= 160))
2020-06-20 02:12:36 -07:00
{
2018-12-27 16:27:57 -07:00
return MediaFormatProfile.PNG_TN;
2020-06-20 02:12:36 -07:00
}
2018-12-27 16:27:57 -07:00
}
return MediaFormatProfile.PNG_LRG;
}
}
}