我已经使用NodeJS + Express创建了一个API,它应该在我们调用/videos/stream/{id}
路由时发送视频流。这是代码:
router.get('/stream/:id', function(req, res) {
fs.readFile(app.get('path') + '/videos/' + req.params.id + '/video.mp4', function (err,data) {
if (err) { ... }
res.writeHead(200, {
'Content-type' : 'application/octet-stream',
'Content-Length' : data.length
});
res.end(data);
});
});
此代码正常工作,因为当我将整个URI放入浏览器时,我可以使用VLC打开流或保存它。
问题是我无法通过视频网框架看到视频流。当我复制/粘贴"如何开始"代码进入我的AngularJS应用程序,如下所示:
sources: [
{src: $sce.trustAsResourceUrl("http://static.videogular.com/assets/videos/videogular.mp4"), type: "video/mp4"},
{src: $sce.trustAsResourceUrl("http://static.videogular.com/assets/videos/videogular.webm"), type: "video/webm"},
{src: $sce.trustAsResourceUrl("http://static.videogular.com/assets/videos/videogular.ogg"), type: "video/ogg"}
],
这很有效,我可以看到播放器中的视频,但是当我用我的API替换URI时:
sources: [
{src: $sce.trustAsResourceUrl("http://localhost:3000/api/videos/stream/556747d8f657a9b81c7f3543"), type: "application/octet-stream"}
],
玩家仍然是黑人,没有视频播放,我不知道为什么。
答案 0 :(得分:1)
我找到了解决方案,但这非常特定于我的API工作方式。在API上传视频时,它会自动转换为MP4。
所以这是我的配置:
$scope.config = {
sources: [
{src: $sce.trustAsResourceUrl("http://localhost:3000/api/videos/stream/" + data.video._id), type: "video/mp4"}
],
tracks: [{ ... }],
theme: "bower_components/videogular-themes-default/videogular.css",
plugins: {
poster: "http://www.videogular.com/assets/images/videogular.png"
}
};
请注意type : "video/mp4"
而不是type : "application/octet-stream"
,因为我的API会将所有内容转换为MP4。
当上传的视频是MP4时,此代码非常有用。否则,当在API上传和转换非MP4视频时,我无法将其传输到Videogular的播放器中。
<强> TL; DR 强>
我的API没有正确转换视频,这就是Videogular无法传输视频的原因。