使用ffmpeg插件,完成java后台转码。(统一转MP4)

参看https://blog.csdn.net/qq_32230309/article/details/78875607

下载ffmpeg插件

文件上传的代码代码,我就不放下来了。

我在这里把链接放给各位:

链接:https://pan.baidu.com/s/1ib4IFYwLAQEWDyEZLIqU-w
提取码:1hw6

上传视频方法调用转化的类

videoInfo为前端传回来的实体类
convertVideo.main(videoInfo);//调用转格式ConvertVideo类

后台转化视频的类

@SuppressWarnings(“unused”)
public class ConvertVideo {
private List upload;
static VideoInfo videoInfo;
private ServletContext context1;
private static IVideoInfoService videoInfoService;
private static String inputPath = “”;
private static String outputPath = “”;
private static String ffmpegPath = “”;
private static String ffmpegPath1 = “”;
public static void main(VideoInfo videoInfo2) throws IOException {
videoInfo=videoInfo2;
if(!".mp4".equals(videoInfo.getVideoSuffix()))//判断上传视频是否是
{
getPath(videoInfo2);
if (!checkfile(inputPath)) {
System.out.println(inputPath + " is not file");
return ;
}
if (process()) {
System.out.println(“ok”);
}}else
{
System.out.println(“存在了”);
}
}
public static void getPath(VideoInfo videoInfo) {
// 先获取当前项目路径,在获得源文件、目标文件、转换器的路径
File diretory = new File("");
try {
String currPath = diretory.getAbsolutePath();
inputPath = ServletActionContext.getServletContext().getRealPath(videoInfo.getUploadPath());//原来的
outputPath = ServletActionContext.getServletContext().getRealPath(SystemContext.getvideoURL());//转码文件存放的地方
ffmpegPath1 =ServletActionContext.getServletContext().getRealPath(“ffmpeg\”)+"\"; //“D:\ffmpeg\”
System.out.println(ffmpegPath1);
System.out.println(currPath);
}
catch (Exception e) {
System.out.println(“getPath出错”);
}
}
public static boolean process() {
int type = checkContentType();
boolean status = false;
System.out.println(“直接转成mp4格式”);
status = processMp4(inputPath);// 直接转成mp4格式
return status;
}
private static int checkContentType() {
String type = inputPath.substring(inputPath.lastIndexOf(".") + 1, inputPath.length())
.toLowerCase();
// ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
if (type.equals(“avi”)) {
return 0;
} else if (type.equals(“mpg”)) {
return 0;
} else if (type.equals(“wmv”)) {
return 0;
} else if (type.equals(“3gp”)) {
return 0;
} else if (type.equals(“mov”)) {
return 0;
} else if (type.equals(“mp4”)) {
return 0;
} else if (type.equals(“asf”)) {
return 0;
} else if (type.equals(“asx”)) {
return 0;
} else if (type.equals(“flv”)) {
return 0;
}
// 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等),
// 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
else if (type.equals(“wmv9”)) {
return 1;
} else if (type.equals(“rm”)) {
return 1;
} else if (type.equals(“rmvb”)) {
return 1;
}
return 9;
}
private static boolean checkfile(String path) {
File file = new File(path);
if (!file.isFile()) {
return false;
}
return true;
}
private static boolean processMp4(String oldfilepath) {
if (!checkfile(inputPath)) {
System.out.println(oldfilepath + " is not file");
return false;
}
List command = new ArrayList();
command.add(ffmpegPath1 + “ffmpeg”);
command.add("-i");
command.add(oldfilepath);
command.add("-c:v");
command.add(“libx264”);
command.add("-mbd");
command.add(“0”);
command.add("-c:a");
command.add(“aac”);
command.add("-strict");
command.add("-2");
command.add("-pix_fmt");
command.add(“yuv420p”);
command.add("-movflags");
command.add(“faststart”);
command.add(outputPath +"\"+ videoInfo.getVideoContent()+".mp4");//名字
try {
String path=outputPath +"\"+ videoInfo.getVideoContent()+".mp4";
String targetFileName1=videoInfo.getVideoContent()+".mp4";
videoInfo.setUploadPath(SystemContext.getvideoURL() + targetFileName1);//存放转格式后的路径到数据库
String targetFileName = “.mp4”; //文件后缀
videoInfo.setVideoSuffix(targetFileName);
// 方案1
// Process videoProcess = Runtime.getRuntime().exec(ffmpegPath + "ffmpeg -i " + oldfilepath
// + " -ab 56 -ar 22050 -qscale 8 -r 15 -s 600x500 "
// + outputPath + “a.flv”);
Process videoProcess = new ProcessBuilder(command).redirectErrorStream(true).start()
new PrintStream(videoProcess.getErrorStream()).start();
new PrintStream(videoProcess.getInputStream()).start()
videoProcess.waitFor();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public ServletContext getContext1() {
return context1;
}
public void setContext1(ServletContext context1) {
this.context1 = context1;
}
public void setServletContext(ServletContext arg0) {
context1 = arg0;
}
}
class PrintStream extends Thread
{
java.io.InputStream __is = null;
public PrintStream(java.io.InputStream is)
{
__is = is;
}
public void run()
{
try
{
while(this != null)
{
int _ch = __is.read();
if(_ch != -1)
System.out.print((char)_ch);
else break;
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

猜你喜欢

转载自blog.csdn.net/qq_36927587/article/details/88789168