티스토리 뷰

c#

[1분해결] c#에서 ffmpeg 동적으로 사용하기

개발자 고포고 2021. 10. 27. 16:56
반응형

[1분해결] c#에서 ffmpeg 동적으로 사용하기

 

[설명]

runCommand - process를 활용하여 ffmpeg 스크립트 실행

BackgroundWorker - 비동기화 구현

 

 private void Initialize()
        {
            bgworker = new BackgroundWorker();
            bgworker.DoWork += Bgworker_DoWork;
            bgworker.RunWorkerAsync();
            bgworker.RunWorkerCompleted += Bgworker_RunWorkerCompleted;
            lb.Items.Add("start");
        }

        private void Bgworker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            process.Close();
            if(result != string.Empty)
            {
                lb.Items.Add("complete");
            }else
            {
                lb.Items.Add("error");
            }
        }

        private void Bgworker_DoWork(object sender, DoWorkEventArgs e)
        {
            runCommand();
        }

        void runCommand()
        {
            Debug.WriteLine("start");
            //* Create your Process
            process = new Process();
            process.StartInfo.FileName = "ffmpeg.exe";
            process.StartInfo.Arguments = "-i 2.mp4 -c:a aac -b:a 160k -c:v libx264 -crf 22 -preset veryfast 2-6.mp4";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            
            //* Set your output and error (asynchronous) handlers
            process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
            process.ErrorDataReceived += new DataReceivedEventHandler(OutputHandler);
            
            //* Start process and handlers
            process.Start();
            process.BeginOutputReadLine();
            process.BeginErrorReadLine();
            process.WaitForExit();
        }

        void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
        {
            if (outLine.Data!= null)
            {
                if (outLine.Data.Contains("Qavg")){
                    result = outLine.Data;
                }
            }
        }

 

[tag]

#C# #ffmpeg #process #wpf #동영상 #디코딩 #비동기 #비동기화 #영화 #인코딩

반응형
댓글
반응형