Jump to content

Record video from PIXI canvas with CCapture.js


martiniti
 Share

Recommended Posts

Hey guys!

I'm stuck with one task, maybe someone has experience to help with it.

For recording video I using CCapture.js, but when I start it everything stops with no errors.

My basic code is:


const
	videoContainer = document.getElementById('videoContainer');	
	vw = 1280,
  	vh = 720,
  	videoUrl = 'assets/video/landscape.mp4';

PIXI.settings.RESOLUTION = 2;

app = new PIXI.Application({
  width: vw,
  height: vh,
  backgroundColor: bgColor,
});

const videoBg = PIXI.Texture.from(videoUrl);
const videoSprite = new PIXI.Sprite(videoBg);
const videoControler = videoSprite._texture.baseTexture.resource.source;

videoContainer.appendChild(app.view);

let capturer = new CCapture( { format: 'webm' } );

let n = 0;

app.ticker.add(() => {

	if(n == 0){
		capturer.start();
	}

	capturer.capture(app.view);

	if(videoControler.currentTime >= videoControler.duration){

		capturer.stop();

		capturer.save();

		app.ticker.destroy();

	}

	n += 1;

});

 

Also I created some example and you can see it stops at the beginning too: 

https://codepen.io/fjtwmjzf-the-lessful/pen/zYZJwvQ

 

I would be grateful for any help!

Link to comment
Share on other sites

  • 4 weeks later...

Hey guys!

Here is a solution for this question:

function recordVideoWithCCapture(){

   const videoContainer = document.getElementById('recordContainer');

   // Modern
   let capturer = null;

   const isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;

   const options = {
      FirefoxFramerate: 120,
      FirefoxQuality: 100,
      ChromeFramerate: 30,
      ChromeQuality: 99.9,
   }

   function initRecording(){
      capturer = new CCapture( {
         //verbose: true, // print info to console
         display: true, // display record info
         //motionBlurFrames: 10, // blur, problem with time
         framerate: isFirefox ? options.FirefoxFramerate : options.ChromeFramerate,
         quality: isFirefox ? options.FirefoxQuality : options.ChromeQuality, // (99.9 - webm)
         format: isFirefox ? 'webm-mediarecorder' : 'webm', //'webm'
         // timeLimit: 0, //
         // startFrom: 0, //in second
      } );
   }

   function startRecording(){
      initRecording();
      capturer.start();
   }

   function stopRecording(){
      capturer.stop();
      capturer.save();
   }

   videoContainer.innerHTML = "";

   let vw = videoContainer.parentElement.offsetWidth;
   let vh = videoContainer.parentElement.offsetHeight;
   let videoUrl = 'path/to/your_video.mp4';


   app = new PIXI.Application({
      width: vw, height: vh, backgroundColor: 0x1099bb, resolution: window.devicePixelRatio || 1,
   });

   videoContainer.appendChild(app.view);

   const renderer = PIXI.autoDetectRenderer({ width: vw, height: vh, resolution: 2 });

   renderer.view.style.width = `${vw}px`;
   renderer.view.style.height = `${vh}px`;

   // create the root of the scene graph
   const stage = new PIXI.Container();

   app.stage.addChild(stage);

   // create a video texture from a path
   const texture = PIXI.Texture.from(videoUrl);

   // create a new Sprite using the video texture (yes it's that easy)
   const videoSprite = new PIXI.Sprite(texture);

   videoSprite.width = vw;
   videoSprite.height = vh;

   stage.addChild(videoSprite);

   const videoControler = videoSprite._texture.baseTexture.resource.source;

   videoSprite._texture.baseTexture.resource.source.loop = true;

   animate();

   startRecording();

   function animate(i){

      let currentTime = 0;

      if(isFirefox){
         currentTime = videoControler.currentTime *  options.FirefoxFramerate/30;
      } else {
         currentTime = videoControler.currentTime *  options.ChromeFramerate/15;
      }

      if (currentTime >= videoControler.duration) {

         stopRecording();

         // Clean up WebGL memory
         if(app){
            app.destroy();
         }

         videoContainer.innerHTML = '';

      } else {

         // render the stage
         renderer.render(stage);

         requestAnimationFrame(animate);

         /* Record Video */
         if( capturer ) capturer.capture( renderer.view );

      }

   }

}

 

Link to comment
Share on other sites

  • 7 months later...
On 6/10/2021 at 6:20 PM, martiniti said:

The reason why I am trying to use CCapture.js is because I want to keep video in a good quality with 60 FPS.

I also try to record with MediaRecorder but result was with low quality and freezes. 

i try your code ,and the webM video play very fast.like video play 10 times fast in the record video .any suggestion?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...