package pers.lsy.util.io;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
/**
* 合并文件
* 文件格式:
* 1.ts,2.ts3.ts......
*/
public class TsMergeFiles {
public static void main(String[] args) throws Exception {
new TsMergeFiles().mergeFiles(new File("H:\\video\\temp"), new File("H:\\video\\test"));
}
private void mergeFiles(File tempDir, File newFile) {
for (File temps : tempDir.listFiles())
if (temps.isDirectory()) {
FileOutputStream fos = null;
try {
String outPath = newFile.getAbsoluteFile() + File.separator + temps.getName() + ".ts";
if (!new File(outPath).exists()) {
fos = new FileOutputStream(outPath);// 会自动创建文件
File[] temps1 = temps.listFiles();
for (int i = 0; i < temps1.length; i++) {
String tempFileName = "" + i;
if (tempFileName.length() < 3) {
for (int j = 0; j <= (3 - tempFileName.length()); j++) {
tempFileName = "0" + tempFileName;
}
}
File temp = new File(temps, tempFileName + ".ts");
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(temp);
int len = 0;
byte[] buf = new byte[1024];
while ((len = inputStream.read(buf)) != -1) {
fos.write(buf, 0, len);// 写入流中
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (inputStream != null)
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
System.out.println("合并完成: " + outPath);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null)
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
java ts视频文件合并
最新推荐文章于 2024-07-08 02:43:53 发布