java 使用ffmpeg压缩视频

/ Java / 没有评论 / 2074浏览

java 使用ffmpeg压缩视频

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