我正在使用mencoder
来分割文件,如果可能的话,例如使用Java或类似方法,我想将其转换为面向对象的方法。但我不确定最好的方法,所以我把它放在公开的地方。这里是我需要的:反复调用终端命令的最佳方式
我有一个Excel文件的开始时间和结束时间,我需要从视频文件中提取出适当的剪辑。在终端(我在Mac OS X)我一直在使用,比如取得了成功:
mencoder -ss 0 -endpos 10 MyVideo.avi -oac copy -ovc copy -o Output.avi
其中由裁剪视频MyVideo.avi的第一个10秒创建视频Output.avi。
但是,正如我所说的,我想让程序从excel文件读入,并且对于每个开始时间和结束时间多次调用这个mencoder命令(超过100次)。
我知道如何读取Java中的excel文件,但我不确定最好从Java调用此命令。另外,我希望能够看到mencoder
的输出(因为它输出了一个很好的百分比,所以你知道单个命令需要多长时间)。这种类型的事情在shell脚本中可行吗?如果可能的话,我会really
喜欢使用Java,因为我有多年的Java经验并且没有shell脚本经验。
UPDATE
以下是我在Java中已经试过,但它在in.readLine()
File wd = new File("/bin");
System.out.println(wd);
Process proc = null;
try {
proc = Runtime.getRuntime().exec("/bin/bash", null, wd);
}
catch (IOException e) {
e.printStackTrace();
}
if (proc != null) {
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
out.println("cd ..");
out.println("pwd");
String video = "/Users/MyFolder/MyFile.avi";
String output = "/Users/MyFolder/output.avi";
int start = 0;
int end = 6;
String cmd = "mencoder -ss " + start +
" -endpos " + end +
" " + video + " -oac copy -ovc copy -o " + output;
out.println(cmd);
try {
String line;
System.out.println("top");
while ((line = in.readLine()) != null) {
System.out.println(line);
}
System.out.println("end");
proc.waitFor();
in.close();
out.close();
proc.destroy();
}
catch (Exception e) {
e.printStackTrace();
}
}
哦,但进程不足以完成此任务吗? – giorashc 2013-02-16 21:34:41
我在Java中没有取得成功。 – CodeGuy 2013-02-16 21:35:01
嗨@CodeGuy为什么你使用输入流而不是输出流?我的意思是你想看到输出和输入流将等待输入,所以我认为这就是为什么你的循环永远不会结束 – giorashc 2013-02-16 21:38:34