skip to Main Content

I’m trying to convert an mp4 file to an mp3 file and then directly upload it to Firebase storage without saving it locally in my machine, How can I do that currently I’m getting an error when I try to do that the error is "Error: [Error: ENOENT: no such file or directory, open ‘./1695556319341.mp3’]" How can i fix this issue with my Node.js code and make things work?

import { initializeApp } from "firebase/app";
import { getStorage, ref, getDownloadURL, uploadBytesResumable } from "firebase/storage";
import serviceAccount from '../firebase/serviceAccountKey';
import { path as ffmpegPath } from '@ffmpeg-installer/ffmpeg';
import ffmpeg from 'fluent-ffmpeg';
import { readFile, unlink } from 'fs/promises';

initializeApp(serviceAccount);
ffmpeg.setFfmpegPath(ffmpegPath);

async function convertMP4ToMP3AndUploadToFirebase(inputPath: any, firebaseStoragePath: any) {
    const date = Date.now();
    const outputPath = `./${date}.mp3`;

    try {
        await ffmpeg(inputPath)
            .output(outputPath)
            .audioCodec('libmp3lame')
            .format('mp3')
            .run();

        const storage = getStorage();
        const storageRef = ref(storage, firebaseStoragePath);

        const fileBuffer = await readFile(outputPath);
        const metadata = { contentType: 'audio/mpeg' };
        const uploadTask = uploadBytesResumable(storageRef, fileBuffer, metadata);
        const snapshot = await uploadTask;
        const downloadURL = await getDownloadURL(snapshot.ref);
        await unlink(outputPath);

        console.log('MP4 file converted to MP3 and uploaded to Firebase Storage successfully!');
        console.log('Download URL:', downloadURL);
    } catch (error) {
        console.error('Error:', error);
    }
}

const inputPath = './video.mp4';
const date = Date.now();
const firebaseStoragePath = `./${date}.mp3`;

convertMP4ToMP3AndUploadToFirebase(inputPath, firebaseStoragePath);

2

Answers


  1. Use path.join(__dirname,"myfile12132012.mp3")

    Instead of string like ${date}.mp3

    Login or Signup to reply.
  2. With Cloud Functions, you can’t write files anywhere except /tmp. You should know that the /tmp filesystem is entirely in memory, so if you write a large file there, you could run out of memory if the server instance isn’t configured with enough memory to handle it.

    I suggest reviewing the documentation and other resources about this:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search