1
ffmpeg -hide_banner -y -i beach.mkv \
  -vf scale=w=640:h=360:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type vod  -b:v 800k -maxrate 856k -bufsize 1200k -b:a 96k -hls_segment_filename beach/360p_%03d.ts beach/360p.m3u8 \
  -vf scale=w=842:h=480:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type vod -b:v 1400k -maxrate 1498k -bufsize 2100k -b:a 128k -hls_segment_filename beach/480p_%03d.ts beach/480p.m3u8 \
  -vf scale=w=1280:h=720:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type vod -b:v 2800k -maxrate 2996k -bufsize 4200k -b:a 128k -hls_segment_filename beach/720p_%03d.ts beach/720p.m3u8 \
  -vf scale=w=1920:h=1080:force_original_aspect_ratio=decrease -c:a aac -ar 48000 -c:v h264 -profile:v main -crf 20 -sc_threshold 0 -g 48 -keyint_min 48 -hls_time 4 -hls_playlist_type vod -b:v 5000k -maxrate 5350k -bufsize 7500k -b:a 192k -hls_segment_filename beach/1080p_%03d.ts beach/1080p.m3u8

Convert this command to fluent-ffmpeg node js:

My Code:

let ffmpeg = require('fluent-ffmpeg');
new ffmpeg()
        .addInput('./public/class.mp4')
        .outputOptions([
            '-map 0:v',
            '-map 0:v',
            '-map 0:a',
            '-map 0:a',
            '-s:v:0 426x240',
            '-c:v:0 libx264',
            '-b:v:0 400k',
            '-c:a:0 aac',
            '-b:a:0 64k',
            '-s:v:1 640x360',
            '-c:v:1 libx264',
            '-b:v:1 700k',
            '-c:a:1 aac',
            '-b:a:1 96k',
            //'-var_stream_map', '"v:0,a:0 v:1,a:1"',
            '-master_pl_name playlist.m3u8',
            '-f hls',
            '-max_muxing_queue_size 1024',
            '-hls_time 4',
            '-hls_playlist_type vod',
            '-hls_list_size 0',
            '-hls_segment_filename ./public/output/v%v/segment%03d.ts'
        ])
        .output('./public/output/v%v/master.m3u8')
        .on('start', function (commandLine) {
            console.log('Spawned Ffmpeg with command: ' + commandLine);
            res.write('<p>' + commandLine + '</p>');
        })
        .on('error', function (err, stdout, stderr) {
            console.error('An error occurred: ' + err.message, err, stderr);
            res.write('<p>' + err.message + '</p>');
        })
        .on('progress', function (progress) {
            console.log('Processing: ' + progress.percent + '% done');
            console.log(progress);
            /*percent = progress.percent;
            res.write('<h1>' + percent + '</h1>');*/
        })
        .on('end', function (err, stdout, stderr) {
            console.log('Finished processing!' /*, err, stdout, stderr*/);
            res.write('Finished processing!');
        })
        .run();

This code generate only one bitrate .m3u8 stream.

I have tried many ways to generate multi bitrate hls stream through node js but always generate single bitrate. i have tried bash command and that was work fine, now i want to convert this command to nodejs script.

Is any one have idea about this?

  • Why have you commented the `-var_stream_map` out? This one is usually used to tell FFmpeg how the master playlist (contains information about the variants) should be created. – martinr92 Dec 16 '20 at 11:44
  • Yes, I know that. but if I uncomment -var_stream_map than it gives me error (Error: undefine v:1,a:1). and if I comment than it out only one bitrate segment stream. – Vinay Vadachhak Dec 17 '20 at 12:45
  • @VinayVadachhak did you get any solution for this? – maulik sakhare Dec 29 '20 at 05:35
  • @mauliksakhare Yes, I got solution. I generate bash file and execute through node js. We can moderate and dynamically pass bash parameters, for that need to create variables and pass in bash file – Vinay Vadachhak Dec 30 '20 at 07:06

1 Answers1

0

I recently released an npm module that does multi-bitrate transcoding called simple-hls. Out of the box, the following code will get you 360p through 1080p.

npm install --save simple-hls

import {Transcoder} from 'simple-hls'

async function transcodeSomething () {
    //Create a new instance of the transcoder
    //First Parameter is the path to the video that you want to transcode
    //Second Parameter is the path to the folder/directory you would like the HLS Files Saved
    //Third Parameter is an optional options object

    const t = new Transcoder(`${__dirname}/test.mp4`, `${__dirname}/path_to_output_directory`, {showLogs: false});
    try {
        const hlsPath = await t.transcode();
        console.log('Successfully Transcoded Video');
    } catch(e){
        console.log('Something went wrong');
    }
    
}

All renditions will be transcoded and available in the output directory.

Robo Rick
  • 721
  • 6
  • 7