Xamarin, UWP: Youtube downloader
This recipe shows how to download the videos from Youtube by Xamarin Android, Universal App.
NOTE : install YoutubeExtractor from Nuget.
Follow these steps to get the download URLs
// Our test youtube link
string link = "insert youtube link";
/*
* Get the available video formats.
* We'll work with them in the video and audio download examples.
*/
IEnumerable<VideoInfo> videoInfos = DownloadUrlResolver.GetDownloadUrls(link);
Xamarin Android: Download videos
NOTE: In Xamarin Android you have to search any video and then you click that video -> navigate to new paper -> so you can download video here
async void Download_Click(object sender, EventArgs e)
{
// create WebCLient()
var webClient = new WebClient();
if (!string.IsNullOrEmpty(video_url))
{
byte[] bytes = null;
webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
dialog = new ProgressDialog(this);
dialog.SetProgressStyle(ProgressDialogStyle.Horizontal);
dialog.SetTitle("Downloading...");
dialog.SetCancelable(false);
dialog.SetCanceledOnTouchOutside(false);
dialog.Show();
try
{
//Downloads the resource as a Byte array from the URI specified as an asynchronous operation using a task object.
bytes = await webClient.DownloadDataTaskAsync(video_url);
}
catch (TaskCanceledException)
{
Toast.MakeText(this, "Task Canceled", ToastLength.Long).Show();
return;
}
catch (Exception a)
{
Toast.MakeText(this, a.InnerException.Message, ToastLength.Long).Show();
dialog.Progress = 0;
return;
}
//save a video to the path is "My files/ Device storage/ Download"
var documentsPath = Android.OS.Environment.ExternalStorageDirectory + "/Download";
// remove all special symbol in video title
string localFilename = video_title
.Replace('.', 'a')
.Replace('-', 'a')
.Replace('(', 'a')
.Replace(')', 'a')
.Replace('"', 'a')
.Replace(',', 'a')
+ video_extension;
//combine the path and file name together
string localPath = System.IO.Path.Combine(documentsPath, localFilename);
dialog.SetTitle("Download Complete");
//Save file by using writeAsync
FileStream fs = new FileStream(localPath, FileMode.OpenOrCreate);
await fs.WriteAsync(bytes, 0, bytes.Length);
fs.Close();
dialog.Progress = 0;
}
}
void WebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
dialog.Progress = e.ProgressPercentage;
if (e.ProgressPercentage == 100)
{
dialog.Hide();
}
}
UWP: Download videos
public DownloadViewModel(string video_url, string title, string thumb, string type)
{
DownloadManager(video_url, title, thumb, type);
}
private async void DownloadManager(string video_url, string title, string thumb, string type)
{
this.Title = title;
this.Thumbail = thumb;
string result_title = this.Title.Replace("-", " ")
.Replace("&", " ")
.Replace("/", " ")
.Replace("!", " ")
.Replace("(", "")
.Replace(":", "")
.Replace(")", "");
//BackgrounDownloader used to configure downloads prior to the actual creation of the download operation using CreateDownload
var bgDownloader = new BackgroundDownloader();
//get the path of VideosLibrary on PC or Phone
StorageFolder folder = KnownFolders.VideosLibrary;
var part = await folder.CreateFileAsync(result_title + type, CreationCollisionOption.ReplaceExisting);
DownloadOperation downloadOperation = bgDownloader.CreateDownload(new Uri(video_url), part);
await StartDonwloadAsync(downloadOperation);
}
private async Task StartDonwloadAsync(DownloadOperation obj)
{
var process = new Progress<DownloadOperation>(ProgressCallback);
await obj.StartAsync().AsTask(process);
}
private void ProgressCallback(DownloadOperation obj)
{
this.Status = "Downloading...";
this.Downloaded = string.Format("{0:0,000}", (double)(obj.Progress.BytesReceived / 1024));
this.Percentage = ((double)obj.Progress.BytesReceived / obj.Progress.TotalBytesToReceive) * 100;
if(percentage>=100)
{
this.Status = "Completed";
this.Percentage = 0.0;
}
}
Screenshot
Android
UWP
Source
https://code.msdn.microsoft.com/Android-Youtube-Downloader-1c9cc7f0
https://code.msdn.microsoft.com/UWP-Youtube-Downloader-773d7e91