Exemplo n.º 1
0
        private void btnStart_Click(object sender, EventArgs e)
        {
            if (m_NeedToStopAll)
            {
                return;
            }

            if (ckbAutoRestart.Enabled && !ckbAutoRestart.Checked)
            {
                if (MessageBox.Show("Do you want to enable 'auto-restart' at the same time ?", this.Text, MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    ckbAutoRestart.Checked = true;
                }
            }

            List <string> input = new List <string>();

            string outputVideo = "";
            string outputAudio = "";

            input = GenInputPart();
            if (input.Count <= 0)
            {
                return;
            }

            outputVideo = CommandGenerator.GenVideoOutputPart(m_VideoOutputTaskGroup.Tasks);
            outputAudio = CommandGenerator.GenAudioOutputPart(m_AudioOutputTaskGroup.Tasks, m_EncoderAAC);

            if (outputVideo.Length <= 0 && outputAudio.Length <= 0)
            {
                return;
            }

            if (input.Count <= 1)
            {
                m_VideoArgs = input[0] + " " + outputVideo + " " + outputAudio;
                m_AudioArgs = "";
            }
            else
            {
                var mpegItemCount = 0;
                foreach (var taskItem in m_VideoOutputTaskGroup.Tasks)
                {
                    if (taskItem.VideoType == "mpeg")
                    {
                        mpegItemCount++;
                    }
                }
                if (mpegItemCount > 0 && mpegItemCount == m_VideoOutputTaskGroup.Tasks.Count)
                {
                    m_VideoArgs = input[0] + " " + input[1] + " " + outputVideo;
                }
                else
                {
                    m_VideoArgs = outputVideo.Trim().Length <= 0 ? "" : (input[0].Trim().Length > 0 ? input[0] + " " + outputVideo : "");
                }

                m_AudioArgs = outputAudio.Trim().Length <= 0 ? "" : (input[1].Trim().Length > 0 ? input[1] + " " + outputAudio : "");
            }

            m_VideoArgs = m_VideoArgs.Trim();
            m_AudioArgs = m_AudioArgs.Trim();

            //MessageBox.Show(args);
            Console.WriteLine(m_VideoArgs);
            Console.WriteLine(m_AudioArgs);

            gbMediaSource.Enabled = false;
            gbVideoTask.Enabled   = false;
            gbAudioTask.Enabled   = false;

            btnStart.Enabled = false;

            m_UpdatedUI4Start = false;


            try
            {
                StopRunningProcesses();

                mmVideoLogger.Clear();
                mmAudioLogger.Clear();

                if (m_VideoArgs.Length > 0)
                {
                    if (m_VideoArgs.Contains("-f nut pipe:1"))
                    {
                        // in this case, Process.Kill() will just kill "cmd", but not "ffmpeg" ...
                        m_VideoProcess = new ProcessIoWrapper("cmd", "/C ffmpeg " + m_VideoArgs, ProcessIoWrapper.FLAG_INPUT | ProcessIoWrapper.FLAG_ERROR);
                    }
                    else
                    {
                        m_VideoProcess = new ProcessIoWrapper("ffmpeg", m_VideoArgs, ProcessIoWrapper.FLAG_INPUT | ProcessIoWrapper.FLAG_ERROR);
                    }

                    m_LastVideoTime = DateTime.Now;

                    //m_ProcessIoWrapper.OnStandardOutputTextRead = new Action<string>((text) => { OnStderrTextRead(text); });
                    m_VideoProcess.OnStandardErrorTextRead = new Action <string>((text) => { OnVideoStderrTextRead(text); });
                    m_VideoProcess.OnProcessExited         = new Action(() => { OnVideoProcessExited(); });
                    m_VideoProcess.StartProcess();
                }

                if (m_AudioArgs.Length > 0)
                {
                    if (m_AudioArgs.Contains("-f nut pipe:1"))
                    {
                        // in this case, Process.Kill() will just kill "cmd", but not "ffmpeg" ...
                        m_AudioProcess = new ProcessIoWrapper("cmd", "/C ffmpeg " + m_AudioArgs, ProcessIoWrapper.FLAG_INPUT | ProcessIoWrapper.FLAG_ERROR);
                    }
                    else
                    {
                        m_AudioProcess = new ProcessIoWrapper("ffmpeg", m_AudioArgs, ProcessIoWrapper.FLAG_INPUT | ProcessIoWrapper.FLAG_ERROR);
                    }

                    m_LastAudioTime = DateTime.Now;

                    //m_ProcessIoWrapper.OnStandardOutputTextRead = new Action<string>((text) => { OnStderrTextRead(text); });
                    m_AudioProcess.OnStandardErrorTextRead = new Action <string>((text) => { OnAudioStderrTextRead(text); });
                    m_AudioProcess.OnProcessExited         = new Action(() => { OnAudioProcessExited(); });
                    m_AudioProcess.StartProcess();
                }

                btnStop.Enabled = true;

                notifyIconMain.Icon = notifyIconStart.Icon;
                notifyIconMain.Text = this.Text + " (" + notifyIconStart.Text + ")";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                m_UpdatedUI4Start     = true;
                gbMediaSource.Enabled = true;
                gbVideoTask.Enabled   = true;
                gbAudioTask.Enabled   = true;
                btnStart.Enabled      = true;
            }
        }