java使用jave2转换视频

2019-09-11 18:09发布

	注意:需要使用jdk1.8以上
	maven坐标:
		<dependency>
            <groupId>ws.schild</groupId>
            <artifactId>jave-all-deps</artifactId>
            <version>2.6.0</version>
        </dependency>

private static void testMp4ToTs() {
        try {
            File source = new File("C:\\Users\\USER\\Desktop\\5190801.mp4");
            File target = new File("C:\\Users\\USER\\Desktop\\test.ts");

            MultimediaObject multimediaObject = new MultimediaObject(source);
            //音频参数
            AudioAttributes audio = new AudioAttributes();
            //音频 编码器
            audio.setCodec("libmp3lame");
            //比特率
            //audio.setBitRate(128000);
            audio.setBitRate(multimediaObject.getInfo().getAudio().getBitRate());
            //声道
            audio.setChannels(2);
            //audio.setSamplingRate(44100);
            audio.setSamplingRate(multimediaObject.getInfo().getAudio().getSamplingRate());
            //音量
            audio.setVolume(10);
            
            //视频参数
            VideoAttributes videoAttributes = new VideoAttributes();
            //视频编码器
            videoAttributes.setCodec("libx264");
            //视频分辨率
            videoAttributes.setSize(new VideoSize(1920, 1080));
            //帧率
           /* videoAttributes.setFrameRate((int) multimediaObject.getInfo().getVideo().getFrameRate());
           //比特率
            videoAttributes.setBitRate(multimediaObject.getInfo().getVideo().getBitRate());*/

            //Encoding attributes
            EncodingAttributes attrs = new EncodingAttributes();
            //加入视频参数
            attrs.setVideoAttributes(videoAttributes);
            //加入音频参数
             attrs.setAudioAttributes(audio);
             //设置时长
            attrs.setDuration((float) (multimediaObject.getInfo().getDuration()));
            //设置输出格式
            attrs.setFormat("mpegts");
            //设置偏移量*(从第几秒开始转换)
            attrs.setOffset(5);
            
			 //Encode
            Encoder encoder = new Encoder();
            //转换
            encoder.encode(new MultimediaObject(source), target, attrs);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
标签: