const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path;
const spawn = require('child_process').spawn;
ffmpeg = spawn(ffmpegPath, [
// Remove this line, as well as `-shortest`, if you send audio from the browser.
// '-f', 'lavfi', '-i', 'anullsrc',
// FFmpeg will read input video from STDIN
'-i', '-',
// -re flag means to Read input at native frame rate.
'-re', '-y',
// thread_queue_size added to avoid err: Thread message queue blocking; consider raising the thread_queue_size option, required before each input - this is for image2
// '-thread_queue_size', '2048',
// REF TO OVERLAY
// https://stackoverflow.com/questions/10438713/overlay-animated-images-with-transparency-over-a-static-background-image-using-f?rq=1
// -loop loops the background image input so that we don't just have one frame, crucial!
// The image file muxer writes video frames to image files, http://underpop.online.fr/f/ffmpeg/help/image2-1.htm.gz
'-f', 'image2',
// The -loop option is specific to the image file demuxer and gif muxer, so it can't be used for typical video files, but it can be used to infinitely loop a series of input images.
'-loop', '1',
// pattern_type is used to determine the format of the images contained in the files.
// Read images matching the "*.png" glob pattern, that is files terminating with the ".png" suffix
'-pattern_type', 'glob',
// '-i', `images/${streamConfigData.youtube_key}/destination/image-*.png`,
'-i', `images/${streamConfigData.youtube_key}/overlay/abc.png`,
// '-vf', 'scale=1920x1080:flags=lanczos',
// -shortest ends encoding when the shortest input ends, which is necessary as looping the background means that that input will never end.
// 'overlay=shortest=1',
"-filter_complex", "[1:v]format=rgba,colorchannelmixer=aa=1[fg];[0][fg]overlay",
// Because we're using a generated audio source which never ends,
// specify that we'll stop at end of other input. Remove this line if you
// send audio from the browser.
// '-shortest',
// If we're encoding H.264 in-browser, we can set the video codec to 'copy'
// so that we don't waste any CPU and quality with unnecessary transcoding.
// If the browser doesn't support H.264, set the video codec to 'libx264'
// or similar to transcode it to H.264 here on the server.
// '-vcodec', 'libx264',
// it is not possible to filter and stream copy the same stream at the same time. https://stackoverflow.com/a/53526514/4057143
'-vcodec', 'copy',
// if browser not supports encoding AAC, we must transcode the audio to AAC here on the server.
// '-acodec', 'aac',
// Use this rate control mode if you want to keep the best quality and care less about the file size. CRF scale is 0–51, where 0 is lossless, 23 is the default, and 51 is worst quality possible. A lower value generally leads to higher quality, https://trac.ffmpeg.org/wiki/Encode/H.264
'-crf', '23',
// preset provide a certain encoding speed to compression ratio. A slower preset will provide better compression. medium – default preset, https://trac.ffmpeg.org/wiki/Encode/H.264
'-preset', 'ultrafast',
// -r set the frame rate. Generally, -r. Use the filter when you need to change framerate before applying further filters.
// '-r', '30',
// '-framerate', '30',
//debug level logs
'-loglevel', 'debug',
'-v', 'verbose',
// -g GOP_LEN_IN_FRAMES, -g sets the keyframe interval. https://superuser.com/a/908325
'-g', '60',
// video timescale, not sure what it is!
'-video_track_timescale', '1000',
// a live stream with more/less constant bit rate, to be able to control the bandwidth used.
// a live stream with limited bit rate
'-b:v', '15000k',
// '-maxrate', '4000k',
// '-bufsize', '8000k',
// FLV is the container format used in conjunction with RTMP
'-f', 'flv',
// The output RTMP URL.
// For debugging, you could set this to a filename like 'test.flv', and play
// the resulting file with VLC.
rtmpUrl
], {
env: {
NODE_ENV: 'production',
PATH: process.env.PATH
}
});