天天看點

JMF播放AVI格式的視訊

public class Conver {

public void run() {

try {

// 轉換并截圖

String filePath = "E:\\wk\\測試用視訊\\littlethree.avi";

ConverVideo cv = new ConverVideo(filePath);

cv.beginConver();

} catch (Exception e) {

e.printStackTrace();

}

}

public static void main(String args[]) {

Conver c = new Conver();

c.run();

}

}

import java.io.File;

import java.io.IOException;

import java.io.InputStream;

import java.util.Date;

import java.util.List;

public class ConverVideo {

private String PATH;

private String filerealname; // 檔案名 不包括擴充名

private String filename; // 包括擴充名

private String flvfolder = "D:\\"; // flv視訊的目錄

private String ffmpegpath = "D:\\ffmpeg\\bin\\ffmpeg.exe"; // ffmpeg.exe的目錄

public ConverVideo() {

}

public ConverVideo(String path) {

PATH = path;

}

public String getPATH() {

return PATH;

}

public void setPATH(String path) {

PATH = path;

}

public boolean beginConver() {

File fi = new File(PATH);

filename = fi.getName();

filerealname = filename.substring(0, filename.lastIndexOf("."))

.toLowerCase();

if (!checkfile(PATH)) {

System.out.println(PATH + "檔案不存在");

return false;

}

if (process()) { //proess函數用來轉換視訊格式

PATH = null;

return true;

} else {

PATH = null;

return false;

}

}

private boolean process() {

int type = checkContentType();

boolean status = false;

if (type == 0) {

status = processUSEAVI(PATH);

}

return status;

}

//檢查檔案類型

private int checkContentType() {

String type = PATH.substring(PATH.lastIndexOf(".") + 1, PATH.length()).toLowerCase();

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("flv")) {

return 0;

}

return 9;

}

private boolean checkfile(String path) {

File file = new File(path);

if (!file.isFile()) {

return false;

} else {

return true;

}

}

private boolean processUSEAVI(String oldfilepath) {

if (!checkfile(PATH)) {

System.out.println("上傳視訊檔案:"+oldfilepath + "不是檔案");

return false;

}

List commend = new java.util.ArrayList();

commend.add(ffmpegpath); // 添加轉換工具路徑

commend.add("-i"); //添加參數"-i",該參數指定要轉換的檔案

commend.add(oldfilepath); // 添加要轉換格式的視訊檔案的路徑

commend.add("-ab"); //設定音頻碼率 

commend.add("128");

commend.add("-qscale");

commend.add("6");

commend.add("-s");//

commend.add("352x288");

commend.add("-vcodec"); //視訊流編碼

commend.add("h263");

commend.add("-acodec"); //音頻編碼

commend.add("pcm_s16le"); //音頻編碼

commend.add("-ac"); //設定聲道數 1就是單聲道,2就是立體聲

commend.add("2");

commend.add("-ar"); //設定聲音的采樣頻率

commend.add("22050");

commend.add("-r"); //設定幀頻

commend.add("25");

commend.add("-y"); // 添加參數"-y",該參數指定将覆寫已存在的檔案

commend.add(flvfolder + filerealname + ".avi");

try {

ProcessBuilder builder = new ProcessBuilder();

String cmd = commend.toString();

builder.command(commend);

Process p = builder.start();

doWaitFor(p);

p.destroy();

return true;

} catch (Exception e) {

e.printStackTrace();

return false;

}

}

public int doWaitFor(Process p) {

InputStream in = null;

InputStream err = null;

int exitValue = -1; // returned to caller when p is finished

try {

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()視訊上傳轉碼錯誤:" + 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;

}

}