Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
400 views
in Technique[技术] by (71.8m points)

javascript - 由于不建议使用Buffer(),因此将DesktopCapturer保存到Electron中的文件中(Saving DesktopCapturer to file in Electron since Buffer() deprecated)

Hello I have already read and attempted the thread: Saving desktopCapturer to video file in Electron(您好,我已经阅读并尝试了以下线程:将DesktopCapturer保存到Electron中的视频文件)

This is what I have so far:(这是我到目前为止的内容:) const { desktopCapturer } = require('electron') var fs = require('fs'); function startRecording(){ desktopCapturer.getSources({ types: ['window', 'screen'] }).then(async sources => { for (const source of sources) { if (source.name === 'Entire Screen') { try { const stream = await navigator.mediaDevices.getUserMedia({ audio: false, video: { mandatory: { chromeMediaSource: 'desktop', chromeMediaSourceId: source.id, minWidth: 1280, maxWidth: 1280, minHeight: 720, maxHeight: 720 } } }) handleStream(stream) } catch (e) { handleError(e) } return } }}) } function handleStream(stream) { recorder = new MediaRecorder(stream); blobs = []; recorder.ondataavailable = function(event) { blobs.push(event.data); }; recorder.start(); } function stopRecording() { recorder.stop(); console.log(blobs); toArrayBuffer(new Blob(blobs, {type: 'video/webm'}), function(ab) { var buffer = toBuffer(ab); var file = `./videos/example.webm`; fs.writeFile(file, buffer, function(err) { if (err) { console.error('Failed to save video ' + err); } else { console.log('Saved video: ' + file); } }); }); } function handleUserMediaError(e) { console.error('handleUserMediaError', e); } function toArrayBuffer(blob, cb) { let fileReader = new FileReader(); fileReader.onload = function() { let arrayBuffer = this.result; cb(arrayBuffer); }; fileReader.readAsArrayBuffer(blob); } function toBuffer(ab) { let buffer = Buffer.alloc(ab.byteLength); let arr = new Uint8Array(ab); for (let i = 0; i < arr.byteLength; i++) { buffer[i] = arr[i]; } return buffer; } // Record for 3.5 seconds and save to disk startRecording(); setTimeout(function() { stopRecording() }, 3500) I just want to save the recorded video to a file.(我只想将录制的视频保存到文件中。) The file ends up being empty when it saves.(保存后文件最终为空。) I have been stuck at this awhile and would appreciate any advice.(我一直被困在这一段时间,并希望得到任何建议。) Thank you(谢谢)   ask by Jonah Kornberg translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Welp I stumbled upon this thread: Saving desktopCapturer to video file from Electron app(遗憾的是,我偶然发现了这个线程:将DesktopCapturer 保存到Electron应用程序的视频文件中)

and changed my stopRecording() function to :(并将我的stopRecording()函数更改为:) function stopRecording() { const save = () =>{ toArrayBuffer(new Blob(blobs, {type: 'video/webm'}), function(ab) { var buffer = toBuffer(ab); var file = `./videos/example.webm`; fs.writeFile(file, buffer, function(err) { if (err) { console.error('Failed to save video ' + err); } else { console.log('Saved video: ' + file); } }); }); } recorder.onstop = save; recorder.stop(); } and it seems to be working.(它似乎正在工作。) Cool!(凉!)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...