转换MP4后浏览器播放只有声音没有图像的问题

这边想要的是对上传COS的视频在浏览器中直接播放 但是对于MOV的格式浏览器是没法分段请求播放的 所以这边首先想的解决方案是把mov格式直接后台转换成MP4格式 这个方案就导致了如题所示的问题

转换部分代码如下

public class VideoUtil {

    public static void convertMp4File(String videoPath, String savePath) throws EncoderException {
        File source = new File(videoPath);
        File target = new File(savePath);
        try {
            long startTime = System.currentTimeMillis();
            AudioAttributes audio = new AudioAttributes();
            audio.setCodec("libmp3lame");
            audio.setBitRate(new Integer(56000));
            audio.setChannels(new Integer(1));
            audio.setSamplingRate(new Integer(22050));
            VideoAttributes video = new VideoAttributes();
            //这里不能写mpeg4要改成libx264
            //video.setCodec("mpeg4");
            video.setCodec("libx264");
            video.setBitRate(new Integer(800000));
            video.setFrameRate(new Integer(30));
            EncodingAttributes attrs = new EncodingAttributes();
            attrs.setFormat("mp4");
            attrs.setAudioAttributes(audio);
            attrs.setVideoAttributes(video);
            Encoder encoder = new Encoder();
            encoder.encode(new MultimediaObject(source), target, attrs);
            long end = System.currentTimeMillis();
            System.out.println("耗时" + (end - startTime));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws EncoderException {
        convertMp4File("C:\\Users\\ouyangpengfei\\Videos\\中岛美雪.mov", "C:\\Users\\ouyangpengfei\\Videos\\Captures\\中岛美雪.mp4");
    }

}

POM文件引入

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

这里的video.setCodec("mpeg4"); 是用的mpeg4的视频编码方式 但实际是需要H264格式的MP4才能在浏览器中正常播放 具体详见http://www.125jz.com/1895.html

所以这里的代码要改成video.setCodec("libx264"); 这样转换的视频就能播放了

PS:it.sauronsoftware.jave 这个包不好用 应该是缺少h264的编码方式的包 转h264的时候会报错

但COS播放的问题确最终不是以这个方案解决的

偶然测试的时候发现只要上传COS的时候把ContentType设置为MP4的话即使文件是MOV的话也能分段提取缓冲播放 害累死累活的最终这样收场 头秃

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值