Mohit56
Mohit56

Reputation: 11

FFmpeg output video is not seekable. Output video is not playing instantly instead browser loads it completely then plays it

Hi I am using ffmpeg to transcode video. In my code I have used 'fluent-ffmpeg' npm package with nodeJs and 'aws-sdk' to save output video by writestream into s3 bucket.

Problem -> Video is getting transcoded and I am successfully able to save the video into s3 bucket but. As I paste the object_url of that video into browser and try to play, but that video is not playing instantly I have checked on 'developer console tool' browser is loading all the video once that is done then only it starts playing that is a problem.

->Let say if I have output video of size 10GB on that case browser will load all 10GB data then only it will start playing that video.

->If I am not using writestream approach and directly upload the video into local directory first then upload into s3 bucket, In this case if I play the video using object URL then that video plays instantly. In this case I don't have to wait for whole 10GB video to load then play it which is good.

-> Can anybody help me to fix my writestream solution because I don't want to save the output video into my localdirectory. I want to writestream the output video directly into s3 bucket.

Code Snippet

const ffmpeg = require('fluent-ffmpeg');
const AWS = require('aws-sdk'); 
const stream = require("stream");

//set up your aws connection

const command = ffmpeg(inputVideoURL) 
.outputOptions(['-movflags', 'frag_keyframe']) 
.size('854x480') // set the desired resolution (480p) .outputFormat('mp4') 
.videoCodec('libx264') 
.audioCodec('aac') 
.on('progress',(p)=>{ console.log(p) }) 
.on('stderr',(err)=>console.log(err)) 
.on('start', commandLine => console.log('Spawned FFmpeg with command: ' + commandLine)) 
.on('end', () => console.log('Transcoding finished.')) 
.on('error', err => console.error('Error:', err))

//=>To save file into s3 using write steam. command.writeToStream(uploadFolderFileWriteStream('StreamCheck/output2.mp4'));

function uploadFolderFileWriteStream(fileName) { try { const pass = new stream.PassThrough();

const params = {
  Bucket: bucketName,
  Key: fileName,
  Body: pass,
  ACL: "public-read",
  ContentType:'video/mp4' ,
};

const upload = s3.upload(params);

upload.on('error', function(err) {
  console.log("Error uploading to S3:", err);
});

upload.send(function(err, data) {
  if(err) console.log(err);
  else console.log("Upload to S3 completed:", data);
});

return pass;

} catch (err) { console.log("[S3 createWriteStream]", err); } }
I have tried below option as well be all of them not worked 
-> .addOption("-movflags frag_keyframe+empty_moov") 
-> .addOption('-movflags', 'frag_keyframe+empty_moov') 
-> .addOutputOption("-movflags +faststart")
-> .addOption('-movflags', 'faststart+frag_keyframe+empty_moov+default_base_moof')

Upvotes: 0

Views: 149

Answers (0)

Related Questions