Ejemplo n.º 1
0
        /// <summary>
        /// Creates a Process with the given arguments, then returns it.
        /// </summary>
        public static Process StartProcess(string fileName,
                                           string arguments,
                                           Action <Process, string> output,
                                           Action <Process, string> error,
                                           Dictionary <string, string> environmentVariables,
                                           OperationLogger logger)
        {
            var psi = new ProcessStartInfo(fileName, arguments)
            {
                UseShellExecute        = false,
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                RedirectStandardError  = true,
                CreateNoWindow         = true,
                WindowStyle            = ProcessWindowStyle.Hidden,
                WorkingDirectory       = Common.GetLogsDirectory()
            };

            if (environmentVariables != null)
            {
                foreach (KeyValuePair <string, string> pair in environmentVariables)
                {
                    psi.EnvironmentVariables.Add(pair.Key, pair.Value);
                }
            }

            var process = new Process()
            {
                EnableRaisingEvents = true,
                StartInfo           = psi
            };

            process.OutputDataReceived += delegate(object sender, DataReceivedEventArgs e)
            {
                if (string.IsNullOrEmpty(e.Data))
                {
                    return;
                }

                logger?.LogLine(e.Data);
                output?.Invoke(process, e.Data);
            };
            process.ErrorDataReceived += delegate(object sender, DataReceivedEventArgs e)
            {
                if (string.IsNullOrEmpty(e.Data))
                {
                    return;
                }

                logger?.LogLine(e.Data);
                error?.Invoke(process, e.Data);
            };

            process.Start();
            process.BeginOutputReadLine();
            process.BeginErrorReadLine();

            return(process);
        }
Ejemplo n.º 2
0
        public PlaylistReader(string url, int[] videos, bool reverse)
        {
            string json_dir = Common.GetJsonDirectory();

            _playlist_id = Helper.GetPlaylistId(url);

            string range = string.Empty;

            if (videos != null && videos.Length > 0)
            {
                // Make sure the video indexes is sorted, otherwise reversing wont do anything
                Array.Sort(videos);
                range = string.Format(CmdPlaylistRange, string.Join(",", videos));
            }

            string reverseS = reverse ? CmdPlaylistReverse : string.Empty;

            _arguments = string.Format(CmdPlaylistInfo, json_dir, _playlist_id, range, reverseS, url);
            _url       = url;

            _logger = OperationLogger.Create(OperationLogger.YTDLogFile);

            var ytd = new YoutubeDlProcess(_logger, null);

            ytd.LogHeader(_arguments);

            _youtubeDl = Helper.StartProcess(YoutubeDlProcess.YouTubeDlPath,
                                             _arguments,
                                             OutputReadLine,
                                             ErrorReadLine,
                                             null,
                                             _logger);
            _youtubeDl.Exited += delegate
            {
                _processFinished = true;
                ytd.LogFooter();
            };
        }