ffmpeg进行视频转换和截取
来源:互联网 发布:php判断来路域名 编辑:程序博客网 时间:2024/05/22 12:07
package com.compress;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.down.ftps.FtpUitl;
public class ConvertVideo extends Thread{
public File inputPath;
private String outputPath = ""; private String ffmpegPath = ""; public ConvertVideo(File inputPath, String outputPath){ this.inputPath = inputPath; this.outputPath = outputPath; this.ffmpegPath = "ffmpeg"; } public void run(){ File localFile = new File(outputPath); if (!localFile.exists()){ localFile.mkdirs(); } if (!checkfile(inputPath)) { System.out.println(inputPath + " is not file"); return; } if (process()) { System.out.println("ok"); inputPath.delete(); } } private boolean process() { int type = checkContentType(); boolean status = false; if (type == 0) { System.out.println("----截取视频-----"); status = cutVideo(inputPath);//直接开始视频截取 } else if (type == 1) { System.out.println("----插件无法直接截取该视频格式-----"); String avifilepath = processAVI(type); if (avifilepath == null) return false;// 没有得到avi格式 //status = processFLV(avifilepath);// 将avi转成flv格式 } return status; } private int checkContentType() { String type = inputPath.getAbsolutePath().substring(inputPath.getAbsolutePath().lastIndexOf(".") + 1, inputPath.getAbsolutePath().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; } private boolean checkfile(File file) { if (!file.isFile()) { return false; } return true; } // 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等), 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式. private String processAVI(int type) { List<String> commend = new ArrayList<String>(); commend.add(ffmpegPath + "mencoder"); commend.add(inputPath.getAbsolutePath()); commend.add("-oac"); commend.add("lavc"); commend.add("-lavcopts"); commend.add("acodec=mp3:abitrate=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(outputPath + "a.avi"); try { ProcessBuilder builder = new ProcessBuilder(); Process process = builder.command(commend).redirectErrorStream(true).start(); new PrintStream(process.getInputStream()); new PrintStream(process.getErrorStream()); process.waitFor(); return outputPath + "a.avi"; } catch (Exception e) { e.printStackTrace(); return null; } } // ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等) private boolean processFLV(File oldfilepath) { if (!checkfile(inputPath)) { System.out.println(oldfilepath + " is not file"); return false; } List<String> command = new ArrayList<String>(); command.add(ffmpegPath); command.add("-i"); command.add(oldfilepath.getAbsolutePath()); command.add("-ab"); command.add("56"); command.add("-ar"); command.add("22050"); command.add("-qscale"); command.add("8"); command.add("-r"); command.add("15"); command.add("-s"); command.add("600x500"); command.add(outputPath + oldfilepath.getName()); try { // 方案1
// Process videoProcess = Runtime.getRuntime().exec(ffmpegPath + “ffmpeg -i ” + oldfilepath
// + ” -ab 56 -ar 22050 -qscale 8 -r 15 -s 600x500 ”
// + outputPath + “a.flv”);
// 方案2 Process videoProcess = new ProcessBuilder(command).redirectErrorStream(true).start(); //new PrintStream(videoProcess.getErrorStream()).start(); new PrintStream(videoProcess.getInputStream()).start(); videoProcess.waitFor(); System.out.println("结束"); inputPath.delete(); //FtpUitl.xb--; return true; } catch (Exception e) { e.printStackTrace(); return false; } } // 视频截取 @SuppressWarnings("unused")private boolean cutVideo(File oldfilepath) { if (!checkfile(oldfilepath)) { System.out.println(oldfilepath + " is not file"); return false; } List<String> command = new ArrayList<String>(); command.add("D:\\ffmpeg\\ffmpeg.exe"); command.add("-ss"); command.add("00:02:20"); command.add("-t"); command.add("00:01:20"); command.add("-i"); command.add(oldfilepath.getAbsolutePath()); command.add("-vcodec"); command.add("copy"); command.add("-acodec"); command.add("copy"); command.add("D:\\temp.mp4"); try { // 方案2 Process videoProcess = new ProcessBuilder(command).redirectErrorStream(true).start(); new PrintStream(videoProcess.getInputStream()).start(); videoProcess.waitFor(); System.out.println("结束"); inputPath.delete(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } public static void main (String args[]){ File f = new File("D:\\ceshi.mp4"); System.out.println(f.isFile()); ConvertVideo convert = new ConvertVideo(null,null); convert.cutVideo(f); }
}
class PrintStream extends Thread
{
java.io.InputStream __is = null;
public PrintStream(java.io.InputStream is)
{
__is = is;
}
public void run() { try { while(this != null) { int _ch = __is.read(); if(_ch != -1) System.out.print((char)_ch); else break; } } catch (Exception e) { e.printStackTrace(); } }
}
0 0
- ffmpeg进行视频转换和截取
- 使用FFMPEG进行视频截取与合并
- 用dos和ffmpeg进行视频编码转换
- ffmpeg进行视频截图以及视频转换
- (工具ffmpeg)视频转换为flv格式,视频截取图片
- (工具ffmpeg)视频转换为flv格式,视频截取图片
- (工具ffmpeg)视频转换为flv格式,视频截取图片
- 图像视频编码和FFmpeg(3)-----用FFmpeg进行图像格式转换和AVFrame简介
- FFmpeg截取视频
- ffmpeg 截取视频
- ffmpeg转码、截图、截取视频
- Java操作ffmpeg--截取视频
- python+ffmpeg截取视频段
- ffmpeg 截取一段视频命令
- (工具ffmpeg)视频转换为flv格式,视频截取图片(c#)
- FFmpeg的使用——PHP转换视频、截取视频以及JW Player播放器控制
- 利用ffmpeg.exe进行视频转换,抓图
- 使用ffmpeg进行视频解码以及图像转换
- HttpClient请求数据
- JavaWeb---response头--Refresh(刷新)、禁用浏览器缓存、响应体
- SDUT OJ 树结构练习——排序二叉树的中序遍历
- ROS进二阶学习笔记(1) TF 学习笔记2: -- TF Broadcaster 编写 (Python)
- Android 沉浸式状态栏
- ffmpeg进行视频转换和截取
- opencv 对数组(矩阵)的一些操作
- UFLDL Sparse Autoencoder
- 中国纺织企业迈入跨国布局新阶段
- 向剪切板复制文本的js库——clipboard.js
- 关于Lt分发系统的时序图分析
- 实现支付宝支付从底部弹窗效果
- Linux中的线程局部存储(二)
- DB2 自增长列导入、导出测试