wmv格式+java_Java调用ffmepg+mencoder视频格式转换(*)
/*** 功能:将任意格式的视频转化为flv格式,有利于在线视频播放
* ps: ffmpeg 能解析的格式:asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等
* mencoder 解析剩下的格式:wmv9,rm,rmvb
* time:2013.12.11*/
package softflag.core.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import softflag.index.IndexService;
public class VideoConverter {
public synchronized void convert(String inputFile, String outputFile) {
process(inputFile, outputFile);
}
private static String getAviFilePath(String filePath) {
String path = filePath.substring(0, filePath.lastIndexOf(".")) + ".avi";
return path;
}
/**
* 转换过程 :先检查文件类型,在决定调用 processFlv还是processAVI
*
* @param inputFile
* @param outputFile
* @return
*/
private void process(String inputFile, String outputFile) {
int type = checkContentType(inputFile);
if (type == 0) {
processFLV(inputFile, outputFile);// 直接将文件转为flv文件
} else if (type == 1) {
processAVI(type, inputFile,outputFile);
}
}
/**
* 检查视频类型
*
* @param inputFile
* @return ffmpeg 能解析返回0,不能解析返回1
*/
private static int checkContentType(String inputFile) {
String type = inputFile.substring(inputFile.lastIndexOf(".") + 1,
inputFile.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;
}
/**
* ffmepg: 能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
*
* @param inputFile
* @param outputFile
* @return
*/
private void processFLV(String inputFile, String outputFile) {
try {
try {
List commend = new java.util.ArrayList();
// 低精度
commend.add(IndexService.getConfigValue("check_multi_ffmpegPath"));
commend.add("-i");
commend.add(inputFile);
commend.add("-ab");
commend.add("64");
commend.add("-acodec");
commend.add("mp3");
commend.add("-ac");
commend.add("2");
commend.add("-ar");
commend.add("22050");
commend.add("-b");
commend.add("230");
commend.add("-r");
commend.add("24");
commend.add("-y");
commend.add(outputFile);
StringBuffer test = new StringBuffer();
for (int i = 0; i < commend.size(); i++)
test.append(commend.get(i) + " ");
// System.out.println(test);
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
Process p = builder.start();
doWaitFor(p);
p.destroy();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Mencoder: 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等),
* 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
*
* @param type
* @param inputFile
* @return
*/
private void processAVI(int type, String inputFile,String outputFile) {
String aviPath = getAviFilePath(inputFile);
String outputFilePath = outputFile;
File file = new File(aviPath);
if (file.exists())
file.delete();
List commend = new java.util.ArrayList();
commend.add(IndexService.getConfigValue("check_multi_mencoderPath"));
commend.add(inputFile);
commend.add("-oac");
commend.add("mp3lame");
commend.add("-lameopts");
commend.add("preset=64");
commend.add("-ovc");
commend.add("xvid");
commend.add("-xvidencopts");
commend.add("bitrate=600");
commend.add("-of");
commend.add("avi");
commend.add("-o");
commend.add(aviPath);
StringBuffer test = new StringBuffer();
for (int i = 0; i < commend.size(); i++)
test.append(commend.get(i) + " ");
// System.out.println(test);
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
Process p = builder.start();
/**
* 清空Mencoder进程 的输出流和错误流 因为有些本机平台仅针对标准输入和输出流提供有限的缓冲区大小,
* 如果读写子进程的输出流或输入流迅速出现失败,则可能导致子进程阻塞,甚至产生死锁。
*/
final InputStream is1 = p.getInputStream();
final InputStream is2 = p.getErrorStream();
new Thread() {
public void run() {
BufferedReader br = new BufferedReader(
new InputStreamReader(is1));
try {
String lineB = null;
while ((lineB = br.readLine()) != null) {
if (lineB != null)
System.out.println(lineB);
}
is1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
new Thread() {
public void run() {
BufferedReader br2 = new BufferedReader(
new InputStreamReader(is2));
try {
String lineC = null;
while ((lineC = br2.readLine()) != null) {
if (lineC != null)
System.out.println(lineC);
}
br2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
doWaitFor(p);
p.destroy();
processFLV(aviPath,outputFilePath);
} catch (Exception e) {
System.err.println(e);
}
}
public 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;
}
}
wmv格式+java_Java调用ffmepg+mencoder视频格式转换(*)相关推荐
- Java调用ffmepg+mencoder视频格式转换
PS: ffmpeg.rev12665.7z版本相当老了,有很多命令可能出现错误 其他格式转FLV格式,可以用Java调用ffmpeg和memcoder实现 ffmepg版本:ffmpeg.rev12 ...
- Ffmpeg,mencoder视频格式转换
一. Ffmpeg,mencoder视频格式转换 Mencoder 转 flv mencoder -ffourcc FLV1 -lavfopts i_certify_that_my_video_st ...
- macbook视频格式转换_一键视频格式转换器for Mac-一键视频格式转换器Mac版下载 V6.3.37-PC6苹果网...
一键视频格式转换器Mac版是Mac电脑上的一款最好用的视频格式转换器之一.一键视频格式转换器Mac版是一款快速.简单.免费的视频转换器. 软件特色 一键视频格式转换器Mac版作为一款免费视频转换器软件 ...
- Ios php格式视频,ios视频格式转换器,ios视频格式mov转mp4,视频格式转换器
对于视频格式来说,现在使用最多的格式无非就是MP4格式.MP4格式几乎可以兼容各类播放器.不过由于视频格式种类众多,也有一些视频格式只适合独特播放器才能播放.最为常见的就是ios系统的mov视频格式 ...
- 手机怎样转换php格式,手机视频格式转换方法适合所有手机视频格式转换|如何转换手机视频格式...
怎么转换手机视频格式,是不是所有类型的手机视频格式都支持转换?是的!!!狸窝全能视频转换器支持所有手机视频格式转换,并且转换步骤也非常简单,可以说狸窝视频转换器是目前网上最简单易用功能强大的视频格式转 ...
- 让linux 支持视频格式,配置 Nginx 支持 f4v视频格式播放
Nginx默认不支持f4v视频格式播放,解决方法为编辑mime.types文件,在video/mp4加上f4v即可 types { # Data interchange application/ato ...
- html5加载vr视频格式,VR视频格式是什么_VR视频有什么格式_怎么分辨VR视频格式-VR之家...
VR视频格式是什么?很多人不知道什么是VR的视频格式,毕竟现在VR.3D视频的为式实在是太多了,给大家给绕晕了,那么如何才能简单分辨VR视频格式呢?下面就由我来给大家介绍一下如何分辨判断一个VR视频的 ...
- PHP将swf转为gif,如何将flash swf影片格式转换为gif动画?flash视频文件转换工具
打开软件之后我们看到右上角有一个"打开flash文件-"的按钮,我们点击此按钮,然后通过浏览文件位置的方式将我们所要进行转换的flash文件添加进来,当我们将文件添加进来之后系统会 ...
- mov和mp4格式哪个好_在线视频格式转换器哪个好?求一个能转换多种格式的工具...
原标题:在线视频格式转换器哪个好?求一个能转换多种格式的工具 在网络上,大家见到.接触到的视频格式估计也有十几种了,它们所支持.兼容的设备.软件大多不相同.以前,我们还不懂转换格式的时候,习惯了视频格 ...
最新文章
- 采购审批专题总结--bob
- 我的WINCE4.2历程(10)
- Codeforces 955C Sad powers(数论)
- C语言中使用MySQL(Linux下)
- Android 2D游戏引擎AndEngine配置环境
- zip unzip 命令
- 倍增时间 倍增...?
- 命令 启动顺序_笔记一: 启动选项与系统变量
- 使用Spring Security 3.1保护RESTful Web服务,第3部分
- 第三十三章 机械化印刷
- idea中浏览器打开页面404_深圳网站建设中的404页面有什么用
- 网站服务器需要定期重启吗,数据库服务器需要定期重启吗
- SAP License:SAP 清帐函数示例
- delphi构造析构调用顺序
- 大白话解析模拟退火算法、遗传算法
- Typora 下载及使用简介
- 关于HTML页面跳转的5种方法分享。
- 带大家认识下Kvaser CAN总线协议
- 基本数据类型的默认值
- yml连接sqlserver_spring连接sqlserver数据库