JMF播放AVI格式的视频

public class Conver {

    public void run() {
        try {
            // 转换并截图
            String filePath = "E:\\wk\\测试用视频\\littlethree.avi";
            ConverVideo cv = new ConverVideo(filePath);
            cv.beginConver();
         } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String args[]) {
        Conver c = new Conver();
        c.run();
    }
}


import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

public class ConverVideo {

private String PATH;
    private String filerealname; // 文件名 不包括扩展名
    private String filename; // 包括扩展名
    private String flvfolder = "D:\\"; // flv视频的目录
    private String ffmpegpath = "D:\\ffmpeg\\bin\\ffmpeg.exe"; // ffmpeg.exe的目录

    public ConverVideo() {
    }

    public ConverVideo(String path) {
        PATH = path;
    }

    public String getPATH() {
        return PATH;
    }

    public void setPATH(String path) {
        PATH = path;
    }

    public boolean beginConver() {
        File fi = new File(PATH);
        filename = fi.getName();
        filerealname = filename.substring(0, filename.lastIndexOf("."))
                .toLowerCase();
        if (!checkfile(PATH)) {
           System.out.println(PATH + "文件不存在");
           return false;
        }

        if (process()) { //proess函数用来转换视频格式
            PATH = null;
            return true;
        } else {
            PATH = null;
            return false;
       }
    }

    private boolean process() {
        int type = checkContentType();
        boolean status = false;
        if (type == 0) {
        status = processUSEAVI(PATH);
        }
        return status;
    }
    //检查文件类型
    private int checkContentType() {
        String type = PATH.substring(PATH.lastIndexOf(".") + 1, PATH.length()).toLowerCase();
        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("flv")) {
            return 0;
        }
        return 9;
    }

    private boolean checkfile(String path) {
        File file = new File(path);
        if (!file.isFile()) {
            return false;
        } else {
            return true;
        }
    }

    private boolean processUSEAVI(String oldfilepath) {
        if (!checkfile(PATH)) {
            System.out.println("上传视频文件:"+oldfilepath + "不是文件");
            return false;
        }
        List commend = new java.util.ArrayList();
        commend.add(ffmpegpath); // 添加转换工具路径
        commend.add("-i"); //添加参数"-i",该参数指定要转换的文件
        commend.add(oldfilepath); // 添加要转换格式的视频文件的路径
        commend.add("-ab"); //设置音频码率 
        commend.add("128");
      /*commend.add("-b");//比特率
        commend.add("1000");*/
        commend.add("-qscale");
        commend.add("6");
        commend.add("-s");//
        commend.add("352x288");
        commend.add("-vcodec"); //视频流编码
        commend.add("h263");
        commend.add("-acodec"); //音频编码
        commend.add("pcm_s16le"); //音频编码
        commend.add("-ac"); //设置声道数 1就是单声道,2就是立体声
        commend.add("2");
        commend.add("-ar"); //设置声音的采样频率
        commend.add("22050");
        commend.add("-r"); //设置帧频
        commend.add("25");
        commend.add("-y"); // 添加参数"-y",该参数指定将覆盖已存在的文件
        commend.add(flvfolder + filerealname + ".avi");
        try {
            ProcessBuilder builder = new ProcessBuilder();
            String cmd = commend.toString();
            builder.command(commend);
            Process p = builder.start();
            doWaitFor(p);
            p.destroy();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public int doWaitFor(Process p) {
        InputStream in = null;
        InputStream err = null;
        int exitValue = -1; // returned to caller when p is finished
        try {
            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()视频上传转码错误:" + 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;
    }

}

你可能感兴趣的:(JMF AVI)