Search code examples
node.jsffmpegmp3

How to check for corrupt mp3 files using ffmpeg in nodejs


Using the 'ffmpeg-static' npm package I managed to integrate ffmpeg in my node application, and run the [example][1]https://github.com/eugeneware/ffmpeg-static/blob/dce6d42ba772a5769df8181e704772db4456ef16/example.js code.

The gist of the code is:

import pathToFfmpeg from "ffmpeg-static";
import shell from 'any-shell-escape';
import { exec } from "child_process";

 function runFfmpeg(src, dest) {
  //where src is a existing mp3 file in a folder and dest is the destination folder
  const script = shell([
    pathToFfmpeg,
    '-y', '-v', 'error',
    '-i', resolve(process.cwd(), src),
    '-acodec', 'mp3',
    '-format', 'mp3',
    resolve(process.cwd(), dest),
  ]);

  exec(script);
}

This works and this decodes and encodes the source file into mp3 and saves it in the dest folder.

However, when I try what should be the simplest ffmpeg terminal command, such as ffmpeg -i file.mp3 -hide_banner it does not work. I have tried

function runFfmpeg(src, dest) {
  const script = shell([
    pathToFfmpeg,
    '-i', resolve(process.cwd(), src), '-hide_banner'
  ]);

  const fileInfo = exec(script);
  return fileInfo;

In the end, where I want to get to is being able to use my runFfmpeg function to check if an mp3 file has any missing or corrupted frames, using a terminal command that I found in the interwebs: ffmpeg -v error -i video.ext -f null

Any ideas on how to do that?


Solution

  • Short answer is that you're not specifying the output file. FFmpeg requires output file to be defined even if a null format is used. You can use output pipe - (check your interweb source again, I bet it's there):

    ffmpeg -v error -i video.ext -f null -
    

    Now, do you actually need to know what went wrong? (which requires shifting through the log file.)

    If you only care to have a simple yes/no question, you can instead run:

    ffmpeg -xerror -i video.ext -f null -
    

    and check the return code. If non-zero, FFmpeg couldn't process all the frames. Because it exits immediately, it'll save your runtime.