/*** 文件最大100M*/private static long upload_maxsize = 100 * 1024 * 1024;/*** 文件10M*/private static long upload_minsize = 10 * 1024 * 1024;/*** 视频文件允许格式*/private static String[] allowVideoFiles = {".avi", ".mpg", ".wmv", ".3gp",".mov", ".asf", ".asx", ".vob", ".rm", ".rmvb", ".mp4", ".flv"};/*** 图片文件允许格式*/private static String[] allowImgFiles = {".jpg", ".jpeg", ".png"};public static UploadFileDTO uploadVideo(MultipartFile multipartFile,WxProgramConfig wxProgramConfig) {UploadFileDTO entity = new UploadFileDTO();boolean canSave;String fileName = multipartFile.getOriginalFilename();// 判断文件不为空if (multipartFile.getSize() != 0 && !multipartFile.isEmpty()) {// 判断文件大小if (multipartFile.getSize() <= upload_maxsize) {// 文件类型判断if (checkFileType(fileName, allowVideoFiles)) {canSave = true;} else {canSave = false;log.error("视频类型不允许");}} else {canSave = false;log.error("视频大小超范围");}} else {canSave = false;log.error("视频为空");}// 执行保存if (canSave) {String fileEndName = getFileExt(fileName);String result= DateUtil.formatFullsTime(LocalDateTime.now());System.out.println(result);Path sourcePath = Paths.get(   result + fileEndName);try {// 如果没有files文件夹,则创建
//                if (!Files.isWritable(path)) {
//                    Files.createDirectories(Paths.get("files/"));
//                }byte[] bytes = multipartFile.getBytes();// 文件写入指定路径Files.write(sourcePath, bytes);log.debug("文件写入成功...");String ossUrl = "";//视频小于10M 直接上传 不压缩if(multipartFile.getSize() > upload_minsize){// 压缩视频格式 上传到ossossUrl = compressVideo(sourcePath.toString(), "libx264", "fast", 30, "copy", "128k");System.out.println("============"+ossUrl);}else{ossUrl = OSSUploadUtils.localFile(sourcePath.toString(), "app_content");}entity.setPath(ossUrl);// 所有业务完成后 删除本地文件Files.delete(sourcePath);log.debug("文件删除成功...");} catch (IOException e) {e.printStackTrace();}}return entity;}
public static String compressVideo(String videoUrl,String vcodec, String preset,Integer crf,String acodec,String ab) {String videoOutPath = DateUtil.formatFullsTime(LocalDateTime.now())+"video.mp4";String videoAll = videoUrl;try {FFMpegUtil.compressVideo(videoUrl, vcodec, preset, crf, acodec, ab, videoOutPath);videoAll =  OSSUploadUtils.localFile(videoOutPath.toString(), "app_content");Files.delete(Paths.get(videoOutPath));} catch (IOException e) {e.printStackTrace();}return videoAll;}

FFMpegUtil.java

package cn.ysx.fun.common.utils;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;/*** @Title:* @Package:cn.yuanshixiong.common.utils.video* @ClassName:FFMpegUtil* @Description: 合成音频视频* @Author:zhixiang.yang* @CreateDate:2018/9/2014:10* @UpdateUser:zhixiang.yang* @UpdateDate:2018/9/2014:10* @UpdateRemark:* @Version:1.0*/
public class FFMpegUtil {public static String FFMPEG_PATH;public static String IMG_PATH;public static String VIDEO_PATH;static {Properties prop = new Properties();InputStream in = FFMpegUtil.class.getClassLoader().getResourceAsStream("ffmpeg.properties");try {prop.load(in);FFMPEG_PATH=prop.getProperty("ffmpegPath");IMG_PATH=prop.getProperty("imgPath");VIDEO_PATH=prop.getProperty("videoPath");} catch (IOException e) {e.printStackTrace();}}/*** @method  convetor* @Author: zhixiang.yang* @Description:  视频格式转换* @Date: 17:41 2018/9/20* @param videoInputPath  原视频路径* @param videoOutPath   新视频路径* @return: void* @respbody:*/// ffmpeg.exe -i old.mp4 new.avi  public static void convetor(String videoInputPath, String videoOutPath) throws Exception {List<String> command = new ArrayList<String>();command.add(FFMPEG_PATH);command.add("-i");command.add(videoInputPath);command.add(videoOutPath);ProcessBuilder builder = new ProcessBuilder(command);Process process = null;try {process = builder.start();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}// 使用这种方式会在瞬间大量消耗CPU和内存等系统资源,所以这里我们需要对流进行处理InputStream errorStream = process.getErrorStream();InputStreamReader inputStreamReader = new InputStreamReader(errorStream);BufferedReader br = new BufferedReader(inputStreamReader);String line = "";while ((line = br.readLine()) != null) {}if (br != null) {br.close();}if (inputStreamReader != null) {inputStreamReader.close();}if (errorStream != null) {errorStream.close();}}/*** @method  webmToMp4convetor* @Author: yizhe.wang* @Description:  当前用于对webM格式至Map格式转换* @Date: 17:41 2018/9/29* @param videoInputPath  原视频路径* @param videoOutPath   新视频路径* @return: void* @respbody:*/// ffmpeg -fflags +genpts -i 1.webm -r 24 1.mp4public static void webmToMp4convetor(String videoInputPath, String videoOutPath) throws Exception {List<String> command = new ArrayList<String>();command.add(FFMPEG_PATH);command.add("-fflags");command.add("+genpts");command.add("-i");command.add(videoInputPath);command.add("-r");command.add("24");command.add(videoOutPath);ProcessBuilder builder = new ProcessBuilder(command);Process process = null;try {process = builder.start();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}// 使用这种方式会在瞬间大量消耗CPU和内存等系统资源,所以这里我们需要对流进行处理InputStream errorStream = process.getErrorStream();InputStreamReader inputStreamReader = new InputStreamReader(errorStream);BufferedReader br = new BufferedReader(inputStreamReader);String line = "";while ((line = br.readLine()) != null) {}if (br != null) {br.close();}if (inputStreamReader != null) {inputStreamReader.close();}if (errorStream != null) {errorStream.close();}}/*** @method  mergeVideoAndAudio* @Author: zhixiang.yang* @Description:  合并视频音频* @Date: 17:54 2018/9/20* @param videoInputPath 原视频的路径* @param audioInputPath 音频的路径* @param videoOutPath   视频与音频结合之后的视频的路径* @param time           视频的长度 ,单位为 s* @return: void* @respbody:*/// 将视频和音频结合,并指定视频的长度,同时生成结合之后的视频文件  注意先音频再视频// ffmpeg.exe -i tsd.mp4 -i "周笔畅+-+最美的期待.mp3" -t 7 -y new.avipublic static void mergeVideoAndAudio(String videoInputPath,String audioInputPath,String videoOutPath,double time){List<String> command = new ArrayList<String>();command.add(FFMPEG_PATH);command.add("-i");command.add(audioInputPath);command.add("-i");command.add(videoInputPath);command.add("-strict");command.add("-2");command.add("-t");command.add(String.valueOf(time));command.add("-y");command.add(videoOutPath);ProcessBuilder builder = new ProcessBuilder(command);Process process = null;try {process = builder.start();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}// 使用这种方式会在瞬间大量消耗CPU和内存等系统资源,所以这里我们需要对流进行处理InputStream errorStream = process.getErrorStream();InputStreamReader inputStreamReader = new InputStreamReader(errorStream);BufferedReader br = new BufferedReader(inputStreamReader);try {String line = "";while ((line = br.readLine()) != null) {}if (br != null) {br.close();}if (inputStreamReader != null) {inputStreamReader.close();}if (errorStream != null) {errorStream.close();}}catch (Exception e){throw  new RuntimeException("mergeVideoAndAudio视频合成异常!");}}/*** @method  getVideoImg* @Author: zhixiang.yang* @Description:  获取视频截图* @Date: 17:53 2018/9/20* @param time_coverimg   视频的第几秒作为封面图* @param videoInputPath  视频的路径* @param frame           帧数* @param coverOutputPath 视频的封面图的路径* @return: void* @respbody:*/// ffmpeg.exe -ss 00:00:01 -y -i 视频.mp4 -vframes 1 new.jpgpublic static void getVideoImg(String time_coverimg, String videoInputPath, int frame, String coverOutputPath)throws Exception {List<String> command = new ArrayList<String>();command.add(FFMPEG_PATH);command.add("-ss");command.add(time_coverimg);command.add("-y");command.add("-i");command.add(videoInputPath);command.add("-vframes");command.add(String.valueOf(frame));command.add(coverOutputPath);ProcessBuilder builder = new ProcessBuilder(command);Process process = null;try {process = builder.start();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}// 使用这种方式会在瞬间大量消耗CPU和内存等系统资源,所以这里我们需要对流进行处理InputStream errorStream = process.getErrorStream();InputStreamReader inputStreamReader = new InputStreamReader(errorStream);BufferedReader br = new BufferedReader(inputStreamReader);String line = "";while ((line = br.readLine()) != null) {}if (br != null) {br.close();}if (inputStreamReader != null) {inputStreamReader.close();}if (errorStream != null) {errorStream.close();}}/*** @method  compressVideoByRate* @Author: yizhe.wang* @Description:  通过帧率压缩视频* @Date: 17:54 2018/9/20* @param videoInputPath 原视频的路径* @param* @param videoOutPath   视频与音频结合之后的视频的路径* @return: void* @respbody:*/// 将视频和音频结合,并指定视频的长度,同时生成结合之后的视频文件  注意先音频再视频// ffmpeg -threads 2 -i media/0322糖先生1改.mp4 -strict -2 -vcodec libx264 -preset fast -crf 24 -y -acodec libmp3lame  -b:a 128k libmp3lame.mp4public static void compressVideo(String videoInputPath,String vcodec,String preset,Integer crf,String acodec,String ab,String videoOutPath){Integer availableProcessors = Runtime.getRuntime().availableProcessors();//若核数大于1,取处理器半数availableProcessors = availableProcessors==1? 1:availableProcessors/2;List<String> command = new ArrayList<String>();command.add(FFMPEG_PATH);command.add("-threads");command.add(String.valueOf(availableProcessors));command.add("-i");command.add(videoInputPath);command.add("-strict");command.add("-2");command.add("-vcodec");command.add(vcodec);command.add("-preset");command.add(preset);command.add("-crf");command.add(String.valueOf(crf));command.add("-acodec");command.add(acodec);command.add("-b:a");command.add(ab);command.add("-r");        //设置帧频command.add("24");command.add("-y");command.add(videoOutPath);ProcessBuilder builder = new ProcessBuilder(command);Process process = null;try {process = builder.start();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}// 使用这种方式会在瞬间大量消耗CPU和内存等系统资源,所以这里我们需要对流进行处理InputStream errorStream = process.getErrorStream();InputStreamReader inputStreamReader = new InputStreamReader(errorStream);BufferedReader br = new BufferedReader(inputStreamReader);try {String line = "";while ((line = br.readLine()) != null) {System.out.println(br);}if (br != null) {br.close();}if (inputStreamReader != null) {inputStreamReader.close();}if (errorStream != null) {errorStream.close();}}catch (Exception e){throw  new RuntimeException("compressVideo视频压缩异常!");}}
}

视频上传,视频压缩FFMpeg相关推荐

  1. 网站实现视频上传、转码、截图及在线播放功能

    网站实现视频上传.转码.截图及在线播放功能 时间 2014-10-15 22:46:05 IT社区推荐资讯 原文   http://itindex.net/detail/51396-网站-视频-上传 ...

  2. 【FFmpeg】java实现利用ffmpeg视频上传转码同时截取一帧保存为同名图片,并获取视频分辨率

    〇.前情提要 完成了视频上传,接下来是给上传的视频保存一张同名的jpg文件在同样文件夹中,并且获取到视频的分辨率. macOS Catalina 10.15.1 ffmpeg version 4.3. ...

  3. java实现视频上传和播放SpringMVC + Mybatis + ckplayer+ffmpeg+mencoder

    参考了很多大牛的文章,终于成功了,先感谢以下大牛,然后从前辈身上获利很多,我也应该把自己成功的经验奉献出来 ffmpeg参考:http://www.cnblogs.com/findingsea/arc ...

  4. iOS图片,视频上传视频内容旋转

    #前言 我最近在接手一个智能盒子的iOS应用,上面有一个功能是这样的.把你本地的照片和视频可以甩屏到你绑定的盒子上. 我的上一位前辈做的时候必须要求再同一个局域网,但是当我做的时候要求不同的局域网也要 ...

  5. java 上传视频并播放_java实现视频上传和播放..doc

    java实现视频上传和播放. Java实现视频网站的视频上传.视频转码.视频关键帧抽图, 及视频播放功能 视频网站中提供的在线视频播放功能,播放的都是FLV格式的文件,它是Flash动画文件,可通过F ...

  6. java上传视频代码下载_java 实现视频上传

    [实例简介] java上传视频转码播放的一个demo,实现java上传视频.转码.截图和播放功能 [实例截图] [核心代码] java视频上传,转码,播放实现 └── java视频上传,转码,播放实现 ...

  7. avs v7.0 php,AVS v7.0 – PHP在线视频上传分享程序

    释放双眼,带上耳机,听听看~! 源码简介 AVS在线视频上传分享脚本是一个老牌的国外视频CMS,您可以使用该源码创建在线视频,照片,游戏网站. 用户可以上传自己的视频.照片以及flash游戏,程序有多 ...

  8. web/java实现多种格式视频上传、转码、截图、播放、下载等功能附源码(详细)

    web /java 实现多种格式视频上传.转码.播放.下载 1.前言 前段时间一直在做一个生物资源共享平台,采用SSM框架技术,其中涉及一个模块,是关于视频资源的播放. 本来不是很大的问题,但是无奈用 ...

  9. 「小程序JAVA实战」小程序视频上传方法的抽象复用(57)

    转自:https://idig8.com/2018/09/23/xiaochengxujavashizhanxiaochengxushipinshangchuanfangfadechouxiangfu ...

  10. java web转码_web/java实现多种格式视频上传、转码、截图、播放、下载等功能附源码(详细)...

    /** * @Description:(视频资源的单独上传的接收) * @param:@param request * @param:@param response * @param:@param s ...

最新文章

  1. C语言0xc0000142错误,第一次用c++编译器出现奇怪的报错
  2. 二叉树的遍历:先序 中序 后序遍历的递归与非递归实现及层序遍历
  3. mysql数据库修改数据库名称_MySQL数据库之MySQL 修改数据库名称的一个新奇方法...
  4. 云漫圈 | 谈谈怎么做【服务隔离】
  5. c/c++通用头文件
  6. Ext Gantt Web甘特图--自定义任务树
  7. 使用MIB来监控你的应用(tuxedo mib)
  8. datetime数据类型_当pandas遇上数据类型问题
  9. 怎么接入WAPI网络防止被蹭网
  10. 概率论 方差公式_2020考研数学:概率论各章节知识点梳理
  11. dell电脑装双系统linux,戴尔电脑怎么装双系统?戴尔装win7+win10双系统详细教程
  12. Python导包的几种方法,自定义包的生成以及导入详解
  13. 测试经典面试问题:一个项目从0到1,你需要做哪些工作?工作的重点是什么?
  14. 48 款数据可视化分析工具大集合
  15. 图像处理(8)–灰度变换函数增强空间域图像
  16. 基于人性的量化交易:期货的反向跟单交易软件系统
  17. java即时通讯im聊天源码,dubbo即时通讯im聊天源码,netty即时通讯im聊天源码,springboot即时通讯im聊天源码
  18. 宽带和流量是分开的吗_宽带、带宽和流量的区别
  19. SSM+医保业财一体化管理系统 毕业设计-附源码151023
  20. 周志华机器学习(西瓜书)学习笔记(持续更新)

热门文章

  1. 高维矩阵求逆的方法,inv、pinv、/
  2. nginx-rtmp(直播点播)配置
  3. 脉搏波信号处理(matlab)
  4. RFID标签吊牌原理及可持续发展
  5. 贵州大数据安全工程研究中心107名DSMM测评师持证上岗
  6. Vivado2019.2 更改字体大小
  7. 两线无源模拟信号VI转换10KV高 隔离变送器
  8. Codeforces Round #383 Div.2 A-E 题解
  9. 上手openSUSE
  10. 变电站电源屏及温湿度和烟感设备协议接入流程记录