java视频格式转换
项目中需要对各种视频格式转码为mp4格式,试了好多办法,最后使用ffmpeg 工具完美转码,ffmpeg能解析的格式:(asx,asf,mpg,wmv,3gp,mp4,mov,avi,flv等)
链接:https://pan.baidu.com/s/1qDcHqCLa3eKgJX2gGtsoNg
提取码:4bdc
package com.dragonsoft.slxf.utils; import cn.hutool.system.OsInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List; public class ConvertVideoUtils { private static Logger logger = LoggerFactory.getLogger(ConvertVideoUtils.class); private String inputPath; private String outputPath; private String ffmpegPath; private String fileName; public ConvertVideoUtils(String inputPath, String outputPath, String ffmpegPath, String fileName) {
this.inputPath = inputPath;
this.outputPath = outputPath;
this.ffmpegPath = ffmpegPath;
this.fileName = fileName;
} public Boolean setVoidInfos() {
if (!checkfile(inputPath)) {
logger.info(inputPath + " is not file");
return false;
}
if (process(inputPath, ffmpegPath, outputPath, fileName)) {
logger.info("ok");
return true;
}
return false;
} public static boolean process(String inputPath, String ffmpegPath, String outputPath, String fileName) {
int type = checkContentType(inputPath);
boolean status = false;
logger.info("直接转成mp4格式");
status = processMp4(inputPath, ffmpegPath, outputPath, fileName);// 直接转成mp4格式
return status;
} private static int checkContentType(String inputPath) {
String type = inputPath.substring(inputPath.lastIndexOf(".") + 1, inputPath.length())
.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;
} private static boolean checkfile(String inputPath) {
File file = new File(inputPath);
if (!file.isFile()) {
return false;
}
return true;
} private static boolean processMp4(String oldfilepath, String ffmpegPath, String outputPath, String fileName) {
if (!checkfile(oldfilepath)) {
logger.info(oldfilepath + " is not file");
return false;
}
List<String> command = new ArrayList<>();
command.add(ffmpegPath);
command.add("-i");
command.add(oldfilepath);
command.add("-c:v");
command.add("libx264");
command.add("-mbd");
command.add("0");
command.add("-c:a");
command.add("aac");
command.add("-strict");
command.add("-2");
command.add("-pix_fmt");
command.add("yuv420p");
command.add("-movflags");
command.add("faststart");
command.add(outputPath + fileName + ".mp4");
try {
if (OSinfo.isWindows()) {
Process videoProcess = new ProcessBuilder(command).redirectErrorStream(true).start();
new PrintStream(videoProcess.getErrorStream()).start();
new PrintStream(videoProcess.getInputStream()).start();
videoProcess.waitFor();
} else {
logger.info("linux开始");
StringBuilder test = new StringBuilder();
for (String s : command) test.append(s).append(" ");
logger.info(test.toString());
// 执行命令
Process p = Runtime.getRuntime().exec(test.toString());
// 取得命令结果的输出流
InputStream fis = p.getInputStream();
// 用一个读输出流类去读
InputStreamReader isr = new InputStreamReader(fis);
// 用缓冲器读行
BufferedReader br = new BufferedReader(isr);
String line = null;
// 直到读完为止
while ((line = br.readLine()) != null) {
logger.info("视频转换:{}",line);
}
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
} public static void main(String[] args) {
String ffmpegPath = "D:/upload/";
String outputPath = "D:/upload/ffmpeg/ffmpeg.exe";
String inputPath = "D:/upload/8ab0c280f0f149c78c3b205d905898c7@111111.avi";
String fileName = "22222";
ConvertVideoUtils convertVideoUtils = new ConvertVideoUtils(inputPath, outputPath, ffmpegPath, fileName);
convertVideoUtils.setVoidInfos();
} }
java视频格式转换的更多相关文章
- java视频格式转换代码
http://blog.163.com/zzf_fly/blog/static/20958915820127217443816/ package com.gkzx.online.action; imp ...
- C#使用FFmpeg 将视频格式转换成Gif图片示例
根据EFmpeg封装的视频转换gif工具:https://my.oschina.net/tianma3798/blog/825317 一.本次使用参数说明 /* * 参数说明: * -i 源文件位置 ...
- Ubuntu18.04下使用Blender进行视频格式转换
Ubuntu下可以使用Blender的Video Editing功能进行视频格式转换, 具体步骤: 打开Blender后, 在顶层菜单栏中, 将Choose Screen Layout修改为Video ...
- Java日期格式转换
Java时间格式转换大全 import java.text.*;import java.util.Calendar;public class VeDate {/** * 获取现在时间 * ...
- Unity 利用FFmpeg实现录屏、直播推流、音频视频格式转换、剪裁等功能
目录 一.FFmpeg简介. 二.FFmpeg常用参数及命令. 三.FFmpeg在Unity 3D中的使用. 1.FFmpeg 录屏. 2.FFmpeg 推流. 3.FFmpeg 其他功能简述. 一. ...
- 视频格式转换.ZC资料
1.20191013: ZC:这些都是 2015年做的尝试,之前貌似没有记录下来,现在 再次用到,把用到的记录下来: ZC: (1) 使用的视频格式转换工具是 "??/XiGua Yings ...
- iSkysoft iMedia Converter Deluxe Mac如何制作视频?视频格式转换工具制作动图的方法
使用iSkysoft iMedia Converter Deluxe Mac如何制作视频?使用视频格式转换工具,你可以轻松进行动图或视频的制作,也可以把你喜欢的视频的某一段提取出来,制作成你自己风格的 ...
- C#使用FFmpeg 将视频格式转换成MP4示例
一.常用视频格式分辨率 640x480p 720p格式,分辨率为1280×720p / 60Hz,行频为45kHz 1080p格式,分辨率为1920×1080逐行扫描,专业格式 二.FFmpeg部分参 ...
- Java时间格式转换工具类
把当前时间修改成指定时间 //把当前时间修改成指定时间 public String dateUtil(Integer seconds, String dateFormatPattern){ Date ...
随机推荐
- WSL2:我在原生的Win10玩转Linux系统
原文地址:梁桂钊的博客 博客地址:http://blog.720ui.com 欢迎关注公众号:「服务端思维」.一群同频者,一起成长,一起精进,打破认知的局限性. WSL2:我在原生的Win10玩转Li ...
- JAVA程序员工作常用英语(细心整理)
基础----进阶 A. array数组accessible 可存取的 area面积audio 音频 addition 加法 action 行动 arithmetic 算法adjustment 调整 a ...
- 一键SSH连接 = SSH密钥登陆 + WindowsTerminal
本文记录如何利用SSH密钥登录和WindowsTerminal/FluentTerminal实现一键SSH连接 目录 一.在本地生成SSH密钥对 二.在远程主机安装公钥 三.在远程主机打开密钥登陆功能 ...
- mysql中delete from t1 where id = 10加锁状况叙述
在Next_Key Lock算法中,不仅仅锁定住所找到的索引,而且还锁定住这些索引覆盖的范围.因此在这个范围内的插入都是不允许的.这样就避免了在这个范围内插入数据导致的幻读问题. delete fro ...
- Linux程序开发中如何判断目录是否为根目录?
问题引入 判断某个目录字符串是否是根目录,咋一听很简单,只要判断字符串是否是"/"即可,但是,很多情况下使用的路径是相对路径,那么如何判断相对路径是根目录呢? 思路分析 熟悉Lin ...
- 11.java设计模式之享元模式
基本需求: 小型的外包项目,给客户A做一个产品展示网站,客户A的朋友感觉效果不错,也希望做这样的产品展示网站,但是要求都有些不同 每个客户要求发布的方式不一样,A要求以新闻的方式发布,B要求以博客的方 ...
- 对App应用架构搭建的一些思考
当下随着App开发技术的越来越成熟,多人协同开发必不可少,一个团队中每个人的代码风格.技术栈都存在差异,因此统一一套成熟的开发架构必不可少,可以提高开发效率.统一代码风格.为项目维护提供便利. 当下A ...
- Django 笔记2018.2.7
1.基础知识 1.1web服务基本原理 1.2 WSGI 目前最通用的web接口规范,python默认支持,在Django中是一个库 1.3WEB基础知识 MTV (Model Template Vi ...
- 初识redis协议
有关redis协议信息(https://redis.io/topics/protocol) 搭建环境 //jedis连接客户端 public class RedisClient { public st ...
- 最多约数问题(Java)
Description 正整数x 的约数是能整除x 的正整数.正整数x的约数个数记为div(x).例如,1,2,5,10 都是正整数10的约数,且div(10)=4.设a 和b是2 个正整数,a≤b, ...