java 使用ffmpeg压缩视频

399 篇文章 12 订阅
335 篇文章 6 订阅

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

jar包地址

//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文件引用
<dependency>
  <groupId>java</groupId>
  <artifactId>jave</artifactId>
  <version>1.0.2.</version>
  <scope>system</scope>
  <systemPath>${project.basedir}/src/main/resources/lib/jave-1.0.2.jar</systemPath>
</dependency>

 2.在Mac上跑

jar包地址
ffmpeg地址

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文件引用
<dependency>
  <groupId>java</groupId>
  <artifactId>jave</artifactId>
  <version>2.0</version>
  <scope>system</scope>
  <systemPath>${project.basedir}/src/main/resources/lib/jave-2.0.jar</systemPath>
</dependency>

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

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;
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值