Here i am providing shorter version of the code that includes the necessary changes:
const { sanitizeEntity } = require(“strapi-utils”);
const path = require(“path”);
const ffmpeg = require(“fluent-ffmpeg”);
module.exports = {
async find(ctx) {
const entities = ctx.query._q
? await strapi.services.albums.search(ctx.query)
: await strapi.services.albums.find(ctx.query);
const updatedEntities = await Promise.all(
entities.map(async (entity) => {
const albums = sanitizeEntity(entity, {
model: strapi.models.albums,
});
await Promise.all(
albums.media.map(async (video) => {
if (video.width === null || video.height === null) {
const videoFile = path.resolve(
__dirname,
"../../../public" + video.url
);
try {
const metadata = await ffmpeg.ffprobeSync(videoFile);
metadata.streams.forEach((element) => {
video.width = element.width;
video.height = element.height;
});
} catch (err) {
console.error(err);
}
}
})
);
return albums;
})
);
return updatedEntities;
},
};
I used the same code for my client blog which is Basket Random Unblocked.
In this shortened version, the changes include:
1- Replaced the if statement for querying entities with a ternary operator for a more concise code.
2- Used ffprobeSync instead of ffprobe to make the function synchronous and avoid using additional promises.
3- Wrapped the inner map function with async and used await when calling ffprobeSync to handle the asynchronous code execution.
4- Replaced the forEach loop with forEach to iterate over the streams and set the width and height values.
5- Added a try/catch block to handle any errors that occur during the ffprobeSync call.