所以我有以下节点代码用于将视频上传到我的节点服务器:
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');
});
});
}
现在不是使用直接路径加载视频,而是希望使用节点
加载它但是我不太清楚如何做到这一点
使用直接路径我会做这样的事情:
$scope.videos.push(
{
sources: [
{src: $sce.trustAsResourceUrl($scope.component.video_mp4_path), type: "video/mp4"}
]
}
其中video_mp4_path变量将是视频的直接路径,即:myproject/resources/video.mp4
但是我需要以某种方式调用节点而不是即时路径。
正如我所说,我不太确定如何做到这一点可能有人指出我正确的方向
答案 0 :(得分:1)
你可以使用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列表。如果您的文件名是视频的路径,您可以在角度视图中使用它来创建视频表并使用HTML5视频标记播放它们(几乎所有现代浏览器都支持)。