首先完成基本的Java调用ffmpeg代码实现
package com.example.ffmpegtest01.service.impl;
import com.example.ffmpegtest01.service.OperationService;
import org.springframework.stereotype.Service;
import java.io.File;
@Service
public class OperationServiceImpl implements OperationService {
@Override
public Boolean ForAvi(String sourcePath, String targetPath) {//sourcePtah是对应的文件的路径
String webroot = "D:\\devsoft\\ffmpeg\\bin";
Runtime run= null;
System.out.println(new File(webroot).getAbsolutePath());
try{
run = Runtime.getRuntime();
long start = System.currentTimeMillis();
//File diretory = new File("");
System.out.println("开始");
Process p = run.exec(new File(webroot).getAbsolutePath()+"/ffmpeg -i "+sourcePath+" -codec copy "+targetPath);
System.out.println("结束");
long end = System.currentTimeMillis();
System.out.println("转化结束"+(end-start));
p.getOutputStream().close();
p.getInputStream().close();
p.getErrorStream().close();
p.waitFor();
return true;
}catch (Exception e){
e.printStackTrace();
return false;
}finally {
run.freeMemory();
}
}
}
Java操作ffmpeg优秀博客
ffmpeg一些命令
博客借鉴
ffmfeg