/// <summary> /// Selects formats based on a format specifier from the list of available formats. /// Note that this only supports single formats, precedence lists ("/") and merges ("+"). /// </summary> /// <returns>The selected formats or null if none existing.</returns> public static FormatData[] SelectFormat(this VideoData videoData, string formatSpecifier) { // 1. Split by precedence list: "/" foreach (string group in formatSpecifier.Split('/')) { // 2. Split merging group: "+" string[] splits = group.Split('+'); if (splits.Length == 1) { FormatData format = videoData.SelectSingleFormat(splits[0]); if (format != null) { return new[] { format } } ; else { continue; } } else if (splits.Length == 2) { FormatData video = videoData.SelectSingleFormat(splits[0]); FormatData audio = videoData.SelectSingleFormat(splits[1]); // continue with next option if one of video or audio is not existing if (video == null || audio == null) { continue; } if (video.VideoCodec == "none") { throw new ArgumentException("The first format in a merge must contain a video."); } if (audio.AudioCodec == "none") { throw new ArgumentException("The second format in a merge must contain an audio."); } return(new[] { video, audio }); } else { throw new ArgumentException($"Invalid formar specifier {formatSpecifier}"); } } // we have found none of the given formats return(null); }