javascript - 如何使用nodejs在服务器端将javascript动画转换为视频?

标签 javascript node.js html animation ffmpeg

我有一个应用程序,用户可以在其中创建动画,我希望能够在服务器端将这些动画转换为视频,以便用户可以保存并共享它们,例如 YouTube 等

这是我到目前为止使用 create js 和 ffmpegserver.js 创建的动画。

ffmpegserver.js。

这是一个简单的 Node 服务器和库,它将 Canvas 帧发送到服务器并使用 FFmpeg 压缩视频。它可以独立使用或与 CCapture.js 一起使用

测试3.html

<!DOCTYPE html>
<html>
<head>
    <title>TweenJS: Simple Tween Demo</title>
<style>

canvas {
          border: 1px solid #08bf31;
          justify-content: center;
          display: flex;
          align-items: center;
          margin: 0px auto;
          margin-bottom: 40px;
      }

      a {
        width: 150px;
        height: 45px;
        background: red;
        text-align: center;
        display: flex;
        justify-content: center;
        align-items: center;
        border-radius: 300px;
        color: white;
      }
      #container{
        flex-direction: column;
        justify-content: center;
        display: flex;
        align-items: center;
        margin: 0px auto;
      }
    #progress{
        margin: 30px;
    }
    #preview{
        margin: 40px;
        width: 150px;
        height: 45px;
        background: deepskyblue;
        color: white;
        border: none;
        border-radius: 300px;
    }


</style>

</head>
<body onload="init();">

<div>

<div id="container">
        <h1>Simple Tween Demo</h1>
    <canvas id="testCanvas" width="500" height="400"></canvas>
    <div id="progress"></div>
</div>
</div>
<script src="http://localhost:8081/ffmpegserver/CCapture.js"></script>
<script src="http://localhost:8081/ffmpegserver/ffmpegserver.js"></script>
<script src="https://code.createjs.com/1.0.0/createjs.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tween.js/17.2.0/Tween.js"></script>
<script src="test3.js"></script>
</body>
</html>

Test3.js

/* eslint-disable eol-last */
/* eslint-disable no-undef */
/* eslint-disable quotes */
var canvas, stage;
    function init() {
        var framesPerSecond = 60;
        var numFrames = framesPerSecond * 5; // a 5 second 60fps video
        var frameNum = 0;

        var progressElem = document.getElementById("progress");
        var progressNode = document.createTextNode("");
        progressElem.appendChild(progressNode);

        function onProgress(progress) {
          progressNode.nodeValue = (progress * 100).toFixed(1) + "%";
        }

        function showVideoLink(url, size) {
          size = size ? (" [size: " + (size / 1024 / 1024).toFixed(1) + "meg]") : " [unknown size]";
          var a = document.createElement("a");
          a.href = url;
          var filename = url;
          var slashNdx = filename.lastIndexOf("/");
          if (slashNdx >= 0) {
            filename = filename.substr(slashNdx + 1);
          }
          a.download = filename;
          a.appendChild(document.createTextNode("Download"));
          var container = document.getElementById("container").insertBefore(a, progressElem);

        }

        var capturer = new CCapture( {
          format: 'ffmpegserver',
          //workersPath: "3rdparty/",
          //format: 'gif',
          //verbose: true,
          framerate: framesPerSecond,
          onProgress: onProgress,
          //extension: ".mp4",
          //codec: "libx264",
        } );
        capturer.start();


        canvas = document.getElementById("testCanvas");
        stage = new createjs.Stage(canvas);
        var ball = new createjs.Shape();
        ball.graphics.setStrokeStyle(5, 'round', 'round');
        // eslint-disable-next-line quotes
        ball.graphics.beginStroke('#000000');
        ball.graphics.beginFill("#FF0000").drawCircle(0, 0, 50);
        ball.graphics.setStrokeStyle(1, 'round', 'round');
        ball.graphics.beginStroke('#000000');
        ball.graphics.moveTo(0, 0);
        ball.graphics.lineTo(0, 50);
        ball.graphics.endStroke();
        ball.x = 200;
        ball.y = -50;
        createjs.Tween.get(ball, {loop: -1})
            .to({x: ball.x, y: canvas.height - 55, rotation: -360}, 1500, createjs.Ease.bounceOut)
            .wait(1000)
            .to({x: canvas.width - 55, rotation: 360}, 2500, createjs.Ease.bounceOut)
            .wait(1000)
            .to({scaleX: 2, scaleY: 2}, 2500, createjs.Ease.quadOut)
            .wait(1000)
        stage.addChild(ball);
        createjs.Ticker.addEventListener("tick", stage);


        function render() {
            requestAnimationFrame(render);
            capturer.capture( canvas );

            ++frameNum;
            if (frameNum < numFrames) {
            progressNode.nodeValue = "rendered frame# " + frameNum + " of " + numFrames;
            } else if (frameNum === numFrames) {
            capturer.stop();
            capturer.save(showVideoLink);
            }
        }

        render();
}

一切正常,如果需要,您可以通过克隆存储库自行测试。

现在动画渲染发生在客户端,我希望这个动画渲染发生在后端

我需要更改什么才能使用 Nodejs 在后端服务器端渲染此动画?任何帮助或建议将不胜感激。

最佳答案

您需要使用node.js 和express(或任何类型的服务器API 处理程序)构建Rest Api

。一旦你获得了 outpot 动画,你将必须向 node.js 服务器发出 ajax post 方法请求,并让服务器处理 CCapture.js ..

服务器中的主页看起来像这样。

var app = require('express')();
var bodyParser = require('body-parser');
var upload = multer(); // for parsing multipart/form-data

app.post('/profile', function (req, res, next) {
  console.log(req.body);
  res.json(req.body);
});

另请检查 NPM 以获取更多适用于 Node.js 的软件包

关于javascript - 如何使用nodejs在服务器端将javascript动画转换为视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56106880/

相关文章:

javascript - 浏览器: Google Cloud Messaging/node-gcm - data is always null

javascript - 如何以 CSV 格式导出特定列?

node.js:导出普通函数或箭头函数?

javascript - HTML 表格循环遍历每一行

javascript - 如何使用 getElementsByClassName 选择具有类的元素并单击它们?

javascript - Nodejs Express MongoClient 集合检索

node.js - 如何为winston@3记录器制作自定义json格式化程序?

javascript - 在 Node.js 中覆盖文件

javascript - 更改标签在 DOM 中的位置

javascript - 如何使引导列匹配高度?