海康威视NVR视频转换ffmpeg

时间:2017-12-11 18:59:27

标签: ffmpeg video-processing ip-camera

我有一个海康威视NVR,它存储我需要在网站上显示的安全摄像机镜头。我知道海康威视使用专有的H.264编解码器,无法在流行的视频播放器(如VLC)中播放(连贯),除非您在任何地方安装该编解码器。

我的计划是使用ffmpeg将视频转码为常规H.264编解码器和AAC用于音频,但生成的文件与原始文件具有相同的问题 - 播放时没有音频和非常具有破坏性的视频。 所以问题是,ffmpeg是否支持海康威视视频/音频编解码器的编码?或者也许应该尝试使用ffmpeg转换为不同的支持Web的编解码器? 我的ffmpeg命令如下所示:

ffmpeg -i C:\1.mp4  -c:v libx264 -preset fast -crf 30 -b:v 200k -c:a aac -strict experimental -movflags faststart -threads 0 C:\2.mp4

编辑:有趣的是ffplay.exe打开并播放原始视频文件没有任何问题,即使在海康威视编解码器没有安装的计算机上也是如此,因此我认为转换也应该是可行的?

相关视频文件的Mediainfo输出:

General
CompleteName                     : C:\DownLoad\1.mp4
Format                           : MPEG-PS
FileSize/String                  : 8.60 MiB
Duration/String                  : 2 h 7 min
OverallBitRate/String            : 9 395 b/s
FileExtension_Invalid            : mpeg mpg m2p vob pss evo

Video
ID/String                        : 224 (0xE0)
Format                           : AVC
Format/Info                      : Advanced Video Codec
Format_Profile                   : Baseline@L4
Format_Settings                  : 1 Ref Frames
Format_Settings_CABAC/String     : No
Format_Settings_RefFrames/String : 1 frame
Format_Settings_GOP              : M=1, N=30
Duration/String                  : 2 min 0 s
Width/String                     : 1 920 pixels
Height/String                    : 1 080 pixels
DisplayAspectRatio/String        : 16:9
FrameRate_Mode/String            : Variable
ColorSpace                       : YUV
ChromaSubsampling/String         : 4:2:0
BitDepth/String                  : 8 bits
ScanType/String                  : Progressive

Audio
ID/String                        : 192 (0xC0)
Format                           : MPEG Audio
Duration/String                  : 2 h 7 min
Compression_Mode/String          : Lossy
Video_Delay/String               : -33 min 40 s

输出ffmpeg:

C:\ffmpeg\bin>ffmpeg -i C:\DownLoad\1.mp4  -c:v libx264 -preset fast -crf 30 -b:v 75k -c:a aac -strict experimental -movflags faststart -threads 0 C:\DownLoad\2.mp4
ffmpeg version N-86537-gae6f6d4 Copyright (c) 2000-2017 the FFmpeg developers
  built with gcc 7.1.0 (GCC)
  configuration: --enable-gpl --enable-version3 --enable-cuda --enable-cuvid --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-nvenc --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzimg --enable-lzma --enable-zlib
  libavutil      55. 66.100 / 55. 66.100
  libavcodec     57. 99.100 / 57. 99.100
  libavformat    57. 73.100 / 57. 73.100
  libavdevice    57.  7.100 / 57.  7.100
  libavfilter     6. 94.100 /  6. 94.100
  libswscale      4.  7.101 /  4.  7.101
  libswresample   2.  8.100 /  2.  8.100
  libpostproc    54.  6.100 / 54.  6.100
Input #0, mpeg, from 'C:\DownLoad\1.mp4':
  Duration: 02:07:57.93, start: 789.820800, bitrate: 9 kb/s
    Stream #0:0[0x1e0]: Video: h264 (Baseline), yuv420p(progressive), 1920x1080, 25 fps, 25 tbr, 90k tbn, 50 tbc
    Stream #0:1[0x1c0]: Audio: pcm_mulaw, 8000 Hz, mono, s16, 64 kb/s
File 'C:\DownLoad\2.mp4' already exists. Overwrite ? [y/N] y
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
  Stream #0:1 -> #0:1 (pcm_mulaw (native) -> aac (native))
Press [q] to stop, [?] for help
[aac @ 0000000002cd0280] Too many bits 8832.000000 > 6144 per frame requested, clamping to max
[libx264 @ 0000000002514c80] using cpu capabilities: MMX2 SSE2Fast LZCNT SSSE3 SSE4.2 AVX XOP FMA4
[libx264 @ 0000000002514c80] profile High, level 4.0
[libx264 @ 0000000002514c80] 264 - core 150 r2833 df79067 - H.264/MPEG-4 AVC codec - Copyleft 2003-2017 - http://www.videolan.org/x264.html - options: cabac=1 ref=2 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=6 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=12 lookahead_threads=2 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=1 keyint=250 keyint_min=25 scenecut=40 intra_refresh=0 rc_lookahead=30 rc=crf mbtree=1 crf=30.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'C:\DownLoad\2.mp4':
  Metadata:
    encoder         : Lavf57.73.100
    Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 1920x1080, q=-1--1, 75 kb/s, 25 fps, 12800 tbn, 25 tbc
    Metadata:
      encoder         : Lavc57.99.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/75000 buffer size: 0 vbv_delay: -1
    Stream #0:1: Audio: aac (LC) ([64][0][0][0] / 0x0040), 8000 Hz, mono, fltp, 48 kb/s
    Metadata:
      encoder         : Lavc57.99.100 aac
[mp4 @ 00000000010e9e00] Starting second pass: moving the moov atom to the beginning of the file speed= 116x
frame= 3269 fps= 66 q=-1.0 Lsize=   11086kB time=01:34:24.38 bitrate=  16.0kbits/s dup=269 drop=0 speed= 115x
video:10429kB audio:592kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.594114%
[libx264 @ 0000000002514c80] frame I:14    Avg QP:21.86  size: 59795
[libx264 @ 0000000002514c80] frame P:833   Avg QP:24.81  size:  8993
[libx264 @ 0000000002514c80] frame B:2422  Avg QP:28.70  size:   970
[libx264 @ 0000000002514c80] consecutive B-frames:  1.0%  0.2%  1.4% 97.4%
[libx264 @ 0000000002514c80] mb I  I16..4: 18.9% 66.3% 14.8%
[libx264 @ 0000000002514c80] mb P  I16..4:  4.0%  7.7%  0.4%  P16..4: 16.2%  2.0%  0.6%  0.0%  0.0%    skip:69.1%
[libx264 @ 0000000002514c80] mb B  I16..4:  0.6%  0.2%  0.0%  B16..8:  5.5%  0.1%  0.0%  direct: 0.7%  skip:92.9%  L0:44.0% L1:55.0% BI: 1.0%
[libx264 @ 0000000002514c80] 8x8 transform intra:59.0% inter:83.3%
[libx264 @ 0000000002514c80] coded y,uvDC,uvAC intra: 25.3% 36.1% 7.7% inter: 1.0% 2.3% 0.1%
[libx264 @ 0000000002514c80] i16 v,h,dc,p: 23% 24% 43% 10%
[libx264 @ 0000000002514c80] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 37% 26% 23%  2%  2%  3%  2%  3%  3%
[libx264 @ 0000000002514c80] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 43% 23% 12%  4%  4%  5%  4%  4%  2%
[libx264 @ 0000000002514c80] i8c dc,h,v,p: 81%  7%  9%  3%
[libx264 @ 0000000002514c80] Weighted P-Frames: Y:1.0% UV:0.0%
[libx264 @ 0000000002514c80] ref P L0: 73.6% 26.4%
[libx264 @ 0000000002514c80] ref B L0: 80.9% 19.1%
[libx264 @ 0000000002514c80] ref B L1: 90.0% 10.0%
[libx264 @ 0000000002514c80] kb/s:653.30
[aac @ 0000000002cd0280] Qavg: 64512.656

C:\ffmpeg\bin>

下载示例链接:

https://www.dropbox.com/s/9ccptsuiqk2ntsv/1.zip?dl=0

这个样本正好是2分钟,但VLC会告诉你。

1 个答案:

答案 0 :(得分:3)

通过执行以下操作,我能够生成标准化的视频文件:

  1. 使用ffmpeg并使用-acodec aac从我的MPEG-PS视频文件中提取音频流。

  2. 使用ffmpeg和-v:c copy从原始MPEG-PS视频文件中删除音频流,并使用-t选项指定视频的实际持续时间。

  3. 将两个文件合并在一起。

  4. 结果是可在任何视频播放器中播放的文件。在VLC,MPC-HC上测试。

    编辑30.07.2018

    从那以后,我遇到了相同视频源的多个其他问题,最终决定重新编码视频和音频轨道以获得标准化输出。 当我将它们与原始文件分开时,主要问题之一是视频和音轨的持续时间不同 - 有时音频比视频长7-15秒,有时会更短。有时,视频会有额外的未知持续时间附加到它上面,没有明显的原因。为了解决这个问题,我不得不根据哪一个需要校正来重新编码音频和视频轨道。 (注意:我知道视频的实时时间,因为我会使用其Web界面从Hikvision NVR手动请求我需要的确切块) 所以这就是我提出的C#代码的逻辑:

    使用ffmpeg:

    将input.mp4文件拆分为视频和音轨
    ffmpeg -y -i input.mp4 -vn -acodec libmp3lame  output_audio.mp3
    
    ffmpeg -y -i input.mp4 -an -vcodec copy output_video.mp4
    

    注意:我将音频编码为libmp3lame,因为海康威视设备在其mp4容器中使用G.711 PCM作为音频而且不适合我。

    获取视频和音频曲目的持续时间,因为ffmpeg使用ffprobe识别它们:

    ffprobe.exe -show_entries stream=duration -of compact -v 0  output_audio.mp3
    ffprobe.exe -show_entries stream=duration -of compact -v 0  output_video.mp4
    

    持续时间显示在这两个命令的输出中,我捕获此输出并对其进行过滤以获取该特定字符串。或者,如果您不打算自动执行整个过程,则可以手动记录它。

    将这些持续时间与实际持续时间进行比较,并采取相应措施:

    如果音频持续时间与实际音频持续时间匹配但视频持续时间较长 - 请使用ffmpeg和setpts过滤器缩小视频轨道,如下所示:

    ffmpeg -y -i output_video.mp4 -filter:v setpts=RATIO*PTS output_video_edited.mp4
    

    其中RATIO是一个数字,您可以通过将音轨的持续时间除以视频曲目的持续时间来获得。例如,如果视频持续时间为:45.11秒,音频持续时间为39.76秒,则RATIO = 39.76 / 45.11 = 0.8814010197 PTS是ffmpeg输入自身的视频轨道的当前PTS,此字符串是命令的一部分,而不是您需要更改的内容。

    如果视频时长与实际视频时长相匹配,但音频较短较长,那么我会使用ffmpeg的atempo过滤器重新编码音频,如下所示:

    ffmpeg -y -i output_audio.mp3 -acodec libmp3lame -filter:a atempo=RATIO output_audio_edited.mp3
    

    RATIO是音频持续时间/视频时长。

    在此之后,我得到了标准化的视频和音轨,我可以使用ffmpeg合并,例如:

    ffmpeg -i output_video_edited.mp3 -i output_audio_edited.mp3 -c:v copy -c:a copy final_product.mp4
    

    如果给予选择,我将永远不会使用另一种海康威视设备。