Exemple #1
0
        /// <summary>
        /// Returns a <see cref="VideoInfo"/> of the given video.
        /// </summary>
        /// <param name="url">The url to the video.</param>
        public static VideoInfo GetVideoInfo(string url,
                                             YTDAuthentication authentication = null,
                                             OperationLogger logger           = null)
        {
            string json_dir  = Common.GetJsonDirectory();
            string json_file = string.Empty;
            string arguments = string.Format(Commands.GetJsonInfo,
                                             json_dir,
                                             url,
                                             authentication == null ? string.Empty : authentication.ToCmdArgument());
            VideoInfo video = new VideoInfo();

            LogHeader(logger, arguments);

            Helper.StartProcess(YouTubeDlPath, arguments,
                                delegate(Process process, string line)
            {
                line = line.Trim();

                if (line.StartsWith("[info] Writing video description metadata as JSON to:"))
                {
                    // Store file path
                    json_file = line.Substring(line.IndexOf(":") + 1).Trim();
                }
                else if (line.Contains("Refetching age-gated info webpage"))
                {
                    video.RequiresAuthentication = true;
                }
            },
                                delegate(Process process, string error)
            {
                error = error.Trim();

                if (error.Contains(ErrorSignIn))
                {
                    video.RequiresAuthentication = true;
                }
                else if (error.StartsWith("ERROR:"))
                {
                    video.Failure       = true;
                    video.FailureReason = error.Substring("ERROR: ".Length);
                }
            }, null, logger)
            .WaitForExit();

            if (!video.Failure && !video.RequiresAuthentication)
            {
                video.DeserializeJson(json_file);
            }

            return(video);
        }
Exemple #2
0
        public static async Task GetVideoInfoBatchAsync(ICollection <string> urls,
                                                        Action <VideoInfo> videoReady,
                                                        YTDAuthentication authentication = null,
                                                        OperationLogger logger           = null)
        {
            string json_dir        = Common.GetJsonDirectory();
            string arguments       = string.Format(Commands.GetJsonInfoBatch, json_dir, string.Join(" ", urls));
            var    videos          = new OrderedDictionary();
            var    jsonFiles       = new Dictionary <string, string>();
            var    findVideoID     = new Regex(@"(?:\]|ERROR:)\s(.{11}):", RegexOptions.Compiled);
            var    findVideoIDJson = new Regex(@":\s.*\\(.{11})_", RegexOptions.Compiled);

            LogHeader(logger, arguments);

            await Task.Run(() =>
            {
                Helper.StartProcess(YouTubeDlPath, arguments,
                                    (Process process, string line) =>
                {
                    line = line.Trim();
                    Match m;
                    string id;
                    VideoInfo video = null;

                    if ((m = findVideoID.Match(line)).Success)
                    {
                        id    = findVideoID.Match(line).Groups[1].Value;
                        video = videos.Get <VideoInfo>(id, new VideoInfo()
                        {
                            ID = id
                        });
                    }

                    if (line.StartsWith("[info] Writing video description metadata as JSON to:"))
                    {
                        id           = findVideoIDJson.Match(line).Groups[1].Value;
                        var jsonFile = line.Substring(line.IndexOf(":") + 1).Trim();
                        jsonFiles.Put(id, jsonFile);

                        video = videos[id] as VideoInfo;
                        video.DeserializeJson(jsonFile);
                        videoReady(video);
                    }
                    else if (line.Contains("Refetching age-gated info webpage"))
                    {
                        video.RequiresAuthentication = true;
                    }
                },
                                    (Process process, string error) =>
                {
                    error     = error.Trim();
                    var id    = findVideoID.Match(error).Groups[1].Value;
                    var video = videos.Get <VideoInfo>(id, new VideoInfo()
                    {
                        ID = id
                    });

                    if (error.Contains(ErrorSignIn))
                    {
                        video.RequiresAuthentication = true;
                    }
                    else if (error.StartsWith("ERROR:"))
                    {
                        video.Failure       = true;
                        video.FailureReason = error.Substring("ERROR: ".Length);
                    }
                }, null, logger)
                .WaitForExit();
            });
        }
 public YoutubeDlProcess(OperationLogger logger, YTDAuthentication authentication)
 {
     _logger         = logger;
     _authentication = authentication;
 }