jave (java的ffmpeg框架)简单使用

引入文件(

jave-native-win64       windows 64位系统
jave-native-linux64 linux 64位系统
按自己服务器系统来替换

复制代码
<dependency>
    <groupId>ws.schild</groupId>
    <artifactId>jave-core</artifactId>
    <version>2.4.6</version>
</dependency>
<dependency>
    <groupId>ws.schild</groupId>
    <artifactId>jave-native-win64</artifactId>
     <version>2.4.6</version>
</dependency>
复制代码

 

压缩视频  音频使用 libmp3lame 压缩,ios默认播放器播放会没有声音,还是使用aac

复制代码
File source = new File("e:/视频/第八课.mp4");
        File target = new File("e:/视频/第八课_ys.mp4");
        long time = System.currentTimeMillis();
        try {
            MultimediaObject object = new MultimediaObject(source);
            AudioInfo audioInfo = object.getInfo().getAudio();
            AudioAttributes audio = new AudioAttributes();
            audio.setCodec("aac");
            if(audioInfo.getBitRate() > 128000){
                audio.setBitRate(new Integer(128000));
            }
            audio.setChannels(audioInfo.getChannels());
            if(audioInfo.getSamplingRate() > 48050){
                audio.setSamplingRate(new Integer(48050));
            }
            VideoInfo videoInfo = object.getInfo().getVideo();
            VideoAttributes video = new VideoAttributes();
            video.setCodec("h264");
            if(videoInfo.getBitRate() > 800000){
                video.setBitRate(new Integer(800000));
            }
            if(videoInfo.getFrameRate() > 20){
                video.setFrameRate(new Integer(20));
            }
            int width = videoInfo.getSize().getWidth();
            int height = videoInfo.getSize().getHeight();
            if(width > 1280){
                float rat = (float) width / 1280;
                video.setSize(new VideoSize(1280,(int)(height/rat)));
            }
            EncodingAttributes attr = new EncodingAttributes();
            attr.setFormat("mp4");
            attr.setAudioAttributes(audio);
            attr.setVideoAttributes(video);
            Encoder encoder = new Encoder();
            encoder.encode(object, target, attr);
            System.out.println("耗时:" + (System.currentTimeMillis() - time)/1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
复制代码

获取视频某个帧的图片

复制代码
long times = System.currentTimeMillis();
        File source = new File("e:/视频/第八课_ys.mp4");
        File target = new File("e:/视频/第八课_ys.png");
        MultimediaObject object = new MultimediaObject(source);
        try {
            VideoInfo videoInfo = object.getInfo().getVideo();
            VideoAttributes video = new VideoAttributes();
            video.setCodec("png");
            video.setSize(videoInfo.getSize());
            EncodingAttributes attrs = new EncodingAttributes();
            attrs.setFormat("image2");
            attrs.setOffset(3f);//设置偏移位置,即开始转码位置(3秒)
            attrs.setDuration(0.01f);//设置转码持续时间(0.01秒)
            attrs.setVideoAttributes(video);
            Encoder encoder = new Encoder();
            encoder.encode(object,target,attrs);
        } catch (EncoderException e) {
            e.printStackTrace();
        }
        System.out.println("耗时:"+(System.currentTimeMillis() - times));
复制代码

 

posted @   荣超  阅读(9526)  评论(3编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示