Comments (5)
@derekantrican nope, you would have to manage this yourself via the stream coming from GetMediaStreamAsync
from youtubeexplode.
Ok, for anyone that finds this later I have adapted this solution into this:
Usage:
FileDownload fileDownload;
private async void DownloadVideo()
{
//Do YoutubeClient & MuxedStreamInfo setup stuff
using (MediaStream stream = await client.GetMediaStreamAsync(videoQuality).ConfigureAwait(false))
{
fileDownload = new FileDownload(stream, targetPath, progress: progress);
await fileDownload.Start();
}
}
private void PauseDownload()
{
fileDownload.Pause();
}
private void ResumeDownload()
{
fileDownload.Start();
}
FileDownload class
public class FileDownload
{
private volatile bool _allowedToRun;
private Stream _sourceStream;
private string _sourceUrl;
private string _destination;
private bool _disposeOnCompletion;
private int _chunkSize;
private IProgress<double> _progress;
private Lazy<int> _contentLength;
public int BytesWritten { get; private set; }
public int ContentLength { get { return _contentLength.Value; } }
public bool Done { get { return ContentLength == BytesWritten; } }
public FileDownload(Stream source, string destination, bool disposeOnCompletion = true, int chunkSizeInBytes = 10000 /*Default to 0.01 mb*/, IProgress<double> progress = null)
{
_allowedToRun = true;
_sourceStream = source;
_destination = destination;
_disposeOnCompletion = disposeOnCompletion;
_chunkSize = chunkSizeInBytes;
_contentLength = new Lazy<int>(() => Convert.ToInt32(GetContentLength()));
_progress = progress;
BytesWritten = 0;
}
public FileDownload(string source, string destination, int chunkSizeInBytes = 10000 /*Default to 0.01 mb*/, IProgress<double> progress = null)
{
_allowedToRun = true;
_sourceUrl = source;
_destination = destination;
_chunkSize = chunkSizeInBytes;
_contentLength = new Lazy<int>(() => Convert.ToInt32(GetContentLength()));
_progress = progress;
BytesWritten = 0;
}
private long GetContentLength()
{
if (_sourceStream != null)
return _sourceStream.Length;
else
{
var request = (HttpWebRequest)WebRequest.Create(_sourceUrl);
request.Method = "HEAD";
using (var response = request.GetResponse())
return response.ContentLength;
}
}
private async Task Start(int range)
{
if (!_allowedToRun)
throw new InvalidOperationException();
if (_sourceStream != null)
{
using (var fs = new FileStream(_destination, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
{
while (_allowedToRun)
{
var buffer = new byte[_chunkSize];
var bytesRead = await _sourceStream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
if (bytesRead == 0) break;
await fs.WriteAsync(buffer, 0, bytesRead);
BytesWritten += bytesRead;
_progress?.Report((double)BytesWritten / ContentLength);
}
await fs.FlushAsync();
}
//Control whether the stream should be disposed here or outside of this class
if (BytesWritten == ContentLength && _disposeOnCompletion)
_sourceStream?.Dispose();
}
else
{
var request = (HttpWebRequest)WebRequest.Create(_sourceUrl);
request.Method = "GET";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
request.AddRange(range);
using (var response = await request.GetResponseAsync())
{
using (var responseStream = response.GetResponseStream())
{
using (var fs = new FileStream(_destination, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
{
while (_allowedToRun)
{
var buffer = new byte[_chunkSize];
var bytesRead = await responseStream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
if (bytesRead == 0) break;
await fs.WriteAsync(buffer, 0, bytesRead);
BytesWritten += bytesRead;
_progress?.Report((double)BytesWritten / ContentLength);
}
await fs.FlushAsync();
}
}
}
}
}
public Task Start()
{
_allowedToRun = true;
return Start(BytesWritten);
}
public void Pause()
{
_allowedToRun = false;
}
}
from youtubeexplode.
If you're using DownloadMediaStreamAsync
then it has CancellationToken
parameter
from youtubeexplode.
Do you have any more questions regarding this issue?
from youtubeexplode.
@Tyrrrz I know this is old but my question is on topic - is there a way to "pause" and "resume" DownloadMediaStreamAsync
?
from youtubeexplode.
Related Issues (20)
- Cannot get stream manifest of a video that is still being processed HOT 8
- Search feature crashes on mobile devices HOT 1
- Audio files are downloaded as WebM, Mp3 does not work HOT 3
- Using a live video URL throws an exception
- 403 (Forbidden) HOT 1
- Occasionally streams just dies HOT 13
- Streams are sometimes unstable
- Exception "Could not extract video upload date" with specific video
- GetVideosAsync does not return all videos of a playlist
- GetManifestAsync silent crash on Android HOT 9
- YoutubeExplode failed to fetch video HOT 1
- Closed captions that erroneously contain valid SRT segments within them break when processing with FFmpeg HOT 1
- Closed captions that contain invalid offsets and/or durations break when processing with FFmpeg
- Some streams can return 404 HOT 4
- Getting `System.Net.CookieException` on some requests HOT 6
- Invalid Youtube Video ID or URL error
- Searching fails when encountering pay-to-watch videos HOT 4
- Video downloads, but only audio, no video HOT 30
- Download private video by passing list of cookies, bit got 401
- 500 error when calling GetManifestAsync HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from youtubeexplode.