FFmpeg编码数据包持续时间始终为零

时间:2019-11-11 16:04:54

标签: ffmpeg

我正在尝试学习使用FFmpeg的libav库。我正在使用Node的Beamcoder包装器。我有一个程序可以解码MP4文件,然后尝试将其编码为h264。来自编码步骤的结果数据包始终将其持续时间设置为零。任何想法为什么会这样?

const beamcoder = require('beamcoder');

async function run() {
    const dm = await beamcoder.demuxer('file:Video_001.mp4');
    let videoStream = null;

    for (const stream of dm.streams) {
        const codecpar = stream.codecpar;

        if (codecpar.codec_type === 'video') {
            videoStream = stream;
        }
    }

    const videoDecoder = beamcoder.decoder({
        demuxer: dm,
        stream_index: videoStream.index
    });

    const videoEncoder = beamcoder.encoder({
        name: 'h264',
        time_base: videoStream.time_base,
        framerate: videoStream.r_frame_rate,
        pix_fmt: videoStream.codecpar.format,
        height: videoStream.codecpar.height,
        width: videoStream.codecpar.width,
    });

    let packet = await dm.read();
    while (packet !== null) {
        if (packet.stream_index === videoStream.index) {
            const decResult = await videoDecoder.decode(packet);

            if (decResult.frames.length > 0) {
                const encResult = await videoEncoder.encode(decResult.frames);
                if (encResult.packets.length > 0) {
                    console.log(encResult.packets[0].duration); // Always prints 0
                }
            }
        }

        packet = await dm.read();
    }
}

(async () => {
    try {
        await run();
    } catch (err) {
        console.log('FAILED', err);
    }
})();

1 个答案:

答案 0 :(得分:2)

并非所有文件类型都对数据包持续时间进行编码,某些文件仅对绝对帧时间戳进行编码。这是正常现象,只是您必须在代码中处理的事情。