Ad

Scrolling Page In PhantomJS

- 1 answer

I use PhantomJS and ffmpeg to render video from the frames. I try to do scroll frames and render it, but nothing success. My code is:

  const page = require("webpage").create();

  const getImage = (link, duration) => {
  page.viewportSize = { width: windowWidth, height: windowHeight };
  page.scrollPosition = {top: 0, left: 0};
  let videoDuration = Math.floor(duration * 25);

  if (link.startsWith("http://") || link.startsWith("https://")) {
    page.open(link, () => {
      let frame = 0;
      setInterval(() => {
        page.render("frames/image" + frame++ + ".png", { format: "png" 
  });
        page.evaluate(function () { window.scrollBy = 100; });
        if (frame > videoDuration) {
          phantom.exit();
        }
      }, 25);
    });
  } else {
    console.log("Enter a valid link");
    phantom.exit();
  }
};

getImage(imageLink, duration);

When I run the rendered video, it only plays as any video and hasn't any scroll. What do I make wrong? PS: I found few solutions with page.scroolPosition - but they are not working too.

Ad

Answer

As I can analyze, for this site "jet.gml.aisnovations.com/" I can't create video rendering, BUT... but I had made it for https://ru.wikipedia.org

Here is the code:

  const imageLink = "https://ru.wikipedia.org";
  const duration = 10; 

  const page = require("webpage").create();

  const saveImages = (link, duration) => {
  const width = 1024;
  const height = 768;
  page.viewportSize = { width, height };

  let videoDuration = Math.floor(duration * 25);

  if (
    link.startsWith("http://") ||
    link.startsWith("https://")
  ) {
    page.open(link, () => {
      const scrollHeight = page.evaluate(() => {
        return document.body.scrollHeight;
      });

      const scrollStep = (scrollHeight - height) / videoDuration;

      for (let i = 0; i < videoDuration; i += 1) {

        page.clipRect = {
          width,
          height,
          left: 0,
          top: scrollStep * i
        };

        page.render("frames/image" + (i + 1) + ".png", { format: "png" });
      }

      phantom.exit();
    });
  } else {
    console.log("Enter a valid link");
    phantom.exit();
  }
};

saveImages(imageLink, duration);

Script save images in folder frames and then render from them video clip with command

`ffmpeg -start_number 10 -i frames/image%02d.png -c:v libx264 -r 25 -pix_fmt yuv420p out.mp4`
Ad
source: stackoverflow.com
Ad