Skip to content

Commit d301b3f

Browse files
committed
duration added and image bug fixed
1 parent 0c984ca commit d301b3f

File tree

8 files changed

+256
-14
lines changed

8 files changed

+256
-14
lines changed

app.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const express = require("express");
22
const dotenv = require("dotenv");
33
const multer = require("multer");
44

5+
const { spawn } = require("child_process");
6+
57
const {
68
S3Client,
79
PutObjectCommand,
@@ -71,7 +73,11 @@ app.get("/image/:imageName", async (req, res) => {
7173

7274
const url = await getSignedUrl(s3, command, { expiresIn: 3600 });
7375

74-
res.send(url);
76+
// get duration of audio
77+
const duration = await getAudioDuration(url);
78+
console.log("Duration:", duration);
79+
80+
res.send({ url, duration });
7581
} catch (error) {
7682
console.log(error);
7783
res.status(500).send("Error getting image");
@@ -100,3 +106,26 @@ app.delete("/image/:imageName", async (req, res) => {
100106
app.listen(port, () => {
101107
console.log(`Server is running on port ${port}`);
102108
});
109+
110+
const getAudioDuration = (url) => {
111+
return new Promise((resolve, reject) => {
112+
const ffprobe = spawn("ffprobe", [
113+
"-v",
114+
"error",
115+
"-show_entries",
116+
"format=duration",
117+
"-of",
118+
"default=noprint_wrappers=1:nokey=1",
119+
url,
120+
]);
121+
122+
ffprobe.stdout.on("data", (data) => {
123+
const durationInSeconds = Math.ceil(parseFloat(data.toString()));
124+
resolve(durationInSeconds);
125+
});
126+
127+
ffprobe.stderr.on("data", (data) => {
128+
reject(data.toString());
129+
});
130+
});
131+
};

controllers/video-content.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,21 @@ const createVideoContent = async (req, res) => {
2828

2929
const imageUrls = [];
3030
const audioUrls = [];
31+
const durationInSeconds = [];
3132

3233
for (let i = 0; i < imagePrompts.length; i++) {
3334
console.log("getting image " + i);
34-
getImage(imagePrompts[i], videoID + i);
35+
i == imagePrompts.length - 1
36+
? await getImage(imagePrompts[i], videoID + i)
37+
: getImage(imagePrompts[i], videoID + i);
3538
imageUrls.push(baseUrl + "/file/" + videoID + i + ".jpg");
3639
}
3740

3841
for (let i = 0; i < voiceScripts.length; i++) {
3942
console.log("getting audio " + i);
40-
const audioUrl = await getSpeech(voiceScripts[i], videoID + i);
41-
audioUrls.push(audioUrl);
43+
const audioData = await getSpeech(voiceScripts[i], videoID + i);
44+
audioUrls.push(audioData.url);
45+
durationInSeconds.push(audioData.duration);
4246
}
4347

4448
const video = new Video({
@@ -47,13 +51,14 @@ const createVideoContent = async (req, res) => {
4751
imagePrompts,
4852
imageUrls,
4953
audioUrls,
54+
durationInSeconds,
5055
});
5156

5257
await video.save();
5358

5459
setTimeout(() => {
5560
res.status(200).json(video);
56-
}, 20000);
61+
}, 5000);
5762
} catch (error) {
5863
console.log(error);
5964
res.status(500).send("Error creating video content");

models/Video.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ const VideoSchema = new mongoose.Schema({
2727
finalAudioUrl: {
2828
type: String,
2929
},
30+
durationInSeconds: [
31+
{
32+
type: Number,
33+
},
34+
],
3035

3136
createdBy: {
3237
type: String,

package-lock.json

Lines changed: 157 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
"description": "",
55
"main": "app.js",
66
"scripts": {
7+
"build":"npm install --output public",
8+
"start": "node ./api/index.js",
9+
"dev": "nodemon ./api/index.js",
710
"test": "echo \"Error: no test specified\" && exit 1"
811
},
912
"keywords": [],
@@ -21,7 +24,9 @@
2124
"mongodb": "^6.5.0",
2225
"mongoose": "^8.2.2",
2326
"multer": "^1.4.5-lts.1",
27+
"music-metadata": "^7.14.0",
2428
"openai": "^4.29.1",
25-
"sharp": "^0.33.2"
29+
"sharp": "^0.33.2",
30+
"wavefile": "^11.0.0"
2631
}
2732
}

services/audio_service/duration.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const { spawn } = require("child_process");
2+
3+
const getAudioDuration = (url) => {
4+
return new Promise((resolve, reject) => {
5+
const ffprobe = spawn("ffprobe", [
6+
"-v",
7+
"error",
8+
"-show_entries",
9+
"format=duration",
10+
"-of",
11+
"default=noprint_wrappers=1:nokey=1",
12+
url,
13+
]);
14+
15+
ffprobe.stdout.on("data", (data) => {
16+
const durationInSeconds = parseFloat(data.toString());
17+
resolve(durationInSeconds);
18+
});
19+
20+
ffprobe.stderr.on("data", (data) => {
21+
reject(data.toString());
22+
});
23+
});
24+
};
25+
26+
module.exports = getAudioDuration;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy