Commit 0bdcc938aa0e3407567acb433d19c9b5fa7dc934

Authored by xingwenliang
1 parent 99788557

feat: add download audio demo

2022-07-14-video-recorder.md 0 → 100644
  1 +---
  2 +layout: post
  3 +title: 视频录制
  4 +# date 同时用作关联 github issue 的唯一标识,所以不可重复
  5 +date: 2022-07-014 14:14:54+0800
  6 +sync_link: https://xwenliang.cn/p/xx
  7 +categories: translation
  8 +# permalink: /xxx/
  9 +
  10 +# https://jekyllrb.com/docs/structure/
  11 +# name of this file show be in format of: YEAR-MONTH-DAY-title.MARKUP
  12 +---
  13 +
  14 +最近有小伙伴花了大几千买了某视频网课的课程,快到期了还没看过,问我能不能下载到本地
  15 +
  16 +先去 github 逛了一圈,发现有个叫做 CocoCut 的浏览器插件,可以下载视频,对于加密视频,只要能播放就能下载
  17 +
  18 +试用后发现存在几个问题:
  19 +
  20 +1. 无法自动化,对于加密视频需要每次打开网页后点击插件弹出面板上一个叫做「force download」的按钮,跳转到其官网后打开 「Recording mode」, 此时播放视频的网页会刷新,然后开始录制,最后提供 mp4 格式的视频下载
  21 +
  22 +2. 需要付费,只能免费下载几个小时的时长
  23 +
  24 +既然它能做,那么咱们应该也可以。先了解下目前市面上常见的视频加密手段:
  25 +
  26 +1. 微软 DRM SDK, 浏览器兼容较差
  27 +
  28 +保利威视 v12 加密,canvas+wasm
0 29 \ No newline at end of file
... ...
background/background.js
... ... @@ -12,6 +12,7 @@
12 12 // test();
13 13  
14 14 const videos = {};
  15 +window.videos = videos;
15 16  
16 17 /**
17 18 * data: fr.result,
... ... @@ -50,9 +51,11 @@ function download(id){
50 51 return ;
51 52 }
52 53 const blob = new Blob(tar.blob, {type: tar.mimeType});
  54 + const ext = tar.mimeType.includes('webm') ? 'webm' : 'mkb';
  55 + const filename = `${tar.name || 'anonymous'}.${ext}`;
53 56 chrome.downloads.download({
54 57 url: URL.createObjectURL(blob),
55   - filename: `${tar.name}.mkv`
  58 + filename
56 59 }, resp => {
57 60 delete videos[id];
58 61 });
... ...
content/audio_download.js 0 → 100644
  1 +// audio tag
  2 +const $tar = document.getElementById('audio_remote');
  3 +
  4 +const mimeType = 'audio/webm';
  5 +const recorder = new MediaRecorder($tar.captureStream(), {
  6 + mimeType
  7 +});
  8 +recorder.onstart = function(e){
  9 + console.log('start', e);
  10 +};
  11 +recorder.ondataavailable = function(e){
  12 + console.log('ondata', e);
  13 + saveBlobData(e.data);
  14 +};
  15 +recorder.onstop = function(e){
  16 + console.log('stop', e);
  17 +};
  18 +recorder.start(1000);
  19 +
  20 +function saveBlobData(data){
  21 + const blob = new Blob(data, {
  22 + type: mimeType
  23 + });
  24 + const url = URL.createObjectURL(blob);
  25 + const a = document.createElement('a');
  26 + document.body.appendChild(a);
  27 + a.style = 'display: none';
  28 + a.href = url;
  29 + a.download = document.title;
  30 + a.click();
  31 + window.URL.revokeObjectURL(url);
  32 +};
0 33 \ No newline at end of file
... ...
popup/popup.html
... ... @@ -7,17 +7,7 @@
7 7 </head>
8 8 <body>
9 9  
10   - <div class="tit">111将生词添加到我的有道单词本</div>
11   - <div class="login-box hide">
12   - <div class="username"><input type="text" id="username" placeholder="请输入用户名" autofocus="true"></div>
13   - <div class="password"><input type="password" id="password" placeholder="请输入密码"></div>
14   - <div class="login-btn"><button id="login">登录</button></div>
15   - </div>
16   -
17   - <div class="add-box hide">
18   - <div class="vocab"><input id="vocab" type="text" placeholder="请输入想添加的单词" autofocus="true"></div>
19   - <div class="save-btn"><button id="save">添加</button></div>
20   - </div>
  10 + <div class="tit"><h1>VideoRecorder</h1></div>
21 11  
22 12 <div class="msg" id="msg"></div>
23 13  
... ...