Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webm进度条问题分析与解决 #10

Open
BlackHole1 opened this issue May 21, 2019 · 3 comments
Open

Webm进度条问题分析与解决 #10

BlackHole1 opened this issue May 21, 2019 · 3 comments

Comments

@BlackHole1
Copy link
Contributor

BlackHole1 commented May 21, 2019

介绍

当我们使用getUserMediaMediaRecorder等API生成的webm视频时,会发现最终的webm是无法拖动进度条的。除非使用FFmpeg把webm转成其他格式的视频文件,或者等webm视频播放完后,就可以拖动了。

分析

经过几个小时的排查,发现并不是MediaRecorder使用有问题,因为在网上找的其他demo生成的webm也都不行。

一开始把分析点放在了进度条那里,结果在网上没有搜到任何相关文章,尝试了各种关键词都不行。

后来想到,可以使用FFmpeg来对视频文件进行分析。于是使用ffprobe rebirth-demo.webm命令进行分析:

$ ffprobe rebirth-demo.webm
ffprobe version 4.1.3 Copyright (c) 2007-2019 the FFmpeg developers
  built with Apple LLVM version 10.0.1 (clang-1001.0.46.4)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.1.3_1 --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags='-I/Library/Java/JavaVirtualMachines/adoptopenjdk-11.0.2.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/adoptopenjdk-11.0.2.jdk/Contents/Home/include/darwin' --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libmp3lame --enable-libopus --enable-librubberband --enable-libsnappy --enable-libtesseract --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-videotoolbox --disable-libjack --disable-indev=jack --enable-libaom --enable-libsoxr
  libavutil      56. 22.100 / 56. 22.100
  libavcodec     58. 35.100 / 58. 35.100
  libavformat    58. 20.100 / 58. 20.100
  libavdevice    58.  5.100 / 58.  5.100
  libavfilter     7. 40.101 /  7. 40.101
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  3.100 /  5.  3.100
  libswresample   3.  3.100 /  3.  3.100
  libpostproc    55.  3.100 / 55.  3.100
Input #0, matroska,webm, from 'rebirth-demo.webm':
  Metadata:
    encoder         : Chrome
  Duration: N/A, start: 0.000000, bitrate: N/A
    Stream #0:0(eng): Audio: opus, 48000 Hz, stereo, fltp (default)
    Stream #0:1(eng): Video: vp8, yuv420p(progressive), 1920x1080, SAR 1:1 DAR 16:9, 60 tbr, 1k tbn, 1k tbc (default)
    Metadata:
      alpha_mode      : 1

关键点来了,可以发现其中:Durationbitrate的值都是N/A,这是不正常的,于是去搜一下webm duration,果然网上有很多的说明文章。

大体意思是,因为getUserMediaMediaRecorder在生成webm时,并没有提供相关Durationbitrate。导致出现这种问题。

解决方案

一、计算视频长度,分配给blob

这种方法的核心就是,在start开始录制时,记录一个开始时间,然后在stop停止录制后,把当前时间与记录的开始时间相减,在把时间赋值给blob来解决这个问题。相关解决方案可见:fix-webm-duration

二、通过给audio标签一个很大的时间

在播放webm视频时,可以动态的给audio一个很大的时间,来解决这个问题,但是目前只针对chrome有效。相关解决方案可见:How can I add predefined length to audio recorded from MediaRecorder in Chrome?

三、跳到结尾,再跳到开头

因为上文说过,当视频播放完后,就可以拖动了,那么只需要通过JS来控制当前的视频位置就可以解决了。相关解决方案可见:hello-its-me

四、通过ffmpeg来解决

第一个解决的命令如下:ffmpeg -i rebirth-demo.webm xixi.webm,但是这个命令会很漫长,不太推荐。30秒的视频,需要花费3分钟左右的时间。

第二个命令是:ffmpeg -i rebirth-demo.webm -vcodec copy -acodec copy new_rebirth-demo.webm。这个十分的快,因为本身是直接复制,而不是转化:

ffmpeg -i rebirth-demo.webm  -vcodec copy -acodec copy new_rebirth-demo.webm
ffmpeg version 4.1.3 Copyright (c) 2000-2019 the FFmpeg developers
  built with Apple LLVM version 10.0.1 (clang-1001.0.46.4)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.1.3_1 --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags='-I/Library/Java/JavaVirtualMachines/adoptopenjdk-11.0.2.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/adoptopenjdk-11.0.2.jdk/Contents/Home/include/darwin' --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libmp3lame --enable-libopus --enable-librubberband --enable-libsnappy --enable-libtesseract --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-videotoolbox --disable-libjack --disable-indev=jack --enable-libaom --enable-libsoxr
  libavutil      56. 22.100 / 56. 22.100
  libavcodec     58. 35.100 / 58. 35.100
  libavformat    58. 20.100 / 58. 20.100
  libavdevice    58.  5.100 / 58.  5.100
  libavfilter     7. 40.101 /  7. 40.101
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  3.100 /  5.  3.100
  libswresample   3.  3.100 /  3.  3.100
  libpostproc    55.  3.100 / 55.  3.100
Input #0, matroska,webm, from 'rebirth-demo.webm':
  Metadata:
    encoder         : Chrome
  Duration: N/A, start: 0.000000, bitrate: N/A
    Stream #0:0(eng): Audio: opus, 48000 Hz, stereo, fltp (default)
    Stream #0:1(eng): Video: vp8, yuv420p(progressive), 1920x1080, SAR 1:1 DAR 16:9, 60 tbr, 1k tbn, 1k tbc (default)
    Metadata:
      alpha_mode      : 1
Output #0, webm, to 'new_rebirth-demo.webm':
  Metadata:
    encoder         : Lavf58.20.100
    Stream #0:0(eng): Video: vp8, yuv420p(progressive), 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 60 tbr, 1k tbn, 1k tbc (default)
    Metadata:
      alpha_mode      : 1
    Stream #0:1(eng): Audio: opus, 48000 Hz, stereo, fltp (default)
Stream mapping:
  Stream #0:1 -> #0:0 (copy)
  Stream #0:0 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame= 3589 fps=0.0 q=-1.0 Lsize=    2107kB time=00:01:59.92 bitrate= 143.9kbits/s speed=4.75e+03x
video:2053kB audio:16kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.849351%

$ ffprobe new_rebirth-demo.webm
ffprobe version 4.1.3 Copyright (c) 2007-2019 the FFmpeg developers
  built with Apple LLVM version 10.0.1 (clang-1001.0.46.4)
  configuration: --prefix=/usr/local/Cellar/ffmpeg/4.1.3_1 --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags='-I/Library/Java/JavaVirtualMachines/adoptopenjdk-11.0.2.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/adoptopenjdk-11.0.2.jdk/Contents/Home/include/darwin' --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libmp3lame --enable-libopus --enable-librubberband --enable-libsnappy --enable-libtesseract --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-videotoolbox --disable-libjack --disable-indev=jack --enable-libaom --enable-libsoxr
  libavutil      56. 22.100 / 56. 22.100
  libavcodec     58. 35.100 / 58. 35.100
  libavformat    58. 20.100 / 58. 20.100
  libavdevice    58.  5.100 / 58.  5.100
  libavfilter     7. 40.101 /  7. 40.101
  libavresample   4.  0.  0 /  4.  0.  0
  libswscale      5.  3.100 /  5.  3.100
  libswresample   3.  3.100 /  3.  3.100
  libpostproc    55.  3.100 / 55.  3.100
Input #0, matroska,webm, from 'new_rebirth-demo.webm':
  Metadata:
    ENCODER         : Lavf58.20.100
  Duration: 00:01:59.96, start: 0.000000, bitrate: 143 kb/s
    Stream #0:0(eng): Video: vp8, yuv420p(progressive), 1920x1080, SAR 1:1 DAR 16:9, 60 tbr, 1k tbn, 1k tbc (default)
    Metadata:
      ALPHA_MODE      : 1
      DURATION        : 00:01:59.928000000
    Stream #0:1(eng): Audio: opus, 48000 Hz, stereo, fltp (default)
    Metadata:
      DURATION        : 00:01:59.955000000

可见已经没有问题了

总结

我个人倾向于最后一种,因为前面几个方法并没有实际性解决这个问题。

目前chrome团队不认为这是一个BUG。之前社区也讨论过。

社区讨论地址:https://bugs.chromium.org/p/chromium/issues/detail?id=642012

@BlackHole1 BlackHole1 changed the title 关于webm无法拖动进度条问题 解决webm无法拖动进度条问题 May 21, 2019
@BlackHole1 BlackHole1 changed the title 解决webm无法拖动进度条问题 解决webm进度条问题 May 21, 2019
@BlackHole1 BlackHole1 changed the title 解决webm进度条问题 webm进度条问题分析与解决 May 21, 2019
@BlackHole1 BlackHole1 changed the title webm进度条问题分析与解决 Webm进度条问题分析与解决 May 21, 2019
@BlackHole1
Copy link
Contributor Author

BlackHole1 commented Jun 9, 2019

最终还是选择了第三种方案,但是原来第三种方案是有bug的,在重新设置时间时,不能设置成0。否则视频将直接跳转到后面。最终代码如下:

let flag = true
let mt = $("video");
mt.currentTime = 1e101;
  mt.ontimeupdate = function() {
    if (flag) {
      mt.currentTime = 0.001;
      flag = false;
    }
  }

@BlackHole1
Copy link
Contributor Author

BlackHole1 commented Jun 9, 2019

第一种方案我个人总觉得有误差,而且要很多额外的计算步骤

第二种方案,目前只有chrome支持,所以暂时不考虑

第四种方案,会让程序多出很多不必要的步骤。原本就直接把流推到S3了,现在的话还要先保存到本地,再由本地进行ffmpeg进行转码。再发到S3上,所以暂时也不考虑。

最终选择了第二种方案,项目本身不需要额外的变动,消费方去加上如上代码就好。

@BlackHole1
Copy link
Contributor Author

原本想的是通过chrome的PPAPI来对chrome植入一个ffmpeg,但是水平太菜,折腾不好。。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant