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

Add Safe video trim mode to circumvent rare encoding issues #1112

Merged
merged 5 commits into from
Jun 27, 2024
Merged
Changes from 1 commit
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
Next Next commit
Make videoLength calculation not dependent on Twitch API response
ScrubN committed Jun 25, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 8a4eeb91d3158efcd16b884bbb5dc6559d877d2e
25 changes: 12 additions & 13 deletions TwitchDownloaderCore/VideoDownloader.cs
Original file line number Diff line number Diff line change
@@ -96,7 +96,7 @@ private async Task DownloadAsyncImpl(FileInfo outputFileInfo, FileStream outputF
var videoInfo = videoInfoResponse.data.video;
var (playlist, airDate) = await GetVideoPlaylist(playlistUrl, cancellationToken);

var videoListCrop = GetStreamListTrim(playlist.Streams, videoInfo, out var videoLength, out var startOffset, out var endOffset);
var videoListCrop = GetStreamListTrim(playlist.Streams, out var videoLength, out var startOffset, out var endDuration);

CheckAvailableStorageSpace(qualityPlaylist.StreamInfo.Bandwidth, videoLength);

@@ -128,7 +128,7 @@ await FfmpegMetadata.SerializeAsync(metadataPath, videoInfo.owner.displayName, d
var ffmpegRetries = 0;
do
{
ffmpegExitCode = await Task.Run(() => RunFfmpegVideoCopy(downloadFolder, outputFileInfo, concatListPath, metadataPath, startOffset, endOffset, videoLength), cancellationToken);
ffmpegExitCode = await Task.Run(() => RunFfmpegVideoCopy(downloadFolder, outputFileInfo, concatListPath, metadataPath, startOffset, endDuration, videoLength), cancellationToken);
if (ffmpegExitCode != 0)
{
_progress.LogError($"Failed to finalize video (code {ffmpegExitCode}), retrying in 10 seconds...");
@@ -326,7 +326,7 @@ private async Task VerifyDownloadedParts(ICollection<M3U8.Stream> playlist, Rang
}
}

private int RunFfmpegVideoCopy(string tempFolder, FileInfo outputFile, string concatListPath, string metadataPath, decimal startOffset, decimal endOffset, TimeSpan videoLength)
private int RunFfmpegVideoCopy(string tempFolder, FileInfo outputFile, string concatListPath, string metadataPath, decimal startOffset, decimal endDuration, TimeSpan videoLength)
{
using var process = new Process
{
@@ -358,7 +358,7 @@ private int RunFfmpegVideoCopy(string tempFolder, FileInfo outputFile, string co
};

// TODO: Make this optional - "Safe" and "Exact" trimming methods
if (endOffset > 0)
if (endDuration > 0)
{
args.Insert(0, "-t");
args.Insert(1, videoLength.TotalSeconds.ToString(CultureInfo.InvariantCulture));
@@ -438,15 +438,15 @@ private void HandleFfmpegOutput(string output, Regex encodingTimeRegex, TimeSpan
return (playlist, airDate);
}

private Range GetStreamListTrim(IList<M3U8.Stream> streamList, VideoInfo videoInfo, out TimeSpan videoLength, out decimal startOffset, out decimal endOffset)
private Range GetStreamListTrim(IList<M3U8.Stream> streamList, out TimeSpan videoLength, out decimal startOffset, out decimal endDuration)
{
startOffset = 0;
endOffset = 0;
endDuration = 0;

var startIndex = 0;
var startTime = 0m;
if (downloadOptions.TrimBeginning)
{
var startTime = 0m;
var trimTotalSeconds = (decimal)downloadOptions.TrimBeginningTime.TotalSeconds;
foreach (var videoPart in streamList)
{
@@ -462,17 +462,18 @@ private Range GetStreamListTrim(IList<M3U8.Stream> streamList, VideoInfo videoIn
}

var endIndex = streamList.Count;
var endTime = streamList.Sum(x => x.PartInfo.Duration);
var endOffset = 0m;
if (downloadOptions.TrimEnding)
{
var endTime = streamList.Sum(x => x.PartInfo.Duration);
var trimTotalSeconds = (decimal)downloadOptions.TrimEndingTime.TotalSeconds;
for (var i = streamList.Count - 1; i >= 0; i--)
{
var videoPart = streamList[i];
if (endTime - videoPart.PartInfo.Duration < trimTotalSeconds)
{
var offset = endTime - trimTotalSeconds;
if (offset > 0) endOffset = videoPart.PartInfo.Duration - offset;
endOffset = endTime - trimTotalSeconds;
if (endOffset > 0) endDuration = videoPart.PartInfo.Duration - endOffset;

break;
}
@@ -482,9 +483,7 @@ private Range GetStreamListTrim(IList<M3U8.Stream> streamList, VideoInfo videoIn
}
}

videoLength =
(downloadOptions.TrimEnding ? downloadOptions.TrimEndingTime : TimeSpan.FromSeconds(videoInfo.lengthSeconds))
- (downloadOptions.TrimBeginning ? downloadOptions.TrimBeginningTime : TimeSpan.Zero);
videoLength = TimeSpan.FromSeconds((double)((endTime - endOffset) - (startTime + startOffset)));

return new Range(startIndex, endIndex);
}