java 视频压缩_java 使用ffmpeg压缩视频

这篇博客详细介绍了如何在Java中使用jave库调用ffmpeg进行视频压缩。分别展示了在Linux、Mac环境下配置ffmpeg路径,以及设置音频和视频编码参数的方法。通过示例代码,演示了视频压缩的过程,并提供了pom文件中jave库的引用方式。
摘要由CSDN通过智能技术生成

1.在linux下使用的话,需要导入jave包

//java代码

private boolean ffmpegChange1 (File file,File fileSave){

try{

AudioAttributes audio = new AudioAttributes();

audio.setCodec("libmp3lame");

audio.setBitRate(new Integer(56000));

audio.setChannels(new Integer(1));

audio.setSamplingRate(new Integer(22050));

VideoAttributes video = new VideoAttributes();

// video.setCodec(myConfig.getFileFFmpegPath());

// video.setCodec("libx264");

video.setCodec("mpeg4");

// video.setCodec("h264");

video.setBitRate(new Integer(800000));

video.setFrameRate(new Integer(15));

EncodingAttributes attrs = new EncodingAttributes();

attrs.setFormat("mp4");

attrs.setAudioAttributes(audio);

attrs.setVideoAttributes(video);

Encoder encoder = new Encoder();

long beginTime = System.currentTimeMillis();

MultimediaInfo m = encoder.getInfo(file);

System.out.println(m.getDuration()/1000 + "秒");

System.out.println("获取时长花费时间是:" + ((System.currentTimeMillis() - beginTime))/1000 + "秒");

beginTime = System.currentTimeMillis();

// encoder.encode(source, fileSave, attrs);

encoder.encode(file, fileSave, attrs);

// System.out.println("压缩完成...");

System.out.println("压缩完成花费时间是:" + ((System.currentTimeMillis() - beginTime))/1000 + "秒");

return true;

}catch (EncoderException e){

e.printStackTrace();

System.out.println("压缩错误信息是1:"+e.getMessage());

return false;

}

}

//pom文件引用

java

jave

1.0.2.

system

${project.basedir}/src/main/resources/lib/jave-1.0.2.jar

2.在Mac上跑

private boolean ffmpegChange (File file,File fileSave){

try{

AudioAttributes audio = new AudioAttributes();

audio.setCodec("libmp3lame");

audio.setBitRate(new Integer(56000));

audio.setChannels(new Integer(1));

audio.setSamplingRate(new Integer(22050));

VideoAttributes video = new VideoAttributes();

//video.setCodec("mpeg4");

video.setCodec("h264");

video.setBitRate(new Integer(800000));

video.setFrameRate(new Integer(1));

EncodingAttributes attrs = new EncodingAttributes();

attrs.setFormat("mp4");

attrs.setAudioAttributes(audio);

attrs.setVideoAttributes(video);

Encoder encoder = new Encoder(new MyFFMPEGExecute());

long beginTime = System.currentTimeMillis();

MultimediaInfo m = encoder.getInfo(file);

System.out.println(m.getDuration()/1000 + "秒");

System.out.println("获取时长花费时间是:" + ((System.currentTimeMillis() - beginTime))/1000 + "秒");

beginTime = System.currentTimeMillis();

// encoder.encode(source, fileSave, attrs);

encoder.encode(file, fileSave, attrs);

// System.out.println("压缩完成...");

System.out.println("压缩完成花费时间是:" + ((System.currentTimeMillis() - beginTime))/1000 + "秒");

return true;

}catch (EncoderException e){

e.printStackTrace();

System.out.println("压缩错误信息是:"+e.getMessage());

return false;

}

}

//重写ffmpeg地址,path我用的是绝对路径,获取ffmpeg

public class MyFFMPEGExecute extends FFMPEGLocator {

@Autowired

MyConfig myConfig;

@Override

protected String getFFMPEGExecutablePath() {

String path1 = "/Users/apple/Desktop/imagecollect/file/ffmpeg";

String path2 = "/root/imagecollect/ffmpeg";

// System.out.println("ffmpegways = " + path);

return path1;

}

}

//pom文件引用

java

jave

2.0

system

${project.basedir}/src/main/resources/lib/jave-2.0.jar

3.在linux中跑 用jave.2.0.jar的方法,代码同上,唯一区别是ffmpeg的执行包不同

//重写ffmpeg地址,path我用的是绝对路径,获取ffmpeg

public class MyFFMPEGExecute extends FFMPEGLocator {

@Autowired

MyConfig myConfig;

@Override

protected String getFFMPEGExecutablePath() {

String path1 = "/Users/apple/Desktop/imagecollect/file/ffmpeg";

String path2 = "/root/imagecollect/ffmpeg";

//此处用path2,此路径是服务器上绝对路径,记得把ffmpeg放到此路径位置就可以

return path2;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值