0

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.

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. 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?

noquierouser
  • 963
  • 2
  • 11
  • 25

1 Answers1

1

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()
    }
})
Darkstep
  • 153
  • 10