C# “错误”;2输入指定的名称,请检查用法“;将视频文件保存到所需路径时

C# “错误”;2输入指定的名称,请检查用法“;将视频文件保存到所需路径时,c#,C#,加载mp4文件并选择开始和结束时间后,单击“剪切+保存”按钮将抛出错误: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.T

加载mp4文件并选择开始和结束时间后,单击“剪切+保存”按钮将抛出错误:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
using WMPLib;

namespace Mp4BoxSplitter {
  public partial class Form1: Form {
    public Form1() {
      InitializeComponent();
    }

    private void BOpen_Click(object sender, EventArgs e) {
      if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
        tFileName.Text = openFileDialog1.FileName;
        this.axWindowsMediaPlayer1.URL = tFileName.Text;
      }
    }

    private void BStartTime_Click(object sender, EventArgs e) {
      tStartTime.Text = axWindowsMediaPlayer1.Ctlcontrols.currentPosition.ToString("0.##");
    }

    private void BEndTime_Click(object sender, EventArgs e) {
      tEndTime.Text = axWindowsMediaPlayer1.Ctlcontrols.currentPosition.ToString("0.##");
    }

    private void BCutSave_Click(object sender2, EventArgs e) {
      axWindowsMediaPlayer1.Ctlcontrols.stop();

      //pokličem mp4box.exe s parametri                        
      Process p = new Process();
      p.StartInfo.FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "mp4box", @ "mp4box.exe");

      string sStartTime = decimal.Parse(tStartTime.Text).ToString("0.##").Replace(",", ".");
      string sEndTime = decimal.Parse(tEndTime.Text).ToString("0.##").Replace(",", ".");

      string inFileName = tFileName.Text;
      string outFileName = inFileName.Substring(0, inFileName.LastIndexOf(".")) + "_" + sStartTime + "-" + sEndTime + inFileName.Substring(inFileName.LastIndexOf("."));

      string @params = "-splitx " + sStartTime + ":" + sEndTime + " " + inFileName + " -out " + outFileName;

      Debug.WriteLine(p.StartInfo.FileName + " " + @params);

      p.StartInfo.Arguments = @params;

      var sb = new StringBuilder();
      sb.AppendLine("mp4box.exe results:");
      // redirect the output
      p.StartInfo.RedirectStandardOutput = true;
      p.StartInfo.RedirectStandardError = true;
      // hookup the eventhandlers to capture the data that is received
      p.OutputDataReceived += (sender, args) => {
        if (args.Data != null && !args.Data.StartsWith("Splitting:") && !args.Data.StartsWith("ISO File Writing:")) {
          sb.AppendLine(args.Data);
        }
      };
      p.ErrorDataReceived += (sender, args) => {
        if (args.Data != null && !args.Data.StartsWith("Splitting:") && !args.Data.StartsWith("ISO File Writing:")) {
          sb.AppendLine(args.Data);
        }
      };

      // direct start
      p.StartInfo.UseShellExecute = false;

      p.Start();
      // start our event pumps
      p.BeginOutputReadLine();
      p.BeginErrorReadLine();

      // until we are done
      p.WaitForExit();
      tResult.Text = sb.ToString();
    }

    private void TFileName_KeyDown(object sender, KeyEventArgs e) {
      if (e.KeyCode == Keys.Enter) {
        this.axWindowsMediaPlayer1.URL = tFileName.Text;
      }
    }

    private void TFileName_DragEnter(object sender, DragEventArgs e) {
      if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
        e.Effect = DragDropEffects.Copy;
      } else {
        e.Effect = DragDropEffects.None;
      }
    }

    private void TFileName_DragDrop(object sender, DragEventArgs e) {
      if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
        string[] files = (string[]) e.Data.GetData(DataFormats.FileDrop);
        tFileName.Text = files[0];
        this.axWindowsMediaPlayer1.URL = tFileName.Text;
      }
    }

    private void BMinus1Sec_Click(object sender, EventArgs e) {
      axWindowsMediaPlayer1.Ctlcontrols.currentPosition -= 1;
      axWindowsMediaPlayer1.Refresh();
    }

    private void BMinus5Sec_Click(object sender, EventArgs e) {
      axWindowsMediaPlayer1.Ctlcontrols.currentPosition -= 5;
      axWindowsMediaPlayer1.Refresh();
    }

    private void BMinus10s_Click(object sender, EventArgs e) {
      axWindowsMediaPlayer1.Ctlcontrols.currentPosition -= 10;
      axWindowsMediaPlayer1.Refresh();
    }

    private void BToStart_Click(object sender, EventArgs e) {
      axWindowsMediaPlayer1.Ctlcontrols.currentPosition = 0;
      axWindowsMediaPlayer1.Refresh();
    }

    private void BPlus1Sec_Click(object sender, EventArgs e) {
      axWindowsMediaPlayer1.Ctlcontrols.currentPosition += 1;
      axWindowsMediaPlayer1.Refresh();
    }

    private void BPlus5Sec_Click(object sender, EventArgs e) {
      axWindowsMediaPlayer1.Ctlcontrols.currentPosition += 5;
      axWindowsMediaPlayer1.Refresh();
    }

    private void BPlus10Sec_Click(object sender, EventArgs e) {
      axWindowsMediaPlayer1.Ctlcontrols.currentPosition += 10;
      axWindowsMediaPlayer1.Refresh();
    }

    private void BToEnd_Click(object sender, EventArgs e) {
      axWindowsMediaPlayer1.Ctlcontrols.currentPosition = axWindowsMediaPlayer1.currentMedia.duration;
      axWindowsMediaPlayer1.Refresh();
    }

    private void BMinusDot5_Click(object sender, EventArgs e) {
      axWindowsMediaPlayer1.Ctlcontrols.currentPosition -= 0.5;
      axWindowsMediaPlayer1.Refresh();
    }

    private void BMinusDot2_Click(object sender, EventArgs e) {
      axWindowsMediaPlayer1.Ctlcontrols.currentPosition -= 0.2;
      axWindowsMediaPlayer1.Refresh();
    }

    private void BPlusDot5_Click(object sender, EventArgs e) {
      axWindowsMediaPlayer1.Ctlcontrols.currentPosition += 0.5;
      axWindowsMediaPlayer1.Refresh();
    }

    private void BPlusDot2_Click(object sender, EventArgs e) {
      axWindowsMediaPlayer1.Ctlcontrols.currentPosition += 0.2;
      axWindowsMediaPlayer1.Refresh();

    }

    private void BPlusFrame_Click(object sender, EventArgs e) {
      ((IWMPControls2) axWindowsMediaPlayer1.Ctlcontrols).step(1);
      axWindowsMediaPlayer1.Refresh();
    }

    private void Button1_Click(object sender, EventArgs e) {
      axWindowsMediaPlayer1.Ctlcontrols.pause();
    }

    private void BPlayPart_Click(object sender, EventArgs e) {
      axWindowsMediaPlayer1.Ctlcontrols.currentPosition = double.Parse(tStartTime.Text);
      double dInterval = (double.Parse(tEndTime.Text) - double.Parse(tStartTime.Text)) * 1000;

      System.Timers.Timer aTimer = new System.Timers.Timer();
      aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
      aTimer.Interval = dInterval;

      axWindowsMediaPlayer1.Ctlcontrols.play();
      aTimer.Enabled = true;

    }
    private void OnTimedEvent(object sender, ElapsedEventArgs e) {
      axWindowsMediaPlayer1.Ctlcontrols.pause();
      var timer = (System.Timers.Timer) sender;
      timer.Dispose();
    }

    private void BExplore_Click(object sender, EventArgs e) {
      string sFolderPath = Path.GetDirectoryName(tFileName.Text);

      ProcessStartInfo startInfo = new ProcessStartInfo {
        Arguments = sFolderPath,
          FileName = "explorer.exe"
      };
      Process.Start(startInfo);
    }
  }
}
它不会进入生产线:

private void BCutSave_Click(object sender2, EventArgs e) {
  axWindowsMediaPlayer1.Ctlcontrols.stop();

  //pokličem mp4box.exe s parametri                        
  Process p = new Process();
  p.StartInfo.FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "mp4box", @ "mp4box.exe");

  string sStartTime = decimal.Parse(tStartTime.Text).ToString("0.##").Replace(",", ".");
  string sEndTime = decimal.Parse(tEndTime.Text).ToString("0.##").Replace(",", ".");

  string inFileName = tFileName.Text;
  string outFileName = inFileName.Substring(0, inFileName.LastIndexOf(".")) + "_" + sStartTime + "-" + sEndTime + inFileName.Substring(inFileName.LastIndexOf("."));

  string @params = "-splitx " + sStartTime + ":" + sEndTime + " " + inFileName + " -out " + outFileName;

  Debug.WriteLine(p.StartInfo.FileName + " " + @params);

  p.StartInfo.Arguments = @params;

  var sb = new StringBuilder();
  sb.AppendLine("mp4box.exe results:");
  // redirect the output
  p.StartInfo.RedirectStandardOutput = true;
  p.StartInfo.RedirectStandardError = true;
  // hookup the eventhandlers to capture the data that is received
  p.OutputDataReceived += (sender, args) => {
    if (args.Data != null && !args.Data.StartsWith("Splitting:") && !args.Data.StartsWith("ISO File Writing:")) {
      sb.AppendLine(args.Data);
    }
  };
  p.ErrorDataReceived += (sender, args) => {
    if (args.Data != null && !args.Data.StartsWith("Splitting:") && !args.Data.StartsWith("ISO File Writing:")) {
      sb.AppendLine(args.Data);
    }
  };

  // direct start
  p.StartInfo.UseShellExecute = false;

  p.Start();
  // start our event pumps
  p.BeginOutputReadLine();
  p.BeginErrorReadLine();

  // until we are done
  p.WaitForExit();
  tResult.Text = sb.ToString();
}
但会跳到台词上:

sb.AppendLine(args.Data);
错误是:

p.ErrorDataReceived += (sender, args) => {
    if (args.Data != null && !args.Data.StartsWith("Splitting:") && !args.Data.StartsWith("ISO File Writing:")) {
        sb.AppendLine(args.Data);
    }
};

也许您的
tFileName.Text
中有空格,您最终生成了一个命令行,如
mp4box.exe-splitx 1:2 C:\program files\some\file.ext-out blah.mp4
和mp4box认为您的两个输入文件是
C:\program
files\some\file.ext

要解决从C#运行另一个命令的问题,该过程通常是:

  • 使用调试器准确地找出生成字符串的参数-在
    var sb=new StringBuilder()上放置断点
    并查看Locals或Autos面板中
    p.StartInfo.Arguments
    的值
  • 复制该字符串(右键单击并选择“复制值”)
  • 直接在命令提示符下运行它(将其粘贴到mp4box.exe的路径后)
  • 修复任何问题(例如,在任何带空格的路径周围添加
    ),以便mp4box在命令提示符下成功运行,然后将您在命令行上所做的修复传输到代码中,例如:

ps;使用字符串插值。对于这类事情,它比串联可读性强得多

总是说哪行代码引发异常
mp4box.exe results:
Error - 2 input names specified, please check usage
     string @params = $"-splitx {sStartTime}:{sEndTime} \"{inFileName}\" -out \"{outFileName}\"";