Bootstrap

java使用ffmpeg截取视频流片段

public int ffmpegScreenMp4(String filePath,String rtsp,String outPath) {
        List<String> command = new ArrayList<String>();

        command.add("/usr/bin/ffmpeg");  //ffmpeg路径
        command.add("-rtsp_transport");
        command.add("tcp");
        command.add("-i");
        command.add(rtsp);//文件输入源
        command.add("-c:v");
        command.add("libx264");
        command.add("-b:v");
        command.add("3000k");
        command.add("-maxrate");
        command.add("3000k");
        command.add("-bufsize");
        command.add("6000k");
        command.add("-c:a");
        command.add("aac");
        command.add("-b:a");
        command.add("128k");
        command.add("-ar");
        command.add("44100");
        command.add("-pix_fmt");
        command.add("yuv420p");
        command.add("-profile:v");
        command.add("main");
        command.add("-preset");
        command.add("veryfast");
        command.add("-t");
        command.add("15");//截取视频时长 15代表15秒
        command.add(outPath);//文件输出路径

        Runtime runtime = Runtime.getRuntime();
        int i = -1;
        try {
            File file = new File(filePath);
            if(!file.exists()){
                boolean mkdirs = file.mkdirs();

            }
            Process exec = new ProcessBuilder(command).start();
            BufferedReader br = new BufferedReader(new InputStreamReader(exec.getErrorStream(),"gb2312"), 4096);
            String line = null;
            while ((line = br.readLine()) != null) {
                i++;

            }
        } catch (Exception e) {
            Thread.currentThread().interrupt();
        }
        return i;
    }
;