ffmpeg - How to prevent innacurate duration when converting from pcm to wav?

1

1

I have the following command:

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

input.pcm is the pcm data that comes from a 2 seconds wav file. However, when encoding with ffmpeg, I get the following log:

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%

As you can see, the estimated duration for the input is 4 seconds, instead of 2 seconds (so obviously the output is also 4 seconds instead of 2 seconds).

I know pcm data is raw data without metadata. What additional metadata do I have have to pass ffmpeg so that it can correctly infer the duration of the file? I could use -t, but my guess is that there is a better way to let ffmpeg know what the duration is (e.g. more metadata).

EDIT

Code that does the interleaving:

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;
}

maximedupre

Posted 2019-06-03T18:37:54.227

Reputation: 27

So your output also only contains 2 seconds worth of actual audio? – slhck – 2019-06-03T19:50:20.107

The output contains 4 seconds of audio. I expect that it contains 2 seconds of audio. – maximedupre – 2019-06-03T20:13:56.347

What was the command to generate the pcm file? – Gyan – 2019-06-04T06:18:24.483

Two PCM files were created programmatically (see getChannelData()) for the left and right channels and I interleaved them also programmatically (see edit) to create output.pcm.

– maximedupre – 2019-06-04T12:13:24.137

I meant *input.pcm – maximedupre – 2019-06-06T19:07:39.940

No answers