我使用BufferedInputStream和BufferedOutputStream以编程方式复制mp4视频。它被正确复制,除了因为我丢失了音频!这是我的代码:
FileInputStream fi = null;
FileOutputStream fo = null;
try {
File f = new File("C:\\Users\\Zerok\\Videos\\v.mp4");
fi = new FileInputStream(f);
fo = new FileOutputStream("video.mp4");
BufferedInputStream buffIn = new BufferedInputStream(fi);
BufferedOutputStream buffOut = new BufferedOutputStream(fo);
int read;
while((read = buffIn.read()) != -1){
buffOut.write(read);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(JavaApplication13.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(JavaApplication13.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
fi.close();
} catch (IOException ex) {
Logger.getLogger(JavaApplication13.class.getName()).log(Level.SEVERE, null, ex);
}
}
video.dmp4文件正常工作,除了音频缺失。是什么引起了这个?我正确使用这些类吗?
答案 0 :(得分:0)
问题是,我没有关闭我的BufferedOutputStream。我使用了这个try-with-resources块,它会在资源完成后自动关闭它:
try (BufferedOutputStream buffOut = new BufferedOutputStream(fo)) {
int read;
System.out.println("Beginning to copy...");
while((read = buffIn.read()) != -1){
buffOut.write(read);
}
}
现在音频再现完美。