I making a video streaming application with react and node js. I am converting video into different resolution with ffmpeg and storing directly to S3 bucket with the help of piping.
I am streaming uploaded resolution video from S3 bucket directly through cloudfront in HTML5 tag with video.js
When i tried to play original video through cloudfront in player its working fine, But as soon i play ffmpeg converted video to video player in cloudfront i am facing issue:
Video takes a lot load (Downloads in browser) before playing in player.
Below is my ffmpeg command
await loadProcess( [
'i',<s3 url of original video>,
'-movflags',
'frag_keyframe+empty_moov','-vf', 'scale=-2:360','-c:v','h264','-profile:v','baseline','-r',30,'-g', 60,'-b:v','1M','-f','mp4','-']
, outPath,'video/mp4')
this is my loadProcess function: i am using aws cli to direct upload resolution video to S3 bucket:
export function loadProcess(ffmpegOptions, outPath,contentType) {
return new Promise((resolve, reject) => {
const videoPath = outPath.replace(/\\/g, "/");
let ffmpeg = spawn(conf.ffmpeg, ffmpegOptions);
let awsPipe = spawn('aws', ['s3', 'cp', '--content-type', `${contentType}`, '-', `s3://${process.env.AWS_S3_BUCKET}/${process.env.AWS_S3_VIDEOS_FOLDER}${videoPath}` ])
ffmpeg.stdout.pipe(awsPipe.stdin)
// ffmpeg write stream flow
let ffmpegData = ''
ffmpeg.stderr.on('data', (_data) => {
ffmpegData += _data.toString();
})
ffmpeg.on('close', (code) => {
if (code === 0) {
resolve(outPath)
}
else {
let _dataSplit=ffmpegData.split('\n');
_dataSplit.pop();
console.log({action: 'close', message:_dataSplit.pop(), more:[conf.ffmpeg].concat(ffmpegOptions).join(' ')+'\n'+ffmpegData, code})
}
});
ffmpeg.on('error', (err) => {
reject({action: ' ffmpeg error', message: err});
});
// aws s3 cli read stream pipe flow
let awsPipeData = ''
awsPipe.stderr.on('data', _data => {
awsPipeData += _data.toString()
});
awsPipe.on('close', (code) => {
if (code === 0) {
resolve(outPath)
}else{
console.log({action: 'close', message: awsPipeData})
}
});
awsPipe.on('error', (err) => {
reject({action: 'awsPipe error', message: err});
})
})
}
I have tried using +faststart option in ffmpeg but getting command failed error. Can someone please help me this?
Thanks in advance