如何压缩html5 MediaRecorder api录制的网络摄像头视频?

时间:2017-05-19 05:51:31

标签: video encoding ffmpeg webrtc web-mediarecorder

我使用MediaRecorder api成功录制了我的网络摄像头,结果文件大小似乎太大了。

例如,对于一个480x640的8秒视频,我的文件大小为1mB。这似乎不对。

我的记录代码()

navigator.mediaDevices.getUserMedia({video: true, audio: true})
    .then(function(stream){
        var options = {
            mimeType : "video/webm;codecs=vp9"
            //I don't set bitrate here even if I do the quality is too bad
        }
        var media_recorder = new MediaRecorder(media_stream, options);
        var recorded_data = [];
        media_recorder.ondataavailable = function(e){
             recorded_data.push(e.data);
        }
        media_recorder.onstop = function(e){
            recorded_data.push(e.data);
            var recorded_blob = new Blob(recorded_data, { 'type' : 'video/webm; codecs=vp9' });
            var recorded_video_url = window.URL.createObjectURL(recorded_blob);
            //here I write some code to download the blob from this url through a href
        }
    })

通过这种方法获得的文件非常大,这让我想知道它是否在VP9编码时被压缩了?一段7秒的视频约为870kB!

使用mediainfo工具检查文件给我

General
Count                                    : 323
Count of stream of this kind             : 1
Kind of stream                           : General
Kind of stream                           : General
Stream identifier                        : 0
Count of video streams                   : 1
Count of audio streams                   : 1
Video_Format_List                        : VP9
Video_Format_WithHint_List               : VP9
Codecs Video                             : V_VP9
Video_Language_List                      : English
Audio_Format_List                        : Opus
Audio_Format_WithHint_List               : Opus
Audio codecs                             : Opus
Audio_Language_List                      : English
Complete name                            : recorded_video.webm
File name                                : recorded_video
File extension                           : webm
Format                                   : WebM
Format                                   : WebM
Format/Url                               : http://www.webmproject.org/
Format/Extensions usually used           : webm
Commercial name                          : WebM
Format version                           : Version 2
Internet media type                      : video/webm
Codec                                    : WebM
Codec                                    : WebM
Codec/Url                                : http://www.webmproject.org/
Codec/Extensions usually used            : webm
File size                                : 867870
File size                                : 848 KiB
File size                                : 848 KiB
File size                                : 848 KiB
File size                                : 848 KiB
File size                                : 847.5 KiB
File last modification date              : UTC 2017-05-19 05:48:00
File last modification date (local)      : 2017-05-19 17:48:00
Writing application                      : Chrome
Writing application                      : Chrome
Writing library                          : Chrome
Writing library                          : Chrome
IsTruncated                              : Yes

Video
Count                                    : 332
Count of stream of this kind             : 1
Kind of stream                           : Video
Kind of stream                           : Video
Stream identifier                        : 0
StreamOrder                              : 1
ID                                       : 2
ID                                       : 2
Unique ID                                : 62101435245162993
Format                                   : VP9
Commercial name                          : VP9
Codec ID                                 : V_VP9
Codec ID/Url                             : http://www.webmproject.org/
Codec                                    : V_VP9
Codec                                    : V_VP9
Width                                    : 640
Width                                    : 640 pixels
Height                                   : 480
Height                                   : 480 pixels
Pixel aspect ratio                       : 1.000
Display aspect ratio                     : 1.333
Display aspect ratio                     : 4:3
Frame rate mode                          : VFR
Frame rate mode                          : Variable
Language                                 : en
Language                                 : English
Language                                 : English
Language                                 : en
Language                                 : eng
Language                                 : en
Default                                  : Yes
Default                                  : Yes
Forced                                   : No
Forced                                   : No

Audio
Count                                    : 272
Count of stream of this kind             : 1
Kind of stream                           : Audio
Kind of stream                           : Audio
Stream identifier                        : 0
StreamOrder                              : 0
ID                                       : 1
ID                                       : 1
Unique ID                                : 32224324715799545
Format                                   : Opus
Format/Url                               : http://opus-codec.org/
Commercial name                          : Opus
Internet media type                      : audio/opus
Codec ID                                 : A_OPUS
Codec ID/Url                             : http://opus-codec.org
Codec                                    : Opus
Codec                                    : Opus
Codec/Family                             : PCM
Channel(s)                               : 1
Channel(s)                               : 1 channel
Channel positions                        : Front: C
Channel positions                        : 1/0/0
Sampling rate                            : 48000
Sampling rate                            : 48.0 KHz
Compression mode                         : Lossy
Compression mode                         : Lossy
Delay                                    : 718
Delay                                    : 718ms
Delay                                    : 718ms
Delay                                    : 718ms
Delay                                    : 00:00:00.718
Delay, origin                            : Container
Delay, origin                            : Container
Language                                 : en
Language                                 : English
Language                                 : English
Language                                 : en
Language                                 : eng
Language                                 : en
Default                                  : Yes
Default                                  : Yes
Forced                                   : No
Forced                                   : No

我做错了什么?在添加块后,是否必须重新编码?我缺少一些属性吗? VP9应该会大幅减少文件大小。

1 个答案:

答案 0 :(得分:0)

Chrome的VP9单通道编码器,用于通过Media Recorder API捕获的视频,而不是您可以看到VP9和H.264之间的主要差异。

录制网络摄像头视频时,网络摄像头的质量,可用光线以及录制的内容比您使用的视频编解码器影响更大:

  • 低光=>低fps =>较低的比特率
  • 但是在低光照条件下的噪音要难以有效编码
  • 更好的网络摄像头可以在任何光线条件下以更少的噪音捕获更多数据
  • 说话的头需要比很多动作更低的比特率

但是每8-10秒@ 640x480 30fps的1MB是符合我的发现的。我使用this demo试图使用VP9,H.264和VP8(按此顺序)。

  

VP9应该大幅减少文件大小。

与H.264 VP9 does reduce size by about 30%相比,但要看到减少需要使用2遍编码,一个非常好的编码器(它们不同)和大量的CPU功率。这个过程也需要更长的时间。