Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Combinando y grabando ⏺ video 🎥 y audio 🎙 en el...

Combinando y grabando ⏺ video 🎥 y audio 🎙 en el navegador

Mozilla Perú 28 Feb 2019

Lupo Montero

February 28, 2019
Tweet

More Decks by Lupo Montero

Other Decks in Programming

Transcript

  1. Combinando y grabando ⏺ video 🎥 y audio 🎙 en

    el navegador @lupomontero - Mozilla Perú 28 Feb 2019
  2. <video> const streamSettings = stream.getVideoTracks()[0].getSettings(); const video = document.createElement('video'); video.setAttribute('width',

    streamSettings.width); video.setAttribute('height', streamSettings.height); video.srcObject = stream; video.play();
  3. <canvas> const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d'); ctx.drawImage(

    image, dx, dy, dWidth, dHeight, ); https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage
  4. <canvas> const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d'); ctx.drawImage(

    video, dx, dy, dWidth, dHeight, ); https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage
  5. canvas.captureStream() const outputStream = new MediaStream(); const inputStreams = [audioStream,

    canvas.captureStream()]; inputStreams.forEach( stream => stream.getTracks().forEach( track => outputStream.addTrack(track), ), );
  6. window.requestAnimationFrame const update = () => { // redraw frame

    ... window.requestAnimationFrame(update); }; update();
  7. MediaRecorder const recordedBlobs = []; const recorder = new MediaRecorder(outputStream,

    { mimeType: 'video/webm', }); recorder.addEventListener('dataavailable', (e) => { if (e.data && e.data.size > 0) { recordedBlobs.push(e.data); } }); recorder.start(1000);
  8. Blob y window.URL recorder.stop(); const blob = new Blob(recordedBlobs, {

    type: 'video/webm' }); const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.style.display = 'none'; a.href = url; a.download = 'test.webm'; document.body.appendChild(a); a.click(); setTimeout(() => { document.body.removeChild(a); window.URL.revokeObjectURL(url); }, 100);