Threadpool 代码
- package yong.method;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- /**
- *
- * @author 赵永恩
- *
- */
- public class ThreadPool {
- public static ExecutorService exec = Executors.newFixedThreadPool(3);
- public static synchronized void trans(String filePath, String outPath,String outImgPath,String pathAddress){
- ThreadTransCode trans=new ThreadTransCode(filePath,outPath,outImgPath,pathAddress);
- exec.execute(trans);
- }
- }
Threadtranscode代码
- package yong.method;
- /**
- *
- * @author 赵永恩
- *
- */
- public class ThreadTransCode implements Runnable {
- //原始文件
- private String filePath;
- //目标文件
- private String outPath;
- private String outImgPath;
- //工具地址
- private String pathAddress;
- public ThreadTransCode(String filePath, String outPath,String outImgPath,String pathAddress) {
- this.filePath = filePath;
- this.outPath = outPath;
- this.outImgPath = outImgPath;
- this.pathAddress = pathAddress;
- }
- public void run() {
- synchronized (this) {
- //System.out.println("转码开始..............");
- ConvertVideo cv = new ConvertVideo(filePath, outPath,outImgPath,pathAddress);
- cv.process();
- }
- }
- }
Convertvideo 代码
- package yong.method;
- import java.io.*;
- import java.util.List;
- /**
- *
- * @author 赵永恩
- *
- */
- public class ConvertVideo {
- // 原始文件
- private String filePath;
- // 目标文件
- private String outPath;
- private String outImgPath;
- private String pathAddress;
- /**
- * 工具[tool] 地址[address]
- */
- private String mencoderAdd;
- private String ffmpegAdd;
- public ConvertVideo(String filePath, String outPath, String outImgPath,String pathAddress) {
- this.filePath = filePath;
- this.outPath = outPath;
- this.outImgPath = outImgPath;
- this.pathAddress = pathAddress;
- this.mencoderAdd=pathAddress+"tool\ffmpeg\mencoder.exe";
- this.ffmpegAdd=pathAddress+"tool\ffmpeg\ffmpeg.exe";
- //System.out.println(ffmpegAdd);
- }
- public synchronized void process() {
- int type = checkContentType();
- if (type == 0) {
- this.ffmpegTransVideo();
- this.ffmpegTransImage();
- } else if (type == 1) {
- this.mencoderTransVideo();
- }
- }
- public synchronized int checkContentType() {
- String type = filePath.substring(filePath.lastIndexOf(".") + 1,
- filePath.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;
- }
- public synchronized static boolean checkfile(String path) {
- File file = new File(path);
- if (!file.isFile()) {
- return false;
- }
- return true;
- }
- /**
- * 使用mencoder转码
- *
- * @param videoPath
- * 源路径 -- 要转换的视频文件
- * @param targetPath
- * 目标路径 -- 转换后的视频flv
- * @return 返回目标路径
- */
- public synchronized String mencoderTransVideo() {
- List<String> commend = new java.util.ArrayList<String>();
- // commend.add("d:\flv\MediaCoder\codecs\mencoder.exe");
- //commend.add("D:\Tomcat-6.0.29\webapps\zyk\tool\ffmpeg\mencoder.exe");
- commend.add(mencoderAdd);
- commend.add(filePath);
- // 音频采用mp3编码
- commend.add("-oac");
- commend.add("mp3lame");
- // 采用高质DivX视频编码,视频码率为112kbps
- commend.add("-ovc");
- commend.add("lavc");
- commend.add("-lavcopts");
- commend
- .add("vcodec=flv:vbitrate=500:mbd=2:mv0:trell:v4mv:cbp:last_pred=3:dia=-1:cmp=3:vb_strategy=1");
- commend.add("-lameopts");
- commend.add("abr:br=56");
- // 声音采样频率设置,现为22K
- commend.add("-srate");
- commend.add("22050");
- // -sws就是用来设置品质的,默认值为2
- commend.add("-sws");
- commend.add("3");
- // 宽度为208,高度自动调整保持比例;
- // -vf scale=-3:176宽度自动调整保持比例,高度为176;如果想保持原来的大小可以不要这个参数
- commend.add("-vf");
- commend.add("scale=512:-3");
- // 帧速率设置
- commend.add("-ofps");
- commend.add("18");
- /*
- * mode=3:cbr:br=24单声道 音频码率为24kbps;-lameopts
- * mode=0:cbr:br=24立体声,音频码率为24kbps; 还可设置音量,-lameopts
- * mode=3:cbr:br=32:vol=1,设置范置为1~10,但不宜设得太高
- */
- commend.add("-lameopts");
- commend.add("vbr=3:br=128");
- commend.add("-o");
- commend.add(outPath);
- // 控制台显示执行的命令
- System.out.println(commend);
- try {
- ProcessBuilder builder = new ProcessBuilder();
- builder.command(commend);
- builder.start();
- return outPath;
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- /**
- * 使用ffmpeg转码
- *
- * @param videoPath
- * 源路径 -- 要转换的视频文件
- * @param targetPath
- * 目标路径 -- 转换后的视频flv
- * @return 返回目标路径
- */
- public synchronized String ffmpegTransVideo() {
- // if (!checkfile(videoPath)) {
- // System.out.println(videoPath + " is not file aaa");
- // return false;
- // }
- List<String> commend = new java.util.ArrayList<String>();
- //commend.add("d:\flv\MediaCoder\codecs\ffmpeg.exe");
- //commend.add("D:\Tomcat-6.0.29\webapps\zyk\tool\ffmpeg\ffmpeg.exe");
- commend.add(ffmpegAdd);
- commend.add("-i");
- commend.add(filePath);
- commend.add("-ab");
- commend.add("64");
- // commend.add(" -acodec ");
- // commend.add("codec");
- commend.add("-ac");
- commend.add("2");
- commend.add("-ar");
- commend.add("22050");
- // 清晰度 -qscale 4 为最好可是文件大, -qscale 6就可以了
- commend.add("-qscale");
- commend.add("6");
- // commend.add("-b");
- // commend.add("768");
- // commend.add("230");
- // commend.add("-s");
- // commend.add("352x240");
- // commend.add("-r");
- // commend.add("29.97");
- commend.add("-y");
- commend.add(outPath);
- //System.out.println(commend);
- try {
- ProcessBuilder builder = new ProcessBuilder();
- builder.command(commend);
- Process process = builder.start();
- InputStream is = process.getErrorStream();
- InputStreamReader inputStreamReader = new InputStreamReader(is);
- BufferedReader inputBufferedReader = new BufferedReader(
- inputStreamReader);
- String line = null;
- StringBuilder stringBuilder = new StringBuilder();
- while ((line = inputBufferedReader.readLine()) != null) {
- stringBuilder.append(line);
- }
- inputBufferedReader.close();
- inputBufferedReader = null;
- inputStreamReader.close();
- inputStreamReader = null;
- is.close();
- is = null;
- return outPath;
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- // 生成图片 参数String newfilename, String newimg
- public synchronized boolean ffmpegTransImage() {
- List<String> commend = new java.util.ArrayList<String>();
- // commend.add("d:\flv\MediaCoder\codecs\ffmpeg.exe");
- //commend.add("D:\Tomcat-6.0.29\webapps\zyk\tool\ffmpeg\ffmpeg.exe");
- commend.add(ffmpegAdd);
- commend.add("-i");
- commend.add(filePath);
- commend.add("-y");
- commend.add("-f");
- commend.add("image2");
- commend.add("-ss");
- commend.add("38");
- commend.add("-t");
- commend.add("0.001");
- commend.add("-s");
- commend.add("320x240");
- commend.add(outImgPath);
- try {
- ProcessBuilder builder = new ProcessBuilder();
- builder.command(commend);
- builder.start();
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- }
- }
测试类代码
- package yong.method;
- public class A {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- String filePath = "D:\test\a.MP4";
- String outPath = "d:\test\2011q.flv";
- String outImgPath = "d:\test\20111.jpg";
- String pathAddress="D:\Tomcat-6.0.29\webapps\zyk\";
- ThreadPool.trans(filePath,outPath,outImgPath,pathAddress);
- }
- }