Beispiel #1
0
        public string SplitOneFileSynchronous(string input, double start, double length,
                                              int i, ref string log)
        {
            var outFilename = input + "." + string.Format(
                "{0}", i.ToString("D3")) + Path.GetExtension(input);

            var args = new List <string>();

            args.Add("-nostdin");
            args.Add("-i");
            args.Add(input);

            // important: put the options -ss and -t between -i and output
            args.Add("-ss");
            args.Add(start.ToString());
            args.Add("-t");
            args.Add(length.ToString());
            args.Add("-acodec");
            args.Add("copy");
            args.Add("-vcodec");
            args.Add("copy");

            args.Add(outFilename);
            RunGetStdout(CsDownloadVidFilepaths.GetFfmpeg(),
                         Utils.CombineProcessArguments(args.ToArray()),
                         "split", outFilename, ref log);

            Utils.AssertTrue(File.Exists(outFilename));
            return(outFilename);
        }
Beispiel #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);
        }
Beispiel #3
0
        string GetFileWithFadeoutSynchronous(string input, double start, double fadeLength,
                                             int i, ref string log)
        {
            var outFilename = input + string.Format("_outfade{0}.wav", i.ToString("D2"));
            var args        = new List <string>();

            args.Add("-nostdin");
            args.Add("-i");
            args.Add(input);
            args.Add("-ss");
            args.Add(start.ToString());
            args.Add("-t");
            args.Add(fadeLength.ToString());
            args.Add("-vn");

            // note: hours:min:seconds format does not seem to work in the fade spec.
            args.Add("-af");
            args.Add(string.Format("afade=t=out:st={0}:d={1}",
                                   start.ToString(),
                                   fadeLength.ToString()));

            args.Add(outFilename);
            RunGetStdout(CsDownloadVidFilepaths.GetFfmpeg(),
                         Utils.CombineProcessArguments(args.ToArray()),
                         "get fadeout", outFilename, ref log);

            Utils.AssertTrue(File.Exists(outFilename));
            return(outFilename);
        }
        private void NextStepIsToChooseQuality()
        {
            if (CsDownloadVidFilepaths.GetYtdlPath(cbUsePytube.Checked,
                                                   required: false) == null)
            {
                return;
            }

            listBoxFmts.Items.Clear();
            AddGenericFormatsToListbox();
            GetOptionsFromUI(out List <string> urlsRet, out int waitBetween,
                             out string filenamePattern, out string outDir);

            // look up formats
            txtStatus.Visible                   = true;
            lblShortStatus.Visible              = true;
            panelChooseQuality.Visible          = true;
            btnNextStepIsToChooseOutput.Enabled = false;
            btnNextStepIsToChooseOutput.Text    = "Looking up formats...";
            var urlToGet = urlsRet[0];
            var info     = GetStartInfo(urlToGet, "", true);

            // run all in a separate thread, so that UI remains responsive.
            _runner.RunInThread(() =>
            {
                LoadFormats_StartProc(urlToGet, info);
            });
        }
        private void downloadFromWeb(string url)
        {
            lblShortStatus.Visible = true;
            txtStatus.Visible      = true;
            var urlBeforeParam = Utils.SplitByString(url, "?")[0];

            if (urlBeforeParam.EndsWith(".m3u") || urlBeforeParam.EndsWith(".m3u8"))
            {
                var ext = Utils.AskToConfirm(
                    "Use mp4 (default) or .mkv (more resilient)?") ? ".mp4" : ".mkv";
                var nameShort = Path.GetFileName(urlBeforeParam);
                var destName  = Path.Combine(txtOutputDir.Text,
                                             Path.GetFileNameWithoutExtension(nameShort) + ext);
                MessageBox.Show("Downloading video (via m3u) to " + destName + "...");
                Utils.AssertTrue(!File.Exists(destName), "File already exists here.");
                var args = new List <string>()
                {
                    "-i", url, "-c", "copy"
                };
                if (ext == ".mp4")
                {
                    args.Add("-bsf:a");
                    args.Add("aac_adtstoasc");
                }

                args.Add(destName);
                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.RunInThread(() =>
                {
                    _runner.Trace("Downloading...");
                    _runner.RunProcessSync(info, "Download from web");
                    _runner.Trace("Download complete, to " + destName);
                });
            }
            else
            {
                var destName = Path.Combine(txtOutputDir.Text,
                                            Path.GetFileName(urlBeforeParam));
                MessageBox.Show("Downloading video to " + destName + "...");
                Utils.AssertTrue(!File.Exists(destName), "File already exists here.");
                _runner.RunInThread(() =>
                {
                    _runner.Trace("Downloading...");
                    DownloadLatestPyScriptBase.DownloadFile(url, destName);
                    _runner.Trace("Download complete, to " + destName);
                });
            }
        }
        private static void GetPlaylistImpl(string url, string txtpath, RunToolHelper runner)
        {
            bool isPytube = false; // use only ytdl, not pytube
            var  args     = new List <string>();

            args.Add("--ignore-config"); // don't look for global config file
            args.Add("--no-mark-watched");
            args.Add("--no-call-home");
            args.Add("-j"); // send output in json format
            args.Add("--flat-playlist");
            args.Add("-i"); // continue after errs
            args.Add(url);

            var info = new ProcessStartInfo();

            info.FileName               = CsDownloadVidFilepaths.GetYtdlPath(isPytube);
            info.Arguments              = Utils.CombineProcessArguments(args.ToArray());
            info.CreateNoWindow         = true;
            info.RedirectStandardError  = true;
            info.RedirectStandardOutput = true;
            info.UseShellExecute        = false;

            if (CsDownloadVidFilepaths.GetYtdlPath(isPytube).EndsWith(".py"))
            {
                info.FileName  = CsDownloadVidFilepaths.GetPython();
                info.Arguments = "\"" + CsDownloadVidFilepaths.GetYtdlPath(isPytube) + "\" " +
                                 Utils.CombineProcessArguments(args.ToArray());
            }

            var     stdoutGot = "";
            Process p         = new Process();

            p.StartInfo = info;
            p.Start();
            p.OutputDataReceived += (o, eparam) => { stdoutGot += eparam.Data; };
            p.BeginOutputReadLine();
            p.WaitForExit();

            if (p.ExitCode != 0)
            {
                throw new CsDownloadVidException("error - non-zero exit code of " + p.ExitCode);
            }

            stdoutGot = stdoutGot.Trim();
            if (!stdoutGot.StartsWith("{"))
            {
                throw new CsDownloadVidException("error - did not get valid json back " +
                                                 stdoutGot);
            }

            GetPlaylistImplFromJson(runner, url, stdoutGot, txtpath);
        }
Beispiel #7
0
        private void doCustomEncode(string example, InputBoxHistory key)
        {
            var files = this.GetInputFiles(1);
            var cmd   = InputBoxForm.GetStrInput("Command for the ffmpeg encoder:", example,
                                                 key);

            if (String.IsNullOrEmpty(cmd))
            {
                return;
            }
            else if (!cmd.Contains("%in%") && !Utils.AskToConfirm("Did not see '%in%', " +
                                                                  "won't process input files. Continue?"))
            {
                return;
            }

            if (!Utils.AskToConfirm("Run the command right now? (or copy the command line to the clipboard)"))
            {
                var s = "";
                foreach (var file in files)
                {
                    s += "\r\n\"" + CsDownloadVidFilepaths.GetFfmpeg() + "\" ";
                    s += cmd.Replace("%in%", files[0]);
                }

                Clipboard.SetText(s);
                MessageBox.Show("Command was copied to the clipboard.");
                return;
            }

            RunToolHelper.RunAndCatch(() =>
            {
                var infos = new List <ProcessStartInfo>();
                foreach (var file in files)
                {
                    var info                    = new ProcessStartInfo();
                    info.FileName               = CsDownloadVidFilepaths.GetFfmpeg();
                    info.Arguments              = cmd.Replace("%in%", file);
                    info.CreateNoWindow         = true;
                    info.RedirectStandardError  = true;
                    info.RedirectStandardOutput = true;
                    info.UseShellExecute        = false;
                    infos.Add(info);
                }

                _runner.RunProcesses(infos.ToArray(), "Custom encode");
            });
        }
        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");
            });
        }
        private void btnDownload_Click(object sender, EventArgs e)
        {
            RunToolHelper.RunAndCatch(() =>
            {
                if (chkDashToM4a.Checked || chkAutoCombineAV.Checked)
                {
                    CsDownloadVidFilepaths.GetFfmpeg();
                }

                GetOptionsFromUI(out List <string> urlsRet, out int waitBetween,
                                 out string filenamePattern, out string outDir);

                var formats = GetFormatsFromUI();
                if (formats == null || formats.Length == 0)
                {
                    throw new CsDownloadVidException("No format chosen.");
                }
                else if (formats.Any((fmt) => cbUsePytube.Checked != fmt.StartsWith("<Stream:")))
                {
                    throw new CsDownloadVidException("It looks like you've clicked " +
                                                     "'use pytube instead of ytdl' half-way through-- when you change " +
                                                     "this you have to start over at step 1.");
                }

                Configs.Current.Set(ConfigKey.SaveVideosTo, txtOutputDir.Text);
                _runner.SetWaitBetween(waitBetween);
                List <ProcessStartInfo> listInfos = new List <ProcessStartInfo>();
                foreach (var url in urlsRet)
                {
                    foreach (var fmt in formats)
                    {
                        listInfos.Add(GetStartInfo(url, fmt, false));
                    }
                }

                _runner.RunProcesses(listInfos.ToArray(), "downloading", () =>
                {
                    if (chkAutoCombineAV.Checked && Directory.Exists(txtOutputDir.Text))
                    {
                        runAutocombineAVAll(txtOutputDir.Text);
                    }
                });
            });
        }
Beispiel #10
0
        private ProcessStartInfo MakeTask(bool audioOrVideo, string suggestedFormat, string file)
        {
            if (suggestedFormat.StartsWith("."))
            {
                // let the user type ".m4a" as well as "m4a"
                suggestedFormat = suggestedFormat.Substring(1);
            }

            Utils.AssertTrue(suggestedFormat.Length > 0, "You did not provide a suggested format.");
            var args = new List <string>();

            args.Add("-nostdin");
            args.Add("-i");
            args.Add(file);
            if (audioOrVideo)
            {
                var format = suggestedFormat == "auto" ? "m4a" : suggestedFormat;
                args.Add("-vn");
                args.Add("-acodec");
                args.Add("copy");
                args.Add(file + "_audio." + format);
            }
            else
            {
                var format = suggestedFormat == "auto" ? "m4v" : suggestedFormat;
                args.Add("-an");
                args.Add("-vcodec");
                args.Add("copy");
                args.Add(file + "_video." + format);
            }

            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);
        }
Beispiel #11
0
        string ConvertM4aToAacSynchronous(string input, ref string log)
        {
            Utils.AssertTrue(input.EndsWith(".m4a"));
            var outFilename = Path.GetDirectoryName(input) + Utils.Sep +
                              Path.GetFileNameWithoutExtension(input) + ".aac";

            var args = new List <string>();

            args.Add("-nostdin");
            args.Add("-i");
            args.Add(input);
            args.Add("-acodec");
            args.Add("copy");
            args.Add(outFilename);
            RunGetStdout(CsDownloadVidFilepaths.GetFfmpeg(),
                         Utils.CombineProcessArguments(args.ToArray()),
                         "m4a to aac", outFilename, ref log);

            Utils.AssertTrue(File.Exists(outFilename));
            return(outFilename);
        }
        public FormGetVideo()
        {
            InitializeComponent();
            panelAdvanced.Visible               = chkShowAdvanced.Checked;
            lblEnterUrlsAdvanced.Visible        = chkShowAdvanced.Checked;
            panelChooseQuality.Visible          = false;
            panelChooseOutput.Visible           = false;
            btnNextStepIsToChooseOutput.Enabled = false;
            lblShortStatus.Text = "";
            txtStatus.Visible   = false;

            AddGenericFormatsToListbox();
            txtOutputDir.Text   = CsDownloadVidFilepaths.GetDefaultDownloadsDir();
            lblNamePattern.Text = "Filename pattern, see also" + Utils.NL +
                                  "%(upload_date)s";
            _runner = new RunToolHelper(this.txtStatus, this.lblShortStatus,
                                        (line) => (!line.Contains("[download]")));

            // pre-emptively ensure that we have paths
            var ensurePath = CsDownloadVidFilepaths.GetPython();
        }
        private void runAutocombineAV(string vid, string aud)
        {
            var vidOnly = Path.GetDirectoryName(vid) + "/" + Path.GetFileNameWithoutExtension(vid) +
                          "_{vidonly}" + Path.GetExtension(vid);
            var outFile = Path.GetDirectoryName(vid) + "/" + Path.GetFileNameWithoutExtension(vid) +
                          "_{out}" + Path.GetExtension(vid);

            if (File.Exists(outFile))
            {
                Utils.SoftDelete(outFile);
            }

            var args = Utils.SplitByString("-i|" + vid + "|-an|-c:v|copy|" + vidOnly, "|");
            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, "AutocombineAV: 1) Ensuring video-only version " + vid);
            Utils.AssertTrue(File.Exists(vidOnly) && new FileInfo(vidOnly).Length > 0, "Did not write to " + vidOnly);

            args                        = Utils.SplitByString("-i|" + vidOnly + "|-i|" + aud + "|-c:a|copy|-c:v|copy|" + outFile, "|");
            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, "AutocombineAV: 2) Putting A+V together " + vid);
            Utils.AssertTrue(File.Exists(outFile) && new FileInfo(outFile).Length > 0, "Did not write to " + outFile);

            _runner.Trace("AutocombineAV: 3) Deleting temporaries");
            Utils.SoftDelete(vidOnly);
            Utils.SoftDelete(vid);
            Utils.SoftDelete(aud);
        }
Beispiel #14
0
        private void makeLouderOnes(string input)
        {
            foreach (var scale in new List <string>()
            {
                "2.0", "4.0", "8.0", "16.0", "32.0"
            })
            {
                var outfile = input + ".makelouder" + scale + ".wav";
                var args    = new List <string>();
                args.Add("-nostdin");
                args.Add("-i");
                args.Add(input);
                args.Add("-filter:a");
                args.Add("volume=" + scale);
                args.Add(outfile);
                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, "Make Louder");
                if (!File.Exists(outfile))
                {
                    _runner.Trace("expected to see output at " + outfile);
                    throw new Exception("expected to see output at " + outfile);
                }

                var pathOut = Utils.RunM4aConversion(outfile, "flac");
                if (new FileInfo(pathOut).Length > 1)
                {
                    File.Delete(outfile);
                }
            }
        }
Beispiel #15
0
        string ConvertWavToM4aSynchronous(string input, string bitrate, ref string log)
        {
            var outFilename = Path.GetDirectoryName(input) + Utils.Sep +
                              Path.GetFileNameWithoutExtension(input) + ".m4a";

            var args = new List <string>();

            args.Add("--quality");
            args.Add("2");
            args.Add("-a");
            args.Add(bitrate);
            args.Add("--rate");
            args.Add("keep");
            args.Add(input);
            args.Add("-d");
            args.Add(Path.GetDirectoryName(input));

            RunGetStdout(CsDownloadVidFilepaths.GetQaac(),
                         Utils.CombineProcessArguments(args.ToArray()),
                         "convert to m4a", outFilename, ref log);

            Utils.AssertTrue(File.Exists(outFilename));
            return(outFilename);
        }
Beispiel #16
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);
            }
        }
        ProcessStartInfo GetStartInfo(string url, string format, bool listSupportedFormatsOnly)
        {
            var args = new List <string>();

            args.Add("--ignore-config"); // don't look for global config file
            args.Add("--no-mark-watched");
            args.Add("--no-call-home");
            args.Add("--no-mtime"); // don't adjust the lmt of the file, it's confusing
            args.Add("--no-playlist");

            if (listSupportedFormatsOnly)
            {
                args.Add("--list-formats");
                args.Add("--simulate");
            }
            else
            {
                args.Add("--format");
                args.Add(format);

                // post-process, otherwise the m4a won't show correctly in some media players
                if (chkDashToM4a.Checked)
                {
                    args.Add("--ffmpeg-location");
                    args.Add(CsDownloadVidFilepaths.GetFfmpeg());
                }
            }

            if (cbUsePytube.Checked)
            {
                args.Add("--outputdir=" + txtOutputDir.Text);
            }
            else
            {
                var outputTemplate = Path.Combine(txtOutputDir.Text, txtFilenamePattern.Text);
                args.Add("--output");
                args.Add(outputTemplate);
            }

            var sArgs = Utils.CombineProcessArguments(args.ToArray());

            if (!string.IsNullOrWhiteSpace(txtAdditionalArgs.Text))
            {
                sArgs += " " + txtAdditionalArgs.Text + " ";
            }

            sArgs += " " + Utils.CombineProcessArguments(new string[] { url });

            var info = new ProcessStartInfo();

            info.FileName               = CsDownloadVidFilepaths.GetYtdlPath(this.cbUsePytube.Checked);
            info.Arguments              = sArgs;
            info.CreateNoWindow         = true;
            info.RedirectStandardError  = true;
            info.RedirectStandardOutput = true;
            info.UseShellExecute        = false;

            if (CsDownloadVidFilepaths.GetYtdlPath(this.cbUsePytube.Checked).EndsWith(".py"))
            {
                info.FileName  = CsDownloadVidFilepaths.GetPython();
                info.Arguments = "\"" + CsDownloadVidFilepaths.GetYtdlPath(
                    this.cbUsePytube.Checked) + "\" " + sArgs;
            }

            return(info);
        }