功能如题
前提:本文是在windows下测试可用,linux还未尝试
需提前准备好第三方软件ffmpeg,
且被操作的视频必须为ffmpeg支持的视频格式(ffmpeg支持大部分主流格式)
参考自:http://blog.sina.com.cn/s/blog_a5116c670102wiwj.html
可直接复制尝试使用,当然路径要改成自己的
(注释配合main方法里的调用,应该能看懂)
更多ffmpeg的命令(如:视频纵横比)参考文章一 、文章二
视频分辨率:720p 1280×720
1080p 1920x1080
package com.founder.util.video; import java.util.ArrayList; import java.util.List; public class TransferUtil { public static void main(String[] args) throws FFmpegException { boolean flag = transform("D:\\ffmpeg\\ffmpeg2016\\bin\\ffmpeg.exe", "d:\\ys\\StoryBrooke.mp4", "d:\\ys\\480p.flv", "480x320"); System.out.println(flag); } /** * 视频转换 * @param ffmpegPath ffmpeg路径 * @param oldPath 原视频地址 * @param newPath 新视频存放地址(包含视频格式) * @param resolution 分辨率 * @return * @throws FFmpegException */ public static Boolean transform(String ffmpegPath, String oldPath, String newPath, String resolution) throws FFmpegException { List<String> command = getFfmpegCommand(ffmpegPath, oldPath, newPath, resolution); if (null != command && command.size() > 0) { return process(command); } return false; } private static boolean process(List<String> command) throws FFmpegException { try { if (null == command || command.size() == 0) return false; Process videoProcess = new ProcessBuilder(command).redirectErrorStream(true).start(); videoProcess.getInputStream().close(); int exitcode = videoProcess.waitFor(); if (exitcode == 1) return false; return true; } catch (Exception e) { throw new FFmpegException("file transfer failed", e); } } private static List<String> getFfmpegCommand(String ffmpegPath, String oldfilepath, String outputPath, String resolution) throws FFmpegException { List<String> command = new ArrayList<String>(); command.add(ffmpegPath); // 添加转换工具路径 command.add("-i"); // 添加参数"-i",该参数指定要转换的文件 command.add(oldfilepath); // 添加要转换格式的视频文件的路径 command.add("-qscale"); // 指定转换的质量 command.add("4"); /*command.add("-ab"); //设置音频码率 command.add("64"); command.add("-ac"); //设置声道数 command.add("2"); command.add("-ar"); //设置声音的采样频率 command.add("22050");*/ command.add("-r"); // 设置帧速率 command.add("24"); command.add("-s"); // 设置分辨率 command.add(resolution); command.add("-y"); // 添加参数"-y",该参数指定将覆盖已存在的文件 command.add(outputPath); return command; } } class FFmpegException extends Exception { private static final long serialVersionUID = 1L; public FFmpegException() { super(); } public FFmpegException(String message) { super(message); } public FFmpegException(Throwable cause) { super(cause); } public FFmpegException(String message, Throwable cause) { super(message, cause); } }
浙公网安备 33010602011771号