简体   繁体   English

使用 fluent-ffmpeg 将 ffprobe 元数据返回到另一个 function

[英]Returning ffprobe metadata to another function using fluent-ffmpeg

I'm trying to use fluent-ffmpeg 's ffprobe to get the metadata of a file and add it to a list, but I want to separate the process of getting the metadata from the method related to checking the file, mostly because the addFileToList() function is quite long as is and the ffprobe routine is quite long as well.我正在尝试使用fluent-ffmpegffprobe来获取文件的元数据并将其添加到列表中,但我想将获取元数据的过程与与检查文件相关的方法分开,主要是因为addFileToList() function 很长, ffprobe例程也很长。

I've tried the following code, but it doesn't give the results I'm expecting:我已经尝试了以下代码,但它没有给出我期望的结果:

export default {
  // ...
  methods: {
    getVideoMetadata (file) {
      const ffmpeg = require('fluent-ffmpeg')
      ffmpeg.ffprobe(file.name, (err, metadata) => {
        if (!err) {
          console.log(metadata)  // this shows the metadata just fine
          return metadata
        }
      })
    },
    addFileToList (file) {
      // file checking routines
      console.log(this.getVideoMetadata(file))  // this returns null
      item.metadata = this.getVideoMetadata(file)
      // item saving routines
    }    
  }
}

I've already tried to nest the getVideoMetadata() routines inside addFileToList() , and it works, but not as intended, because the actions are carried, but not the first time, only the second time.我已经尝试将getVideoMetadata()例程嵌套在addFileToList()中,它可以工作,但没有按预期工作,因为执行了这些操作,但不是第一次,只有第二次。 It seems to be an async issue, but I don't know how can I tackle this.这似乎是一个异步问题,但我不知道如何解决这个问题。

What can I do?我能做些什么? Should I stick to my idea of decoupling getVideoMetadata() or should I nest it inside addFileToList() and wrestle with async / await ?我应该坚持我的解耦getVideoMetadata()的想法,还是应该将它嵌套在addFileToList()中并与async / await搏斗?

It turns out that probing for metadata introduces a race condition, so we should structure the code so that the flow continues after the callback:事实证明,对元数据的探测引入了竞争条件,因此我们应该构造代码,以便在回调之后流程继续:

const ffmpeg = require('fluent-ffmpeg')
var data
ffmpeg.ffprobe(file.name, (err, metadata) => {
    if (!err) {
        data = metadata
        continueDoingStuff()
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM