[Java] 纯文本查看 复制代码 package cn.merryyou.file;
import java.io.*;
/**
* 修改视频封面
* Created by [email]i@merryyou.cn[/email] on 2020/3/24
*
* [url=home.php?mod=space&uid=1248337]@version[/url] 1.0
*/
public class ChangeVedioCover {
public static final String FFMPEG_PATH = "D:/ffmpeg/bin/ffmpeg.exe"; // ffmpeg 程序迷路
public static final String FILE_PATH = "E:/BaiduNetdiskDownload/测试"; //需要替换封面的视频目录
public static final String IMAGE_PATH = "E:/BaiduNetdiskDownload/测试/1.png"; // 需要替换的封面照片
public static final String COMMAND = "%s -i %s -i %s -map 1 -map 0 -c copy -disposition:0 attached_pic -y %s"; // ffmpeg 替换封面的命令
public static void main(String[] args) throws Exception {
printPath(new File(FILE_PATH));
}
public static void printPath(File file) throws IOException {
File[] files = file.listFiles();
for (File a : files) {
System.out.println(a.getAbsolutePath());
if (a.getAbsolutePath().endsWith(".mp4")) {
String newPath = a.getParent() + "/" + a.getName().substring(0, a.getName().lastIndexOf(".")) + "_.mp4"; // 新生成的文件名后面添加_ 下划线
String cmd = String.format(COMMAND, FFMPEG_PATH, a.getAbsolutePath(), IMAGE_PATH, newPath);
execCmd(cmd);
a.delete();// 删除源文件
}
if (a.isDirectory()) {
printPath(a);
}
}
}
public static void execCmd(String cmd) {
ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/C", cmd);
Process process = null;
try {
process = builder.redirectErrorStream(true).start();
} catch (IOException e) {
e.printStackTrace();
}
InputStream in = process.getInputStream();
outStream(in);
}
private static void outStream(InputStream in) {
// 用一个读输出流类去读
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
// 逐行读取输出到控制台
try {
while ((line = br.readLine()) != null) {
// System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
详情 ffmpeg 修改视频封面 |