现在在做一个功能是上传视频后,再把视频转换为.flv格式(用ffmpeg转换,不能转换RM格式)
单独调试视频上传和视频格式转换都是成功的,但两个合在一起时,在tomcat中运行时,上传视频成功了,可是转换时老卡,线程ffmpeg.exe好象一直处于wait状态,只有关闭tomcat时才能够把视频格式转换成功,经过多方调查,才找到原因,当转换播放文件没有完成之前,它本身所持有的线程是不能释放的,否则将会出现无限等待情况情况,下面是成功视频转换代码为:
package vedioOperator;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
public class TestVedio {
//private resourcePath String PATH = "c://test//a.avi";//要转化的源文件
public static boolean process(String resourcePath)
{
int type = checkContentType(resourcePath);
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String date = df.format(new Date());
boolean status = false;
if (type==0) {
status = processFLV(resourcePath);// 直接将文件转为flv文件
} /*else if (type==1) {
String avifilepath = processAVI(type);
if (avifilepath == null)
return false;// avi文件没有得到
status = processFLV(avifilepath);// 将avi转为flv
}*/
return status;
}
private static int checkContentType(String resourcePath)
{
String type = resourcePath.substring(resourcePath.lastIndexOf(".") + 1,
resourcePath.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;
} else if(type.equals("mpeg")){
return 0;
}else if(type.equals("mpe")){
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能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
private static boolean processFLV(String resourcePath) {
if(!checkfile(resourcePath)){
System.out.println(resourcePath+" is not file");
return false;
}
//String savename = filename.substring(0, filename.indexOf("."));
//文件命名
String savename = DateUtils.getInstance().getTimeNoSeparate() + Math.round(Math.random()*100000);
List commend=new ArrayList();
commend.add("d://ffmpeg");
commend.add("-i");
commend.add(resourcePath);
commend.add("-ab");
commend.add("56");
commend.add("-ar");
commend.add("22050");
//commend.add("-b");
//commend.add("1500");
commend.add("-qscale");
commend.add("8");
commend.add("-r");
commend.add("15");
commend.add("-s");
commend.add("300x300");
commend.add("c://home//" + savename + ".flv");
try {
Runtime runtime=Runtime.getRuntime();
Process proce = null;
String cmd="";
String cut=" d://ffmpeg.exe -i " + resourcePath + " -y -f image2 -ss 8 -t 0.001 -s 120x96 c://test//" + savename + ".jpg";
String cutCmd=cmd+cut;
proce=runtime.exec(cutCmd);
ProcessBuilder builder = new ProcessBuilder(commend);
//builder.command(commend);
builder.start();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static void main(String[] args) {
if(!checkfile("C://temp//Levi sbabe.asf")){
System.out.println(""+" is not file");
return;
}
if (process("C://temp//Levisbabe.asf")) {
System.out.println("ok");
}
}
}
import java.util.*;
import java.io.*;
public class process
{
public static void main(String args[])
{
try
{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("ffmpeg -i 03.avi -ar 22050 -ab 56 -f flv -y -s 320x240 05.flv");
InputStream stderr = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
System.out.println("<ERROR>");
while ( (line = br.readLine()) != null)
System.out.println(line);
System.out.println("</ERROR>");
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
} catch (Throwable t)
{
t.printStackTrace();
}
}
}
单独调试视频上传和视频格式转换都是成功的,但两个合在一起时,在tomcat中运行时,上传视频成功了,可是转换时老卡,线程ffmpeg.exe好象一直处于wait状态,只有关闭tomcat时才能够把视频格式转换成功,经过多方调查,才找到原因,当转换播放文件没有完成之前,它本身所持有的线程是不能释放的,否则将会出现无限等待情况情况,下面是成功视频转换代码为:
package vedioOperator;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
public class TestVedio {
//private resourcePath String PATH = "c://test//a.avi";//要转化的源文件
public static boolean process(String resourcePath)
{
int type = checkContentType(resourcePath);
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String date = df.format(new Date());
boolean status = false;
if (type==0) {
status = processFLV(resourcePath);// 直接将文件转为flv文件
} /*else if (type==1) {
String avifilepath = processAVI(type);
if (avifilepath == null)
return false;// avi文件没有得到
status = processFLV(avifilepath);// 将avi转为flv
}*/
return status;
}
private static int checkContentType(String resourcePath)
{
String type = resourcePath.substring(resourcePath.lastIndexOf(".") + 1,
resourcePath.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;
} else if(type.equals("mpeg")){
return 0;
}else if(type.equals("mpe")){
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能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
private static boolean processFLV(String resourcePath) {
if(!checkfile(resourcePath)){
System.out.println(resourcePath+" is not file");
return false;
}
//String savename = filename.substring(0, filename.indexOf("."));
//文件命名
String savename = DateUtils.getInstance().getTimeNoSeparate() + Math.round(Math.random()*100000);
List commend=new ArrayList();
commend.add("d://ffmpeg");
commend.add("-i");
commend.add(resourcePath);
commend.add("-ab");
commend.add("56");
commend.add("-ar");
commend.add("22050");
//commend.add("-b");
//commend.add("1500");
commend.add("-qscale");
commend.add("8");
commend.add("-r");
commend.add("15");
commend.add("-s");
commend.add("300x300");
commend.add("c://home//" + savename + ".flv");
try {
Runtime runtime=Runtime.getRuntime();
Process proce = null;
String cmd="";
String cut=" d://ffmpeg.exe -i " + resourcePath + " -y -f image2 -ss 8 -t 0.001 -s 120x96 c://test//" + savename + ".jpg";
String cutCmd=cmd+cut;
proce=runtime.exec(cutCmd);
ProcessBuilder builder = new ProcessBuilder(commend);
//builder.command(commend);
builder.start();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static void main(String[] args) {
if(!checkfile("C://temp//Levi sbabe.asf")){
System.out.println(""+" is not file");
return;
}
if (process("C://temp//Levisbabe.asf")) {
System.out.println("ok");
}
}
}
import java.util.*;
import java.io.*;
public class process
{
public static void main(String args[])
{
try
{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("ffmpeg -i 03.avi -ar 22050 -ab 56 -f flv -y -s 320x240 05.flv");
InputStream stderr = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
System.out.println("<ERROR>");
while ( (line = br.readLine()) != null)
System.out.println(line);
System.out.println("</ERROR>");
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
} catch (Throwable t)
{
t.printStackTrace();
}
}
}