java 视频压缩_Java 压缩视频(无需安装插件)

亲测可用,基于ffmpeg 去进行压缩,

测试原视频大小:835.87 MB

压缩后大小:118.8 MB

压缩比率 :85.78%

好了,开始上代码

1、首先 pom文件中引入下面两个 jar 包

ws.schild

jave-core

2.7.3

具体引用包按本机操作系统来设定

java 视频压缩_Java 压缩视频(无需安装插件)_第1张图片

ws.schild

jave-nativebin-osx64

2.7.3

java 代码

/**

* 传远程视频连接,返回压缩后File

*/

public static File compressionVideo(String href){

try{

// 缓存到临时目录,压缩上传完到远程服务器上之后,记得删除源视频

String savePath = "/tmp/tempVideo/";

long time = System.currentTimeMillis();

URL url=new URL(href);

HttpURLConnection connection=(HttpURLConnection)url.openConnection();

connection.setConnectTimeout(600*1000);

connection.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36");

InputStream in=connection.getInputStream();

String fileName = "zip" + href.substring(href.lastIndexOf("/") + 1);

File saveDir = new File(savePath);

if(!saveDir.exists()){

saveDir.mkdirs();

}

File file = new File(savePath + fileName);

Ou

你可能感兴趣的:(java,视频压缩)