【问题标题】:how can i use multer to upload a video and force the format to be mp4如何使用 multer 上传视频并强制格式为 mp4
【发布时间】:2021-06-05 05:37:38
【问题描述】:

我已经使用 multer 实现了图像上传,并且我正在将图像接收回我使用 angular 构建的前端应用程序。现在我想对视频做同样的事情,上传视频,以便我接受所有视频格式,但以 mp4 或其他可接受的格式存储。我到处搜索,但似乎无法确定 multer 是否可以处理视频。到目前为止,这是我的实现。

const router = require('express-promise-router')();
const VideosController = require('./video.controller');
const { validateParam, validateBody, schemas } = require('../_middleware/routehelpers');
const db = require('../_helpers/db');
const Video = require('./video.model');
const passport = require('passport');
const passportConfig = require('../passport');
const passportLogin = passport.authenticate('local', {session: false});
const passportJwt = passport.authenticate('jwt', { session: false });

const multer = require('multer');
const videoFilter = require('./videoFilter');

// 将文件保存到服务器存储

 var storage = multer.diskStorage({
        destination: (req, file, cb) => {
          cb(null, './public/videos');
        },
        filename: (req, file, cb) => {
    
           
          
          var filetype = '';
          if (file.mimetype === 'video/gif') {
            filetype = 'gif';
          }
          if (file.mimetype === 'video/mp4') {
            filetype = 'mp4';
          }
          if (file.mimetype === 'video/ogg') {
            filetype = 'ogg';
          }
          if (file.mimetype === 'video/wmv') {
            filetype = 'wmv';
          }
          if (file.mimetype === 'video/x-flv') {
            //filetype = mime.getExtension('video/flv');
            filetype = 'flv';
          }
          if (file.mimetype === 'video/avi') {
            filetype = 'avi';
          }
          if (file.mimetype === 'video/webm') {
            filetype = 'webm';
          }
          if (file.mimetype === 'video/mkv') {
            filetype = 'mkv';
          }
          if (file.mimetype === 'video/avchd') {
            filetype = 'avchd';
          }
          if (file.mimetype === 'video/mov') {
            filetype = 'mov';
          }
          cb(null, 'video-' + Date.now() + '.' + filetype);
        }
      
      });
      
      var upload = multer({ storage: storage, fileFilter: videoFilter.videoFilter });
    

// 我的路线

router.route('/')
.post(passportJwt,upload.single('file'),VideosController.addNewVideo);

//my controller

 addNewVideo: async (req, res, next) => {       
        // check file for Validation Error
     if (req.fileValidationError) {
         return res.send(req.fileValidationError);
     }
     else if (!req.file) {
         return res.status(500).send({ message: 'Upload fail'});
     } else {
         // if no Validation Error
        // hbjs.spawn({ input: 'something.avi', output: 'something.m4v' })
       // console.log('req.file.filename',req.file.mimetype )
         req.body.videoUrl = 'http://localhost:3000/videos/' + req.file.filename;
 
         //get the body
         const newVideo = req.body;
         //get the user
         const userId = req.user._id;
         //assign new video to the user
         newVideo.userId = userId;
         // create the new video
         const video = await new Video(newVideo);
         // save the new Video
         await video.save();
         //get the user from db
         const user = await db.User.findById(userId);
         // save the new video to user collections
         await user.user_videos.push(video);
         await user.save();
 
         //respond to client
 
         res.status(200).json(video);
        
     }
 },

////// 我用 .flv 进行了测试,文件保存在 public/videos 上,但我希望只将所有文件转换为流行格式

【问题讨论】:

    标签: node.js mongodb express video multer


    【解决方案1】:

    您需要将节点服务器上保存的视频文件发送到编码器以编码为另一个文件。

    ffmpeg 支持所有视频类型,非常灵活。

    我确实相信有一个节点包,但首选原始命令行。

    【讨论】:

      猜你喜欢
      • 2017-10-19
      • 2017-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-06
      • 2016-05-01
      • 2023-03-31
      • 1970-01-01
      相关资源
      最近更新 更多