Michel
Michel

Reputation: 11755

Getting audio duration on the server and passing it to the client

I have this express server code (see below) defining a route to play an audio file, stored using gridFS. I works fine, but I would like to modify it to also get the duration of the audio. Is that possible to send the duration along with the audio itself ? Or something similar.

In other words, I need to know how to grab the audio duration and then how to pass it to the calling client.

  server.get('/play', (req, res) => {
    const gridFSBucket = new mongoose.mongo.GridFSBucket(conn.db);
    const dataType = "audio/mp3"; //"audio/mpeg";//"video/webm";
    // The next line adjusts the content type based on the audio format.
    // The code still works even when commented out.
    res.set('Content-Type', dataType);
 
    if (req.query.afn) {
      const filename = req.query.afn;
      const downloadStream = gridFSBucket.openDownloadStreamByName(filename);
      res.set('Content-Disposition', `inline; filename="${filename}"`);
      downloadStream.pipe(res)
    }

    if (req.query.id) {
      const recordID = new ObjectId(req.query.id)
      const downloadStream = gridFSBucket.openDownloadStream(recordID);
      res.set('Content-Disposition', `inline; filename=APP-Audio`);
      downloadStream.pipe(res);
    }
  });

Upvotes: 1

Views: 75

Answers (0)

Related Questions