ffmpeg转换视频格式、获取视频截图、获取视频时长

package video;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FfmpegVideo {
    /**
     * 转换视频格式
     *
     * @param oldfilepath
     * @return
     */
    private static boolean convertVideo(String oldfilepath) {

        List<String> command = new java.util.ArrayList<String>();
        command.add("D:\\work\\file\\ffmpeg");// 工具路径
        command.add("-loglevel");// 日志记录级别
        command.add("quiet");// 不输出任何信息的日志级别【避免Process的waitFor方法死锁】
        command.add("-i");// 后面跟"输入文件"
        command.add("D:\\work\\file\\张蓉蓉_庄振凯-诚恳斗阵[68mtv.com].avi");// 原视频路径
        command.add("-ab");// 音频数据流量,一般选择32、64、96、128
        command.add("128");
        command.add("-acodec");// 音频编码
        command.add("libmp3lame");
        command.add("-ac");// 声道数1或2
        command.add("2");
        command.add("-ar");// 声音的采样频率
        command.add("22050");
        command.add("-qscale");// 视频数据流量 【动态码率】
        command.add("4");
        command.add("-r");// 帧数
        command.add("29.97");
        command.add("-y");// 覆盖输出文件
        command.add("D:\\work\\file\\avi_to_mp4_1.mp4");// 输出文件地址(转成文件后缀对应的格式)
        try {
            ProcessBuilder builder = new ProcessBuilder();
            builder.command(command);
            Process p = builder.start();
            p.waitFor();// 导致当前线程等待,如有必要,一直要等到由该 Process 对象表示的进程已经终止
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 获取视频其中一帧图片
     *
     * @param hour
     *            时
     * @param minute
     *            分
     * @param second
     *            秒
     * @throws IOException
     * @throws InterruptedException
     */
    public void getVideoImg(int hour, int minute, int second)
            throws IOException, InterruptedException {
        List<String> command = new ArrayList<String>();
        StringBuilder time = new StringBuilder();
        if (hour >= 10) {
            time.append(hour);
        } else {
            time.append("0").append(hour);
        }
        time.append(":");
        if (minute >= 10) {
            time.append(minute);
        } else {
            time.append("0").append(minute);
        }
        time.append(":");
        if (second >= 10) {
            time.append(second);
        } else {
            if (hour == 0 && minute == 0 && second == 0) {
                time.append("01");
            } else {
                time.append("0").append(second);
            }
        }
        command.add("D:\\work\\file\\ffmpeg");// 工具路径
        command.add("-y");// 覆盖已有文件
        command.add("-i");// 后面跟"输入文件"
        command.add("D:\\work\\file\\张蓉蓉_庄振凯-诚恳斗阵[68mtv.com].avi");// 视频路径
        command.add("-vframes");// 设置转换多少桢(frame)的视频
        command.add("1");
        command.add("-ss");// 从指定的时间(s)开始, [-]hh:mm:ss[.xxx]的格式也支持
        command.add(time.toString());
        command.add("-f");// 指定格式(音频或视频格式)
        command.add("mjpeg");
        command.add("-s");// 指定分辨率 (320x240)
        command.add(240 + "*" + 180);
        command.add("-an");// 取消音频
        command.add("D:\\work\\file\\1.jpg");// 输出文件
        ProcessBuilder pb = new ProcessBuilder();
        pb.command(command);
        pb.redirectErrorStream(true);
        Process p = pb.start();
        p.waitFor();
    }

    /**
     * 获取视频时长
     *
     * @param videoStr
     *            时长字符串
     * @throws IOException
     * @throws InterruptedException
     */
    private static void getVideoTime(String videoStr) throws IOException,
            InterruptedException {
        List<String> command = new ArrayList<String>();
        command.add("D:\\work\\file\\ffmpeg");// 工具路径
        command.add("-i");
        command.add("D:\\work\\file\\张蓉蓉_庄振凯-诚恳斗阵[68mtv.com].avi");
        StringBuffer sb = new StringBuffer();
        ProcessBuilder pb = new ProcessBuilder();
        pb.command(command);
        pb.redirectErrorStream(true);
        Process p = pb.start();
        BufferedReader br = new BufferedReader(new InputStreamReader(
                p.getInputStream()));
        String line = null;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        p.waitFor();
        Pattern pattern = Pattern.compile("Duration: (.*?),");
        Matcher matcher = pattern.matcher(videoStr);
        if (matcher.find()) {
            String time = matcher.group(1);// 视频时间
            String[] parts = time.split(":");
            int hours = Integer.parseInt(parts[0]);// 时
            int minutes = Integer.parseInt(parts[1]);// 分
            float seconds = Float.parseFloat(parts[2]);// 秒
            double milliSeconds = (hours * 60 * 60 * 1000)
                    + (minutes * 60 * 1000) + (seconds * 1000);// 毫秒数
        }
        pattern = Pattern.compile("w:\\d+ h:\\d+");
        matcher = pattern.matcher(videoStr);
        if (matcher.find()) {
            String wh = matcher.group();
            String[] strs = wh.split("\\s+");
            if (strs != null && strs.length == 2) {
                int width = Integer.parseInt(strs[0].split(":")[1]);// 视频width
                int height = Integer.parseInt(strs[1].split(":")[1]);// 视频height
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值