Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write a frame of an embedded YouTube video (via iframe) to a canvas?

I'd like to be able to extract frames from YouTube videos at various points within them (not just at the thumbnails), and do some processing on them. I can embed the video in my website using the iframe API, but I am struggling to find a way to capture that to a canvas. (It's ok if I am forced to capture the entire screen, and ok if I have to make changes to browser settings to allow it.)

like image 605
rob Avatar asked Feb 16 '19 22:02

rob


People also ask

How do I embed an Iframe in canvas?

Navigate to the page in which you want to embed the new iframe, Click "Edit", then toggle to the "HTML Editor", and paste your code Where you would like it to appear on your page. Most likely, you will simply scroll to the end of the code that describes everything you typed at the top of that page.

Can you iframe a YouTube video?

Use the youtube site to find the video you want. Click the 'Share' button below the video. Click the 'Embed' button next to the link they show you. Copy the iframe code given and paste it into the html of your web page.

How do you embed an iframe in an HTML page?

The HTML <iframe> tag specifies an inline frame. The src attribute defines the URL of the page to embed. Always include a title attribute (for screen readers) The height and width attributes specifies the size of the iframe. Use border:none; to remove the border around the iframe.

Can I use AutoPlay With iFrame and object embeds?

The flags, or parameters that you can use with IFRAME and OBJECT embeds are documented here; the details about which parameter works with what player are also clearly mentioned: You will notice that autoplay is supported by all players (AS3, AS2 and HTML5). Show activity on this post.

What is the <iframe> tag used for?

W3Schools is Powered by W3.CSS. The HTML <iframe> tag specifies an inline frame. An inline frame is used to embed another document within the current HTML document. Tip: It is a good practice to always include a title attribute for the <iframe>.

How do I embed a YouTube video in an NPM post?

You can just use an iframe element to achieve this. There is no need to depend on yet another npm package. By the way, if you are wondering why the embed URL looks like it looks, you can just go to an arbitrary Youtube video, click on Share and then you immediately see that we just need to append /embed/:videoId Youtube's base URL.


1 Answers

One option would be to write a browser extension. But I would guess you would want to avoid that.

The other option is to use the Screen Capture API (which is supported in Chrome, Edge and Firefox). Check out the browser compatibility information on MDN.

Using this API, you can allow your user to share his screen or browser tab and play this on a transient video element:

const videoElem = document.createElement('video');
videoElem.autoplay = true;

const displayMediaOptions = {
  video: {
    cursor: "never"
  },
  audio: false
};

async function startCapture() {
  videoElem.srcObject = await navigator.mediaDevices
    .getDisplayMedia(displayMediaOptions);
}

Later you can draw the video on a canvas to get a Base64 frame (basically a screenshot).

function getScreenshotInBase64() {
  let canvas = document.createElement('canvas');
  let context = canvas.getContext('2d');
  let [w, h] = [videoElem.videoWidth, videoElem.videoHeight];
  canvas.width =  w;
  canvas.height = h;
  context.drawImage(videoElem, 0, 0, w, h);
  return canvas.toDataURL();
}

Limitations with this approach:

  • The user may select a different tab / application / screen or the page may be scrolled down and the video not visible.
  • You get more than just the video (but based on your description, this is not necessarily bad).
  • Pretty scarce browser support (if you consider older or mobile browsers).
  • The user has to allow you to do it (basically each time you want to start sharing = recording the video in your case).

I build a small JsFiddle with the above snippets, which gets a Base64 print screen 2 seconds after you start screen sharing (by clicking the start button): https://jsfiddle.net/75Lmbf2o/.

like image 151
Serban Petrescu Avatar answered Nov 05 '22 12:11

Serban Petrescu