Beispiel #1
0
        public static void DownloadVideo(string id)
        {
            if (!AppSettings.DownloadReady)
            {
                Logger.Info("Tools for downloading are not ready yet");

                _ = StatusBoard.PutStatus("downloadResult", id, "false");

                return;
            }

            string path = Directory.GetCurrentDirectory() + @"\youtube-dl.exe";

            int height = 2160, fps = 60;

            switch (AppSettings.PreferredQuality)
            {
            case AppSettings.DownloadQuality.H2160F60:
            {
                break;
            }

            case AppSettings.DownloadQuality.H2160F30:
            {
                fps = 30;
                break;
            }

            case AppSettings.DownloadQuality.H1440F60:
            {
                height = 1440;
                break;
            }

            case AppSettings.DownloadQuality.H1440F30:
            {
                height = 1440;
                fps    = 30;
                break;
            }

            case AppSettings.DownloadQuality.H1080F60:
            {
                height = 1080;
                fps    = 60;
                break;
            }

            case AppSettings.DownloadQuality.H1080F30:
            {
                height = 1080;
                fps    = 30;
                break;
            }

            case AppSettings.DownloadQuality.H720F60:
            {
                height = 720;
                fps    = 60;
                break;
            }

            case AppSettings.DownloadQuality.H720F30:
            {
                height = 720;
                fps    = 30;
                break;
            }

            default:
            {
                Logger.Warn("PreferredQuality is of unexpected value");
                AppSettings.PreferredQuality = AppSettings.DownloadQuality.H1080F60;
                height = 1080;
                fps    = 60;
                break;
            }
            }

            Process dl = new Process();

            dl.StartInfo.FileName = path;

            const char quote = '\u0022';

            dl.StartInfo.Arguments = $@"-f " + quote + $@"bestvideo[height<={height}][fps<={fps}][ext=webm]+bestaudio[ext=webm]/bestvideo[height<={height}][fps<={fps}][ext=mp4]+bestaudio[ext=m4a]/webm/mp4" + quote + $@" -o wwwroot/Videos/%(channel_id)s&%(uploader)s/%(id)s&%(title)s --restrict-filenames --write-thumbnail https://www.youtube.com/watch?v={id}";

            dl.StartInfo.UseShellExecute = false;

            dl.StartInfo.RedirectStandardOutput = true;

            dl.StartInfo.RedirectStandardInput = true;

            dl.StartInfo.CreateNoWindow = true;

            try
            {
                dl.Start();
            }
            catch (Exception m)
            {
                Logger.Info("Couldn't start Download");

                Logger.Info("Make sure youtube-dl.exe is found in the main dir");

                Logger.Error(m.Message);

                _ = StatusBoard.PutStatus("downloadResult", id, "false");
            }

            int highestPercent = 0;

            bool videoDone = false;

            long lastUpdate = DateTime.Now.Ticks - 2000000;

            while (!dl.StandardOutput.EndOfStream)
            {
                string text = dl.StandardOutput.ReadLine();

                if (!videoDone && (DateTime.Now.Ticks - lastUpdate >= 2000000) && text.Contains("download") && text.Contains("%"))
                {
                    try
                    {
                        lastUpdate = DateTime.Now.Ticks;

                        string progress = text.Substring(12, 2);

                        int progressNumber = Int32.Parse(progress);

                        if (progressNumber < highestPercent)
                        {
                            videoDone = true;

                            _ = StatusBoard.PutStatus("downloadProgress", id, "100");
                        }
                        else
                        {
                            highestPercent = progressNumber;

                            _ = StatusBoard.PutStatus("downloadProgress", id, progress);
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.Warn("Failed sending download progress message");

                        Logger.Error(e.Message);
                    }
                }
            }

            LocalCollection.CollectAllDownloadedVideos();

            _ = StatusBoard.PutStatus("downloadResult", id, "true");
        }
Beispiel #2
0
        public Channel AddChannel(string name)
        {
            LifeTime = AppSettings.NewChannelTimeFrame;

            var Request = service.Channels.List("snippet");

            if (name.Length == 24)
            {
                Request.Id = name;
            }
            else
            {
                Request.ForUsername = name;
            }

            var Response = Request.Execute();

            try
            {
                Channel NewChannel = new Channel()
                {
                    Id = Response.Items.First().Id,

                    Username = name,

                    Displayname = Response.Items.First().Snippet.Title,

                    ThumbnailUrl = Response.Items.First().Snippet.Thumbnails.Default__.Url
                };

                using (AppDbContext context = new AppDbContext())
                {
                    context.Channels.Add(NewChannel);

                    context.SaveChanges();

                    _ = StatusBoard.PutStatus("channelResult", name, "true");

                    List <string> list = RequestVideoIdsFromChannel(NewChannel.Id).GetAwaiter().GetResult();

                    if (list.Count == 0)
                    {
                        return(NewChannel);
                    }

                    List <string> requests = CreateRequestList(list);

                    Task[] waitTasks = new Task[requests.Count];

                    int index = 0;

                    foreach (string str in requests)
                    {
                        waitTasks[index] = RequestVideosFromIds(str);

                        index++;
                    }

                    Task.WaitAll(waitTasks);
                }

                return(NewChannel);
            }
            catch (Exception)
            {
                Logger.Info(name + " couldn't be added");

                _ = StatusBoard.PutStatus("channelResult", name, "false");

                return(null);
            }
        }