Node.js-将数据缓冲到Ffmpeg [英] Node.js - Buffer Data to Ffmpeg

查看:84
本文介绍了Node.js-将数据缓冲到Ffmpeg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Node.js和Ffmpeg创建动画.因为我试图避免使用第三方avi/mp4解析器,所以我决定将动画输出为原始rgb24数据文件,然后使用一些程序将其转换为mp4文件.

I used Node.js and Ffmpeg to create animations. Because I was trying to avoid third-party avi/mp4 parsers, I decided to output the animation as raw rgb24 data file and then use some program to convert it to mp4 file.

我发现Ffmpeg是免费的开放源代码,可以做到这一点.因此,我制作了一个Node.js应用程序,该应用程序分配大小为 1920 x 1080 x 3 (宽度乘以高度乘以每个像素的字节数)的 Buffer 渲染上下文库,最后我逐帧进行动画处理,并将每帧连续保存在二进制文件中(使用 fs 模块).

I found that Ffmpeg is free and open source which can do exactly it. So, I made a Node.js application which allocates a Buffer of size 1920 x 1080 x 3 (width times height times number of bytes per pixel), then I created a rendering context library, and finally I animated frame by frame and saved each frame consecutivelly in a binary file (using fs module).

然后我调用Ffmpeg将其转换为mp4文件,并且效果很好.动画非常容易制作,Ffmpeg可以正确执行其工作.

Then I invoked Ffmpeg to convert it to mp4 file and it works very good. Animations are pretty easy to make and Ffmpeg does its job correctly.

但是,唯一的问题是因为它非常慢并且会占用硬盘上的空间.我想制作很长的动画(一个多小时).最终的mp4文件相对较小,但原始视频文件却非常大.每帧约有百分之九十是黑色像素,因此Ffmpeg称赞它很好,但是原始文件无法压缩,有时甚至需要100 GB以上.另外,非常不必要地对相同数据进行双重处理.首先,我在Node.js中对其进行处理以将数据保存到文件中,然后Ffmpeg对其进行读取以将其转换为mp4.有很多不必要的工作.

However, the only problem is because it is very slow and eats space on hard disk. I want to create very long animations (more than a hour). The final mp4 file is relativelly small, but raw video file is extremelly big. About ninety percents of each frame are black pixels, so Ffmpeg comress it very good, but raw file cannot be compressed and it takes sometimes mor ethan 100 Gigabytes. Also, there is very unnecessary double processing same data. Firstly I process it in Node.js to save data to file, and then Ffmpeg reads it to convert it to mp4. There is a lot of unnecessary work.

所以,我正在寻找一种方法(我很确定这是可能的,但是我还没有找到一种方法)可以将原始视频数据(一次一帧)输出到Ffmpeg处理(不保存任何内容到硬盘).

So, I'm looking for a way (and I'm pretty sure it is possible, but I didn't find a way to do it yet) to output raw video data (one frame at a time) to Ffmpeg process (without saving anything to the hard disk).

我的目标是执行以下操作:

My goal is to do the following:

  1. 打开Ffmpeg进程
  2. 在Node.js中渲染框架
  3. 将原始字节流输出到Ffmpeg
  4. 等待Ffmpeg对其进行编码并附加到mp4文件中
  5. 让Ffmpeg等待我的Node.js进程渲染下一帧


有没有办法实现?我确实看不到的原因,因为我当前的代码与我在此处提出的问题无关.我不会遇到语法错误或实现问题.不,相反,我只是不知道要将哪些参数传递给Ffmpeg进程,以实现我已经解释的内容.


Is there a way to achieve it? I really don't see a reason to post code, because my current code has nothing to do with the question I'm asking here. I don't struggle with syntax errors or implementation problems. No, instead I just don't know which parameters to pass to Ffmpeg process in order to achieve what I've already explained.

我在文档中进行了搜索,以找出需要传递给Ffmpeg进程的参数,以便使其能够从stdin而非文件中读取原始数据,并且还等到Node.js进程呈现下一帧(因此以禁用时间限制),因为渲染帧可能需要24个小时以上.因此,Ffmpeg进程应等待而没有时间限制.但是,我在文档中什么都没找到.

I've searched in documentation to find out which parameters I need to pass to Ffmpeg process in order to let it read raw data from stdin instead from file, and also to wait until my Node.js process render next frame (so to disable time limit) because rendering a frame may take more than 24 hours. Therefore, Ffmpeg process should wait without time limit. However, I didn't find anything about it in documentation.

我知道如何从Node.js和类似的技术资料编写stdin,因此无需解释.这里唯一的问题:

I know how to write to stdin from Node.js and similar technical stuff, so no need to explain it. The only question(s) here:

  1. 哪些参数要传递给Ffmpeg?
  2. 我是否需要使用一些特殊选项来创建Ffmpeg进程(使用 child_process )?


先谢谢您.请放心,这是我的第一个问题!:)


Thank you in advance. Please, take it easy, this is my first question! :)

推荐答案

如@Mulvya和@estus所建议,正确的参数为 -i-(以启用来自stdin的输入):

As suggested by @Mulvya and @estus, the correct parameter is -i - (to enable input from stdin):

'use strict';

var cp = require('child_process');

var proc = cp.spawn('ffmpeg', [
  '-hide_banner',
  '-f', 'rawvideo',
  '-pix_fmt', 'rgb24',
  '-s', '2x2',
  '-i', '-',
  '1.png'
]);

proc.stdin.write(Buffer.from([
  255, 0, 0,
  0, 255, 0,
  0, 255, 255,
  255, 0, 255
]));

proc.stdin.end();

proc.stderr.pipe(process.stdout);

它产生此图像:

它也适用于动画.

It produces this image:

It works for animations too.

感谢大家的帮助!

这篇关于Node.js-将数据缓冲到Ffmpeg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆