Skip to content

Commit 2e4aaf7

Browse files
committed
firebase-intermediate-complete
1 parent 4830d9a commit 2e4aaf7

File tree

6 files changed

+741
-12
lines changed

6 files changed

+741
-12
lines changed

app/api/generate-audio-file/route.jsx

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { ElevenLabsClient } from "elevenlabs";
2-
import { writeFile } from "fs/promises";
32
import { NextResponse } from "next/server";
4-
import path from "path";
5-
import { mkdir } from "fs/promises";
3+
import { getDownloadURL, ref, uploadBytes } from "firebase/storage";
4+
import { storage } from "@/configs/FireBaseConfig";
65

76
export async function POST(req) {
87
try {
98

109
const { text, id } = await req.json();
11-
10+
1211
if (!text || !id) {
1312
return NextResponse.json(
1413
{ error: "Missing required fields: text and id" },
@@ -23,23 +22,28 @@ export async function POST(req) {
2322
apiKey: apiKey,
2423
});
2524

26-
const audioResponse = await elevenlabs.generate({
25+
const audioStream = await elevenlabs.generate({
2726

2827
voice: "Jessica" ,
2928
text: text,
3029
modelId: "eleven_multilingual_v2",
3130
outputFormat: "mp3",
3231
});
32+
const chunks = [];
33+
for await (const chunk of audioStream) {
34+
chunks.push(chunk);
35+
}
36+
const audioBuffer = Buffer.concat(chunks);
3337

34-
const audioDir = path.join(process.cwd());
35-
await mkdir(audioDir, { recursive: true });
38+
const storageRef = ref(storage,'video-generator-ai-files/'+id+'.mp3');
3639

37-
const filePath = path.join(audioDir, `output.mp3`);
38-
await writeFile(filePath, audioResponse);
40+
await uploadBytes(storageRef, audioBuffer, { contentType: 'audio/mp3' });
3941

42+
const downloadUrl = await getDownloadURL(storageRef);
43+
4044
return NextResponse.json(
4145
{
42-
Result: 'Success'
46+
Result: downloadUrl
4347
},
4448
{ status: 200 }
4549
);

app/dashboard/create-new/page.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ function CreateNew() {
1616
const [formData,setFormData]= useState([])
1717
const [loading,setLoading]=useState(false)
1818
const [videoScript,setVideoScript]=useState()
19+
const [audioFileUrl,setAudioFileUrl]=useState()
1920

2021
const onHandleChange=(fieldName,fieldValue)=>{
21-
console.log(fieldName,fieldValue)
2222
setFormData(
2323
prev=>({
2424
...prev,
@@ -59,7 +59,7 @@ function CreateNew() {
5959
id: id
6060
}
6161
).then(res=>{
62-
console.log(res)
62+
setAudioFileUrl(res.data.Result);
6363
})
6464
setLoading(false)
6565
}

configs/FireBaseConfig.jsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Import the functions you need from the SDKs you need
2+
import { initializeApp } from "firebase/app";
3+
4+
import { getStorage } from "firebase/storage";
5+
// TODO: Add SDKs for Firebase products that you want to use
6+
// https://firebase.google.com/docs/web/setup#available-libraries
7+
8+
// Your web app's Firebase configuration
9+
const firebaseConfig = {
10+
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
11+
authDomain: "supercool-mental-health-app.firebaseapp.com",
12+
projectId: "supercool-mental-health-app",
13+
storageBucket: "supercool-mental-health-app.appspot.com",
14+
messagingSenderId: "26005611530",
15+
appId: "1:26005611530:web:3eeedbf3f32703373babf3"
16+
};
17+
18+
// Initialize Firebase
19+
const app = initializeApp(firebaseConfig);
20+
21+
export const storage=getStorage(app);

output.mp3

-389 KB
Binary file not shown.

0 commit comments

Comments
 (0)