Skip to content

Commit

Permalink
[Domain] Crash fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Jan 10, 2021
1 parent 6ed3bb3 commit ede57a5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
39 changes: 23 additions & 16 deletions CastIt.Application/Youtube/YoutubeUrlDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -455,27 +455,29 @@ private Dictionary<int, string> GetVideoQualities(string body)
_logger.LogInformation($"{nameof(GetVideoQualities)}: Getting video qualities...");
var formatPattern = @"(\\""formats\\"":\[.*?])";
var formatMatch = Regex.Match(body, formatPattern);

if (formatMatch.Length == 0)
{
_logger.LogInformation($"{nameof(GetVideoQualities)}: Couldn't retrieve formats, checking if we have adaptiveFormats...");
formatPattern = @"(\\""adaptiveFormats\\"":\[.*?])";
_logger.LogInformation(
$"{nameof(GetVideoQualities)}: Couldn't retrieve formats, " +
"checking if the formats is not between slashes using the format keyword...");
formatPattern = @"(\""formats\"":\[.*?])";
formatMatch = Regex.Match(body, formatPattern);
}

if (formatMatch.Length == 0)
{
_logger.LogInformation(
$"{nameof(GetVideoQualities)}: Couldn't retrieve formats, " +
"checking if the formats is not between slashes using the format keyword...");
formatPattern = @"(\""formats\"":\[.*?])";
$"{nameof(GetVideoQualities)}: Couldn't retrieve formats, checking if we have adaptive formats...");
formatPattern = @"(\\""adaptiveFormats\\"":\[.*?])";
formatMatch = Regex.Match(body, formatPattern);
}

if (formatMatch.Length == 0)
{
_logger.LogInformation(
$"{nameof(GetVideoQualities)}: Couldn't retrieve formats, " +
"checking if the formats is not between slashes using the adaptiveFormats keyword...");
$"{nameof(GetVideoQualities)}: Couldn't retrieve adaptive formats, " +
"checking if the formats is not between slashes using the format keyword...");
formatPattern = @"(\""adaptiveFormats\"":\[.*?])";
formatMatch = Regex.Match(body, formatPattern);
}
Expand All @@ -491,24 +493,29 @@ private Dictionary<int, string> GetVideoQualities(string body)
string heightPatternA = @"(?<=\\""height\\"":).+?(?=,)";
string heightPatternB = @"(?<=\""height\"":).+?(?=,)";
var streams = Regex.Matches(streamMap, "{(.*?)}").AsQueryable().OfType<Match>().ToList();
var qualities = streams.ToDictionary(k =>

var qualitiesDictionary = new Dictionary<int, string>();
foreach (var stream in streams)
{
var val = Regex.Match(k.ToString(), heightPatternA).Value;
var match = stream.Value;
var val = Regex.Match(match, heightPatternA).Value;
if (string.IsNullOrEmpty(val))
{
_logger.LogInformation($"Couldn't retrieve height, trying now without backslashes in the pattern");
val = Regex.Match(k.ToString(), heightPatternB).Value;
_logger.LogInformation("Couldn't retrieve height, trying now without backslashes in the pattern");
val = Regex.Match(match, heightPatternB).Value;
}

if (string.IsNullOrEmpty(val))
{
_logger.LogWarning($"Couldn't retrieve height from = {k}");
_logger.LogWarning($"Couldn't retrieve height from = {match}");
}

return int.Parse(string.IsNullOrEmpty(val) ? "-1" : val);
}, v => v.ToString());

var qualitiesDictionary = qualities.Where(kvp => kvp.Key > 0).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
int quality = int.Parse(string.IsNullOrEmpty(val) ? "-1" : val);
if (!qualitiesDictionary.ContainsKey(quality) && quality > 0)
{
qualitiesDictionary.Add(quality, match);
}
}

_logger.LogInformation($"{nameof(GetVideoQualities)}: Got = {qualitiesDictionary.Count} video qualities");

Expand Down
5 changes: 4 additions & 1 deletion CastIt.Domain/Models/FFmpeg/Info/FileInfoStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ public class FileInfoStream
[JsonProperty(PropertyName = "nb_frames")]
public long NumberOfFrames { get; set; }

[JsonProperty(PropertyName = "avg_frame_rate")]
public string AverageFrameRate { get; set; }

[JsonProperty(PropertyName = "tags")]
public FileInfoTag Tag { get; set; }

//The CodecType in an audio file may return video if the stream is a png img
public bool IsVideo
=> CodecType == "video" && Level != 0 && NumberOfFrames > 1;
=> CodecType == "video" && Level != 0 && (NumberOfFrames > 1 || AverageFrameRate != "0/0");

public bool IsAudio
=> CodecType == "audio";
Expand Down

0 comments on commit ede57a5

Please sign in to comment.