public void DoEncode(object sender, DoWorkEventArgs e) { _bw = (BackgroundWorker)sender; string passStr = Processing.GetResourceString("vp8_pass"); string status = Processing.GetResourceString("vp8_encoding_status"); VP8Profile encProfile = (VP8Profile)_jobInfo.VideoProfile; if (!_jobInfo.EncodingProfile.Deinterlace && _jobInfo.VideoStream.Interlaced) { _jobInfo.VideoStream.Interlaced = false; } Size resizeTo = VideoHelper.GetTargetSize(_jobInfo); if (string.IsNullOrEmpty(_jobInfo.AviSynthScript)) { GenerateAviSynthScript(resizeTo); } string inputFile = _jobInfo.AviSynthScript; string outFile = Processing.CreateTempFile( string.IsNullOrEmpty(_jobInfo.TempOutput) ? _jobInfo.BaseName : _jobInfo.TempOutput, "encoded.webm"); _frameCount = _jobInfo.VideoStream.FrameCount; int targetBitrate = 0; if (_jobInfo.EncodingProfile.TargetFileSize > 0) { targetBitrate = Processing.CalculateVideoBitrate(_jobInfo); } int encodeMode = encProfile.EncodingMode; if (encodeMode == 1) { _pass = string.Format(" {1} {0:0}; ", _jobInfo.StreamId, passStr); } _bw.ReportProgress(-10, status + _pass.Replace("; ", string.Empty)); _bw.ReportProgress(0, status); string argument = VP8CommandLineGenerator.Generate(encProfile, targetBitrate, resizeTo.Width, resizeTo.Height, _jobInfo.StreamId, _jobInfo.VideoStream.FrameRateEnumerator, _jobInfo.VideoStream.FrameRateDenominator, outFile); string localExecutable = Path.Combine(AppSettings.ToolsPath, Executable); using (Process encoder = new Process(), decoder = FfMpeg.GenerateDecodeProcess(inputFile, AppSettings.Use64BitEncoders && AppSettings.UseFfmpegScaling, new Size(_jobInfo.VideoStream.Width, _jobInfo.VideoStream.Height), _jobInfo.VideoStream.AspectRatio, _jobInfo.VideoStream.CropRect, resizeTo)) { ProcessStartInfo parameter = new ProcessStartInfo(localExecutable) { WorkingDirectory = AppSettings.DemuxLocation, Arguments = argument, CreateNoWindow = true, UseShellExecute = false, RedirectStandardError = true, RedirectStandardInput = true }; encoder.StartInfo = parameter; encoder.ErrorDataReceived += OnDataReceived; Log.InfoFormat("start parameter: vpxenc {0:s}", argument); bool started; bool decStarted; try { started = encoder.Start(); } catch (Exception ex) { started = false; Log.ErrorFormat("vpxenc exception: {0}", ex); _jobInfo.ExitCode = -1; } NamedPipeServerStream decodePipe = new NamedPipeServerStream(AppSettings.DecodeNamedPipeName, PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.None); try { decStarted = decoder.Start(); } catch (Exception ex) { decStarted = false; Log.ErrorFormat("avconv exception: {0}", ex); _jobInfo.ExitCode = -1; } _startTime = DateTime.Now; if (started && decStarted) { encoder.PriorityClass = AppSettings.GetProcessPriority(); encoder.BeginErrorReadLine(); decoder.PriorityClass = AppSettings.GetProcessPriority(); decoder.BeginErrorReadLine(); Thread pipeReadThread = new Thread(() => { try { ReadThreadStart(decodePipe, encoder); } catch (Exception ex) { Log.Error(ex); } }); pipeReadThread.Start(); pipeReadThread.Priority = ThreadPriority.BelowNormal; encoder.Exited += (o, args) => pipeReadThread.Abort(); while (!encoder.HasExited) { if (_bw.CancellationPending) { encoder.Kill(); decoder.Kill(); } Thread.Sleep(200); } encoder.WaitForExit(10000); encoder.CancelErrorRead(); if (decodePipe.IsConnected) { try { decodePipe.Disconnect(); } catch (Exception ex) { Log.Error(ex); } } try { decodePipe.Close(); decodePipe.Dispose(); } catch (Exception ex) { Log.Error(ex); } decoder.WaitForExit(10000); decoder.CancelErrorRead(); _jobInfo.ExitCode = encoder.ExitCode; Log.InfoFormat("Exit Code: {0:g}", _jobInfo.ExitCode); } } if (_jobInfo.ExitCode == 0) { if ((encProfile.EncodingMode == 1 && _jobInfo.StreamId == 2) || encProfile.EncodingMode == 0) { _jobInfo.VideoStream.Encoded = true; _jobInfo.VideoStream.IsRawStream = false; _jobInfo.TempFiles.Add(_jobInfo.VideoStream.TempFile); _jobInfo.VideoStream.TempFile = outFile; try { _jobInfo.MediaInfo = Processing.GetMediaInfo(_jobInfo.VideoStream.TempFile); } catch (TimeoutException ex) { Log.Error(ex); } _jobInfo.VideoStream = VideoHelper.GetStreamInfo(_jobInfo.MediaInfo, _jobInfo.VideoStream, _jobInfo.EncodingProfile.OutFormat == OutputType.OutputBluRay); string statsFile = Processing.CreateTempFile(outFile, "stats"); _jobInfo.TempFiles.Add(statsFile); _jobInfo.TempFiles.Add(_jobInfo.AviSynthScript); _jobInfo.TempFiles.Add(_jobInfo.FfIndexFile); _jobInfo.TempFiles.Add(_jobInfo.AviSynthStereoConfig); } } _bw.ReportProgress(100); _jobInfo.CompletedStep = _jobInfo.NextStep; e.Result = _jobInfo; }
private void GetCropRect(EncodeInfo job) { FfMpeg ffmpeg = new FfMpeg(); ffmpeg.SetJob(job); _worker.DoWork += ffmpeg.GetCrop; Log.Info("ffmpegEncoder.GetCropRect()"); }
private void DoEncodeAudio(EncodeInfo job) { FfMpeg ffmpeg = new FfMpeg(); OggEnc oggEnc = new OggEnc(); Lame lame = new Lame(); NeroAACEnc aacEnc = new NeroAACEnc(); switch (job.AudioProfile.Type) { case ProfileType.AC3: ffmpeg.SetJob(job); _worker.DoWork += ffmpeg.DoEncodeAc3; Log.Info("ffmpeg.DoEncodeAC3()"); break; case ProfileType.OGG: oggEnc.SetJob(job); _worker.DoWork += oggEnc.DoEncode; Log.Info("oggenc.DoEncode()"); break; case ProfileType.AAC: aacEnc.SetJob(job); _worker.DoWork += aacEnc.DoEncode; Log.Info("NeroAacEnc.DoEncode()"); break; case ProfileType.MP3: lame.SetJob(job); _worker.DoWork += lame.DoEncode; Log.Info("lame.DoEncode()"); break; case ProfileType.Copy: if (job.EncodingProfile.OutFormat == OutputType.OutputDvd && !Processing.CheckAudioDvdCompatible(job.AudioStreams[job.StreamId])) { ffmpeg.SetJob(job); _worker.DoWork += ffmpeg.DoEncodeAc3; Log.Info("ffmpeg.DoEncodeAC3()"); } break; } }
private void DoDemux(EncodeInfo job) { Eac3To eac3Toenc = new Eac3To(); FfMpeg ffmpeg = new FfMpeg(); switch (job.Input) { case InputType.InputBluRay: case InputType.InputAvchd: case InputType.InputHddvd: case InputType.InputDvd: eac3Toenc.SetJob(job); _worker.DoWork += eac3Toenc.DoDemux; Log.Info("eac3toEncoder.DoDemux()"); break; case InputType.InputAvi: case InputType.InputFlash: case InputType.InputMp4: case InputType.InputWm: case InputType.InputMatroska: case InputType.InputMpegps: case InputType.InputTs: case InputType.InputOgg: case InputType.InputWebM: ffmpeg.SetJob(job); _worker.DoWork += ffmpeg.DoDemux; Log.Info("ffmpegEncoder.DoDemux()"); break; } }
public static void GetAppVersions(string encPath = "", string javaPath = "") { if (String.IsNullOrEmpty(encPath)) encPath = AppSettings.ToolsPath; if (String.IsNullOrEmpty(javaPath)) javaPath = AppSettings.JavaInstallPath; X264 x264Enc = new X264(); AppSettings.Lastx264Ver = x264Enc.GetVersionInfo(encPath,false); if (Environment.Is64BitOperatingSystem) AppSettings.Lastx26464Ver = x264Enc.GetVersionInfo(encPath, true); FfMpeg ffmpeg = new FfMpeg(); AppSettings.LastffmpegVer = ffmpeg.GetVersionInfo(encPath, false); if (Environment.Is64BitOperatingSystem) AppSettings.Lastffmpeg64Ver = ffmpeg.GetVersionInfo(encPath, true); Eac3To eac3To = new Eac3To(); AppSettings.Lasteac3ToVer = eac3To.GetVersionInfo(encPath); LsDvd lsdvd = new LsDvd(); AppSettings.LastlsdvdVer = lsdvd.GetVersionInfo(encPath); MkvMerge mkvMerge = new MkvMerge(); AppSettings.LastMKVMergeVer = mkvMerge.GetVersionInfo(encPath); MPlayer mplayer = new MPlayer(); AppSettings.LastMplayerVer = mplayer.GetVersionInfo(encPath); TsMuxeR tsmuxer = new TsMuxeR(); AppSettings.LastTSMuxerVer = tsmuxer.GetVersionInfo(encPath); MJpeg mjpeg = new MJpeg(); AppSettings.LastMJPEGToolsVer = mjpeg.GetVersionInfo(encPath); DvdAuthor dvdauthor = new DvdAuthor(); AppSettings.LastDVDAuthorVer = dvdauthor.GetVersionInfo(encPath); MP4Box mp4Box = new MP4Box(); AppSettings.LastMp4BoxVer = mp4Box.GetVersionInfo(encPath); HcEnc hcenc = new HcEnc(); AppSettings.LastHcEncVer = hcenc.GetVersionInfo(encPath); OggEnc ogg = new OggEnc(); AppSettings.LastOggEncVer = ogg.GetVersionInfo(encPath); NeroAACEnc aac = new NeroAACEnc(); AppSettings.LastNeroAacEncVer = aac.GetVersionInfo(encPath); Lame lame = new Lame(); AppSettings.LastLameVer = lame.GetVersionInfo(encPath); VpxEnc vpxEnc = new VpxEnc(); AppSettings.LastVpxEncVer = vpxEnc.GetVersionInfo(encPath); if (!String.IsNullOrEmpty(javaPath)) { BdSup2SubTool bdSup2Sub = new BdSup2SubTool(); AppSettings.LastBDSup2SubVer = bdSup2Sub.GetVersionInfo(encPath, javaPath); } #region Get AviSynth Version IGraphBuilder graphBuilder = (IGraphBuilder)new FilterGraph(); string avsFile = AviSynthGenerator.GenerateTestFile(); int result = graphBuilder.RenderFile(avsFile, null); Log.DebugFormat("RenderFile Result: {0}", result); if (result < 0) Log.Debug("AviSynth is not installed"); else { FileVersionInfo ver = FileVersionInfo.GetVersionInfo(Path.Combine(Environment.SystemDirectory, "avisynth.dll")); string appVer = String.Format("{0:g}.{1:g}.{2:g}.{3:g}", ver.FileMajorPart, ver.FileMinorPart, ver.FileBuildPart, ver.FilePrivatePart); Log.DebugFormat("Avisynth version {0:s} installed", appVer); AppSettings.LastAviSynthVer = appVer; } File.Delete(avsFile); #endregion GetAviSynthPluginsVer(); GetUpdaterVersion(); AppSettings.UpdateVersions = false; AppSettings.SaveSettings(); }
public void DoEncode(object sender, DoWorkEventArgs e) { _bw = (BackgroundWorker)sender; string passStr = Processing.GetResourceString("x264_pass"); string status = Processing.GetResourceString("x264_encoding_status"); bool use64BitEncoder = AppSettings.Use64BitEncoders && AppSettings.X26464Installed && Environment.Is64BitOperatingSystem; X264Profile encProfile = (X264Profile)_jobInfo.VideoProfile; if (!_jobInfo.EncodingProfile.Deinterlace && _jobInfo.VideoStream.Interlaced) { _jobInfo.VideoStream.Interlaced = false; } Size resizeTo = VideoHelper.GetTargetSize(_jobInfo); if (string.IsNullOrEmpty(_jobInfo.AviSynthScript)) { GenerateAviSynthScript(resizeTo); } string inputFile = _jobInfo.AviSynthScript; string outFile = Processing.CreateTempFile( string.IsNullOrEmpty(_jobInfo.TempOutput) ? _jobInfo.BaseName : _jobInfo.TempOutput, "encoded.264"); int targetBitrate = 0; if (_jobInfo.EncodingProfile.TargetFileSize > 0) { targetBitrate = Processing.CalculateVideoBitrate(_jobInfo); } int encodeMode = encProfile.EncodingMode; if ((encodeMode == 2) || (encodeMode == 3)) { _pass = string.Format(" {1} {0:0}; ", _jobInfo.StreamId, passStr); } _frameCount = _jobInfo.VideoStream.FrameCount; _bw.ReportProgress(-10, status + _pass.Replace("; ", string.Empty)); _bw.ReportProgress(0, status); string argument = X264CommandLineGenerator.Generate(encProfile, targetBitrate, resizeTo.Width, resizeTo.Height, _jobInfo.StreamId, _jobInfo.VideoStream.FrameRateEnumerator, _jobInfo.VideoStream.FrameRateDenominator, _jobInfo.EncodingProfile.StereoType, _jobInfo.VideoStream.PicSize, // check if we use 64 bit version //use64BitEncoder ? "-" : inputFile, "-", outFile); string localExecutable = Path.Combine(AppSettings.ToolsPath, use64BitEncoder ? Executable64 : Executable); using (Process encoder = new Process()) { ProcessStartInfo parameter = new ProcessStartInfo(localExecutable) { WorkingDirectory = AppSettings.DemuxLocation, Arguments = argument, CreateNoWindow = true, UseShellExecute = false, RedirectStandardError = true, RedirectStandardInput = use64BitEncoder }; encoder.StartInfo = parameter; encoder.ErrorDataReceived += OnDataReceived; Log.InfoFormat("start parameter: x264 {0:s}", argument); bool started; bool decStarted; NamedPipeServerStream decodePipe = new NamedPipeServerStream(AppSettings.DecodeNamedPipeName, PipeDirection.InOut, 3, PipeTransmissionMode.Byte, PipeOptions.Asynchronous); decodePipe.BeginWaitForConnection(DecoderConnected, null); Size originalSize = new Size(_jobInfo.VideoStream.Width, _jobInfo.VideoStream.Height); if (_jobInfo.VideoStream.Width < _jobInfo.VideoStream.Height * _jobInfo.VideoStream.AspectRatio) { originalSize.Width = (int)(_jobInfo.VideoStream.Height * _jobInfo.VideoStream.AspectRatio); int temp; Math.DivRem(originalSize.Width, 2, out temp); originalSize.Width += temp; } Process decoder = FfMpeg.GenerateDecodeProcess(inputFile, AppSettings.Use64BitEncoders && AppSettings.UseFfmpegScaling, originalSize, _jobInfo.VideoStream.AspectRatio, _jobInfo.VideoStream.CropRect, resizeTo); try { decStarted = decoder.Start(); } catch (Exception ex) { decStarted = false; Log.ErrorFormat("avconv exception: {0}", ex); _jobInfo.ExitCode = -1; } try { started = encoder.Start(); } catch (Exception ex) { started = false; Log.ErrorFormat("x264 encoder exception: {0}", ex); _jobInfo.ExitCode = -1; } _startTime = DateTime.Now; if (started && decStarted) { encoder.PriorityClass = AppSettings.GetProcessPriority(); encoder.BeginErrorReadLine(); decoder.PriorityClass = AppSettings.GetProcessPriority(); decoder.BeginErrorReadLine(); Thread pipeReadThread = new Thread(() => { try { if (encoder != null) { ReadThreadStart(decodePipe, encoder); } } catch (Exception ex) { Log.Error(ex); } }); pipeReadThread.Start(); pipeReadThread.Priority = ThreadPriority.BelowNormal; decoder.Exited += (o, args) => { try { decodePipe.Disconnect(); decodePipe.Close(); decodePipe.Dispose(); } catch (Exception ex) { Log.Error(ex); } }; encoder.Exited += (o, args) => pipeReadThread.Abort(); while (!encoder.HasExited && !decoder.HasExited) { if (_bw.CancellationPending) { encoder.Kill(); decoder.Kill(); } Thread.Sleep(200); } encoder.WaitForExit(10000); encoder.CancelErrorRead(); decoder.WaitForExit(10000); decoder.CancelErrorRead(); _jobInfo.ExitCode = encoder.ExitCode; Log.InfoFormat("Exit Code: {0:g}", _jobInfo.ExitCode); } } if (_jobInfo.ExitCode == 0) { if ((encProfile.EncodingMode == 2 && _jobInfo.StreamId == 2) || (encProfile.EncodingMode == 3 && _jobInfo.StreamId == 3) || (encProfile.EncodingMode < 2 || _jobInfo.StreamId > 3)) { _jobInfo.VideoStream.Encoded = true; _jobInfo.VideoStream.IsRawStream = true; _jobInfo.TempFiles.Add(_jobInfo.VideoStream.TempFile); _jobInfo.VideoStream.TempFile = outFile; try { _jobInfo.MediaInfo = Processing.GetMediaInfo(_jobInfo.VideoStream.TempFile); } catch (TimeoutException ex) { Log.Error(ex); } _jobInfo.VideoStream = VideoHelper.GetStreamInfo(_jobInfo.MediaInfo, _jobInfo.VideoStream, _jobInfo.EncodingProfile.OutFormat == OutputType.OutputBluRay); _jobInfo.TempFiles.Add(Path.Combine(AppSettings.DemuxLocation, "x264_2pass.log")); _jobInfo.TempFiles.Add(Path.Combine(AppSettings.DemuxLocation, "x264_2pass.log.mbtree")); _jobInfo.TempFiles.Add(_jobInfo.AviSynthScript); _jobInfo.TempFiles.Add(_jobInfo.FfIndexFile); _jobInfo.TempFiles.Add(_jobInfo.AviSynthStereoConfig); } } _bw.ReportProgress(100); _jobInfo.CompletedStep = _jobInfo.NextStep; e.Result = _jobInfo; }