scale.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. 'use strict';
  2. const Controller = require('egg').Controller;
  3. const ffmpeg = require('fluent-ffmpeg');
  4. const assert = require('assert');
  5. class ScaleController extends Controller {
  6. // 调整视频分辨率
  7. async scale() {
  8. const { ctx, app } = this;
  9. const { appid } = ctx.params;
  10. assert(appid);
  11. const dirs = [ appid ];
  12. const rootPath = `${app.config.cdn.repos_root_path}`;
  13. const rootUrl = `${app.config.cdn.repos_root_url}`;
  14. const filename = ctx.request.body.filename;
  15. const file = `${rootPath}/${dirs.join('/')}/${filename}`;
  16. const filearry = filename.split('.');
  17. const savedfilename = filearry[0] + '_' + ctx.request.body.scale + '.' + filearry[1];
  18. const uri = `${rootUrl}/${dirs.join('/')}/${savedfilename}`;
  19. const outPath = `${rootPath}/${dirs.join('/')}/${savedfilename}`;
  20. new ffmpeg({ source: file })
  21. .withAspect('4:3')
  22. .withSize(ctx.request.body.scale + 'x?')
  23. .applyAutopadding(true, 'white')
  24. .saveToFile(outPath, function(retcode, error) {
  25. // 成功处理
  26. });
  27. ctx.body = { errcode: 0, errmsg: 'ok', name: savedfilename, uri };
  28. }
  29. // 设置视频封面图
  30. async takeshots() {
  31. const { ctx, app } = this;
  32. const { appid } = ctx.params;
  33. assert(appid);
  34. const dirs = [ appid ];
  35. const rootPath = `${app.config.cdn.repos_root_path}`;
  36. const rootUrl = `${app.config.cdn.repos_root_url}`;
  37. const filename = ctx.request.body.filename;
  38. const file = `${rootPath}/${dirs.join('/')}/${filename}`;
  39. const filearry = filename.split('.');
  40. const savedfilename = filearry[0] + '.png';
  41. const outPath = `${rootPath}/${dirs.join('/')}`;
  42. const outfilename = filearry[0] + '_1.png';
  43. const uri = `${rootUrl}/${dirs.join('/')}/${outfilename}`;
  44. new ffmpeg({ source: file })
  45. .screenshots({
  46. timestamps: [ 30.5, '45%', '01:05.123' ],
  47. filename: savedfilename,
  48. folder: outPath,
  49. size: '640x480',
  50. });
  51. // 取得视频信息
  52. const metainfo = await this.getMetainfo(file);
  53. const metadata = metainfo.streams[0];
  54. const scale = metadata.width + '*' + metadata.height;
  55. ctx.body = { errcode: 0, errmsg: 'ok', name: outfilename, uri, scale };
  56. }
  57. // 视频剪辑处理
  58. async clip() {
  59. const { ctx, app } = this;
  60. const { appid } = ctx.params;
  61. assert(appid);
  62. const dirs = [ appid ];
  63. const rootPath = `${app.config.cdn.repos_root_path}`;
  64. const rootUrl = `${app.config.cdn.repos_root_url}`;
  65. const filename = ctx.request.body.filename;
  66. const file = `${rootPath}/${dirs.join('/')}/${filename}`;
  67. const filearry = filename.split('.');
  68. const savedfilename = filearry[0] + '_clip.' + filearry[1];
  69. const uri = `${rootUrl}/${dirs.join('/')}/${savedfilename}`;
  70. const outPath = `${rootPath}/${dirs.join('/')}/${savedfilename}`;
  71. new ffmpeg({ source: file })
  72. .seek(ctx.request.body.seek)
  73. .duration(ctx.request.body.duration)
  74. .saveToFile(outPath, function(retcode, error) {
  75. // 成功处理
  76. });
  77. ctx.body = { errcode: 0, errmsg: 'ok', name: savedfilename, uri };
  78. }
  79. // 获取视频信息
  80. async metainfo() {
  81. const { ctx, app } = this;
  82. const { appid } = ctx.params;
  83. assert(appid);
  84. const dirs = [ appid ];
  85. const rootPath = `${app.config.cdn.repos_root_path}`;
  86. const filename = ctx.request.body.filename;
  87. const file = `${rootPath}/${dirs.join('/')}/${filename}`;
  88. const metainfo = await this.getMetainfo(file);
  89. const metadata = metainfo.streams[0];
  90. const scale = metadata.width + '*' + metadata.height;
  91. ctx.body = { errcode: 0, errmsg: 'ok', scale };
  92. }
  93. async getMetainfo(file) {
  94. let metainfo = {};
  95. return new Promise(function(resolve) {
  96. new ffmpeg.ffprobe(file, function(err, metadata) {
  97. metainfo = metadata;
  98. resolve(metainfo);
  99. });
  100. });
  101. }
  102. }
  103. module.exports = ScaleController;