Hopefully fix stalling and videos will stop downloading 5 minutes before the six hour mark

This commit is contained in:
Mike Nolan 2022-10-18 03:18:42 -05:00
parent 1473dace87
commit 75fe70a1e7
3 changed files with 56 additions and 21 deletions

View File

@ -11,6 +11,7 @@ namespace Tesses.YouTubeDownloader
{ {
public class BestStreams public class BestStreams
{ {
public TimeSpan TillExpire {get;set;}
public bool VideoFrozen {get;set;} public bool VideoFrozen {get;set;}
public BestStreamInfo MuxedStreamInfo {get;set;} public BestStreamInfo MuxedStreamInfo {get;set;}
@ -88,6 +89,7 @@ namespace Tesses.YouTubeDownloader
if(DateTime.Now < serialization.Expires || !expire_check) if(DateTime.Now < serialization.Expires || !expire_check)
{ {
BestStreams streams=new BestStreams(); BestStreams streams=new BestStreams();
streams.TillExpire = serialization.Expires - DateTime.Now;
streams.VideoOnlyStreamInfo = new BestStreamInfo(serialization.VideoOnly); streams.VideoOnlyStreamInfo = new BestStreamInfo(serialization.VideoOnly);
streams.AudioOnlyStreamInfo = new BestStreamInfo(serialization.Audio); streams.AudioOnlyStreamInfo = new BestStreamInfo(serialization.Audio);
streams.MuxedStreamInfo =new BestStreamInfo(serialization.Muxed); streams.MuxedStreamInfo =new BestStreamInfo(serialization.Muxed);
@ -103,7 +105,7 @@ namespace Tesses.YouTubeDownloader
if(video.VideoFrozen) if(video.VideoFrozen)
{ {
return new BestStreams() {VideoFrozen=true,MuxedStreamInfo=null,VideoOnlyStreamInfo=null,AudioOnlyStreamInfo=null}; return new BestStreams() {VideoFrozen=true,MuxedStreamInfo=null,VideoOnlyStreamInfo=null,AudioOnlyStreamInfo=null,TillExpire=new TimeSpan(0,0,0)};
} }
} }
var res=await storage.YoutubeClient.Videos.Streams.GetManifestAsync(id,token); var res=await storage.YoutubeClient.Videos.Streams.GetManifestAsync(id,token);
@ -114,6 +116,7 @@ namespace Tesses.YouTubeDownloader
BestStreamsSerialized serialized=new BestStreamsSerialized(); BestStreamsSerialized serialized=new BestStreamsSerialized();
serialized.Expires=expires; serialized.Expires=expires;
BestStreams streams1 = new BestStreams(); BestStreams streams1 = new BestStreams();
streams1.TillExpire = new TimeSpan(6,0,0);
streams1.VideoOnlyStreamInfo=new BestStreamInfo(); streams1.VideoOnlyStreamInfo=new BestStreamInfo();
streams1.VideoOnlyStreamInfo.SetInfo(videoOnly); streams1.VideoOnlyStreamInfo.SetInfo(videoOnly);
serialized.VideoOnly=streams1.VideoOnlyStreamInfo.Serialization; serialized.VideoOnly=streams1.VideoOnlyStreamInfo.Serialization;

View File

@ -40,7 +40,7 @@ namespace Tesses.YouTubeDownloader
} }
} }
} }
private CancellationTokenSource currentVideoCancelSource=null;
public readonly SavedVideoProgress Progress = new SavedVideoProgress(); public readonly SavedVideoProgress Progress = new SavedVideoProgress();
private async Task ReportProgress(double progress) private async Task ReportProgress(double progress)
{ {
@ -179,6 +179,7 @@ namespace Tesses.YouTubeDownloader
{ {
cancelDownload=true; cancelDownload=true;
restartDownload=restart; restartDownload=restart;
if(currentVideoCancelSource != null) currentVideoCancelSource.Cancel();
} }
private async Task DownloadFileAsync(SavedVideo video, CancellationToken token, IProgress<double> progress, bool report) private async Task DownloadFileAsync(SavedVideo video, CancellationToken token, IProgress<double> progress, bool report)
@ -521,6 +522,7 @@ namespace Tesses.YouTubeDownloader
} }
private async Task DownloadVideoMuxedAsync(SavedVideo video,CancellationToken token,IProgress<double> progress,bool report=true) private async Task DownloadVideoMuxedAsync(SavedVideo video,CancellationToken token,IProgress<double> progress,bool report=true)
{ {
bool isValid=true; bool isValid=true;
isValid=await DownloadVideoOnlyAsync(video,token,progress,report); isValid=await DownloadVideoOnlyAsync(video,token,progress,report);
if(token.IsCancellationRequested || !isValid || cancelDownload || restartDownload) if(token.IsCancellationRequested || !isValid || cancelDownload || restartDownload)
@ -564,9 +566,15 @@ namespace Tesses.YouTubeDownloader
} }
public async Task<bool> DownloadVideoOnlyAsync(SavedVideo video,CancellationToken token,IProgress<double> progress,bool report=true) public async Task<bool> DownloadVideoOnlyAsync(SavedVideo video,CancellationToken token,IProgress<double> progress,bool report=true)
{ {
currentVideoCancelSource = new CancellationTokenSource();
token.Register(()=>{
currentVideoCancelSource.Cancel();
});
bool ret=false; bool ret=false;
var streams = await BestStreamInfo.GetBestStreams(this, video.Id, token, false); var streams = await BestStreamInfo.GetBestStreams(this, video.Id, currentVideoCancelSource.Token, false);
if(streams != null) if(streams != null)
{ {
@ -581,10 +589,12 @@ namespace Tesses.YouTubeDownloader
if(await Continue(complete)) if(await Continue(complete))
{ {
if(!can_download) return false; if(!can_download) return false;
streams = await BestStreamInfo.GetBestStreams(this,video.Id,token); streams = await BestStreamInfo.GetBestStreams(this,video.Id,currentVideoCancelSource.Token);
if(streams != null) if(streams != null)
{ {
using(var strm = await YoutubeClient.Videos.Streams.GetAsync(streams.VideoOnlyStreamInfo,token)) currentVideoCancelSource.CancelAfter(streams.TillExpire - new TimeSpan(0,5,0));
using(var strm = await YoutubeClient.Videos.Streams.GetAsync(streams.VideoOnlyStreamInfo,currentVideoCancelSource.Token))
{ {
if(token.IsCancellationRequested || cancelDownload || restartDownload) if(token.IsCancellationRequested || cancelDownload || restartDownload)
{ {
@ -596,7 +606,9 @@ namespace Tesses.YouTubeDownloader
using(var dest = await OpenOrCreateAsync(incomplete)) using(var dest = await OpenOrCreateAsync(incomplete))
{ {
ret=await CopyStreamAsync(strm,dest,dest.Length,streams.VideoOnlyStreamInfo.Size.Bytes,4096,progress,token); ret=await CopyStreamAsync(strm,dest,dest.Length,streams.VideoOnlyStreamInfo.Size.Bytes,4096,progress,currentVideoCancelSource.Token);
currentVideoCancelSource.Dispose();
currentVideoCancelSource=null;
} }
if(ret) if(ret)
{ {
@ -665,10 +677,15 @@ namespace Tesses.YouTubeDownloader
} }
private async Task<bool> DownloadAudioOnlyAsync(SavedVideo video,CancellationToken token,IProgress<double> progress,bool report=true) private async Task<bool> DownloadAudioOnlyAsync(SavedVideo video,CancellationToken token,IProgress<double> progress,bool report=true)
{ {
currentVideoCancelSource = new CancellationTokenSource();
token.Register(()=>{
currentVideoCancelSource.Cancel();
});
bool ret=false; bool ret=false;
var streams = await BestStreamInfo.GetBestStreams(this, video.Id, token, false); var streams = await BestStreamInfo.GetBestStreams(this, video.Id, token, false);
if(streams != null) if(streams != null)
{ {
if(streams.VideoFrozen) if(streams.VideoFrozen)
@ -682,11 +699,13 @@ namespace Tesses.YouTubeDownloader
if(await Continue(complete)) if(await Continue(complete))
{ {
if(!can_download) return false; if(!can_download) return false;
streams = await BestStreamInfo.GetBestStreams(this,video.Id,token); streams = await BestStreamInfo.GetBestStreams(this,video.Id,currentVideoCancelSource.Token);
if(streams != null) if(streams != null)
{ {
currentVideoCancelSource.CancelAfter(streams.TillExpire - new TimeSpan(0,5,0));
using(var strm = await YoutubeClient.Videos.Streams.GetAsync(streams.AudioOnlyStreamInfo,token)) using(var strm = await YoutubeClient.Videos.Streams.GetAsync(streams.AudioOnlyStreamInfo,currentVideoCancelSource.Token))
{ {
if(token.IsCancellationRequested || cancelDownload || restartDownload) if(token.IsCancellationRequested || cancelDownload || restartDownload)
{ {
@ -698,7 +717,9 @@ namespace Tesses.YouTubeDownloader
using(var dest = await OpenOrCreateAsync(incomplete)) using(var dest = await OpenOrCreateAsync(incomplete))
{ {
ret=await CopyStreamAsync(strm,dest,dest.Length,streams.AudioOnlyStreamInfo.Size.Bytes,4096,progress,token); ret=await CopyStreamAsync(strm,dest,dest.Length,streams.AudioOnlyStreamInfo.Size.Bytes,4096,progress,currentVideoCancelSource.Token);
currentVideoCancelSource.Dispose();
currentVideoCancelSource=null;
} }
if(ret) if(ret)
{ {
@ -719,7 +740,14 @@ namespace Tesses.YouTubeDownloader
} }
private async Task DownloadPreMuxedVideoAsync(SavedVideo video, CancellationToken token,IProgress<double> progress,bool report=true) private async Task DownloadPreMuxedVideoAsync(SavedVideo video, CancellationToken token,IProgress<double> progress,bool report=true)
{ {
var streams = await BestStreamInfo.GetBestStreams(this, video.Id, token, false); currentVideoCancelSource = new CancellationTokenSource();
token.Register(()=>{
currentVideoCancelSource.Cancel();
});
var streams = await BestStreamInfo.GetBestStreams(this, video.Id, currentVideoCancelSource.Token, false);
if(!can_download) return; if(!can_download) return;
if(streams != null) if(streams != null)
{ {
@ -734,11 +762,13 @@ namespace Tesses.YouTubeDownloader
if(await Continue(complete)) if(await Continue(complete))
{ {
streams = await BestStreamInfo.GetBestStreams(this,video.Id,token); streams = await BestStreamInfo.GetBestStreams(this,video.Id,currentVideoCancelSource.Token);
if(streams != null) if(streams != null)
{ {
currentVideoCancelSource.CancelAfter(streams.TillExpire - new TimeSpan(0,5,0));
using(var strm = await YoutubeClient.Videos.Streams.GetAsync(streams.MuxedStreamInfo,token)) using(var strm = await YoutubeClient.Videos.Streams.GetAsync(streams.MuxedStreamInfo,currentVideoCancelSource.Token))
{ {
if(token.IsCancellationRequested || cancelDownload || restartDownload) if(token.IsCancellationRequested || cancelDownload || restartDownload)
{ {
@ -750,7 +780,9 @@ namespace Tesses.YouTubeDownloader
bool ret; bool ret;
using(var dest = await OpenOrCreateAsync(incomplete)) using(var dest = await OpenOrCreateAsync(incomplete))
{ {
ret=await CopyStreamAsync(strm,dest,dest.Length,streams.MuxedStreamInfo.Size.Bytes,4096,progress,token); ret=await CopyStreamAsync(strm,dest,dest.Length,streams.MuxedStreamInfo.Size.Bytes,4096,progress,currentVideoCancelSource.Token);
currentVideoCancelSource.Dispose();
currentVideoCancelSource=null;
} }
//We know its resolution //We know its resolution
if(ret) if(ret)

View File

@ -7,9 +7,9 @@
<PackageId>Tesses.YouTubeDownloader</PackageId> <PackageId>Tesses.YouTubeDownloader</PackageId>
<Author>Mike Nolan</Author> <Author>Mike Nolan</Author>
<Company>Tesses</Company> <Company>Tesses</Company>
<Version>1.2.0</Version> <Version>1.2.1</Version>
<AssemblyVersion>1.2.0</AssemblyVersion> <AssemblyVersion>1.2.1</AssemblyVersion>
<FileVersion>1.2.0</FileVersion> <FileVersion>1.2.1</FileVersion>
<Description>A YouTube Downloader</Description> <Description>A YouTube Downloader</Description>
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression> <PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance> <PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>