Skip to content

josephrocca/getVideoFrames.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 

Repository files navigation

Simple JavaScript library to performantly and reliably break a video down into its individual frames using the new WebCodecs API.

Usage

<canvas id="canvasEl"></canvas>
<br>
<input type="file" accept="video/mp4" onchange="start(this.files[0])">
<script type="module">
  import getVideoFrames from "https://deno.land/x/get_video_frames@v0.0.10/mod.js"
  
  let frameCount = 0;

  window.start = async function(file) {
    let ctx = canvasEl.getContext("2d"); 

    // `getVideoFrames` requires a video URL as input.
    // If you have a file/blob instead of a videoUrl, turn it into a URL like this:
    let videoUrl = URL.createObjectURL(file);

    await getVideoFrames({
      videoUrl,
      onFrame(frame) {  // `frame` is a VideoFrame object: https://developer.mozilla.org/en-US/docs/Web/API/VideoFrame
        ctx.drawImage(frame, 0, 0, canvasEl.width, canvasEl.height);
        frame.close();
        frameCount++;
      },
      onConfig(config) {
        canvasEl.width = config.codedWidth;
        canvasEl.height = config.codedHeight;
      },
      onFinish() {
        console.log("finished!");
        console.log("frameCount", frameCount);
      },
    });

    URL.revokeObjectURL(file); // revoke URL to prevent memory leak
  }
</script> 

About

Simple JavaScript library to break a video down into individual frames (uses WebCodecs API and MP4Box.js)

Resources

License

Stars

Watchers

Forks

Packages

No packages published