Get media (mp3) duration

Hello,

You can achieve this by using extensions.


First of all, create the Duration field for Files.

Copy the file ./node_modules/strapi-plugin-upload/models/File.settings.json to ./extensions/upload/models/File.settings.json and add the duration field to attributes:

{
  "info": {
    "name": "file",
    "description": ""
  },
  "options": {
    "timestamps": true
  },
  "attributes": {
    //... in that case it will hold total ms or total seconds.
    "duration":{
      "type": "integer",
      "configurable": false
    }
    //...
  }
}

Second, copy the ./node_modules/strapi-plugin-upload/services/Upload.js file to ./extensions/upload/services/Upload.js , and modify it like this:

Replace code:
const { bytesToKbytes } = require('../utils/file');
With:
const { bytesToKbytes } = require('../../../node_modules/strapi-plugin-upload/utils/file');


Third, find the enhanceFile function, and add this code before its return statement:

Now, make your custom function that will handle the buffer variable and will return the duration. Set the duration to formattedFile.duration.


Result:

2 Likes