java调用ffmpeg,剪切minio的视频并上传到minio
前提
需要在安装minio并且本地安装ffmpeg中间件。
ffmpeg的安装查看文章
添加链接描述
代码如下
package com.example.springbootdemo.controller;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RuntimeUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.example.springbootdemo.util.MinioUtil;
import com.example.springbootdemo.vo.CutUrlVideoRequest;
import com.example.springbootdemo.vo.CutVideoRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* 视频剪辑测试
*
* @author zhangzheng
* @date 2023/9/20 16:56
**/
@RestController
@RequestMapping("/video")
public class FfmpegController {
@Value("${video.path}")
private String videoPath;
/**
* 剪切E:\video\test.mp4 到 videoPath 中,并上传到minio中,返回minio的方位地址
* 1.拼接剪切命令 (ffmpeg -ss 00:00:20 -to 00:00:36 -i E:\video\test.mp4 -c copy E:\video\output2.mp4)
* ffmpeg -ss 00:00:20 -to 00:00:36 -i E:\video\test.mp4 -c copy E:\video\output2.mp4
* 2.执行命令
* 3.上传到minio,并返回访问地址
*
*
* @param cutVideoRequest
*/
@PostMapping("/cut")
public String cutVideo(@RequestBody CutVideoRequest cutVideoRequest){
String source =videoPath+File.separator +"test.mp4";
String uuid = IdUtil.simpleUUID();
String target = videoPath+File.separator+uuid+".mp4";
String cmd = getCmd(source,target,cutVideoRequest);
try {
Process process = Runtime.getRuntime().exec(cmd);
process.waitFor();
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
// 将target上传到minio
File targetFile = new File(target);
String url = "";
try {
url = MinioUtil.upload(IoUtil.toStream(targetFile), uuid+".mp4");
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
return url;
}
/**
* 剪切视频
* 1.下载视频
* 2.拼接剪切命令
* 3.执行命令
* 4.上传到minio,删除视频,并返回访问地址
*
* @param cutUrlVideoRequest 包含视频地址,开始时间,结束时间
* @return
*/
@PostMapping("/cutUrlVideo")
public String cutUrlVideo(@RequestBody CutUrlVideoRequest cutUrlVideoRequest){
String downloadUrl = cutUrlVideoRequest.getUrl();
String[] urlStrs = downloadUrl.split("/");
String filename =urlStrs[urlStrs.length-1];
String suffix = filename.split("\\.")[1];
String source = videoPath+File.separator + filename;
HttpResponse response = HttpUtil.createGet(downloadUrl).execute();
FileUtil.writeBytes(response.bodyBytes(),source);
String uuid = IdUtil.simpleUUID();
String target = videoPath+File.separator+uuid+"."+suffix;
CutVideoRequest cutVideoRequest = new CutVideoRequest();
BeanUtils.copyProperties(cutUrlVideoRequest,cutVideoRequest);
String cmd = getCmd(source,target,cutVideoRequest);
try {
// Process process = Runtime.getRuntime().exec(cmd);
// process.waitFor();
RuntimeUtil.execForStr(cmd);
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
// 将target上传到minio
File targetFile = new File(target);
String url = "";
try {
url = MinioUtil.upload(IoUtil.toStream(targetFile), uuid+"."+suffix);
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
FileUtil.del(videoPath);
return url;
}
/**
* 获取剪切命令
* @param source
* @param target
* @param cutVideoRequest
* @return
*/
private String getCmd(String source,String target,CutVideoRequest cutVideoRequest) {
List<String> command = new ArrayList<>();
command.add("ffmpeg");
command.add("-ss");
command.add(cutVideoRequest.getStart());
command.add("-to");
command.add(cutVideoRequest.getEnd());
command.add("-i");
command.add(source);
command.add("-c copy");
command.add(target);
String cmd = String.join(" ",command);
return cmd;
}
}
完整程序包
下载地址
https://download.csdn.net/download/tadasii/88365256
本文介绍了如何在SpringBoot应用中使用Java调用ffmpeg工具,对本地或Minio存储的视频进行剪切,并将处理后的视频上传回Minio。详细步骤包括命令构建、文件操作和Minio上传等。
7501

被折叠的 条评论
为什么被折叠?



