使用节点fluent-ffmpeg流mp4视频 [英] stream mp4 video with node fluent-ffmpeg
问题描述
我正在尝试使用节点 fluent-ffmpeg , express 和 ejs 创建视频流服务器和客户端.有一段时间没有解决这个问题.我想做的是在特定时间开始播放视频.以下代码在Windows上使用 Safari浏览器进行了编码,但在其他代码下则进行了几秒钟的循环或显示
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
不支持视频格式
服务器代码(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});
});
客户端代码(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
推荐答案
我相信,要在mp4或mp3中进行搜索,您需要让浏览器知道该内容的长度.在致电 ffmpeg
之前,请尝试插入此简短内容.这样应该可以获取视频文件的大小,并且您可以在流式传输数据之前相应地在响应中设置标头.
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.
在ffmpeg调用之前插入
var fs = require('fs');
var stat = fs.statSync(pathToMovie);
res.writeHead(200, {
'Content-Type': 'video/mp4',
'Content-Length': stat.size
});
这篇关于使用节点fluent-ffmpeg流mp4视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!