ffmpeg的相关方法封装
来源:互联网 发布:百视通网络电视下载 编辑:程序博客网 时间:2024/05/18 03:35
转载自:http://www.cnblogs.com/zhwl/p/3645593.html
1.判断系统支持那些编码
- /**
- * 判断系统支持那些编码
- * @param srcVideoPath
- * @return
- */
- public static void processFfmpegCodeFormat() {
- List<String> commend = new java.util.ArrayList<String>();
- commend.add(ffmpegPath);
- commend.add("-formats");
- try {
- ProcessBuilder builder = new ProcessBuilder();
- builder.command(commend);
- builder.redirectErrorStream(true);
- Process p= builder.start();
- BufferedReader buf = null; // 保存ffmpeg的输出结果流
- String line = null;
- buf = new BufferedReader(new InputStreamReader(p.getInputStream()));
- StringBuffer sb= new StringBuffer();
- while ((line = buf.readLine()) != null) {
- sb.append(line + "\n");
- continue;
- }
- p.waitFor();//这里线程阻塞,将等待外部转换进程运行成功运行结束后,才往下执行
- logger.info("【系统支持的视频编码】" + sb.toString());
- } catch (Exception e) {
- logger.error("获取失败 !");
- }
- }
2.ffmpeg将其他格式转换成FLV格式文件(未指定其他任何参数)
- /**
- * ffmpeg将其他格式转换成FLV格式文件(未指定其他任何参数)
- * ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
- * @param srcVideoPath 视频文件(原)
- * @param tarVideoPath 视频文件(新)
- * @return
- */
- public static boolean processFfmpegOther(String srcVideoPath,String tarVideoPath) {
- if (!checkfile(srcVideoPath)) {
- logger.error("【" + srcVideoPath + "】 不存在 !");
- return false;
- }
- List<String> commend = new java.util.ArrayList<String>();
- // String type =tarVideoPath.substring(tarVideoPath.lastIndexOf(".")+1, tarVideoPath.length());
- commend.add(ffmpegPath);
- commend.add( "-y");
- commend.add( "-i");
- commend.add(srcVideoPath);
- // if(type.toUpperCase().equals("MP4")){
- // commend.add( " -f h264 ");
- // }else{
- //
- // }
- commend.add(tarVideoPath);
- try {
- ProcessBuilder builder = new ProcessBuilder();
- builder.command(commend);
- Process process = builder.start();
- doWaitFor(process);
- process.destroy();
- if (!checkfile(tarVideoPath)) {
- logger.info(tarVideoPath + " is not exit! processFfmpegOther 转换不成功 !");
- return false;
- }
- return true;
- } catch (Exception e) {
- logger.error("【" + srcVideoPath + "】processFfmpegOther 转换不成功 !");
- return false;
- }
- }
- /**
- * ffmpeg按照时间段进行截取
- * @param srcVideoPath 视频文件(原)
- * @param tarImgPath 图片文件Path
- * @return
- */
- public static boolean processFfmpegBySureTime(String srcVideoPath,String tarImgPath) {
- if (!checkfile(srcVideoPath)) {
- logger.error("【" + srcVideoPath + "】 不存在 !");
- return false;
- }
- List<String> commend = new java.util.ArrayList<String>();
- commend.add(ffmpegPath);
- commend.add("-i");
- commend.add(srcVideoPath);
- commend.add("-y"); //覆盖
- commend.add("-f");
- commend.add("image2");
- commend.add("-ss");
- commend.add(BaseCommonUtil.STARTTIME);
- commend.add("-t");
- commend.add(BaseCommonUtil.ENDTIME);
- //commend.add("-s"); //指定图片大小
- //commend.add("500x400");
- commend.add(tarImgPath);
- try {
- ProcessBuilder builder = new ProcessBuilder();
- builder.command(commend);
- Process process = builder.start();
- doWaitFor(process);
- process.destroy();
- if (!checkfile(tarImgPath)) {
- logger.error("【" + srcVideoPath + "】 processFfmpegBySureTime 截图不成功 !");
- return false;
- }
- return true;
- } catch (Exception e) {
- logger.error("【" + srcVideoPath + "】 processFfmpegOther 截图不成功 !");
- return false;
- }
- }
4.ffmpeg按照时间段进行截取(加时间段)
- /**
- * ffmpeg按照时间段进行截取
- * ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
- * @param srcVideoPath 视频文件(原)
- * @param tarVideoPath 视频文件(新)
- * @param startTime 开始时间 形如:00:12:20
- * @param endTime 结束时间 形如:01:20:10
- * @return
- */
- public static boolean processFfmpegByTime(String srcVideoPath,String tarVideoPath,String startTime,String endTime) {
- if (!checkfile(srcVideoPath)) {
- logger.error("【" + srcVideoPath + "】 不存在 !");
- return false;
- }
- List<String> commend = new java.util.ArrayList<String>();
- commend.add(ffmpegPath);
- commend.add("-ss");
- if(startTime != null && !startTime.equals("")){
- commend.add(startTime);
- }else{
- commend.add(BaseCommonUtil.STARTTIME);
- }
- commend.add("-t");
- if(endTime != null && !endTime.equals("")){
- if(!calTime(getSplitStr(startTime),getSplitStr(endTime)).equals("")){
- commend.add(calTime(getSplitStr(startTime),getSplitStr(endTime)));
- }else{
- return false;
- }
- }else{
- commend.add(BaseCommonUtil.ENDTIME);
- }
- commend.add("-y"); //覆盖
- commend.add("-i");
- commend.add(srcVideoPath);
- commend.add(tarVideoPath);
- try {
- ProcessBuilder builder = new ProcessBuilder();
- builder.command(commend);
- Process process = builder.start();
- doWaitFor(process);
- process.destroy();
- if (!checkfile(tarVideoPath)) {
- logger.error("【" + tarVideoPath + "】processFfmpegByTime 截图不成功 !");
- return false;
- }
- return true;
- } catch (Exception e) {
- logger.error("【" + srcVideoPath + "】 processFfmpegByTime 截图不成功 !");
- return false;
- }
- }
5.ffmpeg将其他格式转换成其他格式文件(未指定其他任何参数)
- /**
- * ffmpeg将其他格式转换成其他格式文件(未指定其他任何参数)
- * ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
- * @param prefix 前缀
- * @param srcVideoPath 视频文件(原)
- * @param middlefix 中间的字符转
- * @param srcVideoPath 视频文件(转换后的路径)
- * @param suffix 结束的字符串
- * @return
- */
- public static boolean processFfmpegShellScript(String prefix,String srcVideoPath,String middlefix,String tarVideoPath,String suffix) {
- if (!checkfile(srcVideoPath)) {
- logger.error("【" + srcVideoPath + "】 不存在!");
- return false;
- }
- List<String> commend = new java.util.ArrayList<String>();
- commend.add(ffmpegPath);
- commend.add("-y");
- commend.add("-i");
- if(prefix != null && !prefix.equals("")){
- commend.add(prefix);
- }
- commend.add(srcVideoPath);
- if(middlefix != null && !middlefix.equals("")){
- commend.add(middlefix);
- }
- commend.add(tarVideoPath);
- if(suffix != null && !suffix.equals("")){
- commend.add(suffix);
- }
- try {
- ProcessBuilder builder = new ProcessBuilder();
- builder.command(commend);
- Process process = builder.start();
- doWaitFor(process);
- process.destroy();
- if (!checkfile(tarVideoPath)) {
- logger.error("【" + tarVideoPath + "】 processFfmpegShellScript 转换不成功 !");
- return false;
- }
- return true;
- } catch (Exception e) {
- logger.error("【" + srcVideoPath + "】 processFfmpegShellScript 转换不成功 !");
- return false;
- }
- }
6.ffmpeg转换成swf文件
- /**
- * ffmpeg转换成swf文件
- * @param srcVideoPath
- * @param tarVideoPath
- * @return
- */
- public static boolean processFfmpegSwf(String srcVideoPath,String tarVideoPath) {
- if (!checkfile(srcVideoPath)) {
- logger.error("【" + srcVideoPath + "】不存在!");
- return false;
- }
- List<String> commend = new java.util.ArrayList<String>();
- commend.add(ffmpegPath);
- commend.add("-y");
- commend.add("-i");
- commend.add(srcVideoPath);
- commend.add("-b");
- commend.add("360");
- commend.add("-r");
- commend.add("25");
- commend.add("-s");
- commend.add("640x480");
- commend.add("-ab");
- commend.add("56");
- commend.add("-ar");
- commend.add("22050");
- commend.add("-ac");
- commend.add("1");
- commend.add(tarVideoPath);
- try {
- ProcessBuilder builder = new ProcessBuilder();
- builder.command(commend);
- Process process = builder.start();
- doWaitFor(process);
- process.destroy();
- if (!checkfile(tarVideoPath)) {
- logger.error("【" + tarVideoPath + "】 processFfmpegSwf 转换不成功 !");
- return false;
- }
- return true;
- } catch (Exception e) {
- logger.error("【" + srcVideoPath + "】 processFfmpegSwf 转换不成功 !");
- return false;
- }
- }
7.ffmpeg将其他格式转换成WEBM格式文件
- /**
- * ffmpeg将其他格式转换成WEBM格式文件
- * ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,avi,flv等)
- * @param srcVideoPath 视频文件(原)
- * @param tarVideoPath 视频文件(新)
- * @return
- */
- public static boolean processFfmpegToWebm(String srcVideoPath,String tarVideoPath) {
- if (!checkfile(srcVideoPath)) {
- logger.error("【" + srcVideoPath + "】 不存在 !");
- return false;
- }
- List<String> commend = new java.util.ArrayList<String>();
- commend.add(ffmpegPath);
- commend.add( "-y");
- commend.add( "-i");
- commend.add(srcVideoPath);
- commend.add("-f");
- commend.add("webm");
- commend.add("-vcodec");
- commend.add("libvpx");
- commend.add("-acodec");
- commend.add("libvorbis");
- commend.add("-vb");
- commend.add("1600000");
- commend.add(tarVideoPath);
- try {
- ProcessBuilder builder = new ProcessBuilder();
- builder.command(commend);
- Process process = builder.start();
- doWaitFor(process);
- process.destroy();
- if (!checkfile(tarVideoPath)) {
- logger.info(tarVideoPath + " is not exit! processFfmpegToWebm 转换不成功 !");
- return false;
- }
- return true;
- } catch (Exception e) {
- logger.error("【" + srcVideoPath + "】processFfmpegToWebm 转换不成功 !");
- return false;
- }
- }
8.ffmpeg将其他格式转换成WEBM格式文件(1)
- /**
- * ffmpeg将其他格式转换成WEBM格式文件
- * ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,avi,flv等)
- * @param srcVideoPath 视频文件(原)
- * @param tarVideoPath 视频文件(新)
- * @return
- */
- public static boolean processFfmpegToOggOrOgv(String srcVideoPath,String tarVideoPath) {
- if (!checkfile(srcVideoPath)) {
- logger.error("【" + srcVideoPath + "】 不存在 !");
- return false;
- }
- List<String> commend = new java.util.ArrayList<String>();
- commend.add(ffmpeg2theoraPath);
- commend.add( "-V");
- commend.add( "4000");
- commend.add("-A");
- commend.add("128");
- commend.add(srcVideoPath);
- commend.add("-o");
- commend.add(tarVideoPath);
- try {
- ProcessBuilder builder = new ProcessBuilder();
- builder.command(commend);
- Process process = builder.start();
- doWaitFor(process);
- process.destroy();
- if (!checkfile(tarVideoPath)) {
- logger.info(tarVideoPath + " is not exit! processFfmpegToOggOrOgv 转换不成功 !");
- return false;
- }
- return true;
- } catch (Exception e) {
- logger.error("【" + srcVideoPath + "】processFfmpegToOggOrOgv 转换不成功 !");
- return false;
- }
- }
9.ffmpeg将其他音频格式转换成ogg格式文件
- /**
- * ffmpeg将其他音频格式转换成ogg格式文件
- * ffmpeg能解析的格式:(aac;ac3;au;wav;wma等)
- * @param srcVideoPath 音频文件(原)
- * @param tarVideoPath 音频文件(新)
- * @return
- */
- public static boolean processFfmpegToOgg(String srcVideoPath,String tarVideoPath) {
- if (!checkfile(srcVideoPath)) {
- logger.error("【" + srcVideoPath + "】 不存在 !");
- return false;
- }
- List<String> commend = new java.util.ArrayList<String>();
- commend.add(ffmpegPath);
- commend.add( "-i");
- commend.add(srcVideoPath);
- commend.add("-acodec");
- commend.add("libvorbis");
- commend.add("-ab");
- commend.add("64k");
- commend.add(tarVideoPath);
- try {
- ProcessBuilder builder = new ProcessBuilder();
- builder.command(commend);
- Process process = builder.start();
- doWaitFor(process);
- process.destroy();
- if (!checkfile(tarVideoPath)) {
- logger.info(tarVideoPath + " is not exit! processFfmpegToOggOrOgv 转换不成功 !");
- return false;
- }
- return true;
- } catch (Exception e) {
- logger.error("【" + srcVideoPath + "】processFfmpegToOggOrOgv 转换不成功 !");
- return false;
- }
- }
阅读全文
0 0
- ffmpeg的相关方法封装
- O-C相关05:方法的封装.
- jni封装ffmpeg接口遇到的错误修改方法
- Runtime相关方法封装
- FFMPEG的.Net封装,FFmpeg.NET
- FFmpeg的C++封装:FFmpegWrapper
- FFmpeg的C++封装:FFmpegWrapper
- FFmpeg的C++封装:FFmpegWrapper
- FFmpeg的C++封装:FFmpegWrapper
- FFMPEG的C++封装类
- ffmpeg的相关信息打印
- ffmpeg系列:使用C++类封装ffmpeg,封装读取视频帧功能Read()方法
- ffmpeg系列:使用C++类封装ffmpeg,封装视频帧解码功能Decode()方法
- ffmpeg系列:使用C++类封装ffmpeg,封装视频帧缩放转换rgb功能ToRGB()方法
- 对ffmpeg中的sws_scale的封装类
- 使用ffmpeg封装ts流的问题
- 使用ffmpeg封装ts流的问题
- ffmpeg支持的编解码器和封装格式
- scikit-learn工具学习
- Android Retrofit 2.0(一)初识介绍
- MAC上charles使用教程总结
- struts2的数据校验
- (六)android recovery 升级UI显示之资源文件
- ffmpeg的相关方法封装
- navicat远程连接mysql出现1130的错误
- [leetcode] 582. Kill Process
- 1.数字证书的知识
- 解析mysql不重复字段值求和
- 杭州落户-人才引进(其他)-挂靠人才中心 办理经验
- mysql数据库的主从复制原理以及减少主从复制的延迟方法
- JAVA中ByteArrayInputStream和ByteArrayOutputStream详解
- RARP协议的使用场景