首页 > 解决方案 > 有没有办法在node.js中使用带有读取流的ffprobe(fluent-ffmpeg)输入?

问题描述

我在我的代码中使用 fluent-ffmpeg,我的主要目标是获取音频/视频持续时间,我需要使用流作为我的输入。

根据文件, https://github.com/fluent-ffmpeg/node-fluent-ffmpeg#reading-video-metadata

ffmpeg('/path/to/file1.avi')
  .input('/path/to/file2.avi')
  .ffprobe(function(err, data) {
    console.log('file2 metadata:');
    console.dir(data);
  });

ffmpeg('/path/to/file1.avi')
  .input('/path/to/file2.avi')
  .ffprobe(0, function(err, data) {
    console.log('file1 metadata:');
    console.dir(data);
  });

我试过这些

const ffmpeg = require('fluent-ffmpeg')
const fs = require('fs')

filepath = './scratch_file/assets_audios_10000.wav'
stream = fs.createReadStream(filepath)
ffmpeg(stream)
.input(filepath) // have to put a file path here, possible path dependent
.ffprobe(function (err, metadata) {
    if (err){throw err}
    console.log(metadata.format.duration);
}) //success printing the duration 

以上成功返回时长

ffmpeg(stream)
.input(stream) //
.ffprobe(function (err, metadata) {
    if (err){throw err}
    console.log(metadata.format.duration);
}) // failed

以上失败。

ffmpeg(stream)
.ffprobe(function (err, metadata) {
    if (err){throw err}
    console.log(metadata.format.duration);
}) //returned "N/A"

已退回 N/A

有人可以帮忙吗?我需要类似的东西

ffmpeg.ffprobe(stream, (metadata) => {console.log(metadata.format.duration)} )

谢谢你。

标签: node.jsvideoffmpeg

解决方案


以下代码对我有用。

  let ffmpeg = require('fluent-ffmpeg')

  // create a new readable stream from whatever buffer you have
  let readStream = new Readable()
  readStream._read = () => {}
  readStream.push(imageBufferObject.buffer)
  readStream.push(null)

 // I used a call to a promise based function to await the response
 let metadata = await get_video_meta_data(readStream)
 console.log(metadata)

 async function get_video_meta_data(stream){
  return new Promise((resolve, reject)=>{
    ffmpeg.ffprobe(stream, (err, meta)=>{
      resolve(meta)
    })
 })

}

只需将可读流输入到 ffmpeg.ffprobe() 中,它期望文件路径似乎对我有用,因为我可以在不写入磁盘的情况下提取元数据。


推荐阅读