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;
        }
    }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值