缓冲区内存泄漏

发布于 2025-02-13 08:42:51 字数 2123 浏览 1 评论 0原文

编码结果始终在顶部有故障。如果我只是直接管道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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文