ffmpeg视频转码

本文介绍了如何使用Java库ws.schild.jave进行音频和视频格式转换,包括指定编码格式、比特率和帧率等参数,以及提供了一个示例方法transcode用于将MP4转换为其他格式如MPEG。
摘要由CSDN通过智能技术生成

依赖

<dependency>
    <groupId>ws.schild</groupId>
    <artifactId>jave-core</artifactId>
    <version>2.7.3</version>
</dependency>
<dependency>
    <groupId>ws.schild</groupId>
    <artifactId>jave-nativebin-linux64</artifactId>
    <version>2.7.3</version>
</dependency>
<dependency>
    <groupId>ws.schild</groupId>
    <artifactId>jave-nativebin-win64</artifactId>
    <version>2.7.3</version>
</dependency>

实现

import cn.hutool.core.io.FileUtil;
import ws.schild.jave.AudioAttributes;
import ws.schild.jave.Encoder;
import ws.schild.jave.EncoderException;
import ws.schild.jave.EncodingAttributes;
import ws.schild.jave.InputFormatException;
import ws.schild.jave.MultimediaObject;
import ws.schild.jave.VideoAttributes;
import java.io.File;
import java.io.IOException;



/**
 */
public class Test {

    /**
     * MP4转其他格式
     * @param localPath
     * @param targetPath
     * @return
     * @throws IOException
     */
    public static String transcode(String localPath, String targetPath) throws IOException {

        File source = new File(localPath);
        File target = new File(targetPath);
        AudioAttributes audio = new AudioAttributes();
        audio.setCodec("libmp3lame");//音频编码格式
        //audio.setBitRate(new Integer(56000));//设置比特率,比特率越大,转出来的音频越大(默认是128000,最好默认就行,有特殊要求再设置)
        audio.setChannels(new Integer(1));
        audio.setSamplingRate(new Integer(22050));
        VideoAttributes video = new VideoAttributes();
        video.setCodec("libx264");//视屏编码格式
        //video.setBitRate(new Integer(56000));//设置比特率,比特率越大,转出来的视频越大(默认是128000,最好默认就行,有特殊要求再设置)
        video.setFrameRate(new Integer(15));//数值设置小了,视屏会卡顿
        EncodingAttributes attrs = new EncodingAttributes();
        attrs.setFormat("mp4");
        attrs.setAudioAttributes(audio);
        attrs.setVideoAttributes(video);
        Encoder encoder = new Encoder();
        MultimediaObject multimediaObject=new MultimediaObject(source);
        try {
            encoder.encode(multimediaObject,target,attrs);
        }catch (IllegalArgumentException e){
            e.printStackTrace();
        }catch (InputFormatException e){
            e.printStackTrace();
        }catch (EncoderException e){
            e.printStackTrace();
        }
        FileUtil.del(localPath);

        if (target.exists()) {
            return targetPath;
        }
        return null;
    }

    /**
     * 测试
     * @param args
     */
    public static void main(String[] args) {
        //视频转码
        String localPath ="D:\\output.mp4";
        String targetPath ="D:\\output.mpeg";
        try {
            transcode(localPath,targetPath);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值