Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix VOD download trimming and quality selection #936

Merged
merged 4 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions TwitchDownloaderCore/Extensions/M3U8Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static M3U8.Stream GetStreamOfQuality(this M3U8 m3u8, string qualityStrin

if (qualityString is null)
{
return streams.MaxBy(x => x.StreamInfo.Resolution.Width * x.StreamInfo.Resolution.Height * x.StreamInfo.Framerate);
return m3u8.BestQualityStream();
}

if (qualityString.Contains("audio", StringComparison.OrdinalIgnoreCase) &&
Expand All @@ -57,7 +57,7 @@ public static M3U8.Stream GetStreamOfQuality(this M3U8 m3u8, string qualityStrin
var qualityStringMatch = UserQualityStringRegex.Match(qualityString);
if (!qualityStringMatch.Success)
{
return streams.MaxBy(x => x.StreamInfo.Resolution.Width * x.StreamInfo.Resolution.Height * x.StreamInfo.Framerate);
return m3u8.BestQualityStream();
}

var desiredWidth = qualityStringMatch.Groups["Width"];
Expand All @@ -74,7 +74,7 @@ public static M3U8.Stream GetStreamOfQuality(this M3U8 m3u8, string qualityStrin
{
1 => filteredStreams[0],
2 when !desiredFramerate.Success => filteredStreams.First(x => Math.Abs(x.StreamInfo.Framerate - 30) <= 2),
_ => streams.MaxBy(x => x.StreamInfo.Resolution.Width * x.StreamInfo.Resolution.Height * x.StreamInfo.Framerate)
_ => m3u8.BestQualityStream()
};
}

Expand Down Expand Up @@ -118,5 +118,14 @@ public static string GetResolutionFramerateString(this M3U8.Stream stream)

return $"{frameHeight}p{frameRate}";
}

/// <summary>
/// Returns the best quality stream from the provided M3U8.
/// </summary>
public static M3U8.Stream BestQualityStream(this M3U8 m3u8)
{
var source = Array.Find(m3u8.Streams, x => x.MediaInfo.Name.Equals("source", StringComparison.OrdinalIgnoreCase));
ScrubN marked this conversation as resolved.
Show resolved Hide resolved
return source ?? m3u8.Streams.MaxBy(x => x.StreamInfo.Resolution.Width * x.StreamInfo.Resolution.Height * x.StreamInfo.Framerate);
}
}
}
2 changes: 1 addition & 1 deletion TwitchDownloaderCore/VideoDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public async Task DownloadAsync(CancellationToken cancellationToken)
.Take(videoListCrop.Start.Value)
.Sum(x => x.PartInfo.Duration);

startOffsetSeconds -= downloadOptions.CropBeginningTime;
startOffsetSeconds = downloadOptions.CropBeginningTime - startOffsetSeconds;
double seekDuration = Math.Round(downloadOptions.CropEndingTime - downloadOptions.CropBeginningTime);

string metadataPath = Path.Combine(downloadFolder, "metadata.txt");
Expand Down