How to convert audio to HLS using fluent-ffmpeg in Node.js?

124 Views Asked by At

I used:

ffmpeg.setFfprobePath(ffprobeBin.path);
ffmpeg.setFfmpegPath(ffmpegPath);
ffmpeg(audioPath, {timeout: 432000})
    .audioCodec('aac')
    .audioBitrate('128k')
    .outputOptions(['-hls_time 10', '-hls_list_size 0'])
    .format('hls')
    .output(`${folderPath}/${streamName}`).on('end', () => {
  console.log(`Finished processed ${streamName} for video Id:  ${folderName}`);

  Audio.findByIdAndUpdate(nameFile.split('.')[0], {
    streamUrl: streamName
  }

but it's not working with my audio file (mp3). I think the reason is this mp3 file has image or copyright.

I tried other audio codecs (aac, libmp3lame), but it still does not work.

2

There are 2 best solutions below

0
Phạm Thiên On

Use option map 0-a to pick sound in file;

ffmpeg(audioPath, {timeout: 432000}).addOptions([
            '-map 0:a',
            '-c:a aac',
            '-b:a 128k',
            '-f hls',
            '-hls_time 10',
            '-hls_list_size 0'
          ])
0
soundflix On

When there is more than one stream, the hls encoder may not automatically pick the audio stream. As a precaution, use the -map option to select the first audio stream.

Add this to your code after the audioBitrate method:

.addOptions(['-map 0:a'])

Note: There are no dedicated methods for -map and other stream operations that we know from ffmpeg cli. That’s why we have to use the outputOptions method.