Ejemplo n.º 1
0
        public Process MakeDebugYoutubeProcessAndReturnIt(VideoData video)
        {
            string levelPath = VideoLoader.GetLevelPath(video.level);

            if (!Directory.Exists(levelPath))
            {
                Directory.CreateDirectory(levelPath);
            }

            string videoFileName = video.title;

            // Strip invalid characters
            foreach (var c in Path.GetInvalidFileNameChars())
            {
                videoFileName = videoFileName.Replace(c, '-');
            }

            videoFileName = videoFileName.Replace('\\', '-');
            videoFileName = videoFileName.Replace('/', '-');

            video.videoPath = videoFileName + ".mp4";


            Plugin.logger.Info("Name Created");
            // Download the video via youtube-dl
            var ytProcess = new Process
            {
                StartInfo =
                {
                    FileName  = Environment.CurrentDirectory + "/Youtube-dl/youtube-dl.exe",
                    Arguments = "https://www.youtube.com" + video.URL +
                                " -f \"" + VideoQualitySetting.Format(quality) + "\"" + // Formats
                                " --no-cache-dir" +                                     // Don't use temp storage
                                $" -o \"{EscapeStringForPythonStringFormatter(videoFileName)}.%(ext)s\"" +
                                " --no-playlist" +                                      // Don't download playlists, only the first video
                                " --no-part" +                                          // Don't store download in parts, write directly to file
                                (hasFFMPEG
                                    ? " --recode-video mp4"
                                    : ""
                                ) +                      //Try to recode the video if ffmpeg is installed to fix issue with improper encoding
                                " --no-mtime" +          //Video last modified will be when it was downloaded, not when it was uploaded to youtube
                                " --socket-timeout 10" + //Retry if no response in 10 seconds Note: Not if download takes more than 10 seconds but if the time between any 2 messages from the server is 10 seconds
                                " --no-continue"         //overwrite existing file and force re-download
                    ,
                    RedirectStandardOutput = false,
                    RedirectStandardError  = false,
                    UseShellExecute        = true,
                    CreateNoWindow         = true,
                    WorkingDirectory       = levelPath
                },
                EnableRaisingEvents = true,
                //I think these are added only after Process Started
                //PriorityClass = ProcessPriorityClass.RealTime,
                //PriorityBoostEnabled = true
            };

            YouTubeDownloader.externalProcesses.Add(ytProcess);

            return(ytProcess);
        }
Ejemplo n.º 2
0
        public Process MakeYoutubeProcessAndReturnIt(VideoData video)
        {
            string levelPath = VideoLoader.GetLevelPath(video.level);

            if (!Directory.Exists(levelPath))
            {
                Directory.CreateDirectory(levelPath);
            }

            string videoFileName = video.title;

            // Strip invalid characters
            foreach (var c in Path.GetInvalidFileNameChars())
            {
                videoFileName = videoFileName.Replace(c, '-');
            }
            videoFileName = videoFileName.Replace('\\', '-');
            videoFileName = videoFileName.Replace('/', '-');

            video.videoPath = videoFileName + ".mp4";

            // Download the video via youtube-dl
            var ytProcess = new Process
            {
                StartInfo =
                {
                    FileName  = Environment.CurrentDirectory + "/Youtube-dl/youtube-dl.exe",
                    Arguments = "https://www.youtube.com" + video.URL +
                                " -f \"" + VideoQualitySetting.Format(quality) + "\"" + // Formats
                                " --no-cache-dir" +                                     // Don't use temp storage
                                " -o \"" + levelPath + $"\\{videoFileName}.%(ext)s\"" +
                                " --no-playlist" +                                      // Don't download playlists, only the first video
                                " --no-part" +                                          // Don't store download in parts, write directly to file
                                (hasFFMPEG ? " --recode-video mp4" : ""),
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true,
                    UseShellExecute        = false,
                    CreateNoWindow         = true
                },
                EnableRaisingEvents = true
            };

            return(ytProcess);
        }
        private IEnumerator DownloadVideo()
        {
            downloading = true;

            if (!updated)
            {
                yield return(new WaitUntil(() => updated));
            }

            VideoDownload download = videoQueue.Peek();
            VideoData     video    = download.video;

            if (video.downloadState == DownloadState.Cancelled || download.downloadAttempts > MaxRetries)
            {
                // skip
                videoQueue.Dequeue();

                if (videoQueue.Count > 0)
                {
                    // Start next download
                    DownloadVideo();
                }
                else
                {
                    // queue empty
                    downloading = false;
                    yield break;
                }
            }

            StopCoroutine(Countdown(download));

            video.downloadState = DownloadState.Downloading;
            downloadProgress?.Invoke(video);
            download.Update();

            string levelPath = VideoLoader.GetLevelPath(video.level);

            if (!Directory.Exists(levelPath))
            {
                Directory.CreateDirectory(levelPath);
            }

            string videoFileName = video.title;

            // Strip invalid characters
            foreach (var c in Path.GetInvalidFileNameChars())
            {
                videoFileName = videoFileName.Replace(c, '-');
            }
            videoFileName = videoFileName.Replace('\\', '-');
            videoFileName = videoFileName.Replace('/', '-');

            video.videoPath = videoFileName + ".mp4";

            // Download the video via youtube-dl
            ydl = new Process();

            ydl.StartInfo.FileName  = Environment.CurrentDirectory + "\\Youtube-dl\\youtube-dl.exe";
            ydl.StartInfo.Arguments =
                "https://www.youtube.com" + video.URL +
                " -f \"" + VideoQualitySetting.Format(quality) + "\"" + // Formats
                " --no-cache-dir" +                                     // Don't use temp storage
                " -o \"" + levelPath + $"\\{videoFileName}.%(ext)s\"" +
                " --no-playlist" +                                      // Don't download playlists, only the first video
                " --no-part";                                           // Don't store download in parts, write directly to file

            ydl.StartInfo.RedirectStandardOutput = true;
            ydl.StartInfo.RedirectStandardError  = true;
            ydl.StartInfo.UseShellExecute        = false;
            ydl.StartInfo.CreateNoWindow         = true;
            ydl.EnableRaisingEvents = true;

            ydl.Start();

            // Hook up our output to console
            ydl.BeginOutputReadLine();
            ydl.BeginErrorReadLine();

            ydl.OutputDataReceived += (sender, e) => {
                if (e.Data != null)
                {
                    Regex rx    = new Regex(@"(\d*).\d%+");
                    Match match = rx.Match(e.Data);
                    if (match.Success)
                    {
                        video.downloadProgress = float.Parse(match.Value.Substring(0, match.Value.Length - 1)) / 100;
                        downloadProgress?.Invoke(video);
                        download.Update();
                    }
                }
            };

            ydl.ErrorDataReceived += (sender, e) => {
                if (e.Data.Length < 3)
                {
                    return;
                }
                //to do: check these errors are problems - redownload or skip file when an error occurs
                //video.downloadState = DownloadState.Cancelled;
                downloadProgress?.Invoke(video);
                download.Update();
            };

            ydl.Exited += (sender, e) => {
                StopCoroutine(Countdown(download));

                if (video.downloadState == DownloadState.Cancelled)
                {
                    VideoLoader.Instance.DeleteVideo(video);
                }
                else
                {
                    video.downloadState = DownloadState.Downloaded;
                    VideoLoader.SaveVideoToDisk(video);
                    StartCoroutine(VerifyDownload(video));
                }

                videoQueue.Dequeue();

                if (videoQueue.Count > 0)
                {
                    // Start next download
                    DownloadVideo();
                }
                else
                {
                    // queue empty
                    downloading = false;
                }
                ydl.Dispose();
            };
        }
        private void DownloadVideo()
        {
            VideoData video = videoQueue.Peek();

            if (video.downloadState == DownloadState.Cancelled)
            {
                // skip
                videoQueue.Dequeue();

                if (videoQueue.Count > 0)
                {
                    // Start next download
                    DownloadVideo();
                }
                else
                {
                    // queue empty
                    downloading = false;
                    return;
                }
            }

            downloading         = true;
            video.downloadState = DownloadState.Downloading;
            downloadProgress?.Invoke(video);
            string levelPath = VideoLoader.GetLevelPath(video.level);

            if (!Directory.Exists(levelPath))
            {
                Directory.CreateDirectory(levelPath);
            }
            // Download the video via youtube-dl
            ydl = new Process();

            string videoFileName = video.title;

            // Strip invalid characters
            foreach (var c in Path.GetInvalidFileNameChars())
            {
                videoFileName = videoFileName.Replace(c, '-');
            }
            video.videoPath = videoFileName + ".mp4";

            ydl.StartInfo.FileName  = Environment.CurrentDirectory + "/Youtube-dl/youtube-dl.exe";
            ydl.StartInfo.Arguments =
                "https://www.youtube.com" + video.URL +
                " -f \"" + VideoQualitySetting.Format(quality) + "\"" + // Formats
                " --no-cache-dir" +                                     // Don't use temp storage
                " -o \"" + levelPath + $"\\{videoFileName}.%(ext)s\"" +
                " --no-playlist" +                                      // Don't download playlists, only the first video
                " --no-part";                                           // Don't store download in parts, write directly to file


            ydl.StartInfo.RedirectStandardOutput = true;
            ydl.StartInfo.RedirectStandardError  = true;
            ydl.StartInfo.UseShellExecute        = false;
            ydl.StartInfo.CreateNoWindow         = true;
            ydl.EnableRaisingEvents = true;

            ydl.Start();

            // Hook up our output to console
            ydl.BeginOutputReadLine();
            ydl.BeginErrorReadLine();

            ydl.OutputDataReceived += (sender, e) => {
                if (e.Data != null)
                {
                    //[download]  81.8% of 40.55MiB at  4.80MiB/s ETA 00:01
                    //[download] Resuming download at byte 48809440
                    //
                    Regex rx    = new Regex(@"(\d+).\d%+");
                    Match match = rx.Match(e.Data);
                    if (match.Success)
                    {
                        video.downloadProgress = float.Parse(match.Value.Substring(0, match.Value.Length - 2)) / 100;
                        downloadProgress?.Invoke(video);
                    }
                    Console.WriteLine(e.Data);
                }
            };

            ydl.ErrorDataReceived += (sender, e) => {
                Console.WriteLine(e.Data);
                //to do: check these errors problems - redownload or skip file when an error occurs
            };

            ydl.Exited += (sender, e) => {
                // to do: check that the file was indeed downloaded correctly

                if (video.downloadState == DownloadState.Cancelled)
                {
                    VideoLoader.Instance.DeleteVideo(video);
                }
                else
                {
                    video.downloadState = DownloadState.Downloaded;
                    VideoLoader.SaveVideoToDisk(video);
                    StartCoroutine(VerifyDownload(video));
                }

                videoQueue.Dequeue();

                if (videoQueue.Count > 0)
                {
                    // Start next download
                    DownloadVideo();
                }
                else
                {
                    // queue empty
                    downloading = false;
                }
            };
        }