예제 #1
0
        public static YoutubeDL Get(string url, out string aUrlBest, out string vUrlBest) // will add .info.json
        {
            aUrlBest = null;
            vUrlBest = null;

            string tmpFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

            Process proc = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName               = plugin_path,
                    Arguments              = $"--no-check-certificate --skip-download --write-info-json -o \"{tmpFile}\" \"{url}\"",
                    CreateNoWindow         = true,
                    UseShellExecute        = false,
                    RedirectStandardOutput = true,
                    //RedirectStandardError = true,
                    WindowStyle = ProcessWindowStyle.Hidden
                }
            };

            proc.Start();
            proc.WaitForExit();

            //string lines = "";
            //while (!proc.StandardOutput.EndOfStream)
            //    lines += proc.StandardOutput.ReadLine() + "\r\n";
            //Console.WriteLine(lines);

            if (!File.Exists($"{tmpFile}.info.json"))
            {
                return(null);
            }

            string json = File.ReadAllText($"{tmpFile}.info.json");
            JsonSerializerSettings settings = new JsonSerializerSettings();

            settings.NullValueHandling = NullValueHandling.Ignore;

            YoutubeDL ytdl = JsonConvert.DeserializeObject <YoutubeDL>(json, settings);

            if (ytdl == null || ytdl.formats == null || ytdl.formats.Count == 0)
            {
                return(null);
            }

            for (int i = 0; i < ytdl.formats.Count; i++)
            {
                Dump(ytdl.formats[i]);
            }

            // TODO: To let user choose formats

            //var aUrlsI =
            //    from format in ytdl.formats
            //    where format.abr == 192
            //    orderby format.abr descending
            //    select format;

            //var vUrlsI =
            //    from format in ytdl.formats
            //    where format.height == 1080
            //    orderby format.height descending
            //    select format;

            //aUrlBest = aUrlsI.ToList()[0].url;
            //vUrlBest = vUrlsI.ToList()[0].url;
            //Console.WriteLine("Will use ...");
            //Dump(aUrlsI.ToList()[0]);
            //Dump(vUrlsI.ToList()[0]);

            //var aUrlsI =
            //    from format in ytdl.formats
            //    where format.abr > 0
            //    orderby format.abr descending
            //    select format;

            //var vUrlsI =
            //    from format in ytdl.formats
            //    where format.height > 0
            //    orderby format.height descending
            //    select format;

            //Console.WriteLine("Will use ...");
            //List<Format> aUrls = aUrlsI.ToList();
            //List<Format> vUrls = vUrlsI.ToList();
            //if (aUrls.Count != 0)
            //{
            //    aUrlBest = aUrls[0].url;
            //    Dump(aUrls[0]);
            //}

            //if (vUrls.Count != 0)
            //{
            //    vUrlBest = vUrls.ToList()[0].url;
            //    Dump(vUrls[0]);
            //}

            aUrlBest = null;
            vUrlBest = ytdl.formats[ytdl.formats.Count - 1].url;
            Console.WriteLine("Will use ...");
            Dump(ytdl.formats[ytdl.formats.Count - 1]);

            return(ytdl);
        }
예제 #2
0
파일: YoutubeDL.cs 프로젝트: huerd/Flyleaf
        public static YoutubeDL New(string url, ref int interrupt)
        {
            try
            {
                // Download .Json if not exists already || Disabled should check also expiration timestamps
                string tmpFile = GetJsonPath(url);

                //if (!File.Exists($"{tmpFile}.info.json"))
                //{
                Process proc = new Process
                {
                    /* --flat-playlist                      Do not extract the videos of a playlist, only list them.
                     * --no-playlist                        Download only the video, if the URL refers to a video and a playlist.
                     * --youtube-skip-dash-manifest         Do not download the DASH manifests and related data on YouTube videos
                     * --merge-output-format FORMAT         If a merge is required (e.g. bestvideo+bestaudio), output to given container format. One of mkv, mp4, ogg, webm, flv. Ignored if no merge is required
                     */
                    StartInfo = new ProcessStartInfo
                    {
                        FileName        = plugin_path,
                        Arguments       = $"--no-check-certificate --skip-download --write-info-json -o \"{tmpFile}\" \"{url}\"",
                        CreateNoWindow  = true,
                        UseShellExecute = false,
                        //RedirectStandardOutput  = true,
                        //RedirectStandardError = true,
                        WindowStyle = ProcessWindowStyle.Hidden
                    }
                };
                proc.Start();
                while (!proc.HasExited && interrupt == 0)
                {
                    Thread.Sleep(35);
                }
                if (interrupt == 1)
                {
                    if (!proc.HasExited)
                    {
                        proc.Kill();
                    }
                    return(null);
                }

                if (!File.Exists($"{tmpFile}.info.json"))
                {
                    return(null);
                }
                //}

                // Parse Json Object
                string json = File.ReadAllText($"{tmpFile}.info.json");
                JsonSerializerSettings settings = new JsonSerializerSettings();
                settings.NullValueHandling = NullValueHandling.Ignore;
                YoutubeDL ytdl = JsonConvert.DeserializeObject <YoutubeDL>(json, settings);
                ytdl.json_path = $"{tmpFile}.info.json";
                if (ytdl == null || ytdl.formats == null || ytdl.formats.Count == 0)
                {
                    return(null);
                }

                // Fix Nulls (we are not sure if they have audio/video)
                for (int i = 0; i < ytdl.formats.Count; i++)
                {
                    if (ytdl.formats[i].vcodec == null)
                    {
                        ytdl.formats[i].vcodec = "";
                    }
                    if (ytdl.formats[i].acodec == null)
                    {
                        ytdl.formats[i].acodec = "";
                    }

                    Dump(ytdl.formats[i]);
                }

                return(ytdl);
            } catch (Exception e) { Console.WriteLine($"[Youtube-DL] Error ... {e.Message}"); }

            return(null);
        }