package com.example.demo.util;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* ffmpeg格式轉換
*/
public class ConvertVideoUtil {
private static String inputPath = "";
private static String outputPath = "";
/**
* ffmpeg安裝所在目錄的bin目錄,如果是linux同理
*/
private static String ffmpegPath = "";
public static void main(String[] args) {
getPath();
if (!checkfile(inputPath)) {
System.out.println(inputPath + " is not file");
return;
}
if (process()) {
System.out.println("ok");
}
}
public static void getPath() {
// 先獲取當前項目路徑,在獲得源文件、目標文件、轉換器的路徑
File diretory = new File("");
try {
String currPath = diretory.getAbsolutePath();
inputPath = "D:\\ffmpeg\\Apple Watch~2.mp4";
outputPath = "D:\\ffmpeg\\oss\\";
ffmpegPath = "D:\\dev_tools\\ffmpeg\\ffmpeg-4.2.1-win64-static\\bin\\";
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格式
//processAVI(type);
status = processTs(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;
}
/**
* 對ffmpeg無法解析的文件格式(wmv9,rm,rmvb等), 可以先用別的工具(mencoder)轉換爲avi(ffmpeg能解析的)格式.
* @param type
* @return
*/
private static String processAVI(int type) {
List<String> commend = new ArrayList<String>();
commend.add(ffmpegPath + "mencoder");
commend.add(inputPath);
commend.add("-oac");
commend.add("lavc");
commend.add("-lavcopts");
commend.add("acodec=mp3:abitrate=64");
commend.add("-ovc");
commend.add("xvid");
commend.add("-xvidencopts");
commend.add("bitrate=600");
commend.add("-of");
commend.add("mp4");
commend.add("-o");
commend.add(outputPath + "a.AVI");
try {
ProcessBuilder builder = new ProcessBuilder();
Process process = builder.command(commend).redirectErrorStream(true).start();
new PrintStream(process.getInputStream());
new PrintStream(process.getErrorStream());
process.waitFor();
return outputPath + "a.AVI";
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private static boolean executeCommand(List<String> commond){
try {
Process videoProcess = new ProcessBuilder(commond).redirectErrorStream(true).start();
new PrintStream(videoProcess.getErrorStream()).start();
new PrintStream(videoProcess.getInputStream()).start();
videoProcess.waitFor();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 轉換成TS格式
* @param oldfilepath
* @return
*/
private static boolean processTs(String oldfilepath) {
if (!checkfile(inputPath)) {
System.out.println(oldfilepath + " is not file");
return false;
}
List<String> command = new ArrayList<String>();
command.add(ffmpegPath + "ffmpeg");
command.add("-i");command.add(oldfilepath);
command.add("-pix_fmt");command.add("yuv420p");
command.add("-vcodec");command.add("h264");
command.add("-b:v");command.add("1575k");//比特率,越高越清晰
command.add("-preset");command.add("5");//-preset,一般是5
command.add("-crf");command.add("18");//-crf 18——28是一個合理的範圍。18被認爲是視覺無損的
//command.add("-vf");command.add("scale=iw/2:ih/2");//長和高默認爲原來的2分之一
command.add("-s");command.add("640x480");//分辨率
// command.add("-codec");command.add("copy");
command.add(outputPath + "Apple Watch~.480p-4.ts");
return executeCommand(command);
}
/**
* 轉換成FLV格式
* @param oldfilepath
* @return
*/
private static boolean processFlv(String oldfilepath) {
if (!checkfile(inputPath)) {
System.out.println(oldfilepath + " is not file");
return false;
}
List<String> command = new ArrayList<String>();
command.add(ffmpegPath + "ffmpeg");
command.add("-i");
command.add(oldfilepath);
command.add("-ab");
command.add("56");
command.add("-ar");
command.add("22050");
command.add("-qscale");
command.add("8");
command.add("-r");
command.add("15");
command.add("-s");
command.add("640x480");
command.add(outputPath + "a.flv");
return executeCommand(command);
}
/**
* 轉換成MP4格式
* @param oldfilepath
* @return
*/
private static boolean processMp4(String oldfilepath) {
if (!checkfile(inputPath)) {
System.out.println(oldfilepath + " is not file");
return false;
}
List<String> command = new ArrayList<String>();
command.add(ffmpegPath + "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 + "a.mp4");
return executeCommand(command);
}
static class PrintStream extends Thread {
java.io.InputStream __is = null;
public PrintStream(java.io.InputStream is)
{
__is = is;
}
@Override
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();
}
}
}
}