2021-06-24 01:10:20 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using YoutubeExplode.Videos;
|
|
|
|
|
using YoutubeExplode;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.IO;
|
2021-12-08 01:07:43 +00:00
|
|
|
|
using TYTD.Server.Models;
|
2021-06-24 01:10:20 +00:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
|
|
using YoutubeExplode.Videos.Streams;
|
|
|
|
|
using YoutubeExplode.Channels;
|
|
|
|
|
using YoutubeExplode.Playlists;
|
2021-12-08 01:07:43 +00:00
|
|
|
|
|
2021-08-26 09:04:57 +00:00
|
|
|
|
using Dasync.Collections;
|
2021-12-12 10:04:16 +00:00
|
|
|
|
using System.Threading;
|
2021-12-08 22:02:30 +00:00
|
|
|
|
|
2021-12-08 01:07:43 +00:00
|
|
|
|
namespace TYTD.Server.Functions
|
2021-06-24 01:10:20 +00:00
|
|
|
|
{
|
|
|
|
|
public class Downloader
|
|
|
|
|
{
|
2021-12-12 10:04:16 +00:00
|
|
|
|
|
|
|
|
|
public class Lockable<T>
|
2021-06-24 03:41:34 +00:00
|
|
|
|
{
|
2021-12-12 10:04:16 +00:00
|
|
|
|
public Lockable(Func<T> recreate)
|
|
|
|
|
{
|
2021-12-12 10:35:11 +00:00
|
|
|
|
_recreate = recreate;
|
|
|
|
|
Item = recreate();
|
2021-12-12 10:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
Func<T> _recreate;
|
|
|
|
|
public void Recreate()
|
|
|
|
|
{
|
|
|
|
|
Item = _recreate();
|
|
|
|
|
}
|
|
|
|
|
public T Item { get; set; }
|
2021-06-24 03:41:34 +00:00
|
|
|
|
|
2021-12-12 10:04:16 +00:00
|
|
|
|
public void Change(T item)
|
|
|
|
|
{
|
|
|
|
|
Item = item;
|
|
|
|
|
}
|
|
|
|
|
public static implicit operator T(Lockable<T> item)
|
|
|
|
|
{
|
|
|
|
|
return item.Item;
|
|
|
|
|
}
|
2021-06-24 03:41:34 +00:00
|
|
|
|
}
|
2021-12-08 22:02:30 +00:00
|
|
|
|
|
2021-12-08 01:07:43 +00:00
|
|
|
|
public static bool RedownloadIt = false;
|
2021-09-25 22:30:16 +00:00
|
|
|
|
public List<InfomationQueueItem> infoQueue = new List<InfomationQueueItem>();
|
2021-06-24 01:10:20 +00:00
|
|
|
|
public static YoutubeClient CreateYoutubeClient()
|
|
|
|
|
{
|
2021-09-25 22:30:16 +00:00
|
|
|
|
|
2021-06-24 01:10:20 +00:00
|
|
|
|
ServicePointManager
|
|
|
|
|
.ServerCertificateValidationCallback +=
|
|
|
|
|
(sender, cert, chain, sslPolicyErrors) => true;
|
|
|
|
|
HttpClientHandler handler = new HttpClientHandler();
|
|
|
|
|
handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
|
2021-09-25 22:30:16 +00:00
|
|
|
|
|
2021-09-25 19:33:10 +00:00
|
|
|
|
Directory.CreateDirectory("config");
|
2021-09-25 22:30:16 +00:00
|
|
|
|
string cookiesFile = Path.Combine("config", "cookies.txt");
|
2021-09-25 19:33:10 +00:00
|
|
|
|
if (File.Exists(cookiesFile))
|
|
|
|
|
{
|
2021-09-25 22:30:16 +00:00
|
|
|
|
var cookies = CookiesTxt.Parser.ParseFileAsCookieCollection(cookiesFile);
|
2021-09-25 19:33:10 +00:00
|
|
|
|
handler.CookieContainer.Add(cookies);
|
|
|
|
|
}
|
2021-06-24 01:10:20 +00:00
|
|
|
|
Http = new HttpClient(handler);
|
2021-08-03 19:39:03 +00:00
|
|
|
|
return new YoutubeClient(Http);
|
2021-06-24 01:10:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-08 01:07:43 +00:00
|
|
|
|
public static VideoDownloadProgress GetProgress()
|
2021-06-24 01:10:20 +00:00
|
|
|
|
{
|
|
|
|
|
return P;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 08:30:45 +00:00
|
|
|
|
|
2021-06-24 01:10:20 +00:00
|
|
|
|
static HttpClient Http;
|
2021-09-25 22:30:16 +00:00
|
|
|
|
internal YoutubeClient ytc = CreateYoutubeClient();
|
|
|
|
|
static VideoDownloadProgress P = new VideoDownloadProgress();
|
2021-12-08 01:07:43 +00:00
|
|
|
|
const int NUM_WAITS = 5;
|
2021-11-08 01:42:56 +00:00
|
|
|
|
static int WAITS = 0;
|
2021-12-09 04:31:45 +00:00
|
|
|
|
public static void SendProgress(double p)
|
|
|
|
|
{
|
|
|
|
|
WAITS++;
|
|
|
|
|
if (WAITS <= NUM_WAITS)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
WAITS = 0;
|
|
|
|
|
ApiLoader.SetProgress(p);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
Progress<double> DownloadP = new Progress<double>((e) => { P.Progress = (int)(e * 100.0); P.ProgressRaw = e; SendProgress(e); });
|
2021-12-08 01:07:43 +00:00
|
|
|
|
|
2021-09-25 22:30:16 +00:00
|
|
|
|
public List<SavedVideoObject> Queue = new List<SavedVideoObject>();
|
2021-06-24 01:10:20 +00:00
|
|
|
|
|
2021-12-08 01:07:43 +00:00
|
|
|
|
public static string GetQueue()
|
2021-06-24 01:10:20 +00:00
|
|
|
|
{
|
|
|
|
|
string q;
|
|
|
|
|
lock (DL.Queue)
|
|
|
|
|
{
|
2021-09-25 22:30:16 +00:00
|
|
|
|
q = JsonConvert.SerializeObject(DL.Queue.Select<SavedVideoObject, SavedVideo>(o => o.Video));
|
2021-06-24 01:10:20 +00:00
|
|
|
|
}
|
|
|
|
|
return q;
|
|
|
|
|
}
|
2021-06-25 08:30:45 +00:00
|
|
|
|
public async Task ListenForQueueItem()
|
|
|
|
|
{
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
InfomationQueueItem item;
|
|
|
|
|
bool canAdd = false;
|
|
|
|
|
lock (infoQueue)
|
|
|
|
|
{
|
|
|
|
|
canAdd = infoQueue.Count > 0;
|
|
|
|
|
if (canAdd)
|
|
|
|
|
{
|
|
|
|
|
item = infoQueue[0];
|
|
|
|
|
infoQueue.RemoveAt(0);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
item = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (canAdd)
|
|
|
|
|
{
|
2021-06-26 03:17:59 +00:00
|
|
|
|
try
|
2021-06-25 08:30:45 +00:00
|
|
|
|
{
|
2021-06-26 03:17:59 +00:00
|
|
|
|
var items = await item.DownloadData();
|
|
|
|
|
if (item.DownloadActualDataAfterwards)
|
|
|
|
|
{
|
|
|
|
|
_DownloadVideos(items);
|
|
|
|
|
}
|
2021-09-25 22:30:16 +00:00
|
|
|
|
} catch (Exception ex)
|
2021-06-26 03:17:59 +00:00
|
|
|
|
{
|
2021-06-26 03:22:31 +00:00
|
|
|
|
|
2021-06-26 03:17:59 +00:00
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
_ = ex;
|
|
|
|
|
}
|
2021-06-25 08:30:45 +00:00
|
|
|
|
}
|
|
|
|
|
} while (true);
|
|
|
|
|
}
|
2021-12-08 01:07:43 +00:00
|
|
|
|
public static void ModQueue2(string mvto, string index)
|
2021-09-25 22:30:16 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
//?mv=up|down|top|bottom|remove,int&i=videoId;URL
|
|
|
|
|
lock (DL.Queue)
|
|
|
|
|
{
|
|
|
|
|
int index2 = -1;
|
|
|
|
|
for(int i=0;i<DL.Queue.Count;i++)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if(DL.Queue[i].Video.Id == index)
|
|
|
|
|
{
|
|
|
|
|
index2 = i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(index2 < 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (mvto == "top")
|
|
|
|
|
{
|
|
|
|
|
var v = DL.Queue[index2];
|
|
|
|
|
DL.Queue.Remove(v);
|
|
|
|
|
DL.Queue.Insert(0, v);
|
|
|
|
|
}
|
|
|
|
|
else if (mvto == "bottom")
|
|
|
|
|
{
|
|
|
|
|
var v = DL.Queue[index2];
|
|
|
|
|
DL.Queue.Remove(v);
|
|
|
|
|
DL.Queue.Add(v);
|
|
|
|
|
}
|
|
|
|
|
else if (mvto == "remove")
|
|
|
|
|
{
|
|
|
|
|
var v = DL.Queue[index2];
|
|
|
|
|
DL.Queue.Remove(v);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else if (mvto == "up")
|
|
|
|
|
{
|
|
|
|
|
if (index2 > 0)
|
|
|
|
|
{
|
|
|
|
|
var v = DL.Queue[index2];
|
|
|
|
|
DL.Queue.Remove(v);
|
|
|
|
|
|
|
|
|
|
DL.Queue.Insert(index2 - 1, v);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else if (mvto == "down")
|
|
|
|
|
{
|
|
|
|
|
if (index2 < DL.Queue.Count - 1)
|
|
|
|
|
{
|
|
|
|
|
var v = DL.Queue[index2];
|
|
|
|
|
DL.Queue.Remove(v);
|
|
|
|
|
|
|
|
|
|
DL.Queue.Insert(index2 + 1, v);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int n1;
|
|
|
|
|
|
|
|
|
|
if (int.TryParse(mvto, out n1))
|
|
|
|
|
{
|
|
|
|
|
var v = DL.Queue[index2];
|
|
|
|
|
DL.Queue.Remove(v);
|
|
|
|
|
if (n1 > index2)
|
|
|
|
|
{
|
|
|
|
|
DL.Queue.Insert(n1 - 1, v);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
DL.Queue.Insert(n1, v);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
|
|
|
|
|
_ = ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-08 01:07:43 +00:00
|
|
|
|
public static void ModQueue(string mvto, string index)
|
2021-06-24 01:10:20 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
//?mv=up|down|top|bottom|remove,int&i=0,last
|
|
|
|
|
lock (DL.Queue)
|
|
|
|
|
{
|
|
|
|
|
int index2 = 0;
|
|
|
|
|
if (index == "last")
|
|
|
|
|
{
|
|
|
|
|
index2 = DL.Queue.Count - 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!int.TryParse(index, out index2))
|
|
|
|
|
{
|
|
|
|
|
index2 = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (index2 >= DL.Queue.Count)
|
|
|
|
|
{
|
|
|
|
|
index2 = DL.Queue.Count - 1;
|
|
|
|
|
}
|
|
|
|
|
if (mvto == "top")
|
|
|
|
|
{
|
|
|
|
|
var v = DL.Queue[index2];
|
|
|
|
|
DL.Queue.Remove(v);
|
2021-09-25 22:30:16 +00:00
|
|
|
|
DL.Queue.Insert(0, v);
|
2021-06-24 01:10:20 +00:00
|
|
|
|
}
|
|
|
|
|
else if (mvto == "bottom")
|
|
|
|
|
{
|
|
|
|
|
var v = DL.Queue[index2];
|
|
|
|
|
DL.Queue.Remove(v);
|
|
|
|
|
DL.Queue.Add(v);
|
|
|
|
|
}
|
|
|
|
|
else if (mvto == "remove")
|
|
|
|
|
{
|
|
|
|
|
var v = DL.Queue[index2];
|
|
|
|
|
DL.Queue.Remove(v);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else if (mvto == "up")
|
|
|
|
|
{
|
|
|
|
|
if (index2 > 0)
|
|
|
|
|
{
|
|
|
|
|
var v = DL.Queue[index2];
|
|
|
|
|
DL.Queue.Remove(v);
|
|
|
|
|
|
|
|
|
|
DL.Queue.Insert(index2 - 1, v);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else if (mvto == "down")
|
|
|
|
|
{
|
|
|
|
|
if (index2 < DL.Queue.Count - 1)
|
|
|
|
|
{
|
|
|
|
|
var v = DL.Queue[index2];
|
|
|
|
|
DL.Queue.Remove(v);
|
|
|
|
|
|
|
|
|
|
DL.Queue.Insert(index2 + 1, v);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int n1;
|
|
|
|
|
|
|
|
|
|
if (int.TryParse(mvto, out n1))
|
|
|
|
|
{
|
|
|
|
|
var v = DL.Queue[index2];
|
|
|
|
|
DL.Queue.Remove(v);
|
|
|
|
|
if (n1 > index2)
|
|
|
|
|
{
|
|
|
|
|
DL.Queue.Insert(n1 - 1, v);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
DL.Queue.Insert(n1, v);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2021-09-25 22:30:16 +00:00
|
|
|
|
} catch (Exception ex)
|
2021-06-24 01:10:20 +00:00
|
|
|
|
{
|
2021-06-26 03:22:31 +00:00
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
|
2021-06-24 01:10:20 +00:00
|
|
|
|
_ = ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-25 10:55:27 +00:00
|
|
|
|
|
2021-12-08 01:07:43 +00:00
|
|
|
|
public static void DownloadChannelOnly(string id)
|
2021-06-25 10:55:27 +00:00
|
|
|
|
{
|
|
|
|
|
ChannelId? v = ChannelId.TryParse(id);
|
|
|
|
|
if (v.HasValue)
|
|
|
|
|
{
|
2021-09-25 22:30:16 +00:00
|
|
|
|
InfomationQueueItem item = new InfomationQueueItem(v.Value, Resolution.NoConvert, false);
|
2021-06-25 10:55:27 +00:00
|
|
|
|
lock (DL.infoQueue)
|
|
|
|
|
{
|
|
|
|
|
DL.infoQueue.Insert(0, item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-25 06:18:12 +00:00
|
|
|
|
public static void DownloadItems(List<IDResolutionTypeTriplet> id)
|
|
|
|
|
{
|
|
|
|
|
List<InfomationQueueItem> items = new List<InfomationQueueItem>();
|
2021-09-25 22:30:16 +00:00
|
|
|
|
foreach (var item in id)
|
2021-09-25 06:18:12 +00:00
|
|
|
|
{
|
|
|
|
|
var iqi = item.ToInfomationQueueItem();
|
2021-09-25 22:30:16 +00:00
|
|
|
|
if (iqi != null)
|
2021-09-25 06:18:12 +00:00
|
|
|
|
{
|
|
|
|
|
items.Add(iqi);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (items.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
lock (DL.infoQueue)
|
|
|
|
|
{
|
2021-06-25 10:55:27 +00:00
|
|
|
|
|
2021-09-25 22:30:16 +00:00
|
|
|
|
DL.infoQueue.InsertRange(0, items);
|
2021-09-25 06:18:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-08 01:07:43 +00:00
|
|
|
|
public static void DownloadUserOnly(string id)
|
2021-06-25 10:55:27 +00:00
|
|
|
|
{
|
|
|
|
|
UserName? v = UserName.TryParse(id);
|
|
|
|
|
if (v.HasValue)
|
|
|
|
|
{
|
|
|
|
|
InfomationQueueItem item = new InfomationQueueItem(v.Value, Resolution.NoConvert, false);
|
|
|
|
|
lock (DL.infoQueue)
|
|
|
|
|
{
|
|
|
|
|
DL.infoQueue.Insert(0, item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-24 01:10:20 +00:00
|
|
|
|
public bool Continue(string v)
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(v))
|
|
|
|
|
{
|
2021-09-25 22:30:16 +00:00
|
|
|
|
using (var f = File.OpenRead(v))
|
2021-06-24 01:10:20 +00:00
|
|
|
|
{
|
|
|
|
|
return f.Length == 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
private string gStorageLocation()
|
|
|
|
|
{
|
2021-08-03 19:39:03 +00:00
|
|
|
|
if (File.Exists("loc.txt"))
|
2021-06-24 01:10:20 +00:00
|
|
|
|
{
|
2021-09-25 22:30:16 +00:00
|
|
|
|
string loc = File.ReadAllText("loc.txt");
|
2021-06-24 01:10:20 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2021-08-03 19:39:03 +00:00
|
|
|
|
Directory.CreateDirectory(loc);
|
|
|
|
|
if (Directory.Exists(loc))
|
2021-06-24 01:10:20 +00:00
|
|
|
|
{
|
|
|
|
|
return loc;
|
|
|
|
|
}
|
2021-09-25 22:30:16 +00:00
|
|
|
|
} catch (Exception ex)
|
2021-06-24 01:10:20 +00:00
|
|
|
|
{
|
2021-06-26 03:22:31 +00:00
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
|
2021-06-24 04:05:21 +00:00
|
|
|
|
_ = ex;
|
2021-06-24 01:10:20 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Environment.CurrentDirectory;
|
|
|
|
|
}
|
|
|
|
|
public string StorageLocation { get { return gStorageLocation(); } }
|
2021-06-25 08:30:45 +00:00
|
|
|
|
private void _DownloadVideos(SavedVideoObject[] items)
|
2021-09-25 22:30:16 +00:00
|
|
|
|
{
|
|
|
|
|
|
2021-06-24 01:10:20 +00:00
|
|
|
|
lock (Queue)
|
|
|
|
|
{
|
2021-06-25 08:30:45 +00:00
|
|
|
|
foreach (var item in items)
|
|
|
|
|
{
|
|
|
|
|
Queue.Insert(0, item);
|
|
|
|
|
//new elements get added to begining
|
|
|
|
|
}
|
2021-06-24 01:10:20 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-25 22:30:16 +00:00
|
|
|
|
public bool FileExists(string nameSrc,ref string nameDest,ref int i)
|
|
|
|
|
{
|
|
|
|
|
if(i == 1)
|
|
|
|
|
{
|
|
|
|
|
i++;
|
|
|
|
|
return File.Exists(nameSrc);
|
|
|
|
|
}
|
|
|
|
|
i++;
|
|
|
|
|
nameDest= Path.Combine(Path.GetDirectoryName(nameSrc), $"{Path.GetFileNameWithoutExtension(nameSrc)} ({i}){Path.GetExtension(nameSrc)}");
|
|
|
|
|
return File.Exists(nameDest);
|
|
|
|
|
}
|
2021-12-12 10:04:16 +00:00
|
|
|
|
public Lockable<CancellationTokenSource> cancelSrc = new Lockable<CancellationTokenSource>(()=> { return new CancellationTokenSource(); });
|
2021-12-08 22:02:30 +00:00
|
|
|
|
public static Func<YoutubeClient,VideoId, Task<StreamManifest>> GetManifest;
|
2021-12-12 10:04:16 +00:00
|
|
|
|
private async Task DownloadHDVideo(SavedVideoObject v,CancellationToken token)
|
2021-06-24 01:10:20 +00:00
|
|
|
|
{
|
2021-12-12 10:04:16 +00:00
|
|
|
|
string mypath = GetPath(true, "Converted", v.Video.Id + "-vidonly.bkp");
|
|
|
|
|
string mypathaudio = GetPath(true, "Audio", v.Video.Id + "incomplete.mp4");
|
|
|
|
|
string mypathCompleteAudio = GetPath(true, "Audio", v.Video.Id + ".mp4");
|
|
|
|
|
string mypathComplete = GetPath(true, "Converted", v.Video.Id + ".mp4");
|
|
|
|
|
string mypathIncompleteConverting = GetPath(true, "Converted", "conv.mkv");
|
|
|
|
|
|
|
|
|
|
if (Continue(mypathComplete))
|
2021-06-24 01:10:20 +00:00
|
|
|
|
{
|
2021-12-12 10:04:16 +00:00
|
|
|
|
|
|
|
|
|
var s3 = await GetManifest(ytc, v.Video.Id);
|
|
|
|
|
var best2 = s3.GetAudioOnlyStreams().GetWithHighestBitrate();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var best = s3.GetVideoOnlyStreams().GetWithHighestVideoQuality();
|
|
|
|
|
|
|
|
|
|
P.Length = best.Size.Bytes + best2.Size.Bytes;
|
|
|
|
|
ProgressTwo p = new ProgressTwo(best.Size.Bytes, best2.Size.Bytes, DownloadP);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using (var destStrm = File.Open(mypath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
|
2021-06-24 01:10:20 +00:00
|
|
|
|
{
|
2021-12-12 10:04:16 +00:00
|
|
|
|
long pos = 0;
|
|
|
|
|
long len = 0;
|
|
|
|
|
using (var srcStrm = await ytc.Videos.Streams.GetAsync(best,token))
|
2021-06-24 01:10:20 +00:00
|
|
|
|
{
|
2021-12-12 10:04:16 +00:00
|
|
|
|
if (token.IsCancellationRequested)
|
2021-09-25 22:30:16 +00:00
|
|
|
|
{
|
2021-12-12 10:04:16 +00:00
|
|
|
|
cancelSrc.Item.Dispose();
|
|
|
|
|
cancelSrc.Recreate();
|
|
|
|
|
if (RedownloadIt)
|
|
|
|
|
{
|
|
|
|
|
srcStrm.Dispose();
|
|
|
|
|
destStrm.Dispose();
|
|
|
|
|
lock (cancelSrc)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
token = cancelSrc.Item.Token;
|
|
|
|
|
}
|
|
|
|
|
await DownloadHDVideo(v, token);
|
|
|
|
|
}
|
|
|
|
|
return;
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
2021-12-12 10:04:16 +00:00
|
|
|
|
len = srcStrm.Length;
|
|
|
|
|
pos = destStrm.Length;
|
|
|
|
|
IProgress<double> myProgress = p.Video;
|
|
|
|
|
if (pos >= len)
|
2021-09-25 22:30:16 +00:00
|
|
|
|
{
|
2021-12-12 10:04:16 +00:00
|
|
|
|
|
|
|
|
|
myProgress.Report(1);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
/* This is why videos get corrupted */
|
|
|
|
|
srcStrm.Seek(destStrm.Length, SeekOrigin.Begin);
|
|
|
|
|
destStrm.Seek(destStrm.Length, SeekOrigin.Begin);
|
|
|
|
|
byte[] buffer = new byte[4096];
|
|
|
|
|
int read = 0;
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
read = await srcStrm.ReadAsync(buffer, 0, buffer.Length,token);
|
|
|
|
|
if (token.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
cancelSrc.Item.Dispose();
|
|
|
|
|
cancelSrc.Recreate();
|
|
|
|
|
if (RedownloadIt)
|
|
|
|
|
{
|
|
|
|
|
srcStrm.Dispose();
|
|
|
|
|
destStrm.Dispose();
|
|
|
|
|
lock (cancelSrc)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
token = cancelSrc.Item.Token;
|
|
|
|
|
}
|
|
|
|
|
await DownloadHDVideo(v, token);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
await destStrm.WriteAsync(buffer, 0, read,token);
|
|
|
|
|
if (token.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
cancelSrc.Item.Dispose();
|
|
|
|
|
cancelSrc.Recreate();
|
|
|
|
|
if (RedownloadIt)
|
|
|
|
|
{
|
|
|
|
|
srcStrm.Dispose();
|
|
|
|
|
destStrm.Dispose();
|
|
|
|
|
lock (cancelSrc)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
token = cancelSrc.Item.Token;
|
|
|
|
|
}
|
|
|
|
|
await DownloadHDVideo(v, token);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
pos += read;
|
|
|
|
|
double myP = (double)pos / (double)len;
|
|
|
|
|
myProgress.Report(myP);
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
2021-12-12 10:04:16 +00:00
|
|
|
|
while (read > 0 );
|
2021-09-25 22:30:16 +00:00
|
|
|
|
|
2021-06-24 01:10:20 +00:00
|
|
|
|
}
|
2021-12-12 10:04:16 +00:00
|
|
|
|
|
2021-06-24 01:10:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-12 10:04:16 +00:00
|
|
|
|
IProgress<double> pv = p.Video;
|
|
|
|
|
pv.Report(1);
|
|
|
|
|
if (Continue(mypathCompleteAudio))
|
2021-06-24 01:10:20 +00:00
|
|
|
|
{
|
2021-12-12 10:04:16 +00:00
|
|
|
|
long pos = 0;
|
|
|
|
|
long len = 0;
|
|
|
|
|
using (var destStrm = File.Open(mypathaudio, FileMode.OpenOrCreate, FileAccess.ReadWrite))
|
2021-06-24 01:10:20 +00:00
|
|
|
|
{
|
2021-12-12 10:04:16 +00:00
|
|
|
|
using (var srcStrm = await ytc.Videos.Streams.GetAsync(best2,token))
|
|
|
|
|
{
|
|
|
|
|
if (token.IsCancellationRequested)
|
2021-09-25 22:30:16 +00:00
|
|
|
|
{
|
2021-12-12 10:04:16 +00:00
|
|
|
|
cancelSrc.Item.Dispose();
|
|
|
|
|
cancelSrc.Recreate();
|
|
|
|
|
if (RedownloadIt)
|
2021-09-25 22:30:16 +00:00
|
|
|
|
{
|
2021-12-12 10:04:16 +00:00
|
|
|
|
srcStrm.Dispose();
|
|
|
|
|
destStrm.Dispose();
|
|
|
|
|
lock (cancelSrc)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
token = cancelSrc.Item.Token;
|
|
|
|
|
}
|
|
|
|
|
await DownloadHDVideo(v, token);
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
2021-12-12 10:04:16 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
len = srcStrm.Length;
|
|
|
|
|
pos = destStrm.Length;
|
|
|
|
|
IProgress<double> myProgress = p.Audio;
|
|
|
|
|
if (pos >= len)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
myProgress.Report(1);
|
|
|
|
|
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
2021-12-12 10:04:16 +00:00
|
|
|
|
/* This is why videos get corrupted */
|
|
|
|
|
srcStrm.Seek(destStrm.Length, SeekOrigin.Begin);
|
|
|
|
|
destStrm.Seek(destStrm.Length, SeekOrigin.Begin);
|
2021-09-25 22:30:16 +00:00
|
|
|
|
|
|
|
|
|
byte[] buffer = new byte[4096];
|
2021-12-12 10:04:16 +00:00
|
|
|
|
int read = 0;
|
|
|
|
|
do
|
2021-09-25 22:30:16 +00:00
|
|
|
|
{
|
2021-12-12 10:04:16 +00:00
|
|
|
|
read = await srcStrm.ReadAsync(buffer, 0, buffer.Length,token);
|
|
|
|
|
if (token.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
cancelSrc.Item.Dispose();
|
|
|
|
|
cancelSrc.Recreate();
|
|
|
|
|
if (RedownloadIt)
|
|
|
|
|
{
|
|
|
|
|
srcStrm.Dispose();
|
|
|
|
|
destStrm.Dispose();
|
|
|
|
|
lock (cancelSrc)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
token = cancelSrc.Item.Token;
|
|
|
|
|
}
|
|
|
|
|
await DownloadHDVideo(v, token);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
await destStrm.WriteAsync(buffer, 0, read,token);
|
|
|
|
|
if (token.IsCancellationRequested)
|
2021-09-25 22:30:16 +00:00
|
|
|
|
{
|
2021-12-12 10:04:16 +00:00
|
|
|
|
cancelSrc.Item.Dispose();
|
|
|
|
|
cancelSrc.Recreate();
|
|
|
|
|
if (RedownloadIt)
|
2021-09-25 22:30:16 +00:00
|
|
|
|
{
|
2021-12-12 10:04:16 +00:00
|
|
|
|
srcStrm.Dispose();
|
|
|
|
|
destStrm.Dispose();
|
|
|
|
|
lock (cancelSrc)
|
2021-09-25 22:30:16 +00:00
|
|
|
|
{
|
2021-12-12 10:04:16 +00:00
|
|
|
|
|
|
|
|
|
token = cancelSrc.Item.Token;
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
2021-12-12 10:04:16 +00:00
|
|
|
|
await DownloadHDVideo(v, token);
|
|
|
|
|
}
|
|
|
|
|
return;
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
2021-12-12 10:04:16 +00:00
|
|
|
|
pos += read;
|
|
|
|
|
double myP = (double)pos / (double)len;
|
|
|
|
|
myProgress.Report(myP);
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
2021-12-12 10:04:16 +00:00
|
|
|
|
while (read > 0);
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
2021-06-24 01:10:20 +00:00
|
|
|
|
|
2021-12-12 10:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
File.Move(mypathaudio, mypathCompleteAudio);
|
2021-06-27 17:22:18 +00:00
|
|
|
|
|
|
|
|
|
|
2021-12-12 10:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
IProgress<double> pa = p.Video;
|
|
|
|
|
|
|
|
|
|
pa.Report(1);
|
|
|
|
|
ffmpeg.mux(mypath, mypathCompleteAudio, mypathIncompleteConverting);
|
2021-06-27 17:22:18 +00:00
|
|
|
|
|
2021-12-08 22:02:30 +00:00
|
|
|
|
|
2021-12-12 10:04:16 +00:00
|
|
|
|
File.Move(mypathIncompleteConverting, mypathComplete);
|
|
|
|
|
}
|
2021-06-27 17:22:18 +00:00
|
|
|
|
|
2021-12-12 10:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
private async Task DownloadSDVideo(SavedVideoObject v, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
string mypath2 = GetPath(true, "NotConverted", v.Video.Id + "incomplete.mp4");
|
|
|
|
|
string mypath2Complete = GetPath(true, "NotConverted", v.Video.Id + ".mp4");
|
2021-12-08 22:02:30 +00:00
|
|
|
|
|
2021-12-12 10:04:16 +00:00
|
|
|
|
if (Continue(mypath2Complete))
|
|
|
|
|
{
|
|
|
|
|
var s = await GetManifest(ytc, v.Video.Id);
|
|
|
|
|
var best = s.GetMuxedStreams().GetWithHighestVideoQuality();
|
|
|
|
|
P.Length = best.Size.Bytes;
|
|
|
|
|
long pos = 0;
|
|
|
|
|
long len = 0;
|
|
|
|
|
using (var destStrm = File.Open(mypath2, FileMode.OpenOrCreate, FileAccess.ReadWrite))
|
|
|
|
|
{
|
|
|
|
|
using (var srcStrm = await ytc.Videos.Streams.GetAsync(best, token))
|
|
|
|
|
{
|
|
|
|
|
if (token.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
cancelSrc.Item.Dispose();
|
|
|
|
|
cancelSrc.Recreate();
|
|
|
|
|
if (RedownloadIt)
|
|
|
|
|
{
|
|
|
|
|
srcStrm.Dispose();
|
|
|
|
|
destStrm.Dispose();
|
|
|
|
|
lock (cancelSrc)
|
|
|
|
|
{
|
2021-09-25 22:30:16 +00:00
|
|
|
|
|
2021-12-12 10:04:16 +00:00
|
|
|
|
token = cancelSrc.Item.Token;
|
|
|
|
|
}
|
|
|
|
|
await DownloadSDVideo(v, token);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
len = srcStrm.Length;
|
|
|
|
|
pos = destStrm.Length;
|
|
|
|
|
IProgress<double> myProgress = DownloadP;
|
|
|
|
|
if (pos >= len)
|
|
|
|
|
{
|
2021-09-25 22:30:16 +00:00
|
|
|
|
|
2021-12-12 10:04:16 +00:00
|
|
|
|
myProgress.Report(1);
|
2021-12-08 01:07:43 +00:00
|
|
|
|
|
2021-12-12 10:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
/* This is why videos get corrupted */
|
|
|
|
|
srcStrm.Seek(destStrm.Length, SeekOrigin.Begin);
|
|
|
|
|
destStrm.Seek(destStrm.Length, SeekOrigin.Begin);
|
|
|
|
|
byte[] buffer = new byte[4096];
|
|
|
|
|
int read = 0;
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
read = await srcStrm.ReadAsync(buffer, 0, buffer.Length, token);
|
|
|
|
|
if (token.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
cancelSrc.Item.Dispose();
|
|
|
|
|
cancelSrc.Recreate();
|
|
|
|
|
if (RedownloadIt)
|
|
|
|
|
{
|
|
|
|
|
srcStrm.Dispose();
|
|
|
|
|
destStrm.Dispose();
|
|
|
|
|
lock (cancelSrc)
|
|
|
|
|
{
|
2021-06-27 17:22:18 +00:00
|
|
|
|
|
2021-12-12 10:04:16 +00:00
|
|
|
|
token = cancelSrc.Item.Token;
|
|
|
|
|
}
|
|
|
|
|
await DownloadSDVideo(v, token);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
await destStrm.WriteAsync(buffer, 0, read, token);
|
|
|
|
|
if (token.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
cancelSrc.Item.Dispose();
|
|
|
|
|
cancelSrc.Recreate();
|
|
|
|
|
if (RedownloadIt)
|
|
|
|
|
{
|
|
|
|
|
srcStrm.Dispose();
|
|
|
|
|
destStrm.Dispose();
|
|
|
|
|
lock (cancelSrc)
|
|
|
|
|
{
|
2021-09-25 22:30:16 +00:00
|
|
|
|
|
2021-12-12 10:04:16 +00:00
|
|
|
|
token = cancelSrc.Item.Token;
|
2021-06-27 17:22:18 +00:00
|
|
|
|
}
|
2021-12-12 10:04:16 +00:00
|
|
|
|
await DownloadSDVideo(v, token);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-27 17:22:18 +00:00
|
|
|
|
|
2021-12-12 10:04:16 +00:00
|
|
|
|
pos += read;
|
|
|
|
|
double myP = (double)pos / (double)len;
|
|
|
|
|
myProgress.Report(myP);
|
|
|
|
|
}
|
|
|
|
|
while (read > 0);
|
|
|
|
|
}
|
2021-06-27 17:22:18 +00:00
|
|
|
|
|
2021-12-12 10:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
File.Move(mypath2, mypath2Complete);
|
2021-12-08 01:07:43 +00:00
|
|
|
|
|
|
|
|
|
|
2021-06-27 17:22:18 +00:00
|
|
|
|
|
2021-12-12 10:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private async Task DownloadAudio(SavedVideoObject v, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
string mypath3 = GetPath(true, "Audio", v.Video.Id + "incomplete.mp4");
|
|
|
|
|
string mypath3Complete = GetPath(true, "Audio", v.Video.Id + ".mp4");
|
|
|
|
|
if (Continue(mypath3Complete))
|
|
|
|
|
{
|
|
|
|
|
var s2 = await GetManifest(ytc, v.Video.Id);
|
|
|
|
|
var best2 = s2.GetAudioOnlyStreams().GetWithHighestBitrate();
|
|
|
|
|
P.Length = best2.Size.Bytes;
|
|
|
|
|
long pos = 0;
|
|
|
|
|
long len = 0;
|
|
|
|
|
using (var destStrm = File.Open(mypath3, FileMode.OpenOrCreate, FileAccess.ReadWrite))
|
|
|
|
|
{
|
|
|
|
|
using (var srcStrm = await ytc.Videos.Streams.GetAsync(best2,token))
|
|
|
|
|
{
|
|
|
|
|
if (token.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
cancelSrc.Item.Dispose();
|
|
|
|
|
cancelSrc.Recreate();
|
|
|
|
|
if (RedownloadIt)
|
|
|
|
|
{
|
|
|
|
|
srcStrm.Dispose();
|
|
|
|
|
destStrm.Dispose();
|
|
|
|
|
lock (cancelSrc)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
token = cancelSrc.Item.Token;
|
|
|
|
|
}
|
|
|
|
|
await DownloadAudio(v, token);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
len = srcStrm.Length;
|
|
|
|
|
|
|
|
|
|
pos = destStrm.Length;
|
|
|
|
|
IProgress<double> myProgress = DownloadP;
|
|
|
|
|
if (pos >= len)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
myProgress.Report(1);
|
2021-06-27 17:22:18 +00:00
|
|
|
|
|
2021-12-12 10:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
/* This is why videos get corrupted */
|
|
|
|
|
srcStrm.Seek(destStrm.Length, SeekOrigin.Begin);
|
|
|
|
|
destStrm.Seek(destStrm.Length, SeekOrigin.Begin);
|
|
|
|
|
byte[] buffer = new byte[4096];
|
|
|
|
|
int read = 0;
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
read = await srcStrm.ReadAsync(buffer, 0, buffer.Length,token);
|
|
|
|
|
if (token.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
cancelSrc.Item.Dispose();
|
|
|
|
|
cancelSrc.Recreate();
|
|
|
|
|
if (RedownloadIt)
|
|
|
|
|
{
|
|
|
|
|
srcStrm.Dispose();
|
|
|
|
|
destStrm.Dispose();
|
|
|
|
|
lock (cancelSrc)
|
2021-06-27 17:22:18 +00:00
|
|
|
|
{
|
|
|
|
|
|
2021-12-12 10:04:16 +00:00
|
|
|
|
token = cancelSrc.Item.Token;
|
2021-06-27 17:22:18 +00:00
|
|
|
|
}
|
2021-12-12 10:04:16 +00:00
|
|
|
|
await DownloadAudio(v, token);
|
|
|
|
|
}
|
|
|
|
|
return;
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
2021-12-12 10:04:16 +00:00
|
|
|
|
await destStrm.WriteAsync(buffer, 0, read,token);
|
|
|
|
|
if (token.IsCancellationRequested)
|
2021-12-08 01:07:43 +00:00
|
|
|
|
{
|
2021-12-12 10:04:16 +00:00
|
|
|
|
cancelSrc.Item.Dispose();
|
|
|
|
|
cancelSrc.Recreate();
|
|
|
|
|
if (RedownloadIt)
|
|
|
|
|
{
|
|
|
|
|
srcStrm.Dispose();
|
|
|
|
|
destStrm.Dispose();
|
|
|
|
|
lock (cancelSrc)
|
|
|
|
|
{
|
2021-12-08 01:07:43 +00:00
|
|
|
|
|
2021-12-12 10:04:16 +00:00
|
|
|
|
token = cancelSrc.Item.Token;
|
|
|
|
|
}
|
|
|
|
|
await DownloadAudio(v, token);
|
|
|
|
|
}
|
|
|
|
|
return;
|
2021-12-08 01:07:43 +00:00
|
|
|
|
}
|
2021-12-12 10:04:16 +00:00
|
|
|
|
pos += read;
|
|
|
|
|
double myP = (double)pos / (double)len;
|
|
|
|
|
myProgress.Report(myP);
|
|
|
|
|
}
|
|
|
|
|
while (read > 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2021-12-08 01:07:43 +00:00
|
|
|
|
|
2021-12-12 10:04:16 +00:00
|
|
|
|
|
|
|
|
|
File.Move(mypath3, mypath3Complete);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private async Task DownloadFileAsync(SavedVideoObject v,CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var req = await Http.GetAsync(v.Video.Id, token);
|
|
|
|
|
if (token.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
cancelSrc.Item.Dispose();
|
|
|
|
|
cancelSrc.Recreate();
|
|
|
|
|
if (RedownloadIt)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
lock (cancelSrc)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
token = cancelSrc.Item.Token;
|
|
|
|
|
}
|
|
|
|
|
await DownloadFileAsync(v, token);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
long? Len = req.Content.Headers.ContentLength;
|
|
|
|
|
Uri u = new Uri(v.Video.Id);
|
|
|
|
|
string abs = u.AbsolutePath;
|
|
|
|
|
string name = "file.bin";
|
|
|
|
|
if (abs.Contains("/"))
|
|
|
|
|
{
|
|
|
|
|
name = abs.Substring(abs.LastIndexOf('/') + 1);
|
|
|
|
|
}
|
|
|
|
|
if (req.Content.Headers.Contains("Content-Disposition"))
|
|
|
|
|
{
|
|
|
|
|
name = req.Content.Headers.ContentDisposition.FileName;
|
|
|
|
|
}
|
|
|
|
|
int fileI = 1;
|
|
|
|
|
P.Saved.Title = name;
|
|
|
|
|
name = GetPath(true, "Download", name);
|
|
|
|
|
string filename = name;
|
|
|
|
|
while (FileExists(name, ref filename, ref fileI)) { }
|
|
|
|
|
long Len2 = long.MaxValue;
|
|
|
|
|
if (Len.HasValue)
|
|
|
|
|
{
|
|
|
|
|
if (Len.Value > 0)
|
|
|
|
|
{
|
|
|
|
|
Len2 = Len.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
P.Length = Len2;
|
|
|
|
|
long Pos = 0;
|
|
|
|
|
byte[] buffer = new byte[4096];
|
|
|
|
|
int Cycles = 0;
|
|
|
|
|
IProgress<double> p = DownloadP;
|
|
|
|
|
int CYCLES_BETWEEN_REPORT = 1;
|
|
|
|
|
using (var srcFile = await req.Content.ReadAsStreamAsync())
|
|
|
|
|
{
|
|
|
|
|
using (var destFile = File.Create(filename))
|
|
|
|
|
{
|
|
|
|
|
int read;
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
read = await srcFile.ReadAsync(buffer, 0, buffer.Length, token);
|
|
|
|
|
if (token.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
cancelSrc.Item.Dispose();
|
|
|
|
|
cancelSrc.Recreate();
|
|
|
|
|
if (RedownloadIt)
|
|
|
|
|
{
|
|
|
|
|
srcFile.Dispose();
|
|
|
|
|
destFile.Dispose();
|
|
|
|
|
lock (cancelSrc)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
token = cancelSrc.Item.Token;
|
|
|
|
|
}
|
|
|
|
|
await DownloadFileAsync(v, token);
|
|
|
|
|
}
|
|
|
|
|
return;
|
2021-06-27 17:22:18 +00:00
|
|
|
|
}
|
2021-12-12 10:04:16 +00:00
|
|
|
|
await destFile.WriteAsync(buffer, 0, read, token);
|
|
|
|
|
if (token.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
cancelSrc.Item.Dispose();
|
|
|
|
|
cancelSrc.Recreate();
|
|
|
|
|
if (RedownloadIt)
|
2021-12-08 01:07:43 +00:00
|
|
|
|
{
|
2021-12-12 10:04:16 +00:00
|
|
|
|
srcFile.Dispose();
|
|
|
|
|
destFile.Dispose();
|
|
|
|
|
lock (cancelSrc)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
token = cancelSrc.Item.Token;
|
|
|
|
|
}
|
|
|
|
|
await DownloadFileAsync(v, token);
|
2021-12-08 01:07:43 +00:00
|
|
|
|
}
|
2021-12-12 10:04:16 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Pos += read;
|
|
|
|
|
Cycles++;
|
|
|
|
|
if (Cycles > CYCLES_BETWEEN_REPORT)
|
|
|
|
|
{
|
|
|
|
|
Cycles = 0;
|
|
|
|
|
p.Report(Pos / Len2);
|
|
|
|
|
}
|
|
|
|
|
} while (read > 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
p.Report(1);
|
|
|
|
|
}
|
|
|
|
|
private async Task DownloadItem(CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
P.Progress = 0;
|
|
|
|
|
P.ProgressRaw = 0;
|
|
|
|
|
await Task.Delay(2000);
|
|
|
|
|
bool canDownload = false;
|
|
|
|
|
SavedVideoObject v;
|
|
|
|
|
lock (Queue)
|
|
|
|
|
{
|
|
|
|
|
canDownload = Queue.Count > 0;
|
|
|
|
|
if (canDownload)
|
|
|
|
|
{
|
|
|
|
|
v = Queue[0];
|
|
|
|
|
Queue.RemoveAt(0);
|
|
|
|
|
P.Saved = v.Video;
|
|
|
|
|
if (v.RegularFile)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Download: {v.Video.Id}");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"Download: {v.Video.Title}");
|
|
|
|
|
}
|
2021-12-08 01:07:43 +00:00
|
|
|
|
|
2021-12-12 10:04:16 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
v = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (canDownload)
|
|
|
|
|
{
|
|
|
|
|
DownloadStartEventArgs evt = new DownloadStartEventArgs();
|
|
|
|
|
evt.Cancel = false;
|
|
|
|
|
evt.RegularFile = v.RegularFile;
|
|
|
|
|
evt.Video = v.Video;
|
|
|
|
|
|
|
|
|
|
ApiLoader.DownloadStarted(this, evt);
|
|
|
|
|
if (evt.Cancel)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (v.RegularFile)
|
2021-06-27 17:22:18 +00:00
|
|
|
|
{
|
2021-12-12 10:04:16 +00:00
|
|
|
|
await DownloadFileAsync(v, token);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
switch (v.Resolution)
|
|
|
|
|
{
|
|
|
|
|
case Resolution.Convert:
|
|
|
|
|
await DownloadHDVideo(v, token);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case Resolution.NoConvert:
|
|
|
|
|
|
|
|
|
|
await DownloadSDVideo(v, token);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case Resolution.Audio:
|
|
|
|
|
await DownloadAudio(v, token);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
DownloadCompleteEventArgs evt2 = new DownloadCompleteEventArgs();
|
|
|
|
|
|
|
|
|
|
evt2.RegularFile = v.RegularFile;
|
|
|
|
|
evt2.Video = v.Video;
|
|
|
|
|
|
|
|
|
|
ApiLoader.DownloadComplete(this, evt2);
|
|
|
|
|
ffmpeg.on_video_done(v.Video.Id, (int)v.Resolution);
|
|
|
|
|
|
|
|
|
|
|
2021-06-24 01:10:20 +00:00
|
|
|
|
}
|
2021-12-12 10:04:16 +00:00
|
|
|
|
|
|
|
|
|
|
2021-06-24 01:10:20 +00:00
|
|
|
|
}
|
2021-12-12 10:04:16 +00:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public async Task DownloadThread()
|
|
|
|
|
{
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
await DownloadItem(cancelSrc.Item.Token);
|
2021-12-08 01:07:43 +00:00
|
|
|
|
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
2021-06-24 01:10:20 +00:00
|
|
|
|
while (true);
|
|
|
|
|
}
|
2021-09-25 22:30:16 +00:00
|
|
|
|
internal void _DownloadThumbnail(int w, int h, string id, string tnail)
|
2021-06-24 01:10:20 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-09-25 22:30:16 +00:00
|
|
|
|
string p = GetPath(true, "Thumbnails", w.ToString() + 'x' + h.ToString(), id + ".jpg");
|
2021-06-24 01:10:20 +00:00
|
|
|
|
if (!File.Exists(p))
|
|
|
|
|
{
|
|
|
|
|
ffmpeg.download_thumbnail(tnail, p);
|
2021-09-25 22:30:16 +00:00
|
|
|
|
}
|
2021-06-24 01:10:20 +00:00
|
|
|
|
}
|
2021-09-25 22:30:16 +00:00
|
|
|
|
catch (Exception ex)
|
2021-06-24 01:10:20 +00:00
|
|
|
|
{
|
2021-06-26 03:22:31 +00:00
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
|
2021-06-24 01:10:20 +00:00
|
|
|
|
_ = ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-03 19:39:03 +00:00
|
|
|
|
internal void _DownloadThumbnail2(int w, int h, string id, string tnail)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string p = GetPath(true, "Thumbnails", w.ToString() + 'x' + h.ToString(), id + ".jpg");
|
|
|
|
|
if (!File.Exists(p))
|
|
|
|
|
{
|
|
|
|
|
using (var f = File.Create(p))
|
|
|
|
|
{
|
|
|
|
|
Http.GetStreamAsync(tnail);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
|
|
|
|
|
_ = ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-25 22:30:16 +00:00
|
|
|
|
public static async Task<List<SavedMedia>> Search(string text, bool downloadThumbs = true)
|
2021-06-27 17:22:18 +00:00
|
|
|
|
{
|
|
|
|
|
List<SavedMedia> media = new List<SavedMedia>();
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-09-25 22:30:16 +00:00
|
|
|
|
|
2021-08-25 09:11:08 +00:00
|
|
|
|
await DL.ytc.Search.GetVideosAsync(text).ForEachAsync((e) =>
|
|
|
|
|
{
|
|
|
|
|
if (downloadThumbs)
|
2021-06-28 07:38:35 +00:00
|
|
|
|
{
|
2021-08-25 09:11:08 +00:00
|
|
|
|
foreach (var t in e.Thumbnails)
|
|
|
|
|
{
|
|
|
|
|
DL._DownloadThumbnail2(t.Resolution.Width, t.Resolution.Height, e.Id, t.Url);
|
|
|
|
|
}
|
2021-06-28 07:38:35 +00:00
|
|
|
|
}
|
2021-08-25 09:11:08 +00:00
|
|
|
|
media.Add(new SavedMedia() { Title = e.Title, Id = e.Id, Kind = InfoType.Video });
|
2021-09-25 22:30:16 +00:00
|
|
|
|
|
2021-06-28 07:38:35 +00:00
|
|
|
|
});
|
|
|
|
|
await DL.ytc.Search.GetPlaylistsAsync(text).ForEachAsync((e) => {
|
2021-08-25 09:11:08 +00:00
|
|
|
|
if (downloadThumbs)
|
2021-06-28 07:38:35 +00:00
|
|
|
|
{
|
2021-08-25 09:11:08 +00:00
|
|
|
|
foreach (var t in e.Thumbnails)
|
|
|
|
|
{
|
|
|
|
|
DL._DownloadThumbnail2(t.Resolution.Width, t.Resolution.Height, e.Id, t.Url);
|
|
|
|
|
}
|
2021-06-28 07:38:35 +00:00
|
|
|
|
}
|
2021-09-25 22:30:16 +00:00
|
|
|
|
media.Add(new SavedMedia() { Title = e.Title, Id = e.Id, Kind = InfoType.Playlist });
|
|
|
|
|
|
2021-06-28 07:38:35 +00:00
|
|
|
|
});
|
|
|
|
|
await DL.ytc.Search.GetChannelsAsync(text).ForEachAsync((e) => {
|
2021-08-25 09:11:08 +00:00
|
|
|
|
if (downloadThumbs)
|
2021-06-28 07:38:35 +00:00
|
|
|
|
{
|
2021-08-25 09:11:08 +00:00
|
|
|
|
foreach (var t in e.Thumbnails)
|
|
|
|
|
{
|
|
|
|
|
DL._DownloadThumbnail2(t.Resolution.Width, t.Resolution.Height, e.Id, t.Url);
|
|
|
|
|
}
|
2021-06-28 07:38:35 +00:00
|
|
|
|
}
|
2021-09-25 22:30:16 +00:00
|
|
|
|
media.Add(new SavedMedia() { Title = e.Title, Id = e.Id, Kind = InfoType.Channel });
|
|
|
|
|
|
2021-06-28 07:38:35 +00:00
|
|
|
|
});
|
2021-09-25 22:30:16 +00:00
|
|
|
|
} catch (Exception ex)
|
2021-06-27 17:22:18 +00:00
|
|
|
|
{
|
|
|
|
|
_ = ex;
|
|
|
|
|
}
|
|
|
|
|
return media;
|
|
|
|
|
}
|
2021-12-08 01:07:43 +00:00
|
|
|
|
public static List<SavedVideoObject> GetQueueItems()
|
2021-06-24 01:10:20 +00:00
|
|
|
|
{
|
|
|
|
|
return DL.Queue;
|
|
|
|
|
}
|
2021-09-25 22:30:16 +00:00
|
|
|
|
public static void DownloadFile(string id)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
InfomationQueueItem item = new InfomationQueueItem(new Uri(id));
|
|
|
|
|
lock (DL.infoQueue)
|
|
|
|
|
{
|
|
|
|
|
DL.infoQueue.Insert(0, item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2021-06-24 01:10:20 +00:00
|
|
|
|
|
2021-09-25 22:30:16 +00:00
|
|
|
|
public static void DownloadVideo(string id, Resolution res)
|
2021-06-24 01:10:20 +00:00
|
|
|
|
{
|
2021-06-24 04:05:21 +00:00
|
|
|
|
VideoId? v = VideoId.TryParse(id);
|
|
|
|
|
if (v.HasValue)
|
|
|
|
|
{
|
2021-06-25 08:30:45 +00:00
|
|
|
|
InfomationQueueItem item = new InfomationQueueItem(v.Value, res, true);
|
|
|
|
|
lock (DL.infoQueue)
|
|
|
|
|
{
|
|
|
|
|
DL.infoQueue.Insert(0, item);
|
|
|
|
|
}
|
2021-06-24 04:05:21 +00:00
|
|
|
|
}
|
2021-06-24 01:10:20 +00:00
|
|
|
|
}
|
2021-08-25 09:11:08 +00:00
|
|
|
|
public static void DownloadVideoInfo(string id, Resolution res)
|
|
|
|
|
{
|
|
|
|
|
VideoId? v = VideoId.TryParse(id);
|
|
|
|
|
if (v.HasValue)
|
|
|
|
|
{
|
|
|
|
|
InfomationQueueItem item = new InfomationQueueItem(v.Value, res, false);
|
|
|
|
|
lock (DL.infoQueue)
|
|
|
|
|
{
|
|
|
|
|
DL.infoQueue.Insert(0, item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-24 04:05:21 +00:00
|
|
|
|
public static void DownloadVideo(string v)
|
2021-06-24 01:10:20 +00:00
|
|
|
|
{
|
|
|
|
|
DownloadVideo(v, Resolution.NoConvert);
|
|
|
|
|
}
|
2021-09-25 22:30:16 +00:00
|
|
|
|
public static void DownloadCaptions(string id)
|
|
|
|
|
{
|
|
|
|
|
VideoId? v = VideoId.TryParse(id);
|
|
|
|
|
if(v.HasValue)
|
|
|
|
|
{
|
|
|
|
|
InfomationQueueItem item = new InfomationQueueItem(v.Value);
|
|
|
|
|
lock (DL.infoQueue)
|
|
|
|
|
{
|
|
|
|
|
DL.infoQueue.Insert(0, item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-24 04:05:21 +00:00
|
|
|
|
public static void DownloadPlaylist(string id,Resolution res)
|
2021-06-24 01:10:20 +00:00
|
|
|
|
{
|
2021-06-24 04:05:21 +00:00
|
|
|
|
PlaylistId? v = PlaylistId.TryParse(id);
|
|
|
|
|
if (v.HasValue)
|
|
|
|
|
{
|
2021-06-25 08:30:45 +00:00
|
|
|
|
InfomationQueueItem item = new InfomationQueueItem(v.Value, res, true);
|
|
|
|
|
lock (DL.infoQueue)
|
|
|
|
|
{
|
|
|
|
|
DL.infoQueue.Insert(0, item);
|
|
|
|
|
}
|
2021-06-24 04:05:21 +00:00
|
|
|
|
}
|
2021-06-24 01:10:20 +00:00
|
|
|
|
}
|
2021-08-25 09:11:08 +00:00
|
|
|
|
public static void DownloadPlaylistOnly(string id, Resolution res)
|
|
|
|
|
{
|
|
|
|
|
PlaylistId? v = PlaylistId.TryParse(id);
|
|
|
|
|
if (v.HasValue)
|
|
|
|
|
{
|
|
|
|
|
InfomationQueueItem item = new InfomationQueueItem(v.Value, res, false);
|
|
|
|
|
lock (DL.infoQueue)
|
|
|
|
|
{
|
|
|
|
|
DL.infoQueue.Insert(0, item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-24 04:05:21 +00:00
|
|
|
|
public static void DownloadPlaylist(string id)
|
2021-06-24 01:10:20 +00:00
|
|
|
|
{
|
|
|
|
|
DownloadPlaylist(id, Resolution.NoConvert);
|
|
|
|
|
}
|
2021-06-24 04:05:21 +00:00
|
|
|
|
public static void DownloadChannel(string id,Resolution res)
|
2021-06-24 01:10:20 +00:00
|
|
|
|
{
|
2021-06-24 04:05:21 +00:00
|
|
|
|
ChannelId? v = ChannelId.TryParse(id);
|
|
|
|
|
if (v.HasValue)
|
|
|
|
|
{
|
2021-06-25 08:30:45 +00:00
|
|
|
|
InfomationQueueItem item = new InfomationQueueItem(v.Value, res, true);
|
|
|
|
|
lock (DL.infoQueue)
|
|
|
|
|
{
|
|
|
|
|
DL.infoQueue.Insert(0, item);
|
|
|
|
|
}
|
2021-06-24 04:05:21 +00:00
|
|
|
|
}
|
2021-06-24 01:10:20 +00:00
|
|
|
|
}
|
2021-06-24 04:05:21 +00:00
|
|
|
|
public static void DownloadChannel(string id)
|
2021-06-24 01:10:20 +00:00
|
|
|
|
{
|
|
|
|
|
DownloadChannel(id, Resolution.NoConvert);
|
|
|
|
|
}
|
2021-11-14 12:57:02 +00:00
|
|
|
|
public static void DownloadItem(string id)
|
|
|
|
|
{
|
|
|
|
|
DownloadItem(id, Resolution.NoConvert);
|
|
|
|
|
}
|
|
|
|
|
public static void DownloadItem(string id,Resolution res)
|
|
|
|
|
{
|
|
|
|
|
VideoId? vid = VideoId.TryParse(id);
|
|
|
|
|
PlaylistId? pid = PlaylistId.TryParse(id);
|
|
|
|
|
ChannelId? cid = ChannelId.TryParse(id);
|
|
|
|
|
UserName? user = UserName.TryParse(id);
|
|
|
|
|
|
|
|
|
|
if (id.Length == 11)
|
|
|
|
|
{
|
|
|
|
|
if (vid.HasValue)
|
|
|
|
|
{
|
|
|
|
|
DownloadVideo(vid.Value, res); //shall we download video
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (pid.HasValue)
|
|
|
|
|
{
|
|
|
|
|
DownloadPlaylist(pid.Value, res);
|
|
|
|
|
}
|
|
|
|
|
else if (vid.HasValue)
|
|
|
|
|
{
|
|
|
|
|
DownloadVideo(vid.Value, res);
|
|
|
|
|
}
|
|
|
|
|
else if (cid.HasValue)
|
|
|
|
|
{
|
|
|
|
|
DownloadChannel(cid.Value, res);
|
|
|
|
|
}
|
|
|
|
|
else if (user.HasValue)
|
|
|
|
|
{
|
|
|
|
|
DownloadUser(user.Value, res);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2021-06-24 04:05:21 +00:00
|
|
|
|
public static void DownloadUser(string name, Resolution res)
|
2021-06-24 01:10:20 +00:00
|
|
|
|
{
|
2021-06-24 04:05:21 +00:00
|
|
|
|
UserName? v=UserName.TryParse(name);
|
|
|
|
|
if (v.HasValue)
|
|
|
|
|
{
|
2021-06-25 08:30:45 +00:00
|
|
|
|
InfomationQueueItem item = new InfomationQueueItem(v.Value, res, true);
|
|
|
|
|
lock (DL.infoQueue)
|
|
|
|
|
{
|
|
|
|
|
DL.infoQueue.Insert(0, item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-25 10:31:14 +00:00
|
|
|
|
|
2021-06-24 04:05:21 +00:00
|
|
|
|
public static void DownloadUser(string name)
|
2021-06-24 01:10:20 +00:00
|
|
|
|
{
|
|
|
|
|
DownloadUser(name, Resolution.NoConvert);
|
|
|
|
|
}
|
|
|
|
|
public string GetPath(bool createParent,params string[] _path)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (createParent)
|
|
|
|
|
{
|
|
|
|
|
string dir = GetPath(_path);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(dir));
|
|
|
|
|
}catch(Exception ex)
|
|
|
|
|
{
|
2021-06-26 03:22:31 +00:00
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
|
2021-06-24 01:10:20 +00:00
|
|
|
|
_ = ex;
|
|
|
|
|
}
|
|
|
|
|
return dir;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return GetPath(_path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private string GetPath(params string[] _path)
|
|
|
|
|
{
|
|
|
|
|
string[] array2 = new string[_path.Length + 1];
|
|
|
|
|
array2[0] = StorageLocation;
|
|
|
|
|
Array.Copy(_path, 0, array2, 1,_path.Length);
|
2021-12-12 10:04:16 +00:00
|
|
|
|
return Path.Combine(array2);
|
2021-06-24 01:10:20 +00:00
|
|
|
|
}
|
|
|
|
|
public static Downloader DL = new Downloader();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|