例如:ffmpeg合并视频的命令可以放在 .bat文件里面批量执行
.\ffmpeg.exe -i G:\mov\80\video.m4s -i G:\mov\80\audio.m4s -codec copy G:\mov\80\Output1.mp4
用java 代码解析目录和文件名字拼接好命令,写入到 .bat文件,再用java执行.bat
<!--json-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
package com;
import com.alibaba.fastjson.JSONObject;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* <br>Classname: fileTest
* <br>描述:
* <br>功能:
* <br>作者:
* <br>时间: 2021/2/8 11:17
*/
public class fileTest {
public static void main(String[] args) throws IOException {
//b站下载的目录
String path = "D:\\1MQL5\\95202761";
//调用方法
List<String> list = new ArrayList<String>();
List<String> pathAndName = getFiles(path, list);
//拼接脚本内容
WriteDate("D:");
WriteDate("cd \\1MQL5\\ffmpeg\\ffmpeg-20180605-b748772-win64-static\\bin");
for (int i = 0; i < pathAndName.size(); i++) {
//拼接命令
if (i % 2 == 0) {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(".\\ffmpeg.exe -i ").append(pathAndName.get(i)).
append("\\video.m4s -i ").append(pathAndName.get(i)).
append("\\audio.m4s -codec copy ").append("D:\\1MQL5\\MP4\\")
// .append(pathAndName.get(i + 1)).append(".mp4");
//中文名乱码
.append(i + 1).append(".mp4");
WriteDate(stringBuffer.toString());
}
}
//执行bat
String locationCmd = "cmd /c start D:\\1MQL5\\ffmpeg\\ffmpeg-20180605-b748772-win64-static\\bin\\cmdjava.bat";
callCmd(locationCmd);
//完毕要手动 关cmd窗
System.out.println("执行.bat完成,开始重命名------》");
//修改文件名
for (int i = 0; i < pathAndName.size(); i++) {
if (i % 2 == 0) {
String filepath = "D:\\1MQL5\\MP4\\" + (i + 1) + ".mp4";
String newName = "D:\\1MQL5\\MP4\\"+pathAndName.get(i + 1).replace("MQL5/MT5/EA编程教程MQL5编程","") + ".mp4";
RenameFile(filepath, newName);
}
}
}
/**
* 执行bat D:\1MQL5\ffmpeg\ffmpeg-20180605-b748772-win64-static\bin\cmdjava.bat
*
* @param locationCmd
*/
private static void callCmd(String locationCmd) {
try {
Process child = Runtime.getRuntime().exec(locationCmd);
InputStream in = child.getInputStream();
int c;
while ((c = in.read()) != -1) {
System.out.println((char) c);
}
in.close();
try {
child.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("done");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 重命名一个文件:将原来的文件直接修改
* filePath = "D:/test/我是.conf";
* newpathName="D:/test/我是2.java";
*/
private static void RenameFile(String filePath, String newpathName) {
try {
File oldname = new File(filePath);
File newname = new File(newpathName);
if (!oldname.renameTo(newname)) {
System.out.println("Failed to renameTo file");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
/**
* 递归获取某路径下的所有文件,文件夹,并输出
*/
public static List<String> getFiles(String path, List<String> stringBuffer) throws IOException {
File file = new File(path);
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
// System.out.println("目录:" + files[i].getPath());
if (files[i].getPath().contains("\\64")) {
System.out.println(files[i].getPath());
String pathf = files[i].getPath();//作为路径
stringBuffer.add(pathf);
}
getFiles(files[i].getPath(), stringBuffer);
} else {
if ("entry.json".equals(files[i].getName())) {
//System.out.println("文件:" + files[i].getPath());
File entryjsonfile = new File(files[i].getPath());
// System.out.println(txt2String(entryjsonfile));
String json = txt2String(entryjsonfile);
JSONObject jsStr = JSONObject.parseObject(json);
String page_data = jsStr.getString("page_data");
//System.out.println(page_data);
String download_subtitle = JSONObject.parseObject(page_data).getString("download_subtitle").replace(" ", "");
// download_subtitle 作为文件名
System.out.println(download_subtitle);
stringBuffer.add(download_subtitle);
}
}
}
} /*else {
System.out.println("文件:" + file.getPath());
}*/
return stringBuffer;
}
/**
* 读取txt文件的内容
*
* @param file 想要读取的文件对象
* @return 返回文件内容
*/
public static String txt2String(File file) {
String result = "";
try {
BufferedReader br = new BufferedReader(new FileReader(file));//构造一个 BufferedReader类来读取文件
String s = null;
while ((s = br.readLine()) != null) {//使用readLine方法,一次读一行
result = result + "\n" + s;
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* *写入数据
* <p>
*/
public static void WriteDate(String content) throws IOException {
//定义文件路径,没有该文件会自动创建,如果路径有文件夹,一定要有,不会自动创建文件夹
// 放在ffmpeg 的bin下
String fileName = "D:\\1MQL5\\ffmpeg\\ffmpeg-20180605-b748772-win64-static\\bin\\cmdjava.bat";
// 在文件夹目录下新建文件
File file = new File(fileName);
FileOutputStream fos = null;
OutputStreamWriter osw = null;
try {
if (!file.exists()) {
boolean hasFile = file.createNewFile();
if (hasFile) {
System.out.println("file not exists, create new file");
}
fos = new FileOutputStream(file);
} else {
fos = new FileOutputStream(file, true);
}
osw = new OutputStreamWriter(fos, "utf-8");
// 写入内容
osw.write("\r\n");
osw.write(content);
// 换行
osw.write("\r\n");
System.out.println("成功向文件 [{}] 写入内容:[{}] --->" + fileName);
} catch (Exception e) {
System.out.println("写入文件发生异常" + e);
} finally {
// 关闭流
try {
if (osw != null) {
osw.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
System.out.println("关闭流异常" + e);
}
}
}
}