stream mp4 video with node fluent-ffmpeg

安稳与你 提交于 2021-02-07 09:48:58

问题


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


回答1:


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
});


来源:https://stackoverflow.com/questions/29948551/stream-mp4-video-with-node-fluent-ffmpeg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!