java 不等待 视频转码_java调用ffmpeg,mencoder进行视频转换,读取时长等
以前做的一个基于ffmpeg的视频格式转换的程序,现在抽空整理一下,很多地方都是从别的大神那借鉴的,只是把自己的觉得有用的,对别人有帮助的拿出来分享分享,下面是代码
package video;
import it.sauronsoftware.jave.AudioAttributes;
import it.sauronsoftware.jave.DefaultFFMPEGLocator;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.EncodingAttributes;
import it.sauronsoftware.jave.FFMPEGLocator;
import it.sauronsoftware.jave.InputFormatException;
import it.sauronsoftware.jave.VideoAttributes;
import it.sauronsoftware.jave.VideoSize;
import java.io.File;
import java.util.List;
public class ConvertVideo {
private static File inFolder;//需要转换的文件夹
private static File outFolder;//保存avi文件的文件夹地址
private static File inFile;//每个文件的file对象
private static String name;//文件名称,用在保存avi文件,name.avi,不带扩展名的
private static String Name;//文件名称,(带扩展名的)
public ConvertVideo(File inFolder,File outFolder){//构造函数,进行测试时传入两个文件夹地址
this.inFolder=inFolder;
this.outFolder=outFolder;
}
public static void Test(){
String[] fileList=inFolder.list();
for(int i=0;i
inFile=new File(inFolder.getAbsolutePath()+"\\"+fileList[i]);
Name=inFile.getName();
name=getFileNameNoEx(Name);//获得没有扩展名的文件名
System.out.println(name);
File txtFile=new File(inFolder.getAbsolutePath()+"\\"+name+".txt");//视频上传完毕会生成一个同名的txt文件
if(txtFile.exists()){//判断是否上传完毕
convert();
}
}
}
/**
* 截取不带扩展名的文件名
* @param : String
* @return : String
*
*/
public static String getFileNameNoEx(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot >-1) && (dot < (filename.length()))) {
return filename.substring(0, dot);
}
}
return filename;
}
/**
* 视频转换调用函数
*@return : void
*
*/
public static void convert() {
if(!checkfile(inFile)){
System.out.println(inFile+" is not file");
return;
}
if (process()) {
System.out.println("ok");
}
}
/**
*视频转换函数
* @return : boolean
*
*
*/
private static boolean process() {
int type = checkContentType();
boolean status = false;
System.out.println("name="+name+" "+"Name="+Name+" "+"type="+type);
if (type==0) {
status = processFLV(inFile);// 直接将文件转为avi文件
} else if (type==1) {
processAVI(inFile);
status=true;
}else if(type==2){
System.out.println("你的文件已经是avi格式,无须转换!");
}
return status;
}
/**
*检查视频类型
* @param : void
* @return : int
*
*/
private static int checkContentType() {
String path=inFile.getName();//path是绝对路径
String type = path.substring(path.lastIndexOf(".") + 1,
path.length()).toLowerCase();
//ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
if (type.equals("avi")) {
return 2;
} 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("mkv")){
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;
}
/**
* 检查文件是不是File对象
* @param : File
* @return : boolean
*
*/
private static boolean checkfile(File path){
if(!path.isFile()){
return false;
}
return true;
}
/**
*对于ffmpeg无法解析的wmv9,rm,rmvb视频类型,利用mencoder进行解析,menconder视频转换
* @param : File
* @return : String
*
*
*/
private static String processAVI(File inFile) {
String inpath=inFile.getAbsolutePath();//PATH为file对象,先获得绝对路径,赋值给path
String outpath =outFolder.getAbsolutePath()+"\\"+name+".avi";//为输出的绝对路径
File Test=new File(outpath);
if(Test.exists()){
System.out.println("视频已经转换,无须再次转换!");
return null;
}else{
List commend=new java.util.ArrayList();
commend.add("e:\\mencoder");
commend.add(inpath);
commend.add("-o");
commend.add(outpath);
commend.add("-oac");
commend.add("mp3lame");
commend.add("-lameopts");
commend.add("cbr:br=32");
commend.add("-ovc");
commend.add("xvid");
commend.add("-xvidencopts");
commend.add("bitrate=1024");
commend.add("-vf");
commend.add("scale=720:-3");
//commend.add("-o");
StringBuffer sb=new StringBuffer();
for(int i=0;i
sb.append(commend.get(i)+" ");
}
try{
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
builder.start();
return "ok";
}catch(Exception e){
e.printStackTrace();
return null;
}
}
}
/**
*
* 对于ffmpeg可以解析的格式(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等) ,用ffmpeg进行解析,ffmpeg视频转换
* @param : File
* @return : loolean
*
*/
private static boolean processFLV(File oldfilepath) {
FFMPEGLocator locator = new FFMPEGLocator() {
@Override
protected String getFFMPEGExecutablePath() {
// TODO Auto-generated method stub
return "c://ffmpeg//ffmpeg.exe";
}
};
File source = oldfilepath;
String out=outFolder.getAbsolutePath()+"\\"+name+".avi";//输出的绝对路径
File target=new File(out);
//GetVideoInfo getVideoInfo = new GetVideoInfo();
//String decoder=getVideoInfo.getDecoder(source);
if(target.exists()/*||decoder.equals("h264")*/){
System.out.println("视频无需进行转换!");
return false;
}else{
AudioAttributes audio = new AudioAttributes();//设置音频属性
audio.setCodec("libmp3lame");//音频使用libmp3lame编码
audio.setBitRate(new Integer(128000));//设置比特率
audio.setChannels(new Integer(1));//设置通道,缺省为1
audio.setSamplingRate(new Integer(22050));//
VideoAttributes video = new VideoAttributes();//设置视频属性
video.setCodec("mpeg4");//设置编码格式
video.setBitRate(new Integer(1024000));//设置比特率
video.setFrameRate(new Integer(10));//设置
/*getVideoSize getvideoSize=new getVideoSize();
VideoInfo videoInfo=new VideoInfo();
videoInfo=getvideoSize.getVideoInfo(source);
VideoSize videoSize=videoInfo.getSize();
if(videoSize.getHeight()!=1280||videoSize.getWidth()!=400){
ResourceBundle resource=ResourceBundle.getBundle("parameter",Locale.getDefault());
String a=resource.getString("height");
String b=resource.getString("width");
int height=Integer.valueOf(a);
int width=Integer.valueOf(b);
video.setSize(new VideoSize(width,height));//设置大小
}*/
//setSize set=new setSize();
//set.set(video,source);
video.setSize(new VideoSize(1080,720));
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat("avi");
attrs.setAudioAttributes(audio);
attrs.setVideoAttributes(video);
Encoder encoder = new Encoder(locator);
try {
encoder.encode(source, target, attrs);
return true;
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (InputFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (EncoderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
}
}
这是一个简单的视频处理的方法,轻喷
java 不等待 视频转码_java调用ffmpeg,mencoder进行视频转换,读取时长等相关推荐
- java线程等待按钮点击_java如何用多线程使线程在sleep时等待按钮按下?
比如这是一个八皇后的程序packagecom.equeen;importjava.applet.Applet;importjava.awt.Button;importjava.awt.Color;im ...
- C# 调用FFmpeg处理音视频的示例
FFmpeg 开源.跨平台.体积小.功能强大,提供了录制.转换以及流化音视频的完整解决方案. 官网:https://www.ffmpeg.org/ 百科:https://baike.baidu.com ...
- JAVA调用FFmpeg实现音视频转码加水印功能
目录 目录 写在前面 MAVEN引用 获取音视频基本信息 音频转码成Mp3格式 视频转码成Mp4格式 视频转码成Mp4并添加文字水印 视频转码成Mp4并添加图片水印 测试代码 写在前面 如今各大云厂商 ...
- java用ffmpeg转码_java运用FFMPEG视频转码技术.doc
java运用FFMPEG视频转码技术 基于windows系统安装FFMPEG转码技术 软件特点: 被转视频基本不失真(支持超清.高清.标清): 转码速度比较现成转码软件快非常多(不管文件有多大,此软件 ...
- 使用java调用ffmpeg命令将视频分屏并音视频同时播放(二分屏)
实现想法 使用ffmpeg命令中的filter_complex(滤镜)参数来进行展示 引入javacv版本 <dependency><groupId>org.bytedeco& ...
- java本地方法出现问题怎么办_Java调用本地方法又是怎么一回事
JNI JNI即Java Native Interface,它能在Java层实现对本地方法的调用,一般本地的实现语言主要是C/C++,其实从虚拟机层面来看JNI挺好理解,JVM主要使用C/C++ 和少 ...
- java短信接口源码_java免费短信接口开发源码
java免费短信接口开发源码 更多 作者:捷信通来源:www.jiexintong.cn日期:2014-07-30 17:08:51 微宏捷信通短信接口提供适应C#.Java..NET等多种主流开发语 ...
- FFmpeg Android 学习(一):Android 如何调用 FFMPEG 编辑音视频
一.概述 在Android开发中,我们对一些音视频的处理比较无力,特别是编辑音视频这部分.而且在Android上对视频编辑方面,几乎没有任何API做支持,MediaCodec(硬编码)也没有做支持.那 ...
- php ffmpeg 转码mp4,PHP+FFMPEG实现将视频自动转码成H264标准Mp4文件
配置php.ini文件 file_uploads = on ;//是否允许通过HTTP上传文件的开关.默认为ON即是开 upload_tmp_dir ;//文件上传至服务器上存储临时文件的地方,如果没 ...
最新文章
- SVD(奇异值分解)小结
- 美军重视扩展现实技术的研究和应用
- oracle数据库enq: TX - allocate ITL entry性能诊断
- 每天一个linux命令(6):rmdir 命令
- boost::parameter::template_keyword相关的测试程序
- python 服务注册_将python程序注册为Ubuntu系统服务,并开机启动的方法。
- 我的Serverless实战——引领云计算的下一个十年
- 微型计算机的应用特点,微型计算机的特点及应用
- Pravega Flink Connector Table API 进阶功能探秘
- 用枚举法实现工厂模型
- 软件使用,Microsoft Visual C++运行库合集包
- CVPR2022论文列表(中英对照)
- 黑帽SEO-网页劫持
- 数据结构(四)图 —— 编程作业 04 :哈利·波特的考试
- 用AI生成的画作,在淘宝拍卖到了4位数
- 【转】超级珍贵的卤水秘方大全!
- 千山独行-一个人的创业路(连载一)
- matlab三维实心图,matlab画实心散点图
- 富贵竹的水养殖方法和注意事项,6个步骤帮你养出翠绿富贵竹
- linux打地鼠课程设计,数字电路课程设计打地鼠.doc