使用jave1.0.2.jar进行音视频转码

直接上代码:

/**
 * 使用jave1.0.2.jar进行转码截帧
 * @author chengjs
 *
 */
public class MediaInfo {
    public static boolean flag = true;
    /**
     * 视频转码
     * 因转码出来不是H264的视频编码所以此方法弃用
     * @param source
     * @param targetPath
     * @return
     */
    public static boolean transcodingToMP4(File source,String targetPath){
        //File source = new File("C:/Users/Administrator/Downloads/厨房里的爆炸案.mpg");
        File target = new File(targetPath);
        AudioAttributes audio = new AudioAttributes();// 音频属性
        audio.setCodec("libmp3lame");// libmp3lame 音频编码
        audio.setBitRate(new Integer(128000));// 音频比特率
        audio.setChannels(new Integer(1));// 声道
        audio.setSamplingRate(new Integer(44100));// 采样率
        VideoAttributes video = new VideoAttributes();// 视频属性
        video.setCodec("libxvid");// 视频编码
        video.setBitRate(new Integer(2048000));// 视频比特率
        video.setFrameRate(new Integer(18));// 帧率    1f/s帧频,1是目前测试比较清楚的,越大越模糊  
        //video.setSize(new VideoSize(1920,1080));// 视频宽高
        EncodingAttributes attrs = new EncodingAttributes();// 转码属性
        attrs.setFormat("mp4");// 转码格式
        attrs.setAudioAttributes(audio);// 音频属性
        attrs.setVideoAttributes(video);// 视频属性
        Encoder encoder = new Encoder();// 创建解码器
        long beginTime = System.currentTimeMillis();
        try {
            // 获取时长
            MultimediaInfo m = encoder.getInfo(source);
            System.out.println(m.getDuration()/1000 + "秒");
            System.out.println("获取时长花费时间是:" + ((System.currentTimeMillis() - beginTime))/1000 + "秒");
            beginTime = System.currentTimeMillis();
            encoder.encode(source, target, attrs);
            System.out.println("视频转码花费时间是:" + ((System.currentTimeMillis() - beginTime)/1000) + "秒");
            flag = true;
        } catch (IllegalArgumentException e) {
            flag = false;
            e.printStackTrace();
        } catch (InputFormatException e) {
            flag = false;
            e.printStackTrace();
        } catch (EncoderException e) {
            flag = false;
            e.printStackTrace();
        }
        return flag;
    }
    
    /**
     * 音频转码 转成MP3格式
     * @param source
     * @param targetPath
     * @return
     */
    public static boolean transcodingToMP3(File source,String targetPath){
        //File source = new File("C:/Users/Administrator/Downloads/厨房里的爆炸案.mpg");
        File target = new File(targetPath);
        AudioAttributes audio = new AudioAttributes();// 音频属性
        audio.setCodec("libmp3lame");// libmp3lame 音频编码
        audio.setBitRate(new Integer(128000));// 音频比特率
        audio.setChannels(new Integer(1));// 声道
        audio.setSamplingRate(new Integer(44100));// 采样率
        EncodingAttributes attrs = new EncodingAttributes();// 视频属性
        attrs.setFormat("mp3");// 转码格式
        attrs.setAudioAttributes(audio);// 音频属性
        Encoder encoder = new Encoder();// 创建解码器
        long beginTime = System.currentTimeMillis();
        try {
            // 获取时长
            MultimediaInfo m = encoder.getInfo(source);
            System.out.println(m.getDuration()/1000 + "秒");
            System.out.println("获取时长花费时间是:" + ((System.currentTimeMillis() - beginTime))/1000 + "秒");
            beginTime = System.currentTimeMillis();
            encoder.encode(source, target, attrs);
            System.out.println("音频转码花费时间是:" + ((System.currentTimeMillis() - beginTime)/1000) + "秒");
            flag = true;
        } catch (IllegalArgumentException e) {
            flag = false;
            e.printStackTrace();
        } catch (InputFormatException e) {
            flag = false;
            e.printStackTrace();
        } catch (EncoderException e) {
            flag = false;
            e.printStackTrace();
        }
        return flag;
    }
    
    /**
     * 截取第一帧作为缩略图
     * @param source
     * @param targetPath
     * @return
     */
    public static boolean interceptionToJPG(File source,String targetPath){
        //File source = new File("C:/Users/Administrator/Downloads/火箭少女101 - 卡路里.mp4");
        File target = new File(targetPath);// 转图片
        VideoAttributes video = new VideoAttributes();// 视频属性
        video.setCodec("mjpeg");// 图片编码
        video.setSize(new VideoSize(1200, 800));// 设置图片宽高
        EncodingAttributes attrs = new EncodingAttributes();// 转码属性
        attrs.setFormat("image2");// 转码格式
        attrs.setOffset(3f);// 设置偏移位置,即开始转码位置(3秒)
        attrs.setDuration(0.01f);// 设置转码持续时间(1秒)
        attrs.setVideoAttributes(video);
        Encoder encoder = new Encoder();
        long beginTime = System.currentTimeMillis();
        try {
            //获取时长 
            MultimediaInfo m = encoder.getInfo(source);
            System.out.println(m.getDuration());
            System.out.println("获取时长花费时间是:" + (System.currentTimeMillis() - beginTime));
            beginTime = System.currentTimeMillis();  
            encoder.encode(source, target, attrs);  
            System.out.println("图片转码花费时间是:" + (System.currentTimeMillis() - beginTime));  
            flag = true;
        } catch (IllegalArgumentException e) {
            flag = false;
            e.printStackTrace();
        } catch (InputFormatException e) {
            flag = false;
            e.printStackTrace();
        } catch (EncoderException e) {
            flag = false;
            e.printStackTrace();
        }
        return flag;
    }
    
    public static Long getTime(File file) throws InputFormatException, EncoderException{
        Encoder encoder = new Encoder();
        MultimediaInfo m = encoder.getInfo(file);
        long lengthOfTime = m.getDuration()/1000;
        return lengthOfTime;
    }
    
    public static void main(String[] args) {
       String fileOut = "E://30.mp4";
        File file2 = new File(fileOut);
        boolean flag1 = transcodingToMP3(file2,"C://别人家的小孩 .mp3");
        boolean flag2 = interceptionToJPG(file2,"C://别人家的小孩 .jpg");
        
    }
}

 

该资源包含以下jar包: commons-codec-1.6.jar commons-logging-1.1.1.jar fluent-hc-4.2.5.jar httpclient-4.2.5.jar httpclient-cache-4.2.5.jar httpcore-4.2.4.jar httpmime-4.2.5.jar import org.apache.http.Header; import org.apache.http.HttpException; import org.apache.http.HttpRequest; import org.apache.http.HttpRequestInterceptor; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.params.CoreConnectionPNames; import org.apache.http.protocol.HttpContext; antlr-2.7.7.jar aopalliance.jar asm-3.3.jar asm-commons-3.3.jar asm-tree-3.3.jar aspectjrt.jar aspectjweaver.jar com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.apache.commons.logging-1.1.1.jar com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar commons-fileupload-1.3.1.jar commons-io-2.2.jar commons-lang-2.5.jar commons-lang3-3.2.jar commons-logging-1.1.3.jar dom4j-1.6.1.jar druid-1.0.15.jar fastjson-1.1.37.jar freemarker-2.3.22.jar hibernate-commons-annotations-4.0.1.Final.jar hibernate-core-4.2.0.Final.jar hibernate-jpa-2.0-api-1.0.1.Final.jar jackson-annotations-2.5.0.jar jackson-core-2.5.0.jar jackson-databind-2.5.0.jar javassist-3.11.0.GA.jar javassist-3.15.0-GA.jar jboss-logging-3.1.0.GA.jar jboss-transaction-api_1.1_spec-1.0.0.Final.jar jedis-2.1.0.jar json.jar jsp-api-2.1.jar jstl.jar log4j-1.2.16.jar log4j-api-2.2.jar log4j-core-2.2.jar mybatis-3.3.0.jar mybatis-spring-1.2.3.jar mysql-connector-java-5.1.40-bin.jar og
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值