Skip to content

Commit 9fc16e2

Browse files
committed
Simplify VideoDownloader video part verification
1 parent 3f4a797 commit 9fc16e2

File tree

2 files changed

+5
-29
lines changed

2 files changed

+5
-29
lines changed

TwitchDownloaderCore/Tools/VideoDownloadThread.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private async Task DownloadVideoPartAsync(string videoPartName, CancellationToke
123123
{
124124
const int MAX_RETRIES = 1;
125125

126-
_logger.LogVerbose($"{partFile}: expected {expectedLength:N0}B, got {actualLength:N0}B.");
126+
_logger.LogVerbose($"{partFile} failed to verify: expected {expectedLength:N0}B, got {actualLength:N0}B.");
127127
if (++lengthFailureCount > MAX_RETRIES)
128128
{
129129
throw new Exception($"Failed to download {partFile}: expected {expectedLength:N0}B, got {actualLength:N0}B.");

TwitchDownloaderCore/VideoDownloader.cs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,8 @@ private async Task VerifyDownloadedParts(ICollection<M3U8.Stream> playlist, Rang
298298
foreach (var part in playlist.Take(videoListCrop))
299299
{
300300
var filePath = Path.Combine(downloadFolder, DownloadTools.RemoveQueryString(part.Path));
301-
if (!VerifyVideoPart(filePath))
301+
var fi = new FileInfo(filePath);
302+
if (!fi.Exists || fi.Length == 0)
302303
{
303304
failedParts.Add(part);
304305
}
@@ -312,43 +313,18 @@ private async Task VerifyDownloadedParts(ICollection<M3U8.Stream> playlist, Rang
312313

313314
if (failedParts.Count != 0)
314315
{
315-
if (playlist.Count == 1)
316-
{
317-
// The video is only 1 part, it probably won't be a complete file.
318-
return;
319-
}
320-
321316
if (partCount > 20 && failedParts.Count >= partCount * 0.95)
322317
{
323-
// 19/20 parts failed to verify. Either the VOD is heavily corrupted or something went horribly wrong.
318+
// 19/20 parts are missing or empty, something went horribly wrong.
324319
// TODO: Somehow let the user bypass this. Maybe with callbacks?
325-
throw new Exception($"Too many parts are corrupted or missing ({failedParts.Count}/{partCount}), aborting.");
320+
throw new Exception($"Too many parts are missing ({failedParts.Count}/{partCount}), aborting.");
326321
}
327322

328323
_progress.LogInfo($"The following parts will be redownloaded: {string.Join(", ", failedParts)}");
329324
await DownloadVideoPartsAsync(failedParts, videoListCrop, baseUrl, downloadFolder, vodAirDate, cancellationToken);
330325
}
331326
}
332327

333-
private static bool VerifyVideoPart(string filePath)
334-
{
335-
const int TS_PACKET_LENGTH = 188; // MPEG TS packets are made of a header and a body: [ 4B ][ 184B ] - https://tsduck.io/download/docs/mpegts-introduction.pdf
336-
337-
if (!File.Exists(filePath))
338-
{
339-
return false;
340-
}
341-
342-
using var fs = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
343-
var fileLength = fs.Length;
344-
if (fileLength == 0 || fileLength % TS_PACKET_LENGTH != 0)
345-
{
346-
return false;
347-
}
348-
349-
return true;
350-
}
351-
352328
private int RunFfmpegVideoCopy(string tempFolder, FileInfo outputFile, string concatListPath, string metadataPath, decimal startOffset, decimal endOffset, TimeSpan videoLength)
353329
{
354330
using var process = new Process

0 commit comments

Comments
 (0)