12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- package com.fdkankan.fusion.common.util;
- import lombok.extern.slf4j.Slf4j;
- import java.io.*;
- import java.util.*;
- @Slf4j
- public class VideoUtil {
- public static String mergeVideo(LinkedHashSet<String> fromVideoFileList, String newVideoFile) {
- File tagFile = new File(newVideoFile);
- if(!tagFile.exists()){
- tagFile.mkdirs();
- }
- String fame = UUID.randomUUID().toString().replace("-","");
- String fileName = fame+".mp4";
- String imgName = fame+".jpg";
- try {
- List<String> voidTS = new ArrayList<>();
- for (String fromVideoFile : fromVideoFileList) {
- String format = "%s -y -i %s -vcodec copy -bsf:v h264_mp4toannexb -f mpegts %s";
- String name = UUID.randomUUID().toString().replace("-","");
- String command = String.format(format, "ffmpeg", fromVideoFile, newVideoFile +"/"+name + ".ts");
- ShellUtil.execCmd(command);
- voidTS.add(newVideoFile +"/"+name + ".ts");
- }
- StringBuilder tsPath = new StringBuilder();
- tsPath.append("ffmpeg");
- tsPath.append(" -i ");
- tsPath.append("concat:");
- for (int t = 0; t < voidTS.size(); t++) {
- if (t != voidTS.size() - 1) {
- tsPath.append(voidTS.get(t) + "|");
- } else {
- tsPath.append(voidTS.get(t));
- }
- }
- tsPath.append(" -vcodec ");
- tsPath.append(" copy ");
- tsPath.append(" -bsf:a ");
- tsPath.append(" aac_adtstoasc ");
- tsPath.append(" -movflags ");
- tsPath.append(" +faststart ");
- tsPath.append(newVideoFile).append("/").append(fileName);
- ShellUtil.execCmd(tsPath.toString());
- ffmpegVideo(newVideoFile +"/"+ fileName,newVideoFile +"/"+ imgName,"200","200");
- //删除生成的ts文件
- for (String filePath : voidTS) {
- File file = new File(filePath);
- file.delete();
- }
- return fileName;
- } catch (Exception e) {
- e.printStackTrace();
- log.error(" 合并失败-{}", e.getMessage());
- return null;
- }
- }
- /**
- *
- * @param sourceFile 原文件路径
- * @param thumbFile 目标文件路径
- * @param thumbWidth 宽度
- * @param thumbHigh 高度
- * @return
- * ffmpeg -i bb.mp4 -y -vframes 1 -vf scale=100:100/a thumb.jpg
- */
- public static boolean ffmpegVideo(String sourceFile, String thumbFile, String thumbWidth, String thumbHigh){
- String cmd = " ffmpeg -i " + sourceFile + " -y -vframes 1 -vf scale=" + thumbWidth + ":" + thumbHigh + "/a " + thumbFile;
- ShellUtil.execCmd(cmd);
- File file = new File(thumbFile);
- if(!file.exists()){
- return false;
- }
- return true;
- }
- }
|