我有几百集动漫,我希望将其中的字幕和视频文件合并为一个。我决定编写一个简短的Java程序来遍历文件并合并它们。视频文件名为Bleach1.mkv,字幕为Bleach1.srt。为了避免空间的任何潜在问题,文件被保存在C驱动器的根目录下。我写的代码如下。
根据mkvmerge的文档,相应的命令行调用将为“ mkvmerge -o remux_Bleach1.mkv Bleach1.mkv Bleach1.srt”。我已经确认,这实际上是可行的。目前,我正在从其他文件夹运行Java程序,因此我为每个文件使用了绝对路径。
我的代码中的命令输出为“ [C:\ Program Files \ MKVToolNix \ mkvmerge.exe,-o C:\ Bleach \ remux_Bleach_1.mkv C:\ Bleach \ Bleach_1.mkv C:\ Bleach \ Bleach_1 .srt]“
我从mkvmerge返回的错误消息是“错误:未指定目标文件名。”
我不完全了解使用ProcessBuilder时如何传递参数-我在做什么错?
import org.apache.commons.io.FilenameUtils;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Main {
private static String mkvmergePath = "C:\\Program Files\\MKVToolNix\\mkvmerge.exe";
public static void mergeVideoAndSubtitles(final File folder, String videoExtension, String subtitleExtension) {
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry, videoExtension, subtitleExtension);
// for each mkv file that is found
} else if (FilenameUtils.isExtension(fileEntry.getName(), videoExtension)) {
String parentFolderPath = fileEntry.getParentFile().getPath();
String baseName = FilenameUtils.getBaseName(fileEntry.getName());
String outputFileAbsolutePath = parentFolderPath + "\\remux_" + fileEntry.getName();
String inputVideoAbsolutePath = fileEntry.getAbsolutePath();
String inputSubtitleAbsolutePath = parentFolderPath + "\\" + baseName + "." + subtitleExtension;
String param1 = "-o " + outputFileAbsolutePath + " " + inputVideoAbsolutePath + " " +
inputSubtitleAbsolutePath;
// String param2 = "--default-track \"und\"";
// String param3 = "--language 0:und " + baseName + "." + subtitleExtension + "\"";
// System.out.println(param1);
// System.out.println(param2);
// System.out.println(param3);
BufferedReader br = null;
String line;
try {
List<String> list = new ArrayList<String>();
list.add(mkvmergePath);
list.add(param1);
ProcessBuilder build = new ProcessBuilder(list);
System.out.println(build.command());
Process process = build.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
final File folder = new File("C:\\Bleach");
mergeVideoAndSubtitles(folder, "mkv", "srt");
}
}
答案 0 :(得分:0)
我在Go中编写了类似的程序,并遇到了相同的错误。 显然参数中的空格(例如--language 0:eng)会导致此问题。我试图将它们分成单独的参数,但似乎可行:
public static void main(String[] args) throws IOException {
String mkvmerge = "C:\\Program Files\\MKVToolNix\\mkvmerge.exe";
List<String> command = new ArrayList<String>();
command.add(mkvmerge);
command.add("--ui-language");
command.add("en");
command.add("--output");
command.add("D:\\Dump\\test.mkv");
command.add("--language");
command.add("0:eng");
command.add("--default-track");
command.add("0:yes");
command.add("--language");
command.add("1:eng");
command.add("--default-track");
command.add("1:yes");
command.add("--language");
command.add("2:eng");
command.add("D:\\Dump\\Game of Thrones\\Game.of.Thrones.S01.1080p.WEB-DL.DD5.1.H.264-SA89[rartv]\\Game.of.Thrones.S01E01.Winter.Is.Coming.1080p.WEB-DL.DD5.1.H.264-SA89.mkv");
command.add("--sub-charset");
command.add("0:UTF-8");
command.add("--language");
command.add("0:eng");
command.add("D:\\Dump\\Game of Thrones\\Game.of.Thrones.S01.1080p.WEB-DL.DD5.1.H.264-SA89[rartv]\\Subs\\Game.of.Thrones.S01E01.Winter.Is.Coming.1080p.WEB-DL.DD5.1.H.264-SA89.srt");
command.add("--track-order");
command.add("0:0,0:1,0:2,1:0");
ProcessBuilder builder = new ProcessBuilder(command);
builder.redirectErrorStream(true);
Process process = builder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
由于某些原因,这不适用于路径中的空格。 这同样适用于Go。