使用FFmpeg将视屏剪辑并上传到华为服务器上

注意需要自己在windows上安装好FFmpeg组件
教程地址:https://www.jianshu.com/p/2b609afb9800
这套代码不管在linux还是windows系统上都能使用
linux上剪辑视屏也是需要进行组件的安装:https://blog.csdn.net/L1569850979/article/details/123269527?spm=1001.2014.3001.5501
上代码
导包

 <!--视屏剪辑--><dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>30.1.1-jre</version></dependency><dependency><groupId>org.bytedeco</groupId><artifactId>ffmpeg-platform</artifactId><version>4.4-1.5.6</version></dependency>

返回实体

import io.swagger.annotations.ApiModel;
import lombok.Data;import java.io.Serializable;/*** @author lilinchun* @date 2022/3/2 0002 11:45*/
@Data
@ApiModel
public class FileVideoVO implements Serializable {/*** 原视频url*/private String url;/*** 截取视屏url*/private String cutUrl;/*** 文件名称*/private String objectname;}

主要代码

    /*** * @param file 视屏原文件* @param start 剪辑开始位置* @param end 剪辑结束位置* @return* @throws Exception*/
@Overridepublic FileVideoVO videoUpload(MultipartFile file, String start, String end) throws Exception {FileVideoVO fileVideoVO = new FileVideoVO();ObsClient obsClient = new ObsClient(ak, sk, endPoint);//判断文件格式String fileName = file.getOriginalFilename();String fileType = fileName.substring(fileName.lastIndexOf("."));// 判断文件格式switch (fileType) {case OssFormat.MP3:break;case OssFormat.MP4:break;default:throw new BusinessException(ErrorCode.FORMAT_ERROR.getCode(), ErrorCode.FORMAT_ERROR.getMsg());}InputStream inputStreamOld=file.getInputStream();//源文件上传PutObjectResult putObjectResultold = obsClient.putObject("bks", "clipFile/" + fileName, inputStreamOld);fileVideoVO.setUrl(putObjectResultold.getObjectUrl());//剪辑文件上传File file1 = TransferToFile.multipartFileToFile(file);log.info("文件字符串位置------------------:" + file1.getAbsolutePath());String str;if (file1.getAbsolutePath().lastIndexOf("\\") > 0) {str = file1.getAbsolutePath().substring(0, file1.getAbsolutePath().lastIndexOf("\\") + 1);} else {str = file1.getAbsolutePath().substring(0, file1.getAbsolutePath().lastIndexOf("/") + 1);}log.info("后传递路径-----------:" + str);str = new String(str.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);String filPath = FfmpegUtil.cutOutVideo(file1, str, start, end);InputStream inputStream=getInputStream(filPath);//将剪辑文件上传到华为云服务器上PutObjectResult putObjectResult = obsClient.putObject("bks", "clipFile/" + IdUtil.simpleUUID() + fileType, inputStream);inputStreamOld.close();inputStream.close();TransferToFile.deleteTempFile(file1);fileVideoVO.setCutUrl(putObjectResult.getObjectUrl());obsClient.close();//删除临时文件FfmpegUtil.deleteFilePlace(FfmpegUtil.sortOut(filPath));return fileVideoVO;}//读取临时文件public InputStream getInputStream(String resultPath) throws Exception {Thread.sleep(3000);FileInputStream  fis = new FileInputStream(resultPath);int available = fis.available();log.info("外流的大小----------:" + available);if(available==0) {int i = 10;while (i > 1) {log.info("流的大小----------:" + available);if (available == 0) {fis.close();i--;//睡三秒Thread.sleep(3000);} else {i = 0;}fis.close();fis = new FileInputStream(resultPath);available = fis.available();}}if (available == 0) {log.info("文件截取失败---------");throw new BusinessException(ErrorCode.CUT_ERROR.getCode(), ErrorCode.CUT_ERROR.getMsg());}return fis;}

几个工具类

import org.springframework.web.multipart.MultipartFile;import java.io.*;/*** @author lilinchun* @date 2022/3/1 0001 16:26*/
public class TransferToFile {//获取流文件private static void inputStreamToFile(InputStream ins, File file) {try {OutputStream os = new FileOutputStream(file);int bytesRead = 0;byte[] buffer = new byte[8192];while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {os.write(buffer, 0, bytesRead);}os.close();ins.close();} catch (Exception e) {e.printStackTrace();}}/*** MultipartFile 转 File** @param file* @throws Exception*/public static File multipartFileToFile(MultipartFile file) throws Exception {File toFile = null;if (file.equals("") || file.getSize() <= 0) {file = null;} else {InputStream ins = null;ins = file.getInputStream();toFile = new File(file.getOriginalFilename());inputStreamToFile(ins, toFile);ins.close();}return toFile;}/*** 删除本地临时文件* @param file*/public static void deleteTempFile(File file) {if (file != null) {File del = new File(file.toURI());del.delete();}}}
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.base.Joiner;
import com.llcbk.error.BusinessException;
import com.llcbk.error.ErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.bytedeco.javacpp.Loader;import java.io.*;
import java.util.Arrays;/*** @author lilinchun* @date 2022/3/1 0001 16:11*/
@Slf4j
public class FfmpegUtil {/*** 视频裁剪** @param file 视频 文件* @param outputDir 临时目录* @throws Exception 异常** star 开始位置* end 其实位置* -i 读取源文件* -ss 文件开始位置* -to 文件结束文职* -b  设置比特率* -y 表示输出文件,如果已存在则覆盖* -vcodec copy 使用跟原视频相同的视频编解码器* -acodec copy 使用跟原视频相同的音频编解码器*/public static String cutOutVideo(File file, String outputDir,String start,String end) throws Exception {String videoPath ;try {videoPath = file.getAbsolutePath();} catch (Exception e) {log.info("错误----------:" + e.getMessage());throw new BusinessException(ErrorCode.CUT_ERROR.getCode(),ErrorCode.CUT_ERROR.getMsg());}String resultPath =Joiner.on(File.separator).join(Arrays.asList(outputDir, IdUtil.simpleUUID() + "." + "mp4"));ProcessBuilder builder =new ProcessBuilder("ffmpeg","-i",videoPath,"-ss",start,"-to",end,"-c","copy","-y",resultPath);builder.inheritIO().start();return resultPath;}/*** 视频裁剪** @param file 视频 文件* @param outputDir 临时目录* @throws Exception 异常** -i 读取源文件* -ss 文件开始位置* -to 文件结束文职* -b  设置比特率* -y 表示输出文件,如果已存在则覆盖* -vcodec copy 使用跟原视频相同的视频编解码器* -acodec copy 使用跟原视频相同的音频编解码器*/public static String cutOutVideoOld(File file, String outputDir,String star,String end) throws Exception {String videoPath = null;try {videoPath = file.getAbsolutePath();} catch (Exception e) {log.info("错误----------:" + e.getMessage());throw new BusinessException(ErrorCode.CUT_ERROR.getCode(),ErrorCode.CUT_ERROR.getMsg());}String resultPath =outputDir+IdUtil.simpleUUID()+"."+"mp4";String ffmpeg = Loader.load(org.bytedeco.ffmpeg.ffmpeg.class);ProcessBuilder builder =new ProcessBuilder(ffmpeg,"-i",videoPath,"-codec","copy","-ss",star,"-to",end,"-b","2000k","-y","-threads","5","-preset","ultrafast","-strict","-2",resultPath);builder.inheritIO().start();return resultPath;}/*** 删除本地临时文件* @param path*/public static void deleteFilePlace(String path) {System.out.println("path----------------:"+path);File file = new File(path);File del = new File(file.toURI());System.out.println("删除本地临时文件-------"+del.delete());}/*** 整理路径*/public static String sortOut(String path){if(StringUtils.isNotBlank(path)){char[] c = path.toCharArray();String strNew ="";for (int i = 0; i < c.length; i++) {if (c[i] == '\\'&&c[i]==c[i+1]) {continue;}strNew=strNew+c[i];}return strNew;}else {return path;}}}

使用FFmpeg将视屏剪辑并上传到华为服务器上相关推荐

  1. 怎么把文件上传云服务器上,如何把文件上传到云服务器上

    如何把文件上传到云服务器上 内容精选 换一换 将文件上传至Windows云服务器一般会采用MSTSC远程桌面连接的方式.本节为您介绍本地Windows计算机通过远程桌面连接,上传文件至Windows云 ...

  2. 怎么把数据文件上传云服务器,如何将数据上传到云服务器上

    如何将数据上传到云服务器上 内容精选 换一换 您可以通过导出SQL语句的方式将数据库备份到弹性云服务器上.弹性云服务器不限制存放哪些数据,但是数据必须符合国家法律法规.您可以在弹性云服务器上存放数据库 ...

  3. ubuntu服务器ftp无法上传文件,ubuntu服务器上传文件ftp

    ubuntu服务器上传文件ftp 内容精选 换一换 通过Web浏览器登录主机,提供协同分享.文件传输.文件管理和预置命令等功能.用户在主机上执行的所有操作,被云堡垒机记录并生成审计数据.协同分享指会话 ...

  4. html文件上传到云服务器,把html文件上传到云服务器上

    把html文件上传到云服务器上 内容精选 换一换 华为云帮助中心,为用户提供产品简介.价格说明.购买指南.用户指南.API参考.最佳实践.常见问题.视频帮助等技术文档,帮助您快速上手使用华为云服务. ...

  5. moba上传文件到服务器,图片上传到远程服务器上的方法

    图片上传到远程服务器上的方法 内容精选 换一换 将文件上传至Windows云服务器一般会采用MSTSC远程桌面连接的方式.本节为您介绍本地Windows计算机通过远程桌面连接,上传文件至Windows ...

  6. 上传文件到华为云云服务器,文件上传到云服务器上

    文件上传到云服务器上 内容精选 换一换 Winscp无法连接到服务器.SSH连接工具例如Xshell可以正常连接云服务器.其他SSH工具连接云服务器正常,但是Winscp无法连接到服务器.说明SSH服 ...

  7. ios上传文件云服务器上,ios文件上传服务器

    ios文件上传服务器 内容精选 换一换 在当前的迁移流程中,可能会存在迁移后ECS控制台镜像名称与实际操作系统不一致的现象.在当前机制下,该现象属于正常现象.该处显示的是下发ECS时使用的镜像名称,而 ...

  8. Linux文本复制到记事本文本文件乱码,解决“在windows里的记事本里编辑的汉字文本文件,上传到linux服务器上出现乱码“问题...

    一.前期准备 1.首先在windows环境下打开记事本,然后创建一个包含汉字和英文的文本文件,输入内容"测试在windows里的记事本里编辑的文本文 件,上传到linux服务器上会不会出现乱 ...

  9. 华为服务器上传文件,云服务器上传文件方式

    云服务器上传文件方式 内容精选 换一换 安装传输工具在本地主机和Windows云服务器上分别安装数据传输工具,将文件上传到云服务器.例如QQ.exe.在本地主机和Windows云服务器上分别安装数据传 ...

最新文章

  1. 读大话数据结构之二--------算法(上)
  2. Java的12个语法糖【转】
  3. 一文说说这十多年来计算机玩摄影的历史
  4. P4行为模型BMV2依赖关系安装:thrift nanomsg nnpy安装
  5. 华为-公有云-云硬盘-磁盘类型及性能介绍
  6. 基于高斯分布和OneClassSVM的异常点检测
  7. linux阻止程序,Linux:阻止某些应用程序/主机名的IPv6
  8. c里面的fflush函数
  9. mysql二进制日志管理
  10. android浏览器资源嗅探,GitHub - icemanyandy/VBrowser-Android: 全网视频嗅探缓存APP
  11. 测试cpu温度软件mac,mac电脑怎么查看cpu温度和风扇转速的详细步骤
  12. SQL Server~T-SQL编程基础
  13. 卧槽!出了一个Python实时目标跟踪系统神器!
  14. 中国数码纺织印花染料行业运行态势与投资前景预测报告2022-2027
  15. 张一鸣:小成功需要朋友,大成功需要敌人
  16. 【韧性设计】韧性设计模式:重试、回退、超时、断路器
  17. 如何在阿里云上搭建个人网站(学习记录)
  18. PHP 根据文字内容添加图片上实现自动换行的小程序
  19. 耳机是如何是发出声音的?
  20. 钟表时钟时间管理PPT模板

热门文章

  1. Paint.getTextBounds
  2. 顺序表:将两个有序表合并成一个新的有序顺序表
  3. css3拆解正方体动画效果
  4. 图示法求候选键------软考
  5. python课程设计矩阵对角线之和_python对角矩阵
  6. 谭安林:大数据在智能外呼系统的应用
  7. 应届大学生想拿HCIE有必要吗?
  8. 正确的自动驾驶“安全观”应是什么样子?
  9. 提升效率! Linux 管理员必用的10个关键技巧
  10. OD使用教程11(困境) - 调试篇11|解密系列