gpt4 book ai didi

javascript - Node js 和 videogular 使用 Node 加载视频

转载 作者:行者123 更新时间:2023-12-03 10:02:23 26 4
gpt4 key购买 nike

所以我有以下 Node 代码用于将视频上传到我的 Node 服务器:

    var fs = require('fs');

var videoExtensions = ['mp4','flv', 'mov'];
//Media object
function Media(file, targetDirectory) {
this.file = file;
this.targetDir = targetDirectory;
}

Media.prototype.isVideo = function () {
return this.file.mimetype.indexOf('video') >= 0;
};
Media.prototype.getName = function () {
return this.file.originalname.substr(0, this.file.originalname.indexOf('.'))
};

router.route('/moduleUpload')
.post(function (request, response) {
var media = new Media(request.files.file, '../user_resources/module/'+request.body.module_id+'/');
if(!fs.existsSync('../user_resources/module/'+request.body.module_id+'/')){
fs.mkdirSync('../user_resources/module/'+request.body.module_id+'/', 0766, function(err){
if(err){
console.log(err);
response.send("ERROR! Can't make the directory! \n"); // echo the result back
}
});
}
convertVideos(media);
response.status(200).json('user_resources/module/' + request.body.module_id + '/' + request.files.file.name);

});


function convertVideos (media){
var ffmpeg = require('fluent-ffmpeg');
videoExtensions.forEach(function(extension){
var proc = new ffmpeg({source: media.file.path, nolog: false})
.withVideoCodec('libx264')
.withVideoBitrate(800)
.withAudioCodec('libvo_aacenc')
.withAudioBitrate('128k')
.withAudioChannels(2)
.toFormat(extension)
.saveToFile(media.targetDir+media.getName()+'.'+extension,
function (retcode, error) {
console.log('file has been converted succesfully');
});
});

}

现在我希望使用 Node 加载视频,而不是使用直接路径加载视频

但是我不太确定该怎么做

使用直接路径我会做这样的事情:

$scope.videos.push(
{
sources: [
{src: $sce.trustAsResourceUrl($scope.component.video_mp4_path), type: "video/mp4"}
]
}

其中 video_mp4_path 变量将是视频的直接路径,即:myproject/resources/video.mp4

但是不知何故,我需要调用 Node 而不是即时路径。

正如我所说,我不太确定如何做到这一点,有人可以指出我正确的方向

最佳答案

您可以使用express和Multer上传、列出和播放,我发现它们相对简单并且工作可靠。就我而言,我使用 Angular 和常规浏览器进行播放,但应该应用相同的原则。

以下代码摘录对我有用:

上传

// 
router.post('/web_video_upload', function(req, res) {
//Log the request details
Debug console.log(req.body);

//Send a resposne
res.send('Video Uploading');
console.dir(req.files);
});

// POST: video upload route
// multer approach
var multer = require('multer');
app.use(multer({

//Set dstination directory
dest: path.resolve(__dirname, 'public', 'uploaded_videos'),

//Rename file
rename: function (fieldname, filename) {
//Add the current date and time in ISO format, removing the last 'Z' character this usually
//includes
var dateNow = new Date();
return filename + "_" + dateNow.toISOString().slice(0,-1)
},

//Log start of file upload
onFileUploadStart: function (file) {
console.log(file.originalname + ' is starting ...')
},

//Log end of file upload
onFileUploadComplete: function (file) {
console.log(file.originalname + ' uploaded to ' + file.path)
done=true;
}

}));

列出视频

// GET: route to return list of upload videos 
router.get('/video_list', function(req, res) {
//Log the request details
console.log(req.body);

// Get the path for the uploaded_video directory - in a real app the video list would likely be taken from
// a database index table, but this is fine for us for now
var _p;
_p = path.resolve(__dirname, 'public', 'uploaded_videos');

//Find all the files in the diectory and add to a JSON list to return
var resp = [];
fs.readdir(_p, function(err, list) {
//Check if the list is undefined or empty first and if so just return
if ( typeof list == 'undefined' || !list ) {
return;
}
for (var i = list.length - 1; i >= 0; i--) {

// For each file in the directory add an id and filename to the response
resp.push(
{"index": i,
"file_name": list[i]}
);
}

// Set the response to be sent
res.json(resp);
});
});

上面将返回服务器目录中所有视频的 JSON 列表。如果您的文件名是视频的路径,您可以在 Angular View 中使用它来创建视频表格,并使用 HTML5 视频标签(几乎所有现代浏览器都支持)播放它们。

关于javascript - Node js 和 videogular 使用 Node 加载视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30524174/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com