private static void GetPlaylistImplFromJson(RunToolHelper runner, string url,
                                                    string json, string txtPath)
        {
            // we could also use JsonReaderWriterFactory instead
            runner.Trace("process returned successfully. parsing json...");
            var parts = new List <string>(Utils.SplitByString(json, "\"id\": \""));

            parts.RemoveAt(0);
            var linesOut = new List <string>();

            foreach (var part in parts)
            {
                var proposedId = Utils.SplitByString(part, "\"")[0];
                if (proposedId.Length >= 10 && proposedId.Length <= 12 &&
                    !proposedId.Contains(" "))
                {
                    linesOut.Add("https://www.youtube.com/watch?v=" + proposedId);
                    runner.Trace("Found " + "https://www.youtube.com/watch?v=" + proposedId);
                }
            }

            if (linesOut.Count == 0)
            {
                throw new CsDownloadVidException("did not find any videos in this playlist.");
            }

            runner.Trace("Successfully found " + linesOut.Count + " ids.");
            File.WriteAllLines(txtPath, linesOut);
        }
Exemple #2
0
        private ProcessStartInfo MakeTaskCombineAudioVideo(string audioFile, string videoFile,
                                                           string output)
        {
            var args = new List <string>();

            args.Add("-nostdin");
            args.Add("-i");
            args.Add(videoFile);
            args.Add("-i");
            args.Add(audioFile);
            args.Add("-c:v");
            args.Add("copy");
            args.Add("-c:a");
            args.Add("copy");
            args.Add(output);
            _runner.Trace("Saving to " + output);

            var info = new ProcessStartInfo();

            info.FileName               = CsDownloadVidFilepaths.GetFfmpeg();
            info.Arguments              = Utils.CombineProcessArguments(args.ToArray());
            info.CreateNoWindow         = true;
            info.RedirectStandardError  = true;
            info.RedirectStandardOutput = true;
            info.UseShellExecute        = false;
            return(info);
        }
        private void SplitWithFadeout(string inputFile, List <double> splitPoints,
                                      string sFadeLength)
        {
            const int sampleRate  = 44100;
            string    outFilename = inputFile + "_fadeout.m4a";

            if (!double.TryParse(sFadeLength, out double fadeLength) || fadeLength <= 0)
            {
                throw new CsDownloadVidException("Invalid fadelength, expected a number of " +
                                                 "seconds like 4");
            }
            else if (splitPoints.Count == 0)
            {
                throw new CsDownloadVidException("Enter a time, in seconds");
            }
            else if (splitPoints.Count != 1)
            {
                throw new CsDownloadVidException("It looks like you have entered more than " +
                                                 "one time point. Please enter just one time, in seconds.");
            }
            else if (!inputFile.EndsWith(".m4a"))
            {
                throw new CsDownloadVidException("We currently only support adding fadeout " +
                                                 "for m4a files (if you have a .mp4 song, please rename it to .m4a first).");
            }
            else if (File.Exists(outFilename))
            {
                throw new CsDownloadVidException("Output file already exists " + outFilename);
            }

            // preemptively make sure we have a path to qaac.
            CsDownloadVidFilepaths.GetQaac();

            // run all in a separate thread, so that UI remains responsive.
            _runner.RunInThread(() =>
            {
                var log = "";
                new AddFadeoutUsingRawAacData().Go(inputFile, sampleRate, splitPoints[0],
                                                   fadeLength, outFilename, ref log);

                _runner.TraceFiltered(log.Replace("\n", Utils.NL));
                _runner.Trace(File.Exists(outFilename) ? "Successfully saved to " + outFilename :
                              "Error(s) occurred");
            });
        }
        public void Go(RunToolHelper run)
        {
            var prefix = this.GetPrefix();

            Directory.CreateDirectory("./tools");
            var pathCurrentZip  = "./tools/%.zip".Replace("%", prefix);
            var pathCurrentDir  = "./tools/%dir".Replace("%", prefix);
            var pathOldZip      = "./tools/%-old.zip".Replace("%", prefix);
            var pathOldDir      = "./tools/%dir-old".Replace("%", prefix);
            var pathIncomingZip = "./tools/%-incoming.zip".Replace("%", prefix);
            var pathIncomingDir = "./tools/%dir-incoming".Replace("%", prefix);

            // check before calling Delete(), since we want this to work even if
            // dir is currently empty
            run.Trace("Deleting temporary files");
            if (File.Exists(pathIncomingZip))
            {
                File.Delete(pathIncomingZip);
            }

            if (Directory.Exists(pathIncomingDir))
            {
                Directory.Delete(pathIncomingDir, true);
            }

            var url = this.GetUrl();

            run.Trace("Downloading from " + url);
            DownloadFile(url, pathIncomingZip);

            if (!File.Exists(pathIncomingZip))
            {
                throw new CsDownloadVidException("No file was downloaded. " + url);
            }

            if (new FileInfo(pathIncomingZip).Length < 500 * 1024)
            {
                throw new CsDownloadVidException("File downloaded was too small, " +
                                                 "expect > 500k. " + pathIncomingZip);
            }

            var currentHash  = Utils.GetSha512(pathCurrentZip);
            var incomingHash = Utils.GetSha512(pathIncomingZip);

            if (currentHash == incomingHash)
            {
                run.Trace("We seem to have the latest version -- already up to date!");
                return;
            }

            run.Trace("Extracting from zip file...");
            ZipFile.ExtractToDirectory(pathIncomingZip, pathIncomingDir);
            run.Trace("Removing unneeded files...");
            var dir = pathIncomingDir + "/youtube-dl-master/youtube_dl/extractor";

            this.DoPostProcessing(dir, pathIncomingDir);
            run.Trace("Found a newer version!");
            run.Trace("Moving from current to old");
            if (File.Exists(pathOldZip))
            {
                File.Delete(pathOldZip);
            }
            if (Directory.Exists(pathOldDir))
            {
                Directory.Delete(pathOldDir, true);
            }
            if (File.Exists(pathCurrentZip))
            {
                File.Move(pathCurrentZip, pathOldZip);
            }
            if (Directory.Exists(pathCurrentDir))
            {
                Directory.Move(pathCurrentDir, pathOldDir);
            }

            run.Trace("Moving incoming to current");
            File.Move(pathIncomingZip, pathCurrentZip);
            Directory.Move(pathIncomingDir, pathCurrentDir);
        }
Exemple #5
0
        void MediaJoin(string[] lines, string outFormat)
        {
            var parentDirs = (from part in lines
                              select Path.GetDirectoryName(part)).Distinct();

            if (parentDirs.Count() != 1)
            {
                throw new CsDownloadVidException("Input files must be in same directory.");
            }

            var fileExts = (from part in lines select Path.GetExtension(part)).Distinct();

            if (fileExts.Count() != 1)
            {
                throw new CsDownloadVidException("Files have different extensions.");
            }

            var tmpList = parentDirs.First() + Utils.Sep + "temp_csdownloadvid_list.txt";

            if (File.Exists(tmpList))
            {
                File.Delete(tmpList);
                Utils.AssertTrue(!File.Exists(tmpList));
            }

            foreach (var part in lines)
            {
                var file = "file " + EscapeStringForFfmpeg(part) + "\n";
                File.AppendAllText(tmpList, file);
            }

            outFormat = outFormat == "auto" ? fileExts.ToArray()[0] : outFormat;
            var output = lines[0] + "_out" + outFormat;

            if (File.Exists(output))
            {
                throw new CsDownloadVidException("File already exists " + output);
            }

            var args = new List <string>();

            args.Add("-nostdin");
            args.Add("-f");
            args.Add("concat");
            args.Add("-safe"); // many versions of ffmpeg think windows full paths are unsafe
            args.Add("0");     // perhaps better to set current directory + use relative paths
            args.Add("-i");
            args.Add(tmpList);
            args.Add("-acodec");
            args.Add("copy");
            args.Add("-vcodec");
            args.Add("copy");
            args.Add(output);
            _runner.Trace("Saving to " + output);

            var info = new ProcessStartInfo();

            info.FileName               = CsDownloadVidFilepaths.GetFfmpeg();
            info.Arguments              = Utils.CombineProcessArguments(args.ToArray());
            info.CreateNoWindow         = true;
            info.RedirectStandardError  = true;
            info.RedirectStandardOutput = true;
            info.UseShellExecute        = false;
            _runner.RunProcessSync(info, "Join Media");

            if (File.Exists(tmpList))
            {
                File.Delete(tmpList);
            }
        }
        private void LoadFormats_StartProc(string urlToGet, ProcessStartInfo info)
        {
            string log       = "";
            string stderr    = "";
            string stdout    = "";
            bool   succeeded = false;

            Process p = new Process();

            p.StartInfo = info;
            p.Start();
            p.ErrorDataReceived += (o, eparam) => { stderr += eparam.Data; };
            p.BeginErrorReadLine();
            p.WaitForExit();
            log   += "\nRan " + info.FileName + " " + info.Arguments;
            stdout = p.StandardOutput.ReadToEnd();
            if (p.ExitCode == 0)
            {
                succeeded = true;
            }
            else
            {
                log += "\nStdout:" + stdout;
                log += "\nStderr:" + stderr;
            }

            _runner.TraceFiltered(log.Replace("\n", Utils.NL));
            if (succeeded)
            {
                LoadFormats_ToUI(urlToGet, stdout);
            }
            else
            {
                Utils.MessageErr("Could not get formats");
                _runner.Trace("Could not get formats");
            }
        }