using System;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using Kyoo.Models.Attributes;
namespace Kyoo.Models
{
///
/// The list of available stream types.
/// Attachments are only used temporarily by the transcoder but are not stored in a database.
///
public enum StreamType
{
Unknown = 0,
Video = 1,
Audio = 2,
Subtitle = 3,
Attachment = 4
}
///
/// A video, audio or subtitle track for an episode.
///
public class Track : IResource
{
///
public int ID { get; set; }
///
public string Slug
{
get
{
string type = Type switch
{
StreamType.Subtitle => "",
StreamType.Video => "video.",
StreamType.Audio => "audio.",
StreamType.Attachment => "font.",
_ => ""
};
string index = TrackIndex != 0 ? $"-{TrackIndex}" : string.Empty;
string codec = Codec switch
{
"subrip" => ".srt",
{} x => $".{x}"
};
return $"{EpisodeSlug}.{type}{Language}{index}{(IsForced ? "-forced" : "")}{codec}";
}
set
{
Match match = Regex.Match(value, @"(?.*)-s(?\d+)e(?\d+)"
+ @"(\.(?\w*))?\.(?.{0,3})(?-forced)?(\..\w)?");
if (!match.Success)
{
match = Regex.Match(value, @"(?.*)\.(?.{0,3})(?-forced)?(\..\w)?");
if (!match.Success)
throw new ArgumentException("Invalid track slug. " +
"Format: {episodeSlug}.{language}[-forced][.{extension}]");
}
EpisodeSlug = Episode.GetSlug(match.Groups["show"].Value,
match.Groups["season"].Success ? int.Parse(match.Groups["season"].Value) : -1,
match.Groups["episode"].Success ? int.Parse(match.Groups["episode"].Value) : -1);
Language = match.Groups["language"].Value;
IsForced = match.Groups["forced"].Success;
if (match.Groups["type"].Success)
Type = Enum.Parse(match.Groups["type"].Value, true);
}
}
///
/// The slug of the episode that contain this track. If this is not set, this track is ill-formed.
///
[SerializeIgnore] public string EpisodeSlug { private get; set; }
///
/// The title of the stream.
///
public string Title { get; set; }
///
/// The language of this stream (as a ISO-639-2 language code)
///
public string Language { get; set; }
///
/// The codec of this stream.
///
public string Codec { get; set; }
///
/// Is this stream the default one of it's type?
///
public bool IsDefault { get; set; }
///
/// Is this stream tagged as forced?
///
public bool IsForced { get; set; }
///
/// Is this track extern to the episode's file?
///
public bool IsExternal { get; set; }
///
/// The path of this track.
///
[SerializeIgnore] public string Path { get; set; }
///
/// The type of this stream.
///
[SerializeIgnore] public StreamType Type { get; set; }
///
/// The ID of the episode that uses this track. This value is only set when the has been loaded.
///
[SerializeIgnore] public int EpisodeID { get; set; }
///
/// The episode that uses this track.
///
[LoadableRelation(nameof(EpisodeID))] public Episode Episode { get; set; }
///
/// The index of this track on the episode.
///
public int TrackIndex { get; set; }
///
/// A user-friendly name for this track. It does not include the track type.
///
public string DisplayName
{
get
{
string language = GetLanguage(Language);
if (language == null)
return $"Unknown (index: {TrackIndex})";
CultureInfo info = CultureInfo.GetCultures(CultureTypes.NeutralCultures)
.FirstOrDefault(x => x.ThreeLetterISOLanguageName == language);
string name = info?.EnglishName ?? language;
if (IsForced)
name += " Forced";
if (IsExternal)
name += " (External)";
if (Title is {Length: > 1})
name += " - " + Title;
return name;
}
}
//Converting mkv track language to c# system language tag.
private static string GetLanguage(string mkvLanguage)
{
// TODO delete this and have a real way to get the language string from the ISO-639-2.
return mkvLanguage switch
{
"fre" => "fra",
null => "und",
_ => mkvLanguage
};
}
}
}