java使用ffmpeg将MP4转为ts(使用命令)

	public static void main(String[] args) {
		//获取系统名称
        String osName = System.getProperty("os.name");
            logger.info("osName:" + osName);
            //根据系统名称获取ffmpeg,classpath下的路径
            URL resource;
            if (osName.toLowerCase().contains("windows")){
                resource = VideoUtil.class.getResource("/tool/ffmpeg-amd64.exe");//该类名称为VideoUtil
            }else {
                resource = VideoUtil.class.getResource("/tool/ffmpeg-amd64");//该类名称为VideoUtil
            }
			//获取执行命令,音量和比特率可以不传
            List<String> ffmpegCommand = getFfmpegCommand(resource.getPath(), sourceFile, destFile, volume, videoBitrate);
            //执行转换
            boolean process = process(ffmpegCommand);
    }


	/**
     * 获取命令
     *具体命令的含义可以百度
     *
     * @param ffmpegPath  ffmpeg路径
     * @param oldfilepath 源文件
     * @param outputPath  转换后的文件
     * @param videoBitrate 画面比特率
     * @param volume 音量 0-100
     * @return 命令
     */
    private static List<String> getFfmpegCommand(String ffmpegPath,
                         String oldfilepath, String outputPath, Integer volume, Integer videoBitrate) {
        List<String> command = new ArrayList<String>();
        command.add(ffmpegPath);
        command.add("-i");
        command.add(oldfilepath);
        //视频解码器
        command.add("-c:v");
        command.add("libx264");
        command.add("-x264opts");
        command.add("force-cfr=1");
        command.add("-vsync");
        command.add("cfr");
        command.add("-vf");
        command.add("idet,yadif=deint=interlaced");
        if (null != volume){
            command.add("-filter:a");
            command.add("\"volume="+ (volume/100d) +"\"");
        }else {
            command.add("-filter_complex");
            command.add("aresample=async=1000");
        }
        //视频分辨率
        command.add("-s");
        command.add("1920*1080");
        //视频比特率,比特率越大视频越清楚,不能过大也不能过小
        if (null != videoBitrate){
            command.add("-b:v");
            command.add(videoBitrate + "");
        }
        //音频解码器
        command.add("-c:a");
        command.add("libmp3lame");
        command.add("-b:a");
        command.add("192k");
        command.add("-pix_fmt");
        command.add("yuv420p");
        //输出的视频格式
        command.add("-f");
        command.add("mpegts");
        command.add(outputPath);
        return command;
    }

	/**
     * 执行转换
     * @param command 命令
     * @return 返回是否成功
     * @throws Exception 异常
     */
    private static boolean process(List<String> command) throws Exception {

        try {

            if (null == command || command.size() == 0) {
                return false;
            }
            Process videoProcess = new ProcessBuilder(command).redirectErrorStream(true).start();

            new PrintStream(videoProcess.getErrorStream()).start();

            new PrintStream(videoProcess.getInputStream()).start();

            int exitCode = videoProcess.waitFor();

            if (exitCode == 1) {
                return false;
            }
            return true;
        } catch (Exception e) {
            throw new Exception("file uploadfailed", e);
        }
    }

    /**
     * 文件读取
     */
    private static class PrintStream extends Thread {
        InputStream inputStream = null;

        PrintStream(InputStream is) {
            inputStream = is;
        }
        
        @Override
        public void run() {
            try {
                while (this != null) {
                    int ch = inputStream.read();
                    if (ch != -1) {
                        System.out.print((char) ch);
                    } else {
                        break;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值