jellyfin/Jellyfin.Api/Helpers/HlsCodecStringHelpers.cs

183 lines
4.6 KiB
C#
Raw Normal View History

2020-07-31 14:09:17 -07:00
using System;
using System.Globalization;
using System.Text;
2023-01-31 04:18:10 -07:00
namespace Jellyfin.Api.Helpers;
/// <summary>
/// Hls Codec string helpers.
/// </summary>
public static class HlsCodecStringHelpers
2020-07-31 14:09:17 -07:00
{
/// <summary>
2023-01-31 04:18:10 -07:00
/// Codec name for MP3.
/// </summary>
public const string MP3 = "mp4a.40.34";
/// <summary>
/// Codec name for AC-3.
/// </summary>
public const string AC3 = "mp4a.a5";
/// <summary>
/// Codec name for E-AC-3.
/// </summary>
public const string EAC3 = "mp4a.a6";
/// <summary>
/// Codec name for FLAC.
/// </summary>
public const string FLAC = "flac";
/// <summary>
/// Codec name for ALAC.
/// </summary>
public const string ALAC = "alac";
/// <summary>
/// Codec name for OPUS.
/// </summary>
public const string OPUS = "opus";
/// <summary>
/// Gets a MP3 codec string.
2020-07-31 14:09:17 -07:00
/// </summary>
2023-01-31 04:18:10 -07:00
/// <returns>MP3 codec string.</returns>
public static string GetMP3String()
2020-07-31 14:09:17 -07:00
{
2023-01-31 04:18:10 -07:00
return MP3;
}
2020-07-31 14:09:17 -07:00
2023-01-31 04:18:10 -07:00
/// <summary>
/// Gets an AAC codec string.
/// </summary>
/// <param name="profile">AAC profile.</param>
/// <returns>AAC codec string.</returns>
public static string GetAACString(string? profile)
{
StringBuilder result = new StringBuilder("mp4a", 9);
if (string.Equals(profile, "HE", StringComparison.OrdinalIgnoreCase))
2020-07-31 14:09:17 -07:00
{
2023-01-31 04:18:10 -07:00
result.Append(".40.5");
2020-07-31 14:09:17 -07:00
}
2023-01-31 04:18:10 -07:00
else
2020-11-08 02:01:58 -07:00
{
2023-01-31 04:18:10 -07:00
// Default to LC if profile is invalid
result.Append(".40.2");
2020-11-08 02:01:58 -07:00
}
2023-01-31 04:18:10 -07:00
return result.ToString();
}
/// <summary>
/// Gets an AC-3 codec string.
/// </summary>
/// <returns>AC-3 codec string.</returns>
public static string GetAC3String()
{
return AC3;
}
/// <summary>
/// Gets an E-AC-3 codec string.
/// </summary>
/// <returns>E-AC-3 codec string.</returns>
public static string GetEAC3String()
{
return EAC3;
}
/// <summary>
/// Gets an FLAC codec string.
/// </summary>
/// <returns>FLAC codec string.</returns>
public static string GetFLACString()
{
return FLAC;
}
/// <summary>
/// Gets an ALAC codec string.
/// </summary>
/// <returns>ALAC codec string.</returns>
public static string GetALACString()
{
return ALAC;
}
/// <summary>
/// Gets an OPUS codec string.
/// </summary>
/// <returns>OPUS codec string.</returns>
public static string GetOPUSString()
{
return OPUS;
}
/// <summary>
/// Gets a H.264 codec string.
/// </summary>
/// <param name="profile">H.264 profile.</param>
/// <param name="level">H.264 level.</param>
/// <returns>H.264 string.</returns>
public static string GetH264String(string? profile, int level)
{
StringBuilder result = new StringBuilder("avc1", 11);
if (string.Equals(profile, "high", StringComparison.OrdinalIgnoreCase))
2020-11-08 02:01:58 -07:00
{
2023-01-31 04:18:10 -07:00
result.Append(".6400");
2020-11-08 02:01:58 -07:00
}
2023-01-31 04:18:10 -07:00
else if (string.Equals(profile, "main", StringComparison.OrdinalIgnoreCase))
2020-11-08 02:01:58 -07:00
{
2023-01-31 04:18:10 -07:00
result.Append(".4D40");
2020-11-08 02:01:58 -07:00
}
2023-01-31 04:18:10 -07:00
else if (string.Equals(profile, "baseline", StringComparison.OrdinalIgnoreCase))
2020-11-08 02:01:58 -07:00
{
2023-01-31 04:18:10 -07:00
result.Append(".42E0");
2020-11-08 02:01:58 -07:00
}
2023-01-31 04:18:10 -07:00
else
{
2023-01-31 04:18:10 -07:00
// Default to constrained baseline if profile is invalid
result.Append(".4240");
}
2023-01-31 04:18:10 -07:00
string levelHex = level.ToString("X2", CultureInfo.InvariantCulture);
result.Append(levelHex);
return result.ToString();
}
/// <summary>
/// Gets a H.265 codec string.
/// </summary>
/// <param name="profile">H.265 profile.</param>
/// <param name="level">H.265 level.</param>
/// <returns>H.265 string.</returns>
public static string GetH265String(string? profile, int level)
{
// The h265 syntax is a bit of a mystery at the time this comment was written.
// This is what I've found through various sources:
// FORMAT: [codecTag].[profile].[constraint?].L[level * 30].[UNKNOWN]
StringBuilder result = new StringBuilder("hvc1", 16);
if (string.Equals(profile, "main10", StringComparison.OrdinalIgnoreCase)
|| string.Equals(profile, "main 10", StringComparison.OrdinalIgnoreCase))
2020-07-31 14:09:17 -07:00
{
2023-01-31 04:18:10 -07:00
result.Append(".2.4");
2020-07-31 14:09:17 -07:00
}
2023-01-31 04:18:10 -07:00
else
2020-07-31 14:09:17 -07:00
{
2023-01-31 04:18:10 -07:00
// Default to main if profile is invalid
result.Append(".1.4");
2020-07-31 14:09:17 -07:00
}
2023-01-31 04:18:10 -07:00
result.Append(".L")
.Append(level)
.Append(".B0");
return result.ToString();
2020-07-31 14:09:17 -07:00
}
}