Skip to content

Commit

Permalink
feat: hls
Browse files Browse the repository at this point in the history
  • Loading branch information
rosendolu committed May 3, 2024
1 parent eead80c commit b709de3
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions app/middleware/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const path = require('node:path');
const util = require('node:util');
const { rootDir } = require('../common/constant');
const { glob } = require('glob');
const { spawn, exec } = require('child_process');
const logger = require('../common/logger');
module.exports = {
async uploadFile(ctx, next) {
const { uid, nickname } = ctx.session;
Expand All @@ -13,11 +15,28 @@ module.exports = {
assert(files, 'Empty file');
for (let i = 0; i < (files.length || 1); i++) {
const { filepath, mimetype, newFilename, originalFilename, size } = files[i] || files;
const userFilePath = path.join(rootDir, 'temp/upload/', uid, newFilename);
await ensureDir(path.dirname(userFilePath));
const newVideoPath = path.join(uid, newFilename);

const userFilePath = path.join(rootDir, 'temp/upload/', newVideoPath);
const userFilePathDir = path.dirname(userFilePath);
await ensureDir(userFilePathDir);
await move(filepath, userFilePath, { overwrite: true });

if (/(?:\.mp4|\.mov)$/.test(userFilePath)) {
const sourcePath = path.join(userFilePathDir, path.parse(newFilename).name, 'output.m3u8');
await ensureDir(path.dirname(sourcePath));
const command = `ffmpeg -v error -i ${userFilePath} \
-map 0:v -crf 23 -preset medium \
-map 0:a? -c:a aac -b:a 128k \
-hls_time 10 -hls_list_size 6 -f hls ${sourcePath}`;

const { stdout, stderr } = await util.promisify(exec)(command);
stderr && logger.error('ffmpeg hls %s', stderr);
}

ctx.body = {
path: userFilePath,
playUrl: path.join(uid, 'output.m3u8'),
path: newVideoPath,
mimetype,
size,
};
Expand Down

0 comments on commit b709de3

Please sign in to comment.