java实现视频转码
废话不多说,直接上代码:
这是转码工具类:
- package com.gcsoft.pyas.sysbase.utils;
- import com.gcsoft.pyas.AppProperties;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.UUID;
- /**
- * 视频转码工具类
- *
- * @author jwc
- */
- @Component
- public class ConverVideoUtils {
- @Autowired
- private AppProperties appProperties;
- protected final Logger logger = LoggerFactory.getLogger(this.getClass());
- /**
- * 转换视频格式
- *
- * @param sourceVideoPath 视频地址
- * @return
- */
- public String beginConver(String sourceVideoPath) {
- //转码格式
- String targetExtension = appProperties.getVideoFormat();
- //是否删除原文件
- Boolean isDeleteResult = appProperties.getIsDeleteResult();
- File fi = new File(sourceVideoPath);
- String fileName = fi.getName();
- //文件名不带扩展名
- String fileRealName = fileName.substring(0, fileName.lastIndexOf("."));
- logger.info("接收到文件(" + sourceVideoPath + ")需要转换");
- if (!checkfile(sourceVideoPath)) {
- logger.error(sourceVideoPath + "文件不存在" + " ");
- return "";
- }
- long beginTime = System.currentTimeMillis();
- logger.info("开始转文件(" + sourceVideoPath + ")");
- String path = process(fileRealName, sourceVideoPath, targetExtension, isDeleteResult);
- if (StringUtil.isNotEmpty(path)) {
- logger.info("转换成功");
- long endTime = System.currentTimeMillis();
- long timeCha = (endTime - beginTime);
- String totalTime = sumTime(timeCha);
- logger.info("转换视频格式共用了:" + totalTime + " ");
- if (isDeleteResult) {
- deleteFile(sourceVideoPath);
- }
- return path;
- } else {
- return "";
- }
- }
- /**
- * 实际转换视频格式的方法
- *
- * @param fileRealName 文件名不带扩展名
- * @param sourceVideoPath 原文件地址
- * @param targetExtension 目标视频扩展名
- * @param isDeleteResult 转换完成后是否删除源文件
- * @return
- */
- private String process(String fileRealName, String sourceVideoPath, String targetExtension, boolean isDeleteResult) {
- int type = checkContentType(sourceVideoPath);
- String path = "";
- if (type == 0) {
- //如果type为0用ffmpeg直接转换
- path = processVideoFormat(sourceVideoPath, fileRealName, targetExtension, isDeleteResult);
- } else if (type == 1) {
- //如果type为1,将其他文件先转换为avi,然后在用ffmpeg转换为指定格式
- String aviFilePath = processAVI(fileRealName, sourceVideoPath);
- if (aviFilePath == null) {
- // avi文件没有得到
- return "";
- } else {
- logger.info("开始转换:");
- path = processVideoFormat(aviFilePath, fileRealName, targetExtension, isDeleteResult);
- if (isDeleteResult) {
- deleteFile(aviFilePath);
- }
- }
- }
- return path;
- }
- /**
- * 检查文件类型
- *
- * @param sourceVideoPath 原文件地址
- * @return
- */
- private int checkContentType(String sourceVideoPath) {
- String type = sourceVideoPath.substring(sourceVideoPath.lastIndexOf(".") + 1).toLowerCase();
- // ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
- if (type.equals("avi")) {
- return 0;
- } else if (type.equals("mpg")) {
- return 0;
- } else if (type.equals("wmv")) {
- return 0;
- } else if (type.equals("3gp")) {
- return 0;
- } else if (type.equals("mov")) {
- return 0;
- } else if (type.equals("mp4")) {
- return 0;
- } else if (type.equals("asf")) {
- return 0;
- } else if (type.equals("asx")) {
- return 0;
- } else if (type.equals("flv")) {
- return 0;
- }
- // 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等),
- // 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
- else if (type.equals("wmv9")) {
- return 1;
- } else if (type.equals("rm")) {
- return 1;
- } else if (type.equals("rmvb")) {
- return 1;
- }
- return 9;
- }
- /**
- * 检查文件是否存在
- *
- * @param path 文件地址
- * @return
- */
- private boolean checkfile(String path) {
- File file = new File(path);
- if (!file.isFile()) {
- return false;
- } else {
- return true;
- }
- }
- /**
- * 对ffmpeg无法解析的文件格式(wmv9,rm,rmvb等), 可以先用别的工具(mencoder)转换为avi(ffmpeg能解析的)格式.
- *
- * @param fileRealName 文件名不带扩展名
- * @param sourceVideoPath 原文件地址
- * @return
- */
- private String processAVI(String fileRealName, String sourceVideoPath) {
- /**
- * mencoder.exe的地址
- */
- String menCoderPath = appProperties.getMencoderPath();
- /**
- * 转码后的存放视频地址 avi格式
- */
- String videoFolder = appProperties.getUploadAndFormatPath();
- List<String> commend = new java.util.ArrayList<>();
- commend.add(menCoderPath);
- commend.add(sourceVideoPath);
- commend.add("-oac");
- commend.add("mp3lame");
- commend.add("-lameopts");
- commend.add("preset=64");
- commend.add("-ovc");
- commend.add("xvid");
- commend.add("-xvidencopts");
- commend.add("bitrate=600");
- commend.add("-of");
- commend.add("avi");
- commend.add("-o");
- commend.add(videoFolder + fileRealName + ".avi");
- try {
- ProcessBuilder builder = new ProcessBuilder();
- builder.command(commend);
- Process p = builder.start();
- doWaitFor(p);
- return videoFolder + fileRealName + ".avi";
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- /**
- * 转换为指定格式
- * ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
- *
- * @param oldFilePath 源文件地址
- * @param fileRealName 文件名不带扩展名
- * @param targetExtension 目标格式扩展名 .xxx
- * @return
- */
- private String processVideoFormat(String oldFilePath, String fileRealName, String targetExtension, Boolean isDeleteResult) {
- /**
- * ffmpeg.exe的地址
- */
- String ffmpegPath = appProperties.getFfmpegPath();
- /**
- * 转码后的存放视频地址 mp4格式
- */
- String targetFolder = appProperties.getUploadAndFormatPath();
- if (!checkfile(oldFilePath)) {
- logger.error(oldFilePath + "文件不存在");
- return "";
- }
- List<String> commend = new ArrayList<>();
- commend.add(ffmpegPath);
- commend.add("-i");
- commend.add(oldFilePath);
- commend.add("-vcodec");
- commend.add("mpeg4");
- commend.add("-q");
- commend.add("0");
- commend.add("-y");
- commend.add(targetFolder + fileRealName + targetExtension);
- try {
- ProcessBuilder builder = new ProcessBuilder();
- builder.command(commend);
- Process p = builder.start();
- doWaitFor(p);
- p.destroy();
- String videoPath = targetFolder + fileRealName + targetExtension;
- String path = this.processVideoFormatH264(videoPath, ffmpegPath, targetFolder, targetExtension, isDeleteResult);
- return path;
- } catch (Exception e) {
- e.printStackTrace();
- return "";
- }
- }
- /**
- * 将mpeg4转为h264编码 为了支持播放器
- *
- * @param path
- * @param ffmpegPath
- * @return
- */
- private String processVideoFormatH264(String path, String ffmpegPath, String targetFolder, String targetExtension, Boolean isDeleteResult) {
- if (!checkfile(path)) {
- logger.error(path + "文件不存在");
- return "";
- }
- String newFilePath = targetFolder + UUID.randomUUID().toString() + targetExtension;
- List<String> commend = new ArrayList<>();
- commend.add(ffmpegPath);
- commend.add("-i");
- commend.add(path);
- commend.add("-vcodec");
- commend.add("h264");
- commend.add("-q");
- commend.add("0");
- commend.add("-y");
- commend.add(newFilePath);
- try {
- ProcessBuilder builder = new ProcessBuilder();
- builder.command(commend);
- Process p = builder.start();
- doWaitFor(p);
- p.destroy();
- if (isDeleteResult) {
- deleteFile(path);
- }
- return newFilePath;
- } catch (Exception e) {
- e.printStackTrace();
- return "";
- }
- }
- public int doWaitFor(Process p) {
- InputStream in = null;
- InputStream err = null;
- int exitValue = -1;
- try {
- in = p.getInputStream();
- err = p.getErrorStream();
- boolean finished = false;
- while (!finished) {
- try {
- while (in.available() > 0) {
- in.read();
- }
- while (err.available() > 0) {
- err.read();
- }
- exitValue = p.exitValue();
- finished = true;
- } catch (IllegalThreadStateException e) {
- Thread.sleep(500);
- }
- }
- } catch (Exception e) {
- logger.error("doWaitFor();: unexpected exception - " + e.getMessage());
- } finally {
- try {
- if (in != null) {
- in.close();
- }
- } catch (IOException e) {
- logger.info(e.getMessage());
- }
- if (err != null) {
- try {
- err.close();
- } catch (IOException e) {
- logger.info(e.getMessage());
- }
- }
- }
- return exitValue;
- }
- /**
- * 删除文件方法
- *
- * @param filepath
- */
- public void deleteFile(String filepath) {
- File file = new File(filepath);
- if (file.delete()) {
- logger.info("文件" + filepath + "已删除");
- }
- }
- /**
- * 计算转码时间
- *
- * @param ms
- * @return
- */
- public String sumTime(long ms) {
- int ss = 1000;
- long mi = ss * 60;
- long hh = mi * 60;
- long dd = hh * 24;
- long day = ms / dd;
- long hour = (ms - day * dd) / hh;
- long minute = (ms - day * dd - hour * hh) / mi;
- long second = (ms - day * dd - hour * hh - minute * mi) / ss;
- long milliSecond = ms - day * dd - hour * hh - minute * mi - second
- * ss;
- String strDay = day < 10 ? "0" + day + "天" : "" + day + "天";
- String strHour = hour < 10 ? "0" + hour + "小时" : "" + hour + "小时";
- String strMinute = minute < 10 ? "0" + minute + "分" : "" + minute + "分";
- String strSecond = second < 10 ? "0" + second + "秒" : "" + second + "秒";
- String strMilliSecond = milliSecond < 10 ? "0" + milliSecond : ""
- + milliSecond;
- strMilliSecond = milliSecond < 100 ? "0" + strMilliSecond + "毫秒" : ""
- + strMilliSecond + " 毫秒";
- return strDay + " " + strHour + ":" + strMinute + ":" + strSecond + " "
- + strMilliSecond;
- }
- }
工具类用到的参数
- #视频上传和转码后存放的位置
- video.trans.coding=D:/PYAS/TMS/upload/video/
- #ffmpeg地址
- tool.ffmpeg.path=D:/FFmpeg/ffmpeg.exe
- #mencoder地址
- tool.mencoder.path=D:/FFmpeg/mencoder.exe
- #转码格式
- video.format=.mp4
- #是否删除源文件
- video.isdelete.result=false
工具类用到的转码工具分别是:
ffmpeg、mencoder
转码工具 密码 eada
java实现视频转码的更多相关文章
- Java实现视频转码或压缩demo.
先点击这里下载资源包(包括jar和文档) 使用这个资源包,处理起来很简单. demo如下: import java.io.File; import it.sauronsoftware.jave.Aud ...
- java ffmpeg视频转码(自测通过)
import java.io.*; public class VideoTransfer { //ffmepg文件 安装目录 private static String ffmpeg = " ...
- Java实现视频网站的视频上传、视频转码、视频关键帧抽图, 及视频播放功能
视频网站中提供的在线视频播放功能,播放的都是FLV格式的文件,它是Flash动画文件,可通过Flash制作的播放器来播放该文件.项目中用制作的player.swf播放器. 多媒体视频处理工具FFmpe ...
- Java Web 中使用ffmpeg实现视频转码、视频截图
Java Web 中使用ffmpeg实现视频转码.视频截图 转载自:[ http://www.cnblogs.com/dennisit/archive/2013/02/16/2913287.html ...
- Java实现视频网站的视频上传、视频转码、及视频播放功能(ffmpeg)
视频网站中提供的在线视频播放功能,播放的都是FLV格式的文件,它是Flash动画文件,可通过Flash制作的播放器来播放该文件.项目中用制作的player.swf播放器. 多媒体视频处理工具FFmpe ...
- BAT推荐免费下载JAVA转型大数据开发全链路教程(视频+源码)价值19880元
如今随着环境的改变,物联网.AI.大数据.人工智能等,是未来的大趋势,而大数据是这些基石,万物互联,机器学习都是大数据应用场景! 为什么要学习大数据?我们JAVA到底要不要转型大数据? 好比问一个程序 ...
- 利用JAVA生成二维码
本文章整理于慕课网的学习视频<JAVA生成二维码>,如果想看视频内容请移步慕课网. 维基百科上对于二维码的解释. 二维条码是指在一维条码的基础上扩展出另一维具有可读性的条码,使用黑白矩形图 ...
- 最简单的基于FFmpeg的移动端样例:Android 视频转码器
===================================================== 最简单的基于FFmpeg的移动端样例系列文章列表: 最简单的基于FFmpeg的移动端样例:A ...
- 【JDK1.8】 Java小白的源码学习系列:HashMap
目录 Java小白的源码学习系列:HashMap 官方文档解读 基本数据结构 基本源码解读 基本成员变量 构造器 巧妙的tableSizeFor put方法 巧妙的hash方法 JDK1.8的putV ...
随机推荐
- 使用async/await消除callback hell
使用async/await消除callback hell 通过Future回调中再返回Future的方式虽然能避免层层嵌套,但是还是有一层回调,有没有一种方式能够让我们可以像写同步代码那样来执行异步任 ...
- 51nod1681 公共祖先
[传送门] 很明显,可以转化成求每个点在两棵树中对应的子树中有多少个相同的节点,对答案的贡献就是$C(x, 2)$.关键就是怎么求这个东西.一是,对第一棵树求出dfs序,然后dfs第二棵树,用树状数组 ...
- JavaScript基础11——ES5和ES6
ES5↓↓↓ bind方法/this关键字 bind()方法会创建一个新函数,称为绑定函数,当调用这个绑定函数时,绑定函数会以创建它时传入bind()方法的第一个参数作为this,传入 bin ...
- 使用mustache 做为docker容器运行动态配置工具
很多时候我们需要在启动容器的时候基于配置文件运行,如果在配置比较简单的时候我们可以通过环境变量 注入,同时当前12 factors 越来越融入大家的开发中了(对于配置通过环境变量处理),但是好多老的软 ...
- c# 如何进行动态加载dll
最近遇到了在c#中如何进行动态加载dll的话,搞定了,下面介绍一下自己的步骤. 1,新建dll. 打开vs,新建project->Class Library->项目名为testdll1.在 ...
- 洛谷 P1462 通往奥格瑞玛的道路 题解
P1462 通往奥格瑞玛的道路 题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡 ...
- word2vector的tensorflow代码实现
import collections import math import os import random import zipfile import numpy as np import urll ...
- Foxmail: 错误信息::ssl连接错误, errorCode: 5,各种解决方案的大杂烩。
1. 收件数据过多,删除部分邮件可解决 我尝试失败,在foxmail把收件箱全部删完了没解决. 2. 网上最常见的解决方法 https://help.foxmail.com/cgi-bin/hel ...
- unicode欺骗—— hctf - admin
查看源代码,发现<!-- you are not admin --> 提示要以管理员身份登陆 尝试注册管理员账号,提示The username has been registered 于是 ...
- JPA的一对多,多对多用法
一.@OneToOne关系映射 JPA使用@OneToOne来标注一对一的关系. 实体 People :用户. 实体 Address:家庭住址. People 和 Address 是一对一的关系. 这 ...