目录
1、播放异常比对
2、视频编码检查
2.1、正常视频解析
2.2、海康视频解析
2.3、比对工具
3、转码
3.1、maven依赖
3.2、实现代码
4、验证
在前面的文章(海康威视-按时间下载录像文件_海康威视 sdk 下载录像 大小0-CSDN博客)中,通过按时间段下载指定的录像视频。
在实际的播放中,VLC工具可以播放,而放到浏览器中则无法播放
1、播放异常比对
海康视频播放错误:
出现 206 返回码,则视频下载完成,无法播放
普通视频播放正常:
虽然出现206,但播放正常。
所以考虑到,可能是通过设备网络SDK下载的视频编码存在问题
2、视频编码检查
使用ffprobe工具进行视频的详细内容检查
ffprobe.exe -v quiet -print_format json -show_format -show_streams 3.mp4
2.1、正常视频解析
{
"streams": [
{
"index": 0,
"codec_name": "h264",
"codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
"profile": "High",
"codec_type": "video",
"codec_tag_string": "avc1",
"codec_tag": "0x31637661",
"width": 960,
"height": 720,
"coded_width": 960,
"coded_height": 720,
"closed_captions": 0,
"film_grain": 0,
"has_b_frames": 2,
"sample_aspect_ratio": "1:1",
"display_aspect_ratio": "4:3",
"pix_fmt": "yuv420p",
"level": 31,
"color_range": "tv",
"color_space": "bt470bg",
"color_transfer": "smpte170m",
"color_primaries": "bt470bg",
"chroma_location": "left",
"field_order": "progressive",
"refs": 1,
"is_avc": "true",
"nal_length_size": "4",
"id": "0x1",
"r_frame_rate": "25/1",
"avg_frame_rate": "25/1",
"time_base": "1/90000",
"start_pts": 0,
"start_time": "0.000000",
"duration_ts": 22518000,
"duration": "250.200000",
"bit_rate": "645258",
"bits_per_raw_sample": "8",
"nb_frames": "6255",
"extradata_size": 48,
"disposition": {
"default": 1,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0,
"timed_thumbnails": 0,
"captions": 0,
"descriptions": 0,
"metadata": 0,
"dependent": 0,
"still_image": 0
},
"tags": {
"language": "und",
"handler_name": "VideoHandler",
"vendor_id": "[0][0][0][0]",
"encoder": "Lavc58.134.100 libwz264"
}
},
{
"index": 1,
"codec_name": "aac",
"codec_long_name": "AAC (Advanced Audio Coding)",
"profile": "LC",
"codec_type": "audio",
"codec_tag_string": "mp4a",
"codec_tag": "0x6134706d",
"sample_fmt": "fltp",
"sample_rate": "44100",
"channels": 2,
"channel_layout": "stereo",
"bits_per_sample": 0,
"id": "0x2",
"r_frame_rate": "0/0",
"avg_frame_rate": "0/0",
"time_base": "1/44100",
"start_pts": 0,
"start_time": "0.000000",
"duration_ts": 11032761,
"duration": "250.175986",
"bit_rate": "128311",
"nb_frames": "10776",
"extradata_size": 2,
"disposition": {
"default": 1,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0,
"timed_thumbnails": 0,
"captions": 0,
"descriptions": 0,
"metadata": 0,
"dependent": 0,
"still_image": 0
},
"tags": {
"language": "und",
"handler_name": "SoundHandler",
"vendor_id": "[0][0][0][0]"
}
}
],
"format": {
"filename": ".\\2319539678.mp4",
"nb_streams": 2,
"nb_programs": 0,
"format_name": "mov,mp4,m4a,3gp,3g2,mj2",
"format_long_name": "QuickTime / MOV",
"start_time": "0.000000",
"duration": "250.200000",
"size": "24396491",
"bit_rate": "780063",
"probe_score": 100,
"tags": {
"major_brand": "isom",
"minor_version": "512",
"compatible_brands": "isomiso2avc1mp41",
"encoder": "Lavf58.76.100",
"comment": "zy-libx264,vid:2319539678"
}
}
}
2.2、海康视频解析
{
"streams": [
{
"index": 0,
"codec_name": "hevc",
"codec_long_name": "H.265 / HEVC (High Efficiency Video Coding)",
"profile": "Main",
"codec_type": "video",
"codec_tag_string": "[0][0][0][0]",
"codec_tag": "0x0000",
"width": 2560,
"height": 1440,
"coded_width": 2560,
"coded_height": 1440,
"closed_captions": 0,
"film_grain": 0,
"has_b_frames": 0,
"pix_fmt": "yuv420p",
"level": 153,
"color_range": "tv",
"chroma_location": "left",
"refs": 1,
"id": "0x1e0",
"r_frame_rate": "25/1",
"avg_frame_rate": "25/1",
"time_base": "1/90000",
"start_pts": 7612160776,
"start_time": "84579.564178",
"duration_ts": 899820,
"duration": "9.998000",
"extradata_size": 88,
"disposition": {
"default": 0,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0,
"timed_thumbnails": 0,
"captions": 0,
"descriptions": 0,
"metadata": 0,
"dependent": 0,
"still_image": 0
}
},
{
"index": 1,
"codec_name": "pcm_alaw",
"codec_long_name": "PCM A-law / G.711 A-law",
"codec_type": "audio",
"codec_tag_string": "[0][0][0][0]",
"codec_tag": "0x0000",
"sample_fmt": "s16",
"sample_rate": "8000",
"channels": 1,
"channel_layout": "mono",
"bits_per_sample": 8,
"id": "0x1c0",
"r_frame_rate": "0/0",
"avg_frame_rate": "0/0",
"time_base": "1/90000",
"start_pts": 7612153756,
"start_time": "84579.486178",
"duration_ts": 900000,
"duration": "10.000000",
"bit_rate": "64000",
"disposition": {
"default": 0,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0,
"timed_thumbnails": 0,
"captions": 0,
"descriptions": 0,
"metadata": 0,
"dependent": 0,
"still_image": 0
}
}
],
"format": {
"filename": ".\\3.mp4",
"nb_streams": 2,
"nb_programs": 0,
"format_name": "mpeg",
"format_long_name": "MPEG-PS (MPEG-2 Program Stream)",
"start_time": "84579.486178",
"duration": "10.076000",
"size": "3798048",
"bit_rate": "3015520",
"probe_score": 52
}
}
2.3、比对工具
使用BCompare 比对工具,检查两个json的差异性。共有三处主要的不同
左边为正常视频、右边为海康视频
第一处:
海康转码后的格式 MPEG-H Part2/HEVC(H.265)(hevc)
第二处:
第三处:
3、转码
通过上图的对比,优先考虑将海康视频转码为H.264 格式
3.1、maven依赖
<dependency><groupId>ws.schild</groupId><artifactId>jave-core</artifactId><version>2.4.5</version></dependency><!-- 核心包--><dependency><groupId>ws.schild</groupId><artifactId>jave-native-win64</artifactId><version>2.4.5</version></dependency><!-- 这个是在windows环境下的jar,使用不同的环境,会调用不同的jar文件--><dependency><groupId>ws.schild</groupId><artifactId>jave-native-linux64</artifactId><version>2.4.5</version></dependency><!-- 这个是在linux环境下的jar-->
3.2、实现代码
public static void convertH264() throws EncoderException {//源视频位置File file = new File("G:\\video\\ffmpeg\\3.mp4");//目标视频位置File file1 = new File("G:\\video\\ffmpeg\\4.mp4");// 创建转码器AudioAttributes audio = new AudioAttributes();//指定编码audio.setCodec("aac");audio.setBitRate(new Integer(128000));//通道audio.setChannels(new Integer(2));audio.setSamplingRate(new Integer(44100));VideoAttributes video = new VideoAttributes();//设置编解码器video.setCodec("h264");video.setX264Profile(VideoAttributes.X264_PROFILE.BASELINE);video.setBitRate(new Integer(1024*1024 * 2));//设置帧率video.setFrameRate(new Integer(25));//设置大小//video.setSize(new VideoSize(2560, 1440));EncodingAttributes attrs = new EncodingAttributes();//格式attrs.setFormat("mp4");//attrs.setAudioAttributes(audio);attrs.setVideoAttributes(video);// 进行转码Encoder encoder = new Encoder();encoder.encode(new MultimediaObject(file), file1, attrs);}
根据实际需要设置转码中是否需要音频,
调整视频清晰度:通过设置 video.setBitRate 大小,实际测试值为1024*1024*4=4M大小较清晰。
但是转码时间较长,10秒的3.7M视频 需要7秒转换完成,最终大小4.3M
4、验证
转码后视频可以通过浏览器播放。对应的视频详情如下
{
"streams": [
{
"index": 0,
"codec_name": "h264",
"codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
"profile": "Constrained Baseline",
"codec_type": "video",
"codec_tag_string": "avc1",
"codec_tag": "0x31637661",
"width": 2560,
"height": 1440,
"coded_width": 2560,
"coded_height": 1440,
"closed_captions": 0,
"film_grain": 0,
"has_b_frames": 0,
"pix_fmt": "yuv420p",
"level": 50,
"chroma_location": "left",
"field_order": "progressive",
"refs": 1,
"is_avc": "true",
"nal_length_size": "4",
"id": "0x1",
"r_frame_rate": "25/1",
"avg_frame_rate": "25/1",
"time_base": "1/12800",
"start_pts": 0,
"start_time": "0.000000",
"duration_ts": 128000,
"duration": "10.000000",
"bit_rate": "3599484",
"bits_per_raw_sample": "8",
"nb_frames": "250",
"extradata_size": 39,
"disposition": {
"default": 1,
"dub": 0,
"original": 0,
"comment": 0,
"lyrics": 0,
"karaoke": 0,
"forced": 0,
"hearing_impaired": 0,
"visual_impaired": 0,
"clean_effects": 0,
"attached_pic": 0,
"timed_thumbnails": 0,
"captions": 0,
"descriptions": 0,
"metadata": 0,
"dependent": 0,
"still_image": 0
},
"tags": {
"language": "und",
"handler_name": "VideoHandler",
"vendor_id": "[0][0][0][0]"
}
}
],
"format": {
"filename": ".\\4.mp4",
"nb_streams": 1,
"nb_programs": 0,
"format_name": "mov,mp4,m4a,3gp,3g2,mj2",
"format_long_name": "QuickTime / MOV",
"start_time": "0.000000",
"duration": "10.000000",
"size": "4501211",
"bit_rate": "3600968",
"probe_score": 100,
"tags": {
"major_brand": "isom",
"minor_version": "512",
"compatible_brands": "isomiso2avc1mp41",
"encoder": "Lavf58.20.100"
}
}
}