Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

351
Visualizações
Write or stream audio (live-voice) file to variable as a binary in Node.js

I am working with audio streams in Node.js. As for now, my code doesn't have utils.promisfy and I have 3 stages of it. So after the 2nd .pipe I am writing file to disk in wav audio format with required params.

Code example below:

import { FileWriter } from 'wav';

const filename = `./${Date.now()}-${userId}.wav`;
const encoder = new OpusEncoder(16000, 1);

  receiver
    .subscribe(userId, {
      end: {
        behavior: EndBehaviorType.AfterSilence,
        duration: 100,
      },
    })
    // OpusDecodingStream is a custom class, which convert audio, like a gzip stage for file.
   .pipe(new OpusDecodingStream({}, encoder))
   .pipe(
      // Writes wav file to disk, also can be replaces with FileRead, part of wav module
      new FileWriter(filename, {
        channels: 1,
        sampleRate: 16000,
      }),
    );

The problem is: I need to transfer (not streaming!) resulting audio file in binary format via axios POST method. So I guess, it's a bit wrong to write file on disk instead of writing it in variable, and after stream ends, send it right to required URL. Something (by logic) which I'd like to see:

// other code

const fileStringBinary = await receiver
    .subscribe(userId, {
      end: {
        behavior: EndBehaviorType.AfterSilence,
        duration: 100,
      },
    })
   .pipe(new OpusDecodingStream({}, encoder))
   .pipe(
      return new FileWriter(filename, {
        channels: 1,
        sampleRate: 16000,
      }),
    );

await axios.post('https://url.com', {
  data: fileStringBinary 
});

Unfortunately I am not so good with streams and especially with audio one, so I am looking for a bit help or any useful advice will be welcome for me.

I understand, that I could write my file to directory, find it there, read once again with node:steam createReadStream and then POST it to required URL. This is not what I need. I'd like to skip this useless stages with writing and then reading. I believe that there is a way to transform steam to binary format and write it down to js variable.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

That was a bit treaky after all, but I guess I figure it out:

const stream = receiver
    .subscribe(userId, {
        end: {
            behavior: EndBehaviorType.AfterSilence,
            duration: 100,
        },
    })
    .pipe(
        new opus.OggLogicalBitstream({
            opusHead: new opus.OpusHead({
                channelCount: 2,
                sampleRate: 48000,
            }),
            pageSizeControl: {
                maxPackets: 10,
            },
            crc: false,
        }),
    );

const data = [];

stream.on('data', (chunk) => {
    data.push(chunk);
});

stream.on('end', async () => {
    try {
        const response = await axios.post(
            `https://url.com${postParams}`,
            Buffer.concat(data),
            {
                headers: {
                    Authorization: `Api-Key ${token}`,
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
            },
        );

        console.log(response);
    } catch (e) {
        console.log(e);
    }
});

Unfortunately, I haven't found a better solution, then using old-school events model with data and on end. My working case is connected with Discord.js voice recording without file and using stream for voice recognition.

I will be glad if someone will provide a better-syntax solution, and in that case I'll accept this answer as solved.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda