123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- package com.iden.common.videotool;
- import com.iden.common.util.DateUtils;
- import com.iden.common.util.FileUtil;
- import lombok.extern.slf4j.Slf4j;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.InputStreamReader;
- import java.util.Date;
- import java.util.List;
- import java.util.UUID;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- @Slf4j
- public class VideoTool {
- /**
- * 视频分解为图片
- * @param ffmpegPath
- * @param videoFilePath
- * @param saveTmpDir
- * @param shootEndTime
- * @return
- * @throws Exception
- */
- public static Long fetchAllPic(String ffmpegPath,String videoFilePath, String saveTmpDir,String targetDir, Date shootEndTime) throws Exception {
- Long endTime = shootEndTime.getTime() ;
- //ms
- long duration = getVideoTime(ffmpegPath,videoFilePath) * 1000;
- Long beginTime = endTime - duration;
- //每秒多少帧
- int framRate = 1;
- String cmd = ffmpegPath + " -y -i " + videoFilePath + " -r " + framRate + " " + saveTmpDir + "%08d.jpg";
- log.info("start fetchAllPic:" + cmd);
- Runtime.getRuntime().exec(cmd);
- log.info("fetchAllPic end");
- moveFile(beginTime,saveTmpDir,targetDir,framRate);
- return duration;
- }
- private static void moveFile(Long beginTime,String saveTmpDir,String targetDir, int framRate){
- File saveTmpDirFile = new File(saveTmpDir);
- File[] tmpFiles = FileUtil.sortByName(saveTmpDirFile.listFiles());
- if (tmpFiles != null && tmpFiles.length > 0) {
- for (int i = 0; i< tmpFiles.length; i++){
- File tmpFile = tmpFiles[i];
- String tmpFileName = tmpFile.getName();
- String tmpFileExt = tmpFileName.substring(tmpFileName.lastIndexOf(".") + 1);
- long time = beginTime + (i * (1000/framRate));
- String tmpFileNameNew = DateUtils.formatToDateStr(new Date(time),"yyyyMMddHHmmss") + "_" + UUID.randomUUID().toString() + "." + tmpFileExt;
- File tmpNewFile = new File(targetDir,tmpFileNameNew);
- tmpFile.renameTo(tmpNewFile);
- }
- }
- }
- /**
- * 视频剪辑
- * @param ffmpegPath
- * @param beginTime
- * @param endTime
- * @param videoSrcPath
- * @param targetPath
- */
- public static void cutVideo(String ffmpegPath, String beginTime,String endTime,String videoSrcPath,String targetPath) throws Exception {
- Long duration = DateUtils.betweenTimeSS(beginTime, endTime); //s
- String cmd = ffmpegPath + " -ss " + beginTime + " -i " + videoSrcPath
- + " -t " + duration + " -c:v copy -c:a copy " + targetPath;
- log.info("start cutVideo :" + cmd);
- Runtime.getRuntime().exec(cmd);
- log.info("cutVideo end");
- }
- /**
- * 获取视频总时间
- * @param ffmpeg_path ffmpeg路径
- * @param video_path 视频路径
- * @return
- */
- public static int getVideoTime( String ffmpeg_path,String video_path) {
- List<String> commands = new java.util.ArrayList<String>();
- commands.add(ffmpeg_path);
- commands.add("-i");
- commands.add(video_path);
- try {
- ProcessBuilder builder = new ProcessBuilder();
- builder.command(commands);
- final Process p = builder.start();
- //从输入流中读取视频信息
- 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 = getTimelen(m.group(1));
- System.out.println(video_path+",视频时长:"+time+", 开始时间:"+m.group(2)+",比特率:"+m.group(3)+"kb/s");
- return time;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return 0;
- }
- //格式:"00:00:10.68"
- private static int getTimelen(String timelen){
- int min=0;
- String strs[] = timelen.split(":");
- if (strs[0].compareTo("0") > 0) {
- min+=Integer.valueOf(strs[0])*60*60;//秒
- }
- if(strs[1].compareTo("0")>0){
- min+=Integer.valueOf(strs[1])*60;
- }
- if(strs[2].compareTo("0")>0){
- min+=Math.round(Float.valueOf(strs[2]));
- }
- return min;
- }
- public static void main(String[] args) {
- System.out.println(getVideoTime("D:\\program\\ffmpeg\\ffmpeg-4.3.1-2021-01-01-essentials_build\\bin\\ffmpeg.exe","e:/output99.mp4"));
- }
- }
|