验证中...
Languages: Java
Categories: JavaScript 工具
Latest update 2019-07-08 15:29
FFMpegUtils.java
Raw Copy
package com.yiijing.common.utils;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RuntimeUtil;
import com.alibaba.dubbo.common.utils.LogUtil;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.MessageFormat;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author wanghui@weimob.com
* @package com.yiijing.common.utils
* @projectName yiijing-service
* @date 2019年07月04日 16时16分
* @Description: (这里用一句话描述这个类的作用)
*/
@Component
public class FFMpegUtils {
/**
* Windows下 ffmpeg.exe的路径
*/
// private static String ffmpegEXE = "D:\\Downloads\\ffmpeg-20180528-ebf85d3-win64-static\\bin\\ffmpeg.exe";
/**
* Mac下 ffmpeg.exe的路径
*/
private static String ffmpegEXE = "ffmpeg";
private static final ExecutorService pool = Executors.newCachedThreadPool();
/**
*
* ffmpeg.exe -ss 00:00:01 -y -i 视频.mp4 -vframes 1 new.jpg
* @param timeCoverImg 视频的第几秒作为封面图
* @param videoInputPath 视频的路径
* @param frame 帧数
* @param coverOutputPath 视频的封面图的路径
* @throws Exception
*/
public static void convetor(String timeCoverImg, String videoInputPath, int frame, String coverOutputPath)
throws Exception {
Process process = null;
String command = "";
// 将mp3转换成wav 采样率16000 16kbs wav音频文件
// FFMPEG_PATH ffmpeg路径 如:D:/ffmpeg/bin/ffmpeg.exe
command = MessageFormat.format("{0} -ss {1} -y -i {2} -vframes {3} {4}",
ffmpegEXE, timeCoverImg, videoInputPath, String.valueOf(frame), coverOutputPath);
try {
process = Runtime.getRuntime().exec(command);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 使用这种方式会在瞬间大量消耗CPU和内存等系统资源,所以这里我们需要对流进行处理
InputStream errorStream = process.getErrorStream();
InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
BufferedReader br = new BufferedReader(inputStreamReader);
while (br.readLine() != null) {
}
if (br != null) {
br.close();
}
if (inputStreamReader != null) {
inputStreamReader.close();
}
if (errorStream != null) {
errorStream.close();
}
}
/**
* 获取音频\视频总时间
* @param videoPath 视频路径
* @return
*/
public static int getVideoTime(String videoPath) {
try {
String command = "";
// 将mp3转换成wav 采样率16000 16kbs wav音频文件
// FFMPEG_PATH ffmpeg路径 如:D:/ffmpeg/bin/ffmpeg.exe
command = MessageFormat.format("{0} -i {1}",
ffmpegEXE, videoPath);
final Process p = Runtime.getRuntime().exec(command);
//从输入流中读取视频信息
BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
//从视频信息中解析时长
String regexDuration = "Duration: (.*?), start: (.*?), bitrate: (\\d*) kb\\/s";
Pattern pattern = Pattern.compile(regexDuration);
Matcher m = pattern.matcher(sb.toString());
if (m.find()) {
int time = getTimeLength(m.group(1));
System.out.println(videoPath+",视频时长:"+time+", 开始时间:"+m.group(2)+",比特率:"+m.group(3)+"kb/s");
return time;
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
/**
* 格式:"00:00:10.68"
* @param timeLength
* @return
*/
private static int getTimeLength(String timeLength){
int min=0;
String zero = "0";
String[] strs = timeLength.split(":");
if (strs[0].compareTo(zero) > 0) {
//秒
min += Integer.valueOf(strs[0])*60*60;
}
if(strs[1].compareTo(zero) > 0){
min += Integer.valueOf(strs[1])*60;
}
if(strs[2].compareTo(zero) > 0){
min += Math.round(Float.valueOf(strs[2]));
}
return min;
}
public static void main(String[] args) throws Exception {
String videoPath = "/Users/joy/Desktop/9c618ea4dc7945d98ac1b15091647c4c.mp4";
System.out.println(getVideoTime("/Users/joy/Desktop/9c618ea4dc7945d98ac1b15091647c4c.mp4"));
convetor("00:00:04", videoPath, 1, "/Users/joy/Desktop/ffmpeg/".concat(IdUtil.simpleUUID()).concat(".jpg"));
}
}

Comment list( 1 )

hehe 2019-07-08 15:29

这工具不错,简单,好使

You need to Sign in for post a comment

Help Search

183227_9af5e6a8_1826025 111910_4d91f001_1826025