ffmpegwasm / ffmpeg.wasm

FFmpeg for browser, powered by WebAssembly
https://ffmpegwasm.netlify.app
MIT License
14.01k stars 816 forks source link

FFmpeg.run(...) Not Working #483

Closed Konglomneshued closed 1 year ago

Konglomneshued commented 1 year ago

Describe the bug FFmpeg doesn't seem to do anything. I have logger and progress, and tried the setProgress method, nothing prints to my console.

Expected behavior ffmpeg.run(...) convert my file to flac.

Screenshots If applicable, add screenshots to help explain your problem.

const decodeSong = (songPath: Path) => {
   return new Promise(resolve => {
      const ffmpeg = createFFmpeg({
         logger: message => console.log(message),
         progress: progress => console.log(progress),
      });

      ffmpeg.load().then(() => {
         const tempFolder = join(app.getPath('userData'), '.decoded-audio');
         if (!existsSync(tempFolder)) {
            mkdirSync(tempFolder);
         }

         const [ path ] = songPath.split('.');
         const songTitle = path.split('/').at(-1)!;

         const tempFile = join(tempFolder, songTitle + '.flac');

         ffmpeg.run('-i', songPath, '-acodec', 'flac', tempFile).then(() => {
            resolve(tempFile); // I use the file path to attempt playing the audio.
                               // File does not get created, so it's not working.
         }).catch(error => console.error(error));
      });
   });
};

ffmpeg.run(...) produces no errors.

Doesn't create the file

image

Desktop (please complete the following information):

Konglomneshued commented 1 year ago

I managed to get something working. The docs are not helpful.

const decodeSong = async (songPath: Path) => {
   const ffmpeg = createFFmpeg({
      log: true,
      logger: message => console.log(message),
      progress: progress => console.log(progress),
   });

   await ffmpeg.load();

   const decodedSongPath = decodeURI(songPath);
   const songData = await fetchFile(decodedSongPath);

   const tempFolder = join(app.getPath('userData'), '.decoded-audio');
   if (!existsSync(tempFolder)) {
      mkdirSync(tempFolder);
   }

   const [ path, extension ] = decodedSongPath.split('.');
   const songTitle = path.split('/').at(-1)!;

   const originalFile = songTitle + '.' + extension;
   const tempFile = songTitle + '.flac';
   console.log(tempFile);
   ffmpeg.FS('writeFile', originalFile, songData);

   await ffmpeg.run('-i', originalFile, '-acodec', 'flac', tempFile).then(() => {
      console.log('Finished');
   }).catch(error => console.error(error));

   return join(tempFolder, tempFile);
};

Still doesn't work, but I can see FFmpeg doing something. It's not saving the file, though.

Konglomneshued commented 1 year ago

Apparently I needed to use the FS method along with fetchFile to read/write the audio data to memory and then write the file with the Node fs module.

Please update the docs, so they are more helpful. I had no idea why I would use certain methods like FS unless I visit the demo.