您的位置:首页 > 编程语言 > Java开发

java调用本地ffmpeg.exe分离音视频 分帧 图片合成视频 拼接视频

2016-12-01 15:41 696 查看
public class ConvertVideo{

static Properties props=System.getProperties(); //获得系统属性集
static String osName = props.getProperty("os.name"); //操作系统名称
static String osArch = props.getProperty("os.arch"); //操作系统构架
static String osVersion = props.getProperty("os.version"); //操作系统版本

static String WINDOWSPATH = "D:\\ffmpeg-20161110-872b358-win64-static\\bin\\ffmpeg.exe";
static String LINUXPATH = "";
static String WINDOWS = "Windows";
static String LINUX = "Linux";
static String RANDOMNAME = String.valueOf(Calendar.getInstance().getTimeInMillis())+ Math.round(Math.random() * 100000);
static String ABSOULTPATH  =props.getProperty("java.io.tmpdir");
static String MP3OUTPATH =props.getProperty("java.io.tmpdir")+PATH.substring(PATH.length()-24, PATH.length())+".mp3";
static String MP4OUTPATH =props.getProperty("java.io.tmpdir")+PATH.substring(PATH.length()-24, PATH.length())+".mp4";
static String MP4COMPATH =props.getProperty("java.io.tmpdir")+PATH.substring(PATH.length()-24, PATH.length())+"s.mp4";
static String PICOUTPATH =props.getProperty("java.io.tmpdir")+PATH.substring(PATH.length()-24, PATH.length())+"%d.jpg";
static String PICTUREPATH = props.getProperty("java.io.tmpdir")+RANDOMNAME+".png";

public ConvertVideo(StorageService storageService) {
super(storageService);
}

public ConvertVideo() {
}

public static void main(String[] args) throws Exception{

/*System.out.println(osName);
System.out.println(osArch);
System.out.println(osVersion);*/
System.out.println(MP3OUTPATH);

String osNames = "";

if(osName.startsWith(WINDOWS)){
osNames = WINDOWSPATH;
}else{
osNames = LINUXPATH;
}

new Part().partMP3(osNames, "F:\\pics\\IMG_0490.mp4", "F:\\IMG_0490.mp3");//分离MP3
new Part().partMP4(osNames, "F:\\pics\\IMG_0490.mp4", "F:\\IMG_0490.mp4");//分离MP4
new Part().partOneMP4(osNames, "F:\\IMG_0490.mp4", "00:00:00", "00:00:03", "F:\\one.mp4");//分割第一段
new Part().partOneMP4(osNames, "F:\\IMG_0490.mp4", "00:00:03", "00:00:04", "F:\\two.mp4");//分割第二段
new Part().partOneMP4(osNames, "F:\\IMG_0490.mp4", "00:00:04", "00:00:26", "F:\\three.mp4");//分割第三段
new Part().partPic(osNames, "F:\\two.mp4", "F:\\%d.jpg");//生成要改变的MP4帧
if(new File("F:\\two.mp4").exists()){
new File("F:\\two.mp4").delete();
}
/*
这里进行图片的处理
*/
new Part().ComPicMP4(osNames, "F:\\%d.jpg", "F:\\two.mp4");//经过处理后的图片合成新的第二段视频

new Part().changeTs(osNames, "F:\\one.mp4", "F:\\one.ts");//分别转码 来进行拼接
new Part().changeTs(osNames, "F:\\two.mp4", "F:\\two.ts");
new Part().changeTs(osNames, "F:\\three.mp4", "F:\\three.ts");

String shipin[] = {"F:\\one.ts","F:\\two.ts","F:\\three.ts"};

new Part().tsToMP4(osNames, shipin, "F:\\out.mp4");//拼接成新的MP4
new Part().ComMP3MP4(osNames, "F:\\IMG_0490.mp3", "F:\\out.mp4", "F:\\last.mp4");//声音和新视频进行合成

System.out.println("ok");

}

//Part类
public class Part {

public boolean partMP3(String osName,String oldfilepath,String mp3outpath){

/*if (!checkfile(oldfilepath)) {
System.out.println(oldfilepath + " is not file");
return false;
}*/

List<String> commend = new ArrayList<String>();
commend.add(osName);
commend.add("-i");//分离mp3
commend.add(oldfilepath);
commend.add("-vn");
commend.add("-ar");
commend.add("44100");
commend.add("-ac");
commend.add("2");
commend.add("-ab");
commend.add("192");
commend.add("-f");
commend.add("mp3");

commend.add(mp3outpath);

try {
ProcessBuilder builder = new ProcessBuilder(commend);
builder.command(commend);
Process p = builder.start();
doWaitFor(p);
p.destroy();
//new File(oldfilepath).delete();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

public boolean partMP4(String osName,String oldfilepath,String mp4outpath){

/*if (!checkfile(oldfilepath)) {
System.out.println(oldfilepath + " is not file");
return false;
}*/

List<String> commend = new ArrayList<String>();
commend.add(osName);
commend.add("-i");//获取无声视频
commend.add(oldfilepath);
commend.add("-vcodec");
commend.add("copy");
commend.add("-an");

commend.add(mp4outpath);

try {
ProcessBuilder builder = new ProcessBuilder(commend);
builder.command(commend);
Process p = builder.start();
doWaitFor(p);
p.destroy();
//new File(oldfilepath).delete();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

public boolean changeTs(String osName,String oldfilepath,String tsoutpath){

/*if (!checkfile(oldfilepath)) {
System.out.println(oldfilepath + " is not file");
return false;
}*/

List<String> commend = new ArrayList<String>();
commend.add(osName);
commend.add("-i");//MP4更换为ts
commend.add(oldfilepath);
commend.add("-vcodec");
commend.add("copy");
commend.add("-vbsf");
commend.add("h264_mp4toannexb");

commend.add(tsoutpath);

try {
ProcessBuilder builder = new ProcessBuilder(commend);
builder.command(commend);
Process p = builder.start();
doWaitFor(p);
p.destroy();
//new File(oldfilepath).delete();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

public boolean tsToMP4(String osName,String[] oldfilepath,String tsoutpath){

/*if (!checkfile(oldfilepath)) {
System.out.println(oldfilepath + " is not file");
return false;
}*/
String list = "\"concat:"+oldfilepath[0];
if(oldfilepath.length>2){
for(int i = 1;i<oldfilepath.length;i++){
list  =list+"|"+oldfilepath[i];
}
}
list = list+"\"";
List<String> commend = new ArrayList<String>();
commend.add(osName);
commend.add("-i");//ts合成新的MP4
commend.add(list);
commend.add("-vcodec");
commend.add("copy");
commend.add("-vbsf");
commend.add("h264_mp4toannexb");

commend.add(tsoutpath);

try {
ProcessBuilder builder = new ProcessBuilder(commend);
builder.command(commend);
Process p = builder.start();
doWaitFor(p);
p.destroy();
//new File(oldfilepath).delete();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

public boolean partOneMP4(String osName,String oldfilepath,String starTime,String endTime,String mp4outpath){

/*if (!checkfile(oldfilepath)) {
System.out.println(oldfilepath + " is not file");
return false;
}*/

List<String> commend = new ArrayList<String>();
commend.add(osName);
commend.add("-i");//截取视频段
commend.add(oldfilepath);
commend.add("-ss");
commend.add(starTime);//start
commend.add("-t");
commend.add(endTime);//end
commend.add("-acodec");
commend.add("copy");
commend.add("-vcodec");
commend.add("copy");
commend.add(mp4outpath);

try {
ProcessBuilder builder = new ProcessBuilder(commend);
builder.command(commend);
Process p = builder.start();
doWaitFor(p);
p.destroy();
//new File(oldfilepath).delete();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

public boolean ComMP3MP4(String osName,String mp3path,String mp4path,String mp4outpath){

/*if (!checkfile(mp4path)) {
System.out.println(mp4path + " is not file");
return false;
}

if (!checkfile(mp3path)) {
System.out.println(mp3path + " is not file");
return false;
}
*/
List<String> commend = new ArrayList<String>();
commend.add(osName);
commend.add("-i");//合成音视频
commend.add(mp3path);
commend.add("-i");
commend.add(mp4path);
commend.add("-f");
commend.add("mp4");
commend.add("-y");
commend.add(mp4outpath);

try {
ProcessBuilder builder = new ProcessBuilder(commend);
builder.command(commend);
Process p = builder.start();
doWaitFor(p);
p.destroy();
//new File(oldf
9c1a
ilepath).delete();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

public boolean partPic(String osName,String oldfilepath,String picpath){//,String starTime,String endTime

/*if (!checkfile(oldfilepath)) {
System.out.println(oldfilepath + " is not file");
return false;
}*/
//D:\\ffmpeg-20161110-872b358-win64-static\\bin\\ffmpeg.exe -i f:\\pics\\test2.mp4 f:\\pics\\%d.jpg -vcodec mjpeg -ss 0:00:00 -t 00:00:05
List<String> commend = new ArrayList<String>();
commend.add(osName);
commend.add("-i");//分帧
commend.add(oldfilepath);
commend.add(picpath);
commend.add("-vcodec");
commend.add("mjpeg");
/*commend.add("-ss");
commend.add(starTime);
commend.add("-t");
commend.add(endTime);*/

try {
ProcessBuilder builder = new ProcessBuilder(commend);
builder.command(commend);
Process p = builder.start();
doWaitFor(p);
p.destroy();
//new File(oldfilepath).delete();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

public boolean 	ComPicMP4(String osName,String oldpicpath,String mp4outpath){

/*if (!checkfile(oldpicpath)) {
System.out.println(oldpicpath + " is not file");
return false;
}*/

List<String> commend = new ArrayList<String>();
commend.add(osName);
commend.add("-y");//图片合成
commend.add("-r");
commend.add("30");
commend.add("-i");
commend.add(oldpicpath);
commend.add("-absf");
commend.add("aac_adtstoasc");
commend.add(mp4outpath);

try {
ProcessBuilder builder = new ProcessBuilder(commend);
builder.command(commend);
Process p = builder.start();
doWaitFor(p);
p.destroy();
//new File(oldfilepath).delete();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

public static int doWaitFor(Process p) {
InputStream in = null;
InputStream err = null;
int exitValue = -1; // returned to caller when p is finished
try {
System.out.println("comeing");
in = p.getInputStream();
err = p.getErrorStream();
boolean finished = false; // Set to true when p is finished

while (!finished) {
try {
while (in.available() > 0) {
Character c = new Character((char) in.read());
System.out.print(c);
}
while (err.available() > 0) {
Character c = new Character((char) err.read());
System.out.print(c);
}

exitValue = p.exitValue();
finished = true;

} catch (IllegalThreadStateException e) {
Thread.currentThread().sleep(500);
}
}
} catch (Exception e) {
System.err.println("doWaitFor();: unexpected exception - "
+ e.getMessage());
} finally {
try {
if (in != null) {
in.close();
}

} catch (IOException e) {
System.out.println(e.getMessage());
}
if (err != null) {
try {
err.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
return exitValue;
}

private static boolean checkfile(String path) {
File file = new File(path);
if (!file.isFile()) {
return false;
}
return true;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java ffempg