锐英源精品开源心得,转载请注明:“锐英源www.wisestudy.cn,孙老师作品,电话13803810136。”需要全文内容也请联系孙老师。
java调用ffmpeg转换视频,并读取转换后视频的分辨率、大小以及时长
使用ffmpeg转换视频时需要注意的是:
1.传入的原视频是否是ffmpeg支持的格式。
2.转换后的格式是不是ffmpeg能支持的格式。
3.大家要到ffmpeg.org官网下载ffmpeg的exe程序
下载相关文件
ffmpeg地址:https://ffmpeg.zeranoe.com/builds/,我下载是windows 64位static版本。
xuggler下载地址:http://download.csdn.net/download/hpb21/6338317
下面的代码我上传到了github,需要的可以下载下来看看。
地址:https://github.com/zhengdesheng/ffmpeg
步骤:
1.研究java如何调用外部程序
2.研究ffmpeg转换视频格式的命令
3.利用xuggle获取ffmpeg解析的ts流的时长、分辨率以及文件大小。
代码:
1.ffmpeg转换实现
package vedio.ffmpeg;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class FfmpegUtil {
public static Boolean ffmpeg(String ffmpegPath, String inputPath, String outputPath) throws FFmpegException{
if (!checkfile(inputPath)) {
throw new FFmpegException("文件格式不合法");
}
int type = checkContentType(inputPath);
List command = getFfmpegCommand(type, ffmpegPath, inputPath, outputPath);
if (null != command && command.size() > 0) {
return process(command);
}
return false;
}
private static int checkContentType(String inputPath) {
String type = inputPath.substring(inputPath.lastIndexOf(".") + 1, inputPath.length()).toLowerCase();
// ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
if (typ