add search and video resume

This commit is contained in:
Demetria Lovato 2021-06-27 10:22:18 -07:00
parent f336b73b68
commit 6760089173
14 changed files with 225 additions and 58 deletions

Binary file not shown.

View File

@ -34,6 +34,13 @@ namespace youtube_downloader
{
rp.AsFile(rq, Path.Combine(webSitePath, "index.html"));
});
Route.Add("/api/Search/{text}", (rq, rp, args) =>
{
string search=System.Web.HttpUtility.UrlDecode(args["text"]);
string json = JsonConvert.SerializeObject(Server.Functions.Downloader.Search(search));
rp.AsText(json, "application/json");
});
Route.Add("/api/Storage/GetDirectories/{Path}", (rq, rp, args) =>
{
string path = Server.Functions.Downloader.DL.GetPath(true, args["Path"]);
@ -62,6 +69,7 @@ namespace youtube_downloader
rp.AsText("[]", "application/json");
}
});
Route.Add("/api/Storage/FileExists/{Path}", (rq, rp, args) =>
{
string path = Server.Functions.Downloader.DL.GetPath(true, args["Path"]);

View File

@ -283,70 +283,197 @@ namespace youtube_downloader.Server.Functions
if (canDownload)
{
switch (v.Resolution)
try
{
case Resolution.Convert:
switch (v.Resolution)
{
case Resolution.Convert:
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");
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))
{
var s3 = await ytc.Videos.Streams.GetManifestAsync(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);
await ytc.Videos.Streams.DownloadAsync(best,mypath, p.Video);
IProgress<double> pv = p.Video;
pv.Report(1);
if (Continue(mypathCompleteAudio))
if (Continue(mypathComplete))
{
await ytc.Videos.Streams.DownloadAsync(best2, mypathaudio,p.Audio);
File.Move(mypathaudio, mypathCompleteAudio);
var s3 = await ytc.Videos.Streams.GetManifestAsync(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 = System.IO.File.Open(mypath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
long pos = 0;
long len = 0;
using (var srcStrm = await ytc.Videos.Streams.GetAsync(best))
{
len = srcStrm.Length;
pos = destStrm.Length;
IProgress<double> myProgress = p.Video;
if (pos >= len)
{
myProgress.Report(1);
}
srcStrm.Seek(destStrm.Length, SeekOrigin.Begin);
byte[] buffer = new byte[4096];
int read = 0;
do
{
read = srcStrm.Read(buffer, 0, buffer.Length);
destStrm.Write(buffer, 0, read);
pos += read;
double myP = (double)pos / (double)len;
myProgress.Report(myP);
}
while (read > 0);
}
}
IProgress<double> pv = p.Video;
pv.Report(1);
if (Continue(mypathCompleteAudio))
{
long pos = 0;
long len = 0;
using (var destStrm = System.IO.File.Open(mypathaudio, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
using (var srcStrm = await ytc.Videos.Streams.GetAsync(best2))
{
len = srcStrm.Length;
pos = destStrm.Length;
IProgress<double> myProgress = p.Audio;
if (pos >= len)
{
myProgress.Report(1);
}
srcStrm.Seek(destStrm.Length, SeekOrigin.Begin);
byte[] buffer = new byte[4096];
int read = 0;
do
{
read = srcStrm.Read(buffer, 0, buffer.Length);
destStrm.Write(buffer, 0, read);
pos += read;
double myP = (double)pos / (double)len;
myProgress.Report(myP);
}
while (read > 0);
}
}
File.Move(mypathaudio, mypathCompleteAudio);
}
IProgress<double> pa = p.Video;
pa.Report(1);
ffmpeg.mux(mypath, mypathCompleteAudio, mypathIncompleteConverting);
File.Move(mypathIncompleteConverting, mypathComplete);
}
break;
case Resolution.NoConvert:
string mypath2 = GetPath(true, "NotConverted", v.Video.Id + "incomplete.mp4");
string mypath2Complete = GetPath(true, "NotConverted", v.Video.Id + ".mp4");
if (Continue(mypath2Complete))
{
var s = await ytc.Videos.Streams.GetManifestAsync(v.Video.Id);
var best = s.GetMuxedStreams().GetWithHighestVideoQuality();
P.Length = best.Size.Bytes;
long pos = 0;
long len = 0;
using (var destStrm = System.IO.File.Open(mypath2, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
using (var srcStrm = await ytc.Videos.Streams.GetAsync(best))
{
len = srcStrm.Length;
pos = destStrm.Length;
IProgress<double> myProgress = DownloadP;
if (pos >= len)
{
myProgress.Report(1);
}
srcStrm.Seek(destStrm.Length, SeekOrigin.Begin);
byte[] buffer = new byte[4096];
int read = 0;
do
{
read = srcStrm.Read(buffer, 0, buffer.Length);
destStrm.Write(buffer, 0, read);
pos += read;
double myP = (double)pos / (double)len;
myProgress.Report(myP);
}
while (read > 0);
}
}
File.Move(mypath2, mypath2Complete);
}
IProgress<double> pa = p.Video;
pa.Report(1);
ffmpeg.mux(mypath,mypathCompleteAudio,mypathIncompleteConverting);
File.Move(mypathIncompleteConverting, mypathComplete);
}
break;
case Resolution.NoConvert:
string mypath2 = GetPath(true, "NotConverted", v.Video.Id + "incomplete.mp4");
string mypath2Complete = GetPath(true, "NotConverted", v.Video.Id + ".mp4");
break;
case Resolution.Audio:
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 ytc.Videos.Streams.GetManifestAsync(v.Video.Id);
var best2 = s2.GetAudioOnlyStreams().GetWithHighestBitrate();
P.Length = best2.Size.Bytes;
long pos = 0;
long len = 0;
using (var destStrm = System.IO.File.Open(mypath3, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
using (var srcStrm = await ytc.Videos.Streams.GetAsync(best2))
{
len = srcStrm.Length;
pos = destStrm.Length;
IProgress<double> myProgress = DownloadP;
if (pos >= len)
{
if (Continue(mypath2Complete))
{
var s = await ytc.Videos.Streams.GetManifestAsync(v.Video.Id);
var best = s.GetMuxedStreams().GetWithHighestVideoQuality();
P.Length = best.Size.Bytes;
myProgress.Report(1);
await ytc.Videos.Streams.DownloadAsync(best, mypath2, DownloadP);
File.Move(mypath2, mypath2Complete);
}
}
srcStrm.Seek(destStrm.Length, SeekOrigin.Begin);
byte[] buffer = new byte[4096];
int read = 0;
do
{
read = srcStrm.Read(buffer, 0, buffer.Length);
destStrm.Write(buffer, 0, read);
break;
case Resolution.Audio:
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 ytc.Videos.Streams.GetManifestAsync(v.Video.Id);
var best2 = s2.GetAudioOnlyStreams().GetWithHighestBitrate();
P.Length = best2.Size.Bytes;
await ytc.Videos.Streams.DownloadAsync(best2, mypath3, DownloadP);
File.Move(mypath3, mypath3Complete);
}
break;
pos += read;
double myP = (double)pos / (double)len;
myProgress.Report(myP);
}
while (read > 0);
}
}
File.Move(mypath3, mypath3Complete);
}
break;
}
}catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
System.Threading.Thread.Sleep(1);
@ -370,7 +497,20 @@ namespace youtube_downloader.Server.Functions
_ = ex;
}
}
public static async Task<List<SavedMedia>> Search(string text)
{
List<SavedMedia> media = new List<SavedMedia>();
try
{
await DL.ytc.Search.GetVideosAsync(text).ForEachAsync((e) => { media.Add(new SavedMedia() { Title = e.Title, Id = e.Id, Kind = InfoType.Video }); });
await DL.ytc.Search.GetPlaylistsAsync(text).ForEachAsync((e) => { media.Add(new SavedMedia() { Title = e.Title, Id = e.Id, Kind = InfoType.Playlist }); });
await DL.ytc.Search.GetChannelsAsync(text).ForEachAsync((e) => { media.Add(new SavedMedia() { Title = e.Title, Id = e.Id, Kind = InfoType.Channel }); });
}catch (Exception ex)
{
_ = ex;
}
return media;
}
internal static List<SavedVideoObject> GetQueueItems()
{
return DL.Queue;

View File

@ -0,0 +1,15 @@
using TessesYoutubeDownloader.Server.Models;
using youtube_downloader.Server.Models;
namespace youtube_downloader.Server.Functions
{
public class SavedMedia
{
public InfoType Kind { get; set; }
public string Id { get; set; }
public string Title { get; set; }
}
}

View File

@ -3,10 +3,11 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using youtube_downloader.Server.Functions;
namespace TessesYoutubeDownloader.Server.Models
{
class SavedChannel
public class SavedChannel
{
public static SavedChannel FromChannel(YoutubeExplode.Channels.Channel c, Action<int, int, string,string> downloadThumbnail)
{

View File

@ -3,10 +3,11 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using youtube_downloader.Server.Functions;
namespace TessesYoutubeDownloader.Server.Models
{
class SavedPlaylist
public class SavedPlaylist
{
public static async Task<SavedPlaylist> FromPlaylistId(Resolution res,YoutubeExplode.YoutubeClient ytc,YoutubeExplode.Playlists.PlaylistId id,Action<string,Resolution> addToQueue, Action<int, int, string,string> downloadThumbnail)
{

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using youtube_downloader.Server.Functions;
namespace TessesYoutubeDownloader.Server.Models
{

Binary file not shown.

Binary file not shown.

View File

@ -1 +1 @@
d24c4380a60b7d95eb6c0794042e9b1bb52b78fb
05a58042d13f08ad611e38c9c233d47d4e6ac009

Binary file not shown.

Binary file not shown.

View File

@ -133,6 +133,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Server\Functions\Downloader.cs" />
<Compile Include="Server\Functions\ffmpeg.cs" />
<Compile Include="Server\Functions\SavedMedia.cs" />
<Compile Include="Server\Models\InfomationQueueItem.cs" />
<Compile Include="Server\Models\InfoType.cs" />
<Compile Include="Server\Models\SavedChannel.cs" />