利用nodejs實現登入並轉碼視頻(原創)

來源:互聯網
上載者:User

標籤:

nodejs的出現讓前端人員可以使用js打造後台,也許哪天就真的擺脫了對java或者php的依賴了.

今天跟大家分享一個利用nodejs接受前端post請求,並實現視頻轉碼的這樣一個例子.視頻轉碼用到的是ffmpeg,nodejs取到表單的參數採用的是目錄multiparty;具體實現如下:

1.項目主要檔案結構

2.ffmpeg.js檔案是開機檔案

const express = require(‘express‘);const path = require(‘path‘);const multiparty=require(‘multiparty‘);const ffmpeg=require(‘fluent-ffmpeg‘);const fs=require(‘fs‘);const bodyParser = require(‘body-parser‘);const app=express();app.use(express.static(path.join(__dirname, ‘public‘)));//設定靜態檔案根路徑app.use(bodyParser.urlencoded({ extended: false }));app.get(‘/‘,function (req,res) {    res.sendfile(‘./public/html/login.html‘)})app.post(‘/ffuser/login‘,function (req,res) {    var form = new multiparty.Form({uploadDir: ‘./public/upload/‘});    form.parse(req, function (err, fields, files) {        console.log(files);        var filesTmp = JSON.stringify(files, null, 2);        var inputFile = files.avatar[0];        var uploadedPath = inputFile.path;        var dstPath = ‘./public/realvideo/‘ + inputFile.originalFilename;        var exchangePath=‘./public/convert/‘ + inputFile.originalFilename;        fs.rename(uploadedPath, dstPath, function (err) {            if (err) {                console.log(‘rename error: ‘ + err);            } else {                console.log(‘rename ok‘)                if (inputFile.originalFilename.split(‘.‘)[1] == ‘MP4‘ || inputFile.originalFilename.split(‘.‘)[1] == ‘mp4‘) {                    var trans = new ffmpeg({source: dstPath})                        .setFfmpegPath(‘./public/ffmpeg-64/bin/ffmpeg.exe‘)                        .withAspect(‘4:3‘)                        .withSize(‘1280x960‘)                        .applyAutopadding(true, ‘white‘)                        .saveToFile(exchangePath, function (retcode, error) {                            if (error) {                                console.log(error)                            } else {                                console.log(retcode)                            }                        })                        .on(‘end‘,function () {                            console.log(‘轉碼完成!‘)                            res.send({code:‘success‘,json:{fields: fields, video: ‘/convert/‘+inputFile.originalFilename}});                        })                }            }        });    });})app.listen(3000,function () {    console.log(‘server start‘)})

  3.運行ffmpeg.js,並在瀏覽器地址欄輸入 localhost:3000,頁面如下:

4.填好使用者名稱和密碼,選擇好需要上傳的視頻檔案後,點擊登入

5.操作成功後,視頻會先儲存在realvideo這個目錄下,轉碼後的視頻將會儲存在convert這個目錄下:

6.頁面發起的post請求在收到返回參數後,會自動播放返回的視頻檔案

7.本樣本中所作的視頻轉碼僅僅是尺寸的改變,官網上還有更多的轉碼操作,如碼率等等

利用nodejs實現登入並轉碼視頻(原創)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.