Maxim  Cherevatov
Maxim Cherevatov

Reputation: 138

How i can slice audio file with using node.js

tell me please how i can slice audio file with using node.js? Now i read the documentation for ffmpeg module, but don't understand how to slice audio file with using this module. I found this code, but it gives an error error: NaN

ffmpeg('music/ant.mp3')
    .setStartTime('00:00:03')
    .setDuration('10')
    .output('music/ant.mp3')
    .on('end', function(err) {
        if(!err)
        {
            console.log('conversion Done');
        }
    })
    .on('error', function(err){
        console.log('error: ', +err);
    }).run();

Upvotes: 3

Views: 4059

Answers (3)

valem
valem

Reputation: 1870

I did it using child_process

https://nodejs.org/api/child_process.html

const spawn = require('child_process').spawnSync;

var ffmpeg = spawn('ffmpeg', [
  '-i',
  filename,
  '-acodec',
  'copy',
  '-ss',
  ss,
  '-t',
  t,
  output.mp3
], { shell: true });

A few things to note:

  • Ensure you have ffmpeg installed then spawn with the desired command. Put the arguments in an array. spawn does weird things with quotes but {shell:true} should take care of that.

  • Create the desired output directory before hand or with another spawn because ffmpeg won't create it for you if it doesn't already exist.

  • Check the process with this:

    process.on('exit', (code, signal) => {
        console.log(`ffmpeg exited with code ${code} and signal ${signal}`);
    });
    

Upvotes: 1

Tusharsangani
Tusharsangani

Reputation: 129

You could probably use ffmpeg. It is command line based, which is great since it is accessible, but subprocesses can sometimes die. Here is a node interface that abstracts ffmpeg usage out of command line calls: https://npmjs.org/package/ffmpeg

Your final commands, in the command line would probably look like this:

   ffmpeg -i long.mp3 -acodec copy -ss 00:00:00 -t 00:30:00 half1.mp3 
   ffmpeg -i long.mp3 -acodec copy -ss 00:30:00 -t 00:60:00 half2.mp3

This command states:

 - -i: the input file is long.mp3
 - -acodec: use the audio codec       
 - copy: we are making a copy  
 - -ss: start time    
 - -t: length    
 - and finally the output file name

To handle potentially timeouts/hung processes you should 'retry' and supply timeouts. Not sure how well the error callbacks work. That is do they fail appropriately on a process that hangs.

Upvotes: 1

chairmanmow
chairmanmow

Reputation: 650

Not a lot of info here, but from the looks of it you're passing strings into the .setStartTime and .setDuration methods. NaN means not a number, if you didn't know

'00:00:03' is a String, as is '10' because you put quotes around them. Even if you remove the quotes from the '00:00:03', it's not going to be able to parse that as a number. You probably need to pass it the value as some sort of integer, but it may not be number of seconds, it might be milliseconds or such. If it were seconds, you'd put '3' (without quotes), or milliseconds, you'd put '3000' without quotes. Also, if the unit of time to be passed is not seconds but milliseconds or somesuch, you will probably have to change the value '10' (again without quotes) to '10000' (no quotes) for milliseconds, etc.

Upvotes: 0

Related Questions