package com.example.ldb;
import java.io.*;
public class ChangeVedioCover {
public static final StringFFMPEG_PATH ="C:/Users/HIAPAD/Downloads/ffmpeg-2021-03-31-git-61ea0e3191-full_build/bin/ffmpeg.exe"; //
public static final StringFILE_PATH ="C:/Users/HIAPAD/Desktop/安全教育"; //需要替换封面的视频目录
public static final StringCOMMAND ="%s -i %s -i %s -map 1 -map 0 -c copy -disposition:0 attached_pic -y %s"; // ffmpeg
public static final StringCOMMAND1 =
" %s -ss 5 -i %s -y -f image2 -t 0.001 -s 380*260 %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(".")) +".png"; // 新生成的文件名后面添加_ 下划线
String cmd = String.format(COMMAND1, FFMPEG_PATH ,a.getAbsolutePath(), newPath);
execCmd(cmd);
}
}
}
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();
}
}
}
java 端代码实现。。。。同样的功能。。。。