ffmpeg命令旋转视频

第一步,解析视频文件

private static String processVideo(String filePath) {
        List<String> commend=new java.util.ArrayList<String>();
        commend.add("ffmpeg");//可以设置环境变量从而省去这行//d:\ffmpeg\ffmpeg.exe
        commend.add("-i");
        commend.add(filePath);
        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);
                continue;
            }
            p.waitFor();//这里线程阻塞,将等待外部转换进程运行成功运行结束后,才往下执行
            return sb.toString();
        } catch (Exception e) {
            System.out.println("ffmpeg解析视频文件【" + filePath +"】失败!");
            return null;
        }
    }

 第二步,获取相关metadata,获取视频的旋转角度

private static String getRotate() {
        String rotate = "";
        String result =   processVideo(videoPath);
        System.out.println(result);
        if(StringUtils.isNotEmpty(result)){
            PatternCompiler compiler =new Perl5Compiler();
            try {
                String regexRotate ="rotate\\s*:\\s*(\\w*)\\s*";

                Pattern patternRotate = compiler.compile(regexRotate, Perl5Compiler.CASE_INSENSITIVE_MASK);
                PatternMatcher matcherRotate = new Perl5Matcher();

                if(matcherRotate.contains(result, patternRotate)){
                    MatchResult re = matcherRotate.getMatch();

                    rotate = re.group(1);
                    System.out.println("rotate:"+rotate);
                }

            } catch (MalformedPatternException e) {
                System.out.println("获取【" + videoPath +"】视频信息失败!");
            }
            System.out.println("获取【" + videoPath +"】视频信息成功!");
        }else{
            System.out.println("执行成功!但未获取到【" + videoPath +"】视频信息!");
        }
        return rotate;
    }

 第三步,根据rotate角度信息,调用旋转命令

 private static boolean rotate90mp4(String rotate) {
        List<String> commend = new ArrayList<String>();
        //ffmpeg -i demo.mp4 -vf "transpose=1" o.mp4
        try {
            Runtime runtime = Runtime.getRuntime();
// String cmd = " ffmpeg -i "+ videoPath + " -vf \"transpose=1\"  d:\\out\\vertical.mp4";
            StringBuffer buf = new StringBuffer();
            buf.append(" ffmpeg ");
            buf.append(" -i ");
            buf.append(videoPath);
            if(!StringUtils.isBlank(rotate)){
                buf.append(" -vf ");
                if(rotate.equals("90"))
                    buf.append(" \"transpose=1\" ");
                else if(rotate.equals("180"))
                    buf.append(" \"transpose=2,transpose=2\" ");
                else if(rotate.equals("270"))
                    buf.append(" \"transpose=2\" ");
            }
            buf.append(destPath);
            runtime.exec(buf.toString());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

猜你喜欢

转载自zhen217.iteye.com/blog/2372841
今日推荐