2015-05-29 88 views
0

所以,我对于视频上传到我的节点服务器以下节点代码:节点JS&videogular加载视频使用节点

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

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

正如我所说的我不是很清楚如何做到这一点可能有人点我在正确的方向

回答

1

您可以上传,列出,并使用快递和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视频标记进行回放(几乎所有现代浏览器均支持)。

+0

这是上传我需要下载文件并将其插入到视频 –

+0

@MarcRasmussen - 抱歉,误读。添加了代码以响应您的角度应用与服务器的视频列表。 – Mick

+0

将上述代码串流的视频,所以我不使用直接路径,而是使用nodejs流视频播放器 –