(四)、jave2加工视频

准备:

官网地址:https://github.com/a-schild/jave2

步骤

  • 添加依赖
<dependency>
 <groupId>ws.schild</groupId>
 <artifactId>jave-all-deps</artifactId>
 <version>2.4.6</version>
</dependency>

<!--根据环境添加ffmpeg执行包-->
<!--For one platform only (Linux 64Bit in this case)-->
<dependency>
    <groupId>ws.schild</groupId>
    <artifactId>jave-native-linux64</artifactId>
    <version>2.4.6</version>
</dependency>
<!--For one platform only (Windows 64Bit in this case)-->
<dependency>
    <groupId>ws.schild</groupId>
    <artifactId>jave-native-win64</artifactId>
    <version>2.4.6</version>
</dependency>
<!--For one platform only (MACOS 64Bit in this case)-->
<dependency>
    <groupId>ws.schild</groupId>
    <artifactId>jave-native-osx64</artifactId>
    <version>2.4.6</version>
</dependency>
  • 代码编写

代码路径见:https://github.com/muxiyue/docker-test/blob/master/src/test/java/video/VideoTest.java

普通的转码操作

@Test
    public void test() {
        ConvertProgressListener listener = new ConvertProgressListener();

        String inputFilePath = VideoTest.class.getResource("/").getPath() + "file/input/test2.3gp";
        String outputFilePath = VideoTest.class.getResource("/").getPath() + "file/output/test.mp4";

        try {
            File source = new File(inputFilePath);
            File target = new File(outputFilePath);

            AudioAttributes audio = new AudioAttributes();
            audio.setCodec("aac");

            audio.setBitRate(new Integer(236000/2));
//            audio.setChannels(new Integer(1));
            audio.setSamplingRate(new Integer(8000));
            
            VideoAttributes video = new VideoAttributes();
            video.setCodec("mpeg4");
            video.setBitRate(new Integer(128000));
            video.setFrameRate(new Integer(15));
            video.setSize(new VideoSize(1760, 1440));
            EncodingAttributes attrs = new EncodingAttributes();
            attrs.setFormat("3gp");
            attrs.setAudioAttributes(audio);
            attrs.setVideoAttributes(video);
            Encoder encoder = new Encoder();
            try {
                encoder.encode(new MultimediaObject(source), target, attrs, listener);
            }
            catch (Exception e) {
                e.printStackTrace();
                System.out.println(encoder.getUnhandledMessages());
            }


        } catch (Exception ex) {
            ex.printStackTrace();
        }



    }
    
    
     public class ConvertProgressListener implements EncoderProgressListener {

        public ConvertProgressListener() {
            //code
        }

        public void message(String m) {
            //code
        }

        public void progress(int p) {

            //Find %100 progress
            double progress = p / 1000.00;
            System.out.println(progress);

        }

        public void sourceInfo(MultimediaInfo m) {
            //code
        }
    }

filter测试

@Test
    public void test2() {
        ConvertProgressListener listener = new ConvertProgressListener();

        String inputFilePath = VideoTest.class.getResource("/").getPath() + "file/input/test2.3gp";
        String outputFilePath = VideoTest.class.getResource("/").getPath() + "file/output/test.mp4";
        String ttfPath = VideoTest.class.getResource("/").getPath() + "file/input/Songti.ttc";

        try {
            File source = new File(inputFilePath);
            File target = new File(outputFilePath);

            AudioAttributes audio = new AudioAttributes();
            audio.setSamplingRate(new Integer(8000));
            audio.setVolume(512);
            audio.setChannels(1);

            VideoAttributes video = new VideoAttributes();

//            命令行: ffplay -i test2.mp4 -vf drawtext="fontfile=Songti.ttc:text='指定时间段内才能看见':x='if((gte(t\,5) * (lte(t\,10)))\,w-t*50\,NAN)':fontcolor=darkorange:fontsize=30"
//            去掉了反斜杠和drawtext后面的双引号环绕
            VideoFilter videoFilter = new VideoFilter("drawtext=fontfile=" + ttfPath + ":text='指定时间段内才能看见':x='if((gte(t,1) * (lte(t,8))),w-t*50,NAN)':fontcolor=darkorange:fontsize=50");
            video.addFilter(videoFilter);

            EncodingAttributes attrs = new EncodingAttributes();
            attrs.setFormat("3gp");
            attrs.setAudioAttributes(audio);
            attrs.setVideoAttributes(video);


            Encoder encoder = new Encoder();
            try {
                encoder.encode(new MultimediaObject(source), target, attrs, listener);
            }
            catch (Exception e) {
                e.printStackTrace();
                System.out.println(encoder.getUnhandledMessages());
            }


        } catch (Exception ex) {
            ex.printStackTrace();
        }



    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值