123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- 'use strict';
- const Controller = require('egg').Controller;
- const ffmpeg = require('fluent-ffmpeg');
- const assert = require('assert');
- class ScaleController extends Controller {
- // 调整视频分辨率
- async scale() {
- const { ctx, app } = this;
- const { appid } = ctx.params;
- assert(appid);
- const dirs = [ appid ];
- const rootPath = `${app.config.cdn.repos_root_path}`;
- const rootUrl = `${app.config.cdn.repos_root_url}`;
- const filename = ctx.request.body.filename;
- const file = `${rootPath}/${dirs.join('/')}/${filename}`;
- const filearry = filename.split('.');
- const savedfilename = filearry[0] + '_' + ctx.request.body.scale + '.' + filearry[1];
- const uri = `${rootUrl}/${dirs.join('/')}/${savedfilename}`;
- const outPath = `${rootPath}/${dirs.join('/')}/${savedfilename}`;
- new ffmpeg({ source: file })
- .withAspect('4:3')
- .withSize(ctx.request.body.scale + 'x?')
- .applyAutopadding(true, 'white')
- .saveToFile(outPath, function(retcode, error) {
- // 成功处理
- });
- ctx.body = { errcode: 0, errmsg: 'ok', name: savedfilename, uri };
- }
- // 设置视频封面图
- async takeshots() {
- const { ctx, app } = this;
- const { appid } = ctx.params;
- assert(appid);
- const dirs = [ appid ];
- const rootPath = `${app.config.cdn.repos_root_path}`;
- const rootUrl = `${app.config.cdn.repos_root_url}`;
- const filename = ctx.request.body.filename;
- const file = `${rootPath}/${dirs.join('/')}/${filename}`;
- const filearry = filename.split('.');
- const savedfilename = filearry[0] + '.png';
- const outPath = `${rootPath}/${dirs.join('/')}`;
- const outfilename = filearry[0] + '_1.png';
- const uri = `${rootUrl}/${dirs.join('/')}/${outfilename}`;
- new ffmpeg({ source: file })
- .screenshots({
- timestamps: [ 30.5, '45%', '01:05.123' ],
- filename: savedfilename,
- folder: outPath,
- size: '640x480',
- });
- // 取得视频信息
- const metainfo = await this.getMetainfo(file);
- const metadata = metainfo.streams[0];
- const scale = metadata.width + '*' + metadata.height;
- ctx.body = { errcode: 0, errmsg: 'ok', name: outfilename, uri, scale };
- }
- // 视频剪辑处理
- async clip() {
- const { ctx, app } = this;
- const { appid } = ctx.params;
- assert(appid);
- const dirs = [ appid ];
- const rootPath = `${app.config.cdn.repos_root_path}`;
- const rootUrl = `${app.config.cdn.repos_root_url}`;
- const filename = ctx.request.body.filename;
- const file = `${rootPath}/${dirs.join('/')}/${filename}`;
- const filearry = filename.split('.');
- const savedfilename = filearry[0] + '_clip.' + filearry[1];
- const uri = `${rootUrl}/${dirs.join('/')}/${savedfilename}`;
- const outPath = `${rootPath}/${dirs.join('/')}/${savedfilename}`;
- new ffmpeg({ source: file })
- .seek(ctx.request.body.seek)
- .duration(ctx.request.body.duration)
- .saveToFile(outPath, function(retcode, error) {
- // 成功处理
- });
- ctx.body = { errcode: 0, errmsg: 'ok', name: savedfilename, uri };
- }
- // 获取视频信息
- async metainfo() {
- const { ctx, app } = this;
- const { appid } = ctx.params;
- assert(appid);
- const dirs = [ appid ];
- const rootPath = `${app.config.cdn.repos_root_path}`;
- const filename = ctx.request.body.filename;
- const file = `${rootPath}/${dirs.join('/')}/${filename}`;
- const metainfo = await this.getMetainfo(file);
- const metadata = metainfo.streams[0];
- const scale = metadata.width + '*' + metadata.height;
- ctx.body = { errcode: 0, errmsg: 'ok', scale };
- }
- async getMetainfo(file) {
- let metainfo = {};
- return new Promise(function(resolve) {
- new ffmpeg.ffprobe(file, function(err, metadata) {
- metainfo = metadata;
- resolve(metainfo);
- });
- });
- }
- }
- module.exports = ScaleController;
|