private void worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { try { if (m.infileslist.Length > 1) { ShowMessage(Languages.Translate("Sorry, but stream will be extracted only from first file! :("), Languages.Translate("Warning")); } //удаляем старый файл SafeDelete(outfile); //получаем колличество секунд ff = new FFInfo(); ff.Open(source_file); double percentage_k = ff.Duration().TotalSeconds / 100.0; TimeSpan current_sec = TimeSpan.Zero; encoderProcess = new Process(); ProcessStartInfo info = new ProcessStartInfo(); info.FileName = Calculate.StartupPath + "\\apps\\ffmpeg\\ffmpeg.exe"; info.WorkingDirectory = Path.GetDirectoryName(info.FileName); info.UseShellExecute = false; info.RedirectStandardOutput = false; info.RedirectStandardError = true; info.StandardErrorEncoding = Encoding.UTF8; info.CreateNoWindow = true; string map = ""; string sub = " -sn"; string vcodec = " -vn"; string acodec = " -an"; string framerate = ""; string aspect = ""; if (mode == DecoderModes.DecodeVideo) { map = " -map 0:v:0"; vcodec = " -vcodec ffvhuff -pix_fmt yuv420p"; //ffvhuff, ffv1 string dar = ff.StreamDARSelected(ff.FirstVideoStreamID()); aspect = (dar != "") ? " -aspect " + dar : ""; } else if (mode == DecoderModes.DecodeAudio) { map = " -map 0:a:0"; acodec = " -acodec pcm_s16le"; } else //DecodeAV - не используется { map = " -map 0:v:0 -map 0:a:0"; vcodec = " -vcodec ffvhuff -pix_fmt yuv420p"; //ffvhuff, ffv1 acodec = " -acodec pcm_s16le"; m.inframerate = ff.StreamFramerate(ff.FirstVideoStreamID()); m.format = Settings.FormatOut; framerate = " -r " + Format.GetValidFramerate(m).outframerate; string dar = ff.StreamDARSelected(ff.FirstVideoStreamID()); aspect = (dar != "") ? " -aspect " + dar : ""; } //закрываем фф ff.Close(); ff = null; info.Arguments = "-hide_banner -nostdin -i \"" + source_file + "\"" + map + sub + vcodec + acodec + framerate + aspect + " \"" + outfile + "\""; encoderProcess.StartInfo = info; encoderProcess.Start(); string line, pat = @"time=(\d+:\d+:\d+\.?\d*)"; Regex r = new Regex(pat, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled); Match mat; //первый проход while (!encoderProcess.HasExited) { line = encoderProcess.StandardError.ReadLine(); if (line != null) { mat = r.Match(line); if (mat.Success && TimeSpan.TryParse(mat.Groups[1].Value, out current_sec)) { worker.ReportProgress((int)(current_sec.TotalSeconds / percentage_k)); } else { AppendEncoderText(line); } } } //Дочитываем остатки лога, если что-то не успело считаться line = encoderProcess.StandardError.ReadToEnd(); if (!string.IsNullOrEmpty(line)) { AppendEncoderText(Calculate.FilterLogMessage(r, line)); } //чистим ресурсы exit_code = encoderProcess.ExitCode; encoderProcess.Close(); encoderProcess.Dispose(); encoderProcess = null; if (IsAborted) { return; } //проверка на удачное завершение if (exit_code != 0 && encodertext.Length > 0) { //Оставляем только последнюю строчку из всего лога string[] log = encodertext.ToString().Trim().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); throw new Exception(log[log.Length - 1]); } if (!File.Exists(outfile) || new FileInfo(outfile).Length == 0) { if (mode == DecoderModes.DecodeVideo) { throw new Exception(Languages.Translate("Can`t find output video file!")); } if (mode == DecoderModes.DecodeAudio) { throw new Exception(Languages.Translate("Can`t find output audio file!")); } } } catch (Exception ex) { IsErrors = true; error_message = ex.Message; } }
private void worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { try { if (m.infileslist.Length > 1) ShowMessage(Languages.Translate("Sorry, but stream will be extracted only from first file! :("), Languages.Translate("Warning")); //удаляем старый файл SafeDelete(outfile); //получаем колличество секунд ff = new FFInfo(); ff.Open(source_file); double percentage_k = ff.Duration().TotalSeconds / 100.0; TimeSpan current_sec = TimeSpan.Zero; encoderProcess = new Process(); ProcessStartInfo info = new ProcessStartInfo(); info.FileName = Calculate.StartupPath + "\\apps\\ffmpeg\\ffmpeg.exe"; info.WorkingDirectory = Path.GetDirectoryName(info.FileName); info.UseShellExecute = false; info.RedirectStandardOutput = false; info.RedirectStandardError = true; info.StandardErrorEncoding = Encoding.UTF8; info.CreateNoWindow = true; string map = ""; string sub = " -sn"; string vcodec = " -vn"; string acodec = " -an"; string framerate = ""; string aspect = ""; if (mode == DecoderModes.DecodeVideo) { map = " -map 0:v:0"; vcodec = " -vcodec ffvhuff -pix_fmt yuv420p"; //ffvhuff, ffv1 string dar = ff.StreamDARSelected(ff.FirstVideoStreamID()); aspect = (dar != "") ? " -aspect " + dar : ""; } else if (mode == DecoderModes.DecodeAudio) { map = " -map 0:a:0"; acodec = " -acodec pcm_s16le"; } else //DecodeAV - не используется { map = " -map 0:v:0 -map 0:a:0"; vcodec = " -vcodec ffvhuff -pix_fmt yuv420p"; //ffvhuff, ffv1 acodec = " -acodec pcm_s16le"; m.inframerate = ff.StreamFramerate(ff.FirstVideoStreamID()); m.format = Settings.FormatOut; framerate = " -r " + Format.GetValidFramerate(m).outframerate; string dar = ff.StreamDARSelected(ff.FirstVideoStreamID()); aspect = (dar != "") ? " -aspect " + dar : ""; } //закрываем фф ff.Close(); ff = null; info.Arguments = "-hide_banner -nostdin -i \"" + source_file + "\"" + map + sub + vcodec + acodec + framerate + aspect + " \"" + outfile + "\""; encoderProcess.StartInfo = info; encoderProcess.Start(); string line, pat = @"time=(\d+:\d+:\d+\.?\d*)"; Regex r = new Regex(pat, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled); Match mat; //первый проход while (!encoderProcess.HasExited) { line = encoderProcess.StandardError.ReadLine(); if (line != null) { mat = r.Match(line); if (mat.Success && TimeSpan.TryParse(mat.Groups[1].Value, out current_sec)) { worker.ReportProgress((int)(current_sec.TotalSeconds / percentage_k)); } else { AppendEncoderText(line); } } } //Дочитываем остатки лога, если что-то не успело считаться line = encoderProcess.StandardError.ReadToEnd(); if (!string.IsNullOrEmpty(line)) AppendEncoderText(Calculate.FilterLogMessage(r, line)); //чистим ресурсы exit_code = encoderProcess.ExitCode; encoderProcess.Close(); encoderProcess.Dispose(); encoderProcess = null; if (IsAborted) return; //проверка на удачное завершение if (exit_code != 0 && encodertext.Length > 0) { //Оставляем только последнюю строчку из всего лога string[] log = encodertext.ToString().Trim().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); throw new Exception(log[log.Length - 1]); } if (!File.Exists(outfile) || new FileInfo(outfile).Length == 0) { if (mode == DecoderModes.DecodeVideo) throw new Exception(Languages.Translate("Can`t find output video file!")); if (mode == DecoderModes.DecodeAudio) throw new Exception(Languages.Translate("Can`t find output audio file!")); } } catch (Exception ex) { IsErrors = true; error_message = ex.Message; } }
private void worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { try { if (m.infileslist.Length > 1) ShowMessage(Languages.Translate("Sorry, but stream will be extracted only from first file! :("), Languages.Translate("Warning")); //удаляем старый файл SafeDelete(outfile); //получаем колличество секунд ff = new FFInfo(); ff.Open(source_file); int seconds = (int)ff.Duration().TotalSeconds; encoderProcess = new Process(); ProcessStartInfo info = new ProcessStartInfo(); info.FileName = Calculate.StartupPath + "\\apps\\ffmpeg\\ffmpeg.exe"; info.WorkingDirectory = Path.GetDirectoryName(info.FileName); info.UseShellExecute = false; info.RedirectStandardOutput = true; info.RedirectStandardError = true; info.CreateNoWindow = true; string _format = ""; string _vcodec = "-vn"; string _acodec = "-an"; string yv12 = ""; string _framerate = ""; string aspect = ""; if (mode == DecoderModes.DecodeVideo) { _vcodec = "-vcodec ffvhuff";//ffvhuff, ffv1 yv12 = " -pix_fmt yuv420p"; string dar = ff.StreamDARSelected(ff.FirstVideoStreamID()); aspect = (dar != "") ? " -aspect " + dar : ""; } else if (mode == DecoderModes.DecodeAudio) { _acodec = "-acodec pcm_s16le"; } else { _vcodec = "-vcodec ffvhuff";//ffvhuff, ffv1 yv12 = " -pix_fmt yuv420p"; _acodec = "-acodec pcm_s16le"; m.inframerate = ff.StreamFramerate(ff.FirstVideoStreamID()); m.format = Settings.FormatOut; _framerate = " -r " + Format.GetValidFramerate(m).outframerate; string dar = ff.StreamDARSelected(ff.FirstVideoStreamID()); aspect = (dar != "") ? " -aspect " + dar : ""; } //закрываем фф ff.Close(); ff = null; info.Arguments = "-i \"" + source_file + "\" " + _vcodec + " " + _acodec + _format + yv12 + _framerate + aspect + " \"" + outfile + "\""; encoderProcess.StartInfo = info; encoderProcess.Start(); string line, pat = @"time=(\d+.\d+)"; Regex r = new Regex(pat, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled); Match mat; //первый проход while (!encoderProcess.HasExited) { line = encoderProcess.StandardError.ReadLine(); if (line != null) { mat = r.Match(line); if (mat.Success) { double ctime = Calculate.ConvertStringToDouble(mat.Groups[1].Value); double pr = ((double)ctime / (double)seconds) * 100.0; worker.ReportProgress((int)pr); } else { AppendEncoderText(line); } } } //чистим ресурсы exit_code = encoderProcess.ExitCode; AppendEncoderText(encoderProcess.StandardError.ReadToEnd()); encoderProcess.Close(); encoderProcess.Dispose(); encoderProcess = null; if (IsAborted) return; //проверка на удачное завершение if (exit_code != 0 && encodertext.Length > 0) { //Оставляем только последнюю строчку из всего лога string[] log = encodertext.ToString().Trim().Split(new string[] { Environment.NewLine }, StringSplitOptions.None); throw new Exception(log[log.Length - 1]); } if (!File.Exists(outfile) || new FileInfo(outfile).Length == 0) { if (mode == DecoderModes.DecodeVideo) throw new Exception(Languages.Translate("Can`t find output video file!")); if (mode == DecoderModes.DecodeAudio) throw new Exception(Languages.Translate("Can`t find output audio file!")); } } catch (Exception ex) { IsErrors = true; error_message = ex.Message; } }