使用Jave转换音视频格式(网页有声音没图像,小程序不能播放问题)

最近开发微信小程序的时候,遇到了一个问题:
用户在PC后端上传的mp4格式的视频文件,在PC端只有声音没有图像,在小程序端完全不能播放。

最后结果:
1. PC浏览器video标签播放时,只有声音,没有图像。
用户上传不能正常播放的mp4文件编码格式为 hevc,即H.265格式。在浏览器的video标签中,出现只有声音没有图像的问题。
故需要进行转码,转换成H.264编码格式即可。
转码见下文。

2. 小程序端不能播放。
小程序的video标签API说是支持H264格式。经过测试,发现实际支持H264(Main),不支持H264(High)。H264 (Main)可以正常播放,但H264(High)不行。
故需要转码成H264(Main)编码的MP4文件即可。
转码见下文。

文件转码
使用的是ws.schild.jave包

  1. 在pom.xml中引入maven配置
<dependency>
	<groupId>ws.schild</groupId>
	<artifactId>jave-all-deps</artifactId>
	<version>3.3.1</version>
</dependency>
  1. 转码工具类(呼吁各位贴代码的时候请带上import包的信息)
    开发小程序的请注意
    如果你出现了MP4视频文件小程序不能播放的问题。
    请考虑以下两点:
    1 URL连接中是否包含中文,比如文件名字是中文,替换成英文或数字试一下。
    2 请查看视频文件是否是 H264(Main),如果是H264(high)的话,转成H264(Main)试一下。
    video.setX264Profile(X264_PROFILE.MAIN);
    我遇到的就是上面两个问题导致视频在小程序中播放不了。
package com.util;

import java.io.File;
import java.nio.file.Paths;

import ws.schild.jave.Encoder;
import ws.schild.jave.MultimediaObject;
import ws.schild.jave.encode.AudioAttributes;
import ws.schild.jave.encode.EncodingAttributes;
import ws.schild.jave.encode.VideoAttributes;
import ws.schild.jave.encode.enums.X264_PROFILE;
import ws.schild.jave.info.MultimediaInfo;
import ws.schild.jave.info.VideoInfo;
import ws.schild.jave.info.VideoSize;

public class VideoUtil {
    
    public static void convertVideoToMP4(String originalFilePath, String resultFilePath) {
    	
    	try {
            File originalFile = new File(originalFilePath);
            MultimediaObject multimediaObject = new MultimediaObject(originalFile);
            MultimediaInfo info = multimediaObject.getInfo();
            VideoInfo videoInfo = info.getVideo();
            VideoSize size = videoInfo.getSize();
            System.out.println("原视频宽:" + size.getWidth());
            System.out.println("原视频高:" + size.getHeight());
            System.out.println("原视频比特率:" + videoInfo.getBitRate() / 1000);
            System.out.println("原视频编码:" + videoInfo.getDecoder());
 
            VideoAttributes video = new VideoAttributes();
            //设置视频编码
            video.setCodec("h264");
            video.setX264Profile(X264_PROFILE.MAIN);
            File resultFile = new File(resultFilePath);
            AudioAttributes audio = new AudioAttributes();
            //设置编码器名称
            audio.setCodec("aac");
            EncodingAttributes attrs = new EncodingAttributes();
            //设置转换后的格式
            attrs.setOutputFormat("mp4");
            attrs.setAudioAttributes(audio);
            attrs.setVideoAttributes(video);
            Encoder encoder = new Encoder();
            encoder.encode(multimediaObject, resultFile, attrs);
 
            MultimediaObject multimediaObjectOfter = new MultimediaObject(Paths.get(resultFilePath).toFile());
            MultimediaInfo info1 = multimediaObjectOfter.getInfo();
            VideoInfo video1 = info1.getVideo();
            VideoSize size1 = video1.getSize();
            System.out.println("转换后视频宽:" + size1.getWidth());
            System.out.println("转换后视频高:" + size1.getHeight());
            System.out.println("转换后视频比特率:" + video1.getBitRate() / 1000);
            System.out.println("转换后视频编码:" + video1.getDecoder());
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
    	String originalFilePath = "d://1.mp4";
    	String resultFilePath = "d://2.mp4";
    	convertVideoToMP4(originalFilePath, resultFilePath);
	}
    
}

可以看到日志如下:

原视频宽:640
原视频高:340
原视频比特率:223
原视频编码:hevc (Main) (hev1 / 0x31766568)
16:21:19.222 logback [main] INFO  w.s.jave.ConversionOutputAnalyzer - Unhandled message in step: 2 Line: 22 message: <[libx264 @ 00000000028ba280] using SAR=1/1>
16:21:19.227 logback [main] INFO  w.s.jave.ConversionOutputAnalyzer - Unhandled message in step: 2 Line: 23 message: <[libx264 @ 00000000028ba280] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2>
16:21:19.228 logback [main] INFO  w.s.jave.ConversionOutputAnalyzer - Unhandled message in step: 2 Line: 24 message: <[libx264 @ 00000000028ba280] profile Main, level 3.0, 4:2:0, 8-bit>
16:21:19.228 logback [main] INFO  w.s.jave.ConversionOutputAnalyzer - Unhandled message in step: 2 Line: 25 message: <[libx264 @ 00000000028ba280] 264 - core 164 r3075 66a5bc1 - H.264/MPEG-4 AVC codec - Copyleft 2003-2021 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x1:0x111 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=11 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00>
16:21:30.098 logback [main] INFO  w.s.jave.ConversionOutputAnalyzer - Unhandled message in step: 3 Line: 70 message: <[mp4 @ 00000000028b6f80] Starting second pass: moving the moov atom to the beginning of the file>
转换后视频宽:640
转换后视频高:340
转换后视频比特率:281
转换后视频编码:h264 (Main) (avc1 / 0x31637661)

可以看到文件已经转换成h264 (Main) 格式,这种格式可以兼容PC网页和小程序。
如果是h264 (High),则小程序端不能播放,PC页面可以正常播放。

最后附上Jave2 Github传送门:
https://github.com/a-schild/jave2

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值