1.配置ffmpeg的環境變量
2.代碼如下
<code>import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @Author LinXin
* 1.設置視頻輸出目錄
* 2.獲取上傳視頻的路徑以及文件名
* 3.截取後綴名判斷格式
* 4.ffmpeg不支持的格式先使用mencoder轉成AVI格式 丟棄
* 5.AVI再轉成flv格式,同時刪除緩存文件 丟棄
*/
public class Video {
//視頻輸出目錄
private static final String OutSrc = "C:/Users/LinXin/Desktop/測試視頻/flv/";
//截圖輸出目錄
private static final String imageOutSrc = "C:/Users/LinXin/Desktop/測試視頻/Screen/";
//視頻類型
private static final String OutType = ".flv";
//圖片類型
private static final String ImagesType = ".jpg";
//轉碼完視頻路徑
private static String videoOutSrc = null;
public static void main(String[] args) {
String fileName = "C:/Users/LinXin/Desktop/測試視頻/video/1.mp4";
String type = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
File orginFile = new File(fileName);
long start = System.currentTimeMillis();
if (!orginFile.exists()) {
System.out.println("文件不存在");
} else {
int i = checkType(type);
if (i == 0) {
String OutName = fileName.substring(fileName.lastIndexOf("/") +1, fileName.lastIndexOf(".")) + OutType;
File file = new File(OutSrc + OutName);
if (file.exists()) {
System.out.println("文件已存在");
} else {
String command = "cmd /c ffmpeg -i " + fileName + " -c:v copy -c:a copy " + OutSrc + OutName;
if (procedure(command)) {
File file1 = new File(fileName);
// file1.delete();
System.out.println("Success");
videoOutSrc = OutSrc+OutName;
String fileNameWithNoneType = fileName.substring(fileName.lastIndexOf("/") +1, fileName.lastIndexOf("."));
ScreenShot(videoOutSrc,fileNameWithNoneType);
} else {
System.out.println("Failure");
}
}
} else {
System.out.println("文件類型不支持");
}
}
long end = System.currentTimeMillis();
System.out.println("");
System.out.println("耗時:"+(end-start)/1000);
}
//視頻轉碼,圖片截取
public static boolean procedure(String command) {
boolean flag = false;
try {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(command);
//一個線程去獲取正常信息
new Thread(new Runnable() {
BufferedReader infoReader = new BufferedReader(new InputStreamReader(p.getInputStream(), "GBK"));
String line1 = null;
@Override
public void run() {
try {
while ((line1 = infoReader.readLine()) != null) {
System.out.println(line1);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
//一個線程去獲取錯誤信息
new Thread(new Runnable() {
BufferedReader errorReader = new BufferedReader(new InputStreamReader(p.getErrorStream(), "GBK"));
String line2 = null;
@Override
public void run() {
try {
while ((line2 = errorReader.readLine()) != null) {
System.out.println(line2);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
int exitValue = p.waitFor();
if (exitValue == 0) {
flag = true;
}
p.destroy();
System.out.println("Exited with error code " + exitValue);
} catch (Exception e) {
System.out.println(e.getMessage());
flag = false;
}
return flag;
}
//ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv,mkv等)
private static int checkType(String type) {
if (type.equals("asx")) {
return 0;
} else if (type.equals("asf")) {
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("mp4")) {
return 0;
} else if (type.equals("mov")) {
return 0;
} else if (type.equals("avi")) {
return 0;
} else if (type.equals("flv")) {
return 0;
} else if (type.equals("mkv")) {
return 0;
}
return 1;
}
//視頻截圖
public static boolean ScreenShot(String videoPath,String imagesPath){
boolean flag = false;
String command = "ffmpeg -ss 00:00:40 -t 0.001 -i "+videoPath+" -y -f image2 -q:v 2 -s 1920x1080 "+imageOutSrc+imagesPath+ImagesType;
if(procedure(command)){
System.out.println("ScreentShot Success");
}
return flag;
}
}
/<code>
閱讀更多 那一絲漣漪 的文章