缓冲区内存泄漏
编码结果始终在顶部有故障。如果我只是直接管道decode.stdout.pipe(encode.stdin)
,则无故障。我尽力解决。我的问题是在试图读取以稍后进行操纵时出现的。我不知道,这是崛起问题的哪一部分。我认为我需要刷新,来回阅读文档。什么都没有改变。
const { spawn } = require('child_process');
const path = require('path');
const decArgs = [
'-i', path.join(__dirname + '/public/test.mp4'),
'-an',
'-pix_fmt', 'bgr24',
'-f', 'rawvideo',
'-'
];
const decode = spawn('ffmpeg', decArgs, {
stdio: [
'ignore',
'pipe',
'ignore'
]
});
// Encode section
const width = 854;
const height = 480;
const channels = 3;
const fps = 23.97602397602398.toString();
const colorsLength = width*height*channels;
const encArgs = [
'-f', 'rawvideo',
'-pix_fmt', 'bgr24',
'-s', `${width}x${height}`,
'-r', fps,
'-i', '-',
'-c:v', 'libx264',
'-preset', 'ultrafast',
'-crf', '30',
'-pix_fmt', 'yuv420p',
'-r', fps,
'-y',
path.join(__dirname + '/public/output.mp4')
];
const encode = spawn('ffmpeg', encArgs, {
stdio: [
'pipe',
'ignore',
'pipe'
]
});
// colors RGBA
let buffer = Buffer.alloc(0);
decode.stdout.on('data', data => {
const allocSize = buffer.length + data.length;
buffer = Buffer.concat([buffer, data], allocSize);
if (buffer.length > colorsLength) decode.stdout.pause();
});
decode.stdout.on('pause', () => {
// Create new buffer with size of colors length
const bufferData = Buffer.alloc(colorsLength);
buffer.copy(bufferData, 0, 0, colorsLength);
// after manipulate the buffer send it to encode
encode.stdin.write(bufferData);
// Create new buffer to take out left buffer. cause stream length cant be predict.
const leftBuffer = Buffer.alloc(buffer.length - bufferData.length);
buffer.copy(leftBuffer, 0, buffer.length - bufferData.length, buffer.length);
buffer = leftBuffer;
decode.stdout.resume()
});
decode.stdout.on('end', () => {
if (buffer.length) encode.stdin.write(buffer);
encode.stdin.end();
})
encode.stderr.on('data', data => {
console.log(data.toString())
})
Encode result always theres glitch in top. if i just pipe directly decode.stdout.pipe(encode.stdin)
, no glitch. i try my best to solve. my problem arises when try to read as chunk to manipulate later. i dont know again, which part that rise problem. i think i need to refresh, keep back and forth to read documentation. nothing change.
const { spawn } = require('child_process');
const path = require('path');
const decArgs = [
'-i', path.join(__dirname + '/public/test.mp4'),
'-an',
'-pix_fmt', 'bgr24',
'-f', 'rawvideo',
'-'
];
const decode = spawn('ffmpeg', decArgs, {
stdio: [
'ignore',
'pipe',
'ignore'
]
});
// Encode section
const width = 854;
const height = 480;
const channels = 3;
const fps = 23.97602397602398.toString();
const colorsLength = width*height*channels;
const encArgs = [
'-f', 'rawvideo',
'-pix_fmt', 'bgr24',
'-s', `${width}x${height}`,
'-r', fps,
'-i', '-',
'-c:v', 'libx264',
'-preset', 'ultrafast',
'-crf', '30',
'-pix_fmt', 'yuv420p',
'-r', fps,
'-y',
path.join(__dirname + '/public/output.mp4')
];
const encode = spawn('ffmpeg', encArgs, {
stdio: [
'pipe',
'ignore',
'pipe'
]
});
// colors RGBA
let buffer = Buffer.alloc(0);
decode.stdout.on('data', data => {
const allocSize = buffer.length + data.length;
buffer = Buffer.concat([buffer, data], allocSize);
if (buffer.length > colorsLength) decode.stdout.pause();
});
decode.stdout.on('pause', () => {
// Create new buffer with size of colors length
const bufferData = Buffer.alloc(colorsLength);
buffer.copy(bufferData, 0, 0, colorsLength);
// after manipulate the buffer send it to encode
encode.stdin.write(bufferData);
// Create new buffer to take out left buffer. cause stream length cant be predict.
const leftBuffer = Buffer.alloc(buffer.length - bufferData.length);
buffer.copy(leftBuffer, 0, buffer.length - bufferData.length, buffer.length);
buffer = leftBuffer;
decode.stdout.resume()
});
decode.stdout.on('end', () => {
if (buffer.length) encode.stdin.write(buffer);
encode.stdin.end();
})
encode.stderr.on('data', data => {
console.log(data.toString())
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论