简体   繁体   中英

stream mp4 video with node fluent-ffmpeg

I'm trying to create video stream server and client with node fluent-ffmpeg , express and ejs . And a haven't solve this for a while. What I want to do is to play video beginning by certain time. The following codes make it with Safari browser on windows but with others it makes a loop of a few seconds or it says

video format not supported

server code (run.js) :

app.get('/video', function(req, res) {

  //define file path,time to seek the beegining and set ffmpeg binary
  var pathToMovie = '../videos/test.mp4';
  var seektime = 100; 
  proc.setFfmpegPath(__dirname + "/ffmpeg/ffmpeg");


  //encoding the video source
  var proc = new ffmpeg({source: pathToMovie})
         .seekInput(seektime)
         .withVideoBitrate(1024)
         .withVideoCodec('libx264')
         .withAspect('16:9')
         .withFps(24)
         .withAudioBitrate('128k')
         .withAudioCodec('libfaac')
         .toFormat('mp4');

  //pipe 
         .pipe(res, {end: true});
});

client code (index.ejs):

<html>
  <head></head>

  <body>
    <video>
      <source src="video/" type='video/mp4' />
    </video>
  </body>

</html>

Help please. I searched everywhere solution but I didn't find

I believe that in order to seek within a mp4 or mp3, you need to let the browser know the length of that content. Try inserting this quick blurb before you make your call to ffmpeg . This should get the size of the video file and you can set the headers in the response accordingly before streaming the data.

INSERT BEFORE ffmpeg call

var fs = require('fs');
var stat = fs.statSync(pathToMovie);
res.writeHead(200, {
    'Content-Type': 'video/mp4',
    'Content-Length': stat.size
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM