ffmpeg - 如何防止从 pcm 转换为 wav 时出现不准确的持续时间?

ffmpeg - 如何防止从 pcm 转换为 wav 时出现不准确的持续时间?

我有以下命令:

ffmpeg -f f32le -ar 44100 -channels 2 -i input.pcm -y -acodec pcm_s16le -f wav output.wav

input.pcm是来自 2 秒 wav 文件的 pcm 数据。但是,当使用 ffmpeg 编码时,我收到以下日志:

ffmpeg version 4.1-tessus  https://evermeet.cx/ffmpeg/  Copyright (c) 2000-2018 the FFmpeg developers
built with Apple LLVM version 10.0.0 (clang-1000.11.45.5)
configuration: --cc=/usr/bin/clang --prefix=/opt/ffmpeg --extra-version=tessus --enable-avisynth --enable-fontconfig --enable-gpl --enable-libaom --enable-libass --enable-libbluray --enable-libfreetype --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libmysofa --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopus --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzimg --enable-libzmq --enable-libzvbi --enable-version3 --pkg-config-flags=--static --disable-ffplay
libavutil      56. 22.100 / 56. 22.100
libavcodec     58. 35.100 / 58. 35.100
libavformat    58. 20.100 / 58. 20.100
libavdevice    58.  5.100 / 58.  5.100
libavfilter     7. 40.101 /  7. 40.101
libswscale      5.  3.100 /  5.  3.100
libswresample   3.  3.100 /  3.  3.100
libpostproc    55.  3.100 / 55.  3.100
[f32le @ 0x7f8f4d800400] Estimating duration from bitrate, this may be inaccurate
Guessed Channel Layout for Input Stream #0.0 : stereo
Input #0, f32le, from 'input.pcm':
Duration: 00:00:04.00, bitrate: 2822 kb/s
    Stream #0:0: Audio: pcm_f32le, 44100 Hz, stereo, flt, 2822 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (pcm_f32le (native) -> pcm_s16le (native))
Press [q] to stop, [?] for help
Output #0, wav, to 'output.wav':
Metadata:
    ISFT            : Lavf58.20.100
    Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, stereo, s16, 1411 kb/s
    Metadata:
    encoder         : Lavc58.35.100 pcm_s16le
size=     689kB time=00:00:04.00 bitrate=1411.4kbits/s speed= 637x    
video:0kB audio:689kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.011054%

如您所见,输入的预计持续时间为 4 秒,而不是 2 秒(因此显然输出也是 4 秒而不是 2 秒)。

我知道 pcm 数据是没有元数据的原始数据。我需要向 ffmpeg 传递哪些额外的元数据才能使其正确推断文件的时长?我可以使用-t,但我猜有更好的方法让 ffmpeg 知道时长是多少(例如更多元数据)。

编辑

执行交错的代码:

public static interleaveChannelData(channelData: string[]): Float32Array {
    const nbChannels = channelData.length;

    const mapChannelData = (data: string): Float32Array => {
        const ab = Buffer.from(data, 'base64').buffer;

        return new Float32Array(ab);
    };

    const channels = channelData.map(mapChannelData);
    const totalChannelDataLength = channels.reduce((acc: number, data: Float32Array): number => acc + data.length, 0);
    const interleavedChannelData = new Float32Array(totalChannelDataLength);

    for (let i = 0; i < interleavedChannelData.length; i += 1) {
        let channelNb = nbChannels;

        while (i % channelNb !== 0) {
            channelNb -= 1;
        }

        interleavedChannelData[i] = channels[channelNb - 1][Math.floor(i / nbChannels)];
    }

    return interleavedChannelData;
}

相关内容