1 year ago

#324889

test-img

twominds

Stream MongoDB Grid-FS File As Input For fluent-ffmpeg Transcode Process

I am trying to compress/transcode a video file that is saved in my MongoDB database, but I am not sure how I can add it as an input stream into the ffmpeg transcoding process.

Any ideas?

async function transcodeVideo(_id) {
  gfs.collection("originalVideos");

  const video = await gfs.files.findOne({ _id: ObjectId(_id) }); //this is used for other things not shown in this example.

  const gridfsBucket = new mongoose.mongo.GridFSBucket(connection.db, {
    bucketName: "originalVideos",
  });

  const downloadStream = gridfsBucket.openDownloadStream(video._id);

  return new Promise((resolve, reject) => {
    const command = ffmpeg()
      .input(downloadStream)
      .inputFormat("mp4")
      .setFfprobePath(pathToFfprobe.path)
      .setFfmpegPath(pathToFfmpeg)
      .videoCodec("libx264")
      .audioCodec("libmp3lame")
      .size("720x?")
      .on("error", (err) => {
        console.log(err);
      })
      .on("progress", (progress) => {
      })
      .on("end", () => {
      })
      .save("./video.mp4");
  });

enter image description here

javascript

mongodb

express

fluent-ffmpeg

multer-gridfs-storage

0 Answers

Your Answer

Accepted video resources