zoukankan      html  css  js  c++  java
  • ffmpeg+java视频转换基础示例

    引入依赖

    <dependency>
    <groupId>ws.schild</groupId>
    <artifactId>jave-core</artifactId>
    <version>2.7.3</version>
    </dependency>
    <1--此依赖为windows版本,还有os版本和linux版本,注意自行选择-->
    <dependency>
    <groupId>ws.schild</groupId>
    <artifactId>jave-nativebin-win64</artifactId>
    <version>2.7.3</version>
    </dependency>

    1.设置转码的方法
    Encoder encoder = new Encoder();
    encoder.encode(
    java.io.File source,
    java.io.File target,
    it.sauronsoftware.jave.EncodingAttributes attributes)
    第一个参数source:需要转码的源文件
    第二个参数target:需转型成的目标文件

    第三个参数attributes:是一个包含编码所需数据的参数

    2.设置编码的参数
    EncodingAttributes
    构建方法:
    EncodingAttributes attrs = new EncodingAttributes()
    方法:
    setAudioAttributes添加音频转码时所需音频属性

    setVideoAttributes添加视频转码时所需视频属性

    setFormat设置转码格式的方法

    setOffset设置转码偏移位置的方法,例如你想在5秒后开始转码源文件则setOffset(5)

    setEncodingThreads设置编码线程

    setDecodingThreads设置解码线程

    setDuration设置转码持续时间的方法,例如你想持续30秒的转码则setDuration(30)

    3.设置音频转码的属性
    Audio encoding attributes
    构建方法:AudioAttributes audio = new AudioAttributes();
    方法:
    setCodec(java.lang.String codec)//设置编码器
    setBitRate(java.lang.Integer bitRate)//设置比特率
    setSamplingRate(java.lang.Integer bitRate)//设置取样率
    setChannels(java.lang.Integer channels)//设置声音频道
    setVolume(java.lang.Integer volume)//设置音量

    4.设置视频转码参数
    构造方法
    VideoAttributes video = new VideoAttributes();
    方法:
    setCodec(java.lang.String codec)//设置编码器
    setTag(java.lang.String tag)//设置标签(通常用多媒体播放器所选择的视频解码)
    setBitRate(java.lang.Integer bitRate)//设置比特率
    setFrameRate(java.lang.Integer bitRate)//设置帧率
    setSize(it.sauronsoftware.jave.VideoSize size)//设置大小

    示例代码:

    public static void main(String[] args) {
            File file = new File("2.mp4");
            MultimediaObject object = new MultimediaObject(file);
            File file1 = new File("3.mp4");
            try {
                AudioInfo audioInfo = object.getInfo().getAudio();
                VideoInfo videoInfo = object.getInfo().getVideo();
                //设置编码的参数
                EncodingAttributes encodingAttributes = new EncodingAttributes();
                //设置音频转码的参数
                AudioAttributes audioAttributes = new AudioAttributes();
                //设置声道 编码的音频流中使用的声道数(1=单声道,2=双声道)
                audioAttributes.setChannels(2);
                //设置比特率参数除以1000就是多少kpbs
                audioAttributes.setBitRate(128000);
                //设置编码
                audioAttributes.setCodec("aac");
                //设置音量编码时候的音量值,未设置为0,如果256,则音量值不会改变
                audioAttributes.setVolume(256);
                //设置取样率44.1khz
                audioAttributes.setSamplingRate(new Integer(44100));
                //设置视频转码的参数
                VideoAttributes videoAttributes = new VideoAttributes();
                //设置编码器
                videoAttributes.setCodec("h264");
                //设置视频比特率
                videoAttributes.setBitRate(128000);
                /* 设置视频帧率,视频帧率:15 f / s  帧率越低,效果越差
                 * 视频帧率(Frame rate)是用于测量显示帧数的量度。所谓的测量单位为每秒显示帧数(Frames per Second,简:FPS)或“赫兹”(Hz)
                */
                videoAttributes.setFrameRate(12);
                //设置视频大小
                videoAttributes.setSize(new VideoSize(videoInfo.getSize().getWidth(),videoInfo.getSize().getHeight()));
                System.out.println("原视频编码"+videoInfo.getDecoder());
                System.out.println("原视频高度"+videoInfo.getSize().getHeight());
                System.out.println("原视频宽度"+videoInfo.getSize().getWidth());
                System.out.println("原视频帧率"+videoInfo.getFrameRate());
                System.out.println("原视频比特率"+videoInfo.getBitRate());
                //1mb=1024kb,1kb=1024b,1mb=1024*1024
                double mb = Math.ceil(file.length() / (1024 * 1024));
                int second = (int) object.getInfo().getDuration() / 1000;
                BigDecimal bigDecimal = new BigDecimal(String.format("%.4f", mb / second));
                System.out.println("开始压缩视频-->视频每秒平均"+bigDecimal+"MB");
                System.out.println("原音频比率"+audioInfo.getBitRate());
                System.out.println("原音频声道"+audioInfo.getChannels());
                //44.1kHz,表示每秒钟对模拟音频信号进行了44100次取样
                System.out.println("原音频取样率"+audioInfo.getSamplingRate());
                System.out.println("原音频编码"+audioInfo.getDecoder());
                long time = System.currentTimeMillis();
                encodingAttributes.setFormat("mp4");
                encodingAttributes.setAudioAttributes(audioAttributes).setVideoAttributes(videoAttributes);
                encodingAttributes.setEncodingThreads(Runtime.getRuntime().availableProcessors()/2);
                encodingAttributes.setDecodingThreads(Runtime.getRuntime().availableProcessors()/2);
                //encodingAttributes.setDuration(3.0f);
                Encoder encoder = new Encoder();
                encoder.encode(new MultimediaObject(file), file1, encodingAttributes);
                System.out.println("压缩总耗时:" + (System.currentTimeMillis() - time)/1000);
                OutputStream outputStream = new FileOutputStream(new File("4.mp4"));
                FileInputStream fileInputStream = new FileInputStream(file1);
                int len = 0;
                byte[] bytes=new byte[1024];
                while((len=fileInputStream.read(bytes))!=-1){
                    outputStream.write(bytes,0,len);
                }
                outputStream.flush();
                outputStream.close();
                fileInputStream.close();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                if (file1.length()>0){
                    file1.delete();
                }
            }
        }
  • 相关阅读:
    strpbrk函数
    memchr函数
    memset函数
    strrev函数
    strncmp函数
    strset函数
    strtok函数
    计算机经典书籍之程序设计语言
    spring自定义bean的作用域
    lucene文章
  • 原文地址:https://www.cnblogs.com/bin-zhao/p/13998276.html
Copyright © 2011-2022 走看看