private static void Getx265Line(FileConfig fileConfig, int pass, out string x264Line, out string outputpath) { VedioConfig vedioConfig = fileConfig.VedioConfig; x264Line = x265Args; x264Line = x264Line.Replace("$preset$", vedioConfig.preset); if (string.IsNullOrEmpty(vedioConfig.tune)) { x264Line = x264Line.Replace("$tune$", ""); } else { x264Line = x264Line.Replace("$tune$", "--tune " + vedioConfig.tune); } if (vedioConfig.BitType == EncoderBitrateType.crf) { x264Line = x264Line.Replace("$crf$", "--crf " + vedioConfig.crf.ToString()); } else { string twopassstr = "--pass " + pass + " --bitrate " + vedioConfig.bitrate.ToString(); x264ArgsManager manager = new x264ArgsManager(x264Line); x264Line = x264Line.Replace("$crf$", twopassstr); } x264Line = x264Line.Replace("$profile$", ""); outputpath = string.Empty; string fileExtension = "." + fileConfig.Muxer; string inputArg = ""; //if (fileConfig.AudioConfig.CopyStream || !fileConfig.AudioConfig.Enabled) //{ // outputpath = fileConfig.OutputFile + fileExtension; //} //else //{//临时目录 outputpath = FileUtility.AppendRandomName(Config.Temp, Path.GetFileNameWithoutExtension(fileConfig.VedioFileFullName) + ".h265"); //} if (fileConfig.InputType == InputType.AvisynthScriptFile) { x264Line = x264Line.Replace("$input$", fileConfig.AvsFileFullName.Maohao()); } else if (fileConfig.InputType == InputType.AvisynthScript || fileConfig.InputType == InputType.VapoursynthScriptFile) { x264Line = x264Line.Replace("$input$", ""); } else { if (fileConfig.UseBat) { x264Line = x264Line.Replace("$input$", ""); inputArg = " --input - --y4m "; } else { x264Line = x264Line.Replace("$input$", "--input " + fileConfig.VedioFileFullName.Maohao()); } } x264Line = x264Line.Replace("$outputfile$", outputpath); if (fileConfig.UseBat) { x264Line = x264Line.Replace("$userargs$", vedioConfig.UserArgs + inputArg); } else { x264Line = x264Line.Replace("$userargs$", vedioConfig.UserArgs); } }
private void StartOneThread() { int isHandling = -1; while (true) { int thisstate = -1; ListViewItem item = null; FileConfig fileConfig = null; try { lock (handledlock) { if (isHandling >= listView2.Items.Count - 1) { return; } hasHandle++; if (hasHandle > listView2.Items.Count - 1) { return; } isHandling = hasHandle; this.Invoke((Action) delegate() { item = listView2.Items[isHandling]; fileConfig = item.Tag as FileConfig; if (fileConfig.state != -1) { thisstate = -10; } item.SubItems["States"].Text = "视频转码中"; fileConfig.state++; }); if (thisstate == -10) { continue; } this.Invoke((Action) delegate() { if (fileConfig.InputType == InputType.AvisynthScriptFile || fileConfig.InputType == InputType.AvisynthScript) { EncoderTaskInfoForm form = new EncoderTaskInfoForm(); form.fileConfig = fileConfig; form.lbFile.Text = fileConfig.FullName; form.Text = fileConfig.FullName; fileConfig.EncoderTaskInfo.infoForm = form; form.Show(); } }); } string outputfile = ""; string copyto = string.Empty; string ralative = string.Empty; //仅输出视频部分 if (!Directory.Exists(fileConfig.OutputPath)) { Directory.CreateDirectory(fileConfig.OutputPath); } if (fileConfig.KeepDirection) {//保持目录树结构 ralative = FileUtility.MakeRelativePath(fileConfig.DirPath + "/", Path.GetDirectoryName(fileConfig.FullName)); string outpath = Path.Combine(fileConfig.OutputPath, ralative); if (!Directory.Exists(outpath)) { Directory.CreateDirectory(outpath); } outputfile = Path.Combine(outpath, Path.GetFileNameWithoutExtension(fileConfig.FullName)); } else if (fileConfig.OutputPath != "") {//有输出目录 outputfile = Path.Combine(fileConfig.OutputPath, Path.GetFileNameWithoutExtension(fileConfig.FullName)); } else {//输出原路径 outputfile = Path.Combine(Path.GetDirectoryName(fileConfig.FullName), Path.GetFileNameWithoutExtension(fileConfig.FullName)); } if (fileConfig.CompleteDo && !string.IsNullOrEmpty(fileConfig.CompleteActionDir)) { if (fileConfig.KeepDirection) {//保持目录树结构 copyto = Path.Combine(fileConfig.CompleteActionDir, ralative, Path.GetFileNameWithoutExtension(fileConfig.FullName)); } else { copyto = Path.Combine(fileConfig.CompleteActionDir, Path.GetFileNameWithoutExtension(fileConfig.FullName)); } } else { fileConfig.CompleteDo = false; } fileConfig.OutputFile = outputfile; VedioConfig vedioconfig = fileConfig.VedioConfig; string vedioOutputFile = string.Empty; //vedioOutputFile = "D:\\temp\\" + Path.GetFileNameWithoutExtension(fileConfig.VedioFileFullName) + ".h265"; try { if (vedioconfig.Encoder == Encoder.x264) { if (fileConfig.InputType == InputType.Vedio) { fileConfig.mediaInfo = new MediaInfo(fileConfig.FullName); vedioOutputFile = X264Command.RunX264Command(fileConfig); } else if (fileConfig.InputType == InputType.AvisynthScriptFile) { vedioOutputFile = X264Command.RunAvsx264mod(fileConfig); } else if (fileConfig.InputType == InputType.AvisynthScript) { fileConfig.mediaInfo = new MediaInfo(fileConfig.FullName); string avsfilename = Path.Combine(Path.GetTempPath(), Path.ChangeExtension(Path.GetRandomFileName(), ".avs")); File.WriteAllText(avsfilename, vedioconfig.AvsScript, System.Text.Encoding.Default); fileConfig.AvsFileFullName = avsfilename; fileConfig.InputType = InputType.AvisynthScriptFile; vedioOutputFile = X264Command.RunAvsx264mod(fileConfig); } else if (fileConfig.InputType == InputType.VapoursynthScriptFile) { vedioOutputFile = X264Command.RunVSx265(fileConfig); } else if (fileConfig.InputType == InputType.VapoursynthScript) { fileConfig.mediaInfo = new MediaInfo(fileConfig.FullName); string avsfilename = Path.Combine(Config.Temp, Path.ChangeExtension(Path.GetRandomFileName(), ".vpy")); File.WriteAllText(avsfilename, vedioconfig.VapoursynthScript, System.Text.Encoding.UTF8); fileConfig.VapoursynthFileFullName = avsfilename; fileConfig.InputType = InputType.VapoursynthScriptFile; vedioOutputFile = X264Command.RunVSx265(fileConfig); } } else if (vedioconfig.Encoder == Encoder.x265) { if (fileConfig.InputType == InputType.Vedio) { fileConfig.mediaInfo = new MediaInfo(fileConfig.FullName); if (fileConfig.VedioConfig.decoderMode == DecoderMode.defaultStr) { fileConfig.VedioConfig.decoderMode = DecoderMode.pipe; } if (fileConfig.VedioConfig.decoderMode == DecoderMode.pipe) { vedioOutputFile = X265Command.ffmpegPipeX265(fileConfig); } else { vedioOutputFile = X265Command.RunX265Command(fileConfig); } } else if (fileConfig.InputType == InputType.AvisynthScriptFile) { vedioOutputFile = X265Command.RunAvsx264mod(fileConfig); } else if (fileConfig.InputType == InputType.AvisynthScript) { fileConfig.mediaInfo = new MediaInfo(fileConfig.FullName); string avsfilename = Path.Combine(Config.Temp, Path.ChangeExtension(Path.GetRandomFileName(), ".avs")); File.WriteAllText(avsfilename, vedioconfig.AvsScript, System.Text.Encoding.Default); fileConfig.AvsFileFullName = avsfilename; fileConfig.InputType = InputType.AvisynthScriptFile; vedioOutputFile = X265Command.RunAvsx264mod(fileConfig); } else if (fileConfig.InputType == InputType.VapoursynthScriptFile) { vedioOutputFile = X265Command.RunVSx265(fileConfig); } else if (fileConfig.InputType == InputType.VapoursynthScript) { fileConfig.mediaInfo = new MediaInfo(fileConfig.FullName); string avsfilename = Path.Combine(Config.Temp, Path.ChangeExtension(Path.GetRandomFileName(), ".vpy")); File.WriteAllText(avsfilename, vedioconfig.VapoursynthScript, System.Text.Encoding.UTF8); fileConfig.VapoursynthFileFullName = avsfilename; fileConfig.InputType = InputType.VapoursynthScriptFile; vedioOutputFile = X265Command.RunVSx265(fileConfig); } } else if (vedioconfig.Encoder == Encoder.NvEnc_H265 || vedioconfig.Encoder == Encoder.NvEnc_H264) { if (fileConfig.InputType == InputType.Vedio) { fileConfig.mediaInfo = new MediaInfo(fileConfig.FullName); if (fileConfig.VedioConfig.decoderMode == DecoderMode.defaultStr) { fileConfig.VedioConfig.decoderMode = DecoderMode.self; } if (fileConfig.VedioConfig.decoderMode == DecoderMode.self) { vedioOutputFile = NvEncCommand.NvEncSelf(fileConfig); } else { vedioOutputFile = NvEncCommand.ffmpegPipeNvEnc(fileConfig); } } else if (fileConfig.InputType == InputType.VapoursynthScript) { fileConfig.mediaInfo = new MediaInfo(fileConfig.FullName); string avsfilename = Path.Combine(Config.Temp, Path.ChangeExtension(Path.GetRandomFileName(), ".vpy")); File.WriteAllText(avsfilename, vedioconfig.VapoursynthScript, System.Text.Encoding.UTF8); fileConfig.VapoursynthFileFullName = avsfilename; fileConfig.InputType = InputType.VapoursynthScriptFile; if (fileConfig.VedioConfig.decoderMode == DecoderMode.defaultStr) { fileConfig.VedioConfig.decoderMode = DecoderMode.self; } if (fileConfig.VedioConfig.decoderMode == DecoderMode.self) { vedioOutputFile = NvEncCommand.NvEncUseVs(fileConfig); } else { vedioOutputFile = NvEncCommand.VspipeNvEnc(fileConfig); } } else if (fileConfig.InputType == InputType.AvisynthScript) { fileConfig.mediaInfo = new MediaInfo(fileConfig.FullName); string avsfilename = Path.Combine(Config.Temp, Path.ChangeExtension(Path.GetRandomFileName(), ".avs")); File.WriteAllText(avsfilename, vedioconfig.AvsScript, System.Text.Encoding.Default); fileConfig.AvsFileFullName = avsfilename; fileConfig.InputType = InputType.AvisynthScriptFile; vedioOutputFile = NvEncCommand.NvEncUseVs(fileConfig); } else if (fileConfig.InputType == InputType.VapoursynthScriptFile) { if (fileConfig.VedioConfig.decoderMode == DecoderMode.defaultStr) { fileConfig.VedioConfig.decoderMode = DecoderMode.self; } if (fileConfig.VedioConfig.decoderMode == DecoderMode.self) { vedioOutputFile = NvEncCommand.NvEncUseVs(fileConfig); } else { vedioOutputFile = NvEncCommand.VspipeNvEnc(fileConfig); } } else if (fileConfig.InputType == InputType.AvisynthScriptFile) { vedioOutputFile = NvEncCommand.NvEncUseVs(fileConfig); } } else if (vedioconfig.Encoder == Encoder.QSVEnc_H265 || vedioconfig.Encoder == Encoder.QSVEnc_H264) { if (fileConfig.InputType == InputType.Vedio) { fileConfig.mediaInfo = new MediaInfo(fileConfig.FullName); if (fileConfig.VedioConfig.decoderMode == DecoderMode.defaultStr) { fileConfig.VedioConfig.decoderMode = DecoderMode.self; } if (fileConfig.VedioConfig.decoderMode == DecoderMode.self) { vedioOutputFile = QSVEncCommand.QSVEncSelf(fileConfig); } else { vedioOutputFile = QSVEncCommand.ffmpegPipeQSVEnc(fileConfig); } } else if (fileConfig.InputType == InputType.VapoursynthScript) { fileConfig.mediaInfo = new MediaInfo(fileConfig.FullName); string avsfilename = Path.Combine(Config.Temp, Path.ChangeExtension(Path.GetRandomFileName(), ".vpy")); File.WriteAllText(avsfilename, vedioconfig.VapoursynthScript, System.Text.Encoding.UTF8); fileConfig.VapoursynthFileFullName = avsfilename; fileConfig.InputType = InputType.VapoursynthScriptFile; if (fileConfig.VedioConfig.decoderMode == DecoderMode.defaultStr) { fileConfig.VedioConfig.decoderMode = DecoderMode.self; } if (fileConfig.VedioConfig.decoderMode == DecoderMode.defaultStr || fileConfig.VedioConfig.decoderMode == DecoderMode.self) { vedioOutputFile = QSVEncCommand.QSVEncUseVs(fileConfig); } else { vedioOutputFile = QSVEncCommand.VspipeQSVEnc(fileConfig); } } else if (fileConfig.InputType == InputType.AvisynthScript) { fileConfig.mediaInfo = new MediaInfo(fileConfig.FullName); string avsfilename = Path.Combine(Config.Temp, Path.ChangeExtension(Path.GetRandomFileName(), ".avs")); File.WriteAllText(avsfilename, vedioconfig.AvsScript, System.Text.Encoding.Default); fileConfig.AvsFileFullName = avsfilename; fileConfig.InputType = InputType.AvisynthScriptFile; vedioOutputFile = QSVEncCommand.QSVEncUseVs(fileConfig); } else if (fileConfig.InputType == InputType.VapoursynthScriptFile) { if (fileConfig.VedioConfig.decoderMode == DecoderMode.defaultStr) { fileConfig.VedioConfig.decoderMode = DecoderMode.self; } if (fileConfig.VedioConfig.decoderMode == DecoderMode.defaultStr || fileConfig.VedioConfig.decoderMode == DecoderMode.self) { vedioOutputFile = QSVEncCommand.QSVEncUseVs(fileConfig); } else { vedioOutputFile = QSVEncCommand.VspipeQSVEnc(fileConfig); } } else if (fileConfig.InputType == InputType.AvisynthScriptFile) { vedioOutputFile = QSVEncCommand.QSVEncUseVs(fileConfig); } } } catch (EncoderException e) { this.Invoke((Action) delegate() { item.SubItems["States"].Text = e.Message; }); fileConfig.state = -10; continue; } if (!File.Exists(vedioOutputFile)) { this.Invoke((Action) delegate() { item.SubItems["States"].Text = "视频编码失败"; }); fileConfig.state = -10; continue; } int delay = 0; string audiofile = string.Empty; if (fileConfig.AudioConfig.Enabled && fileConfig.state != -10) { //if (fileConfig.InputType == InputType.Vedio && fileConfig.AudioConfig.CopyStream && fileConfig.VedioConfig.Encoder == Encoder.x264) //{ // //直接由x264处理掉 //} //else { if (isHandling >= listView2.Items.Count) { return; } this.Invoke((Action) delegate() { item = listView2.Items[isHandling]; item.SubItems["States"].Text = "音频转码中"; }); if (!fileConfig.AudioConfig.Enabled) { } else if (fileConfig.AudioConfig.CopyStream) { audiofile = CommandHelper.DemuxAudio(fileConfig); } else { if (fileConfig.AudioConfig.UseEac3to) { if (fileConfig.AudioConfig.Encoder == AudioEncoder.aac) { audiofile = Eac3toCommand.ConvertMusic(fileConfig); } else if (fileConfig.AudioConfig.Encoder == AudioEncoder.opus) { audiofile = Eac3toCommand.ConvertAudioTOpus(fileConfig); } else if (fileConfig.AudioConfig.Encoder == AudioEncoder.flac) { audiofile = Eac3toCommand.ConvertAudioToFlac(fileConfig); } } else { if (fileConfig.AudioConfig.Encoder == AudioEncoder.aac) { audiofile = CommandHelper.RunFFmpegToAAC(fileConfig); } else if (fileConfig.AudioConfig.Encoder == AudioEncoder.opus) { audiofile = CommandHelper.RunFFmpegToOpus(fileConfig); } else if (fileConfig.AudioConfig.Encoder == AudioEncoder.flac) { audiofile = FFmpegCommand.RunFFmpegToFlac(fileConfig); } } } if (isHandling >= listView2.Items.Count) { return; } this.Invoke((Action) delegate() { item.SubItems["States"].Text = "封装中"; }); if (fileConfig.mediaInfo != null) { delay = fileConfig.mediaInfo.DelayRelativeToVideo; //delay = delay - 67; } } } if (fileConfig.state != -10) { if (fileConfig.Muxer == "mkv") { vedioOutputFile = CommandHelper.MKVmergin(fileConfig, vedioOutputFile, audiofile, delay); } else if (fileConfig.Muxer == "mp4") { vedioOutputFile = CommandHelper.mp4box(fileConfig, vedioOutputFile, audiofile, delay); } else if (fileConfig.Muxer == "flv") { vedioOutputFile = CommandHelper.ffmpegmux(fileConfig, vedioOutputFile, audiofile, fileConfig.Muxer); if (fileConfig.sinablack) { FlvMain flvbugger = new FlvMain(); flvbugger.addFile(vedioOutputFile); flvbugger.ExecuteBlack(999d, -1, Path.ChangeExtension(vedioOutputFile, ".black.flv")); } else if (fileConfig.sinaPreblack) { FlvMain flvbugger = new FlvMain(); flvbugger.addFile(vedioOutputFile); flvbugger.ExecuteTime(999d, -1, Path.ChangeExtension(vedioOutputFile, ".speed.flv")); } } } if (fileConfig.CompleteDo && fileConfig.state != -10) { try { copyto = copyto + Path.GetExtension(vedioOutputFile); copyto = FileUtility.GetNoSameNameFile(copyto); if (fileConfig.CompleteAction == "拷贝到") { this.Invoke((Action) delegate() { item.SubItems["States"].Text = "拷贝中"; }); File.Copy(vedioOutputFile, copyto, true); } else if (fileConfig.CompleteAction == "剪切到") { this.Invoke((Action) delegate() { item.SubItems["States"].Text = "剪切中"; }); File.Move(vedioOutputFile, copyto); } } catch { } } this.Invoke((Action) delegate() { if (fileConfig.state == -10) { item.SubItems["States"].Text = "失败"; } else { item.SubItems["States"].Text = "完成"; } }); } catch (Exception ex) { this.Invoke((Action) delegate() { item.SubItems["States"].Text = "失败:" + ex.Message; }); } } }
public static string RunAvsx264mod(FileConfig fileConfig) { VedioConfig vedioConfig = fileConfig.VedioConfig; ProcessStartInfo processinfo = new ProcessStartInfo(); processinfo.UseShellExecute = false; //输出信息重定向 processinfo.CreateNoWindow = true; processinfo.RedirectStandardInput = true; processinfo.RedirectStandardOutput = true; processinfo.RedirectStandardError = true; processinfo.WindowStyle = ProcessWindowStyle.Hidden; processinfo.FileName = Path.Combine(Application.StartupPath, avs4x265); var finalX265Path = ""; if (!File.Exists(processinfo.FileName)) { throw new EncoderException("找不到指定程序:" + processinfo.FileName); } finalX265Path = GetX265LiteFullName(vedioConfig); string x264Line; string outputpath = ""; Process avsx264mod = new Process(); if (vedioConfig.BitType == EncoderBitrateType.crf) { Getx265Line(fileConfig, 0, out x264Line, out outputpath); string avsx264modarg = string.Format("--x265-binary \"{0}\" ", Path.GetFileName(finalX265Path)); processinfo.Arguments = avsx264modarg + x264Line; avsx264mod.StartInfo = processinfo; OutputToText(fileConfig, avsx264mod); avsx264mod.Start(); avsx264mod.BeginOutputReadLine(); avsx264mod.BeginErrorReadLine(); avsx264mod.WaitForExit(); } else if (vedioConfig.BitType == EncoderBitrateType.twopass) { Getx265Line(fileConfig, 1, out x264Line, out outputpath); string avsx264modarg = string.Format("--x265-binary \"{0}\" ", finalX265Path); processinfo.Arguments = avsx264modarg + x264Line; avsx264mod.StartInfo = processinfo; OutputToText(fileConfig, avsx264mod); avsx264mod.Start(); avsx264mod.BeginOutputReadLine(); avsx264mod.BeginErrorReadLine(); avsx264mod.WaitForExit(); Getx265Line(fileConfig, 2, out x264Line, out outputpath); avsx264modarg = string.Format("--x265-binary \"{0}\" ", finalX265Path); processinfo.Arguments = avsx264modarg + x264Line; avsx264mod.StartInfo = processinfo; OutputToText(fileConfig, avsx264mod); avsx264mod.Start(); avsx264mod.BeginOutputReadLine(); avsx264mod.BeginErrorReadLine(); avsx264mod.WaitForExit(); } avsx264mod.Dispose(); return(outputpath); }