videoTranscode support options

This commit is contained in:
srgooglo 2022-06-06 12:52:54 +02:00
parent 657b557880
commit a938016cea

View File

@ -2,7 +2,6 @@ import { Controller } from "linebridge/dist/server"
import path from "path" import path from "path"
import fs from "fs" import fs from "fs"
import stream from "stream" import stream from "stream"
const formidable = require("formidable") const formidable = require("formidable")
function resolveToUrl(filepath, req) { function resolveToUrl(filepath, req) {
@ -25,31 +24,39 @@ const acceptedMimeTypes = [
"video/x-ms-wmv", "video/x-ms-wmv",
] ]
function transcodeVideoToWebM(originalFilePath, outputPath) { function videoTranscode(originalFilePath, outputPath, options = {}) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const ffmpeg = require("fluent-ffmpeg") const ffmpeg = require("fluent-ffmpeg")
const filename = path.basename(originalFilePath) const filename = path.basename(originalFilePath)
const outputFilepath = `${outputPath}/${filename.split(".")[0]}.webm` const outputFilepath = `${outputPath}/${filename.split(".")[0]}.${options.format ?? "webm"}`
console.log(`[TRANSCODING] Transcoding ${originalFilePath} to ${outputFilepath}`) console.debug(`[TRANSCODING] Transcoding ${originalFilePath} to ${outputFilepath}`)
const onEnd = async () => {
// remove
await fs.promises.unlink(originalFilePath)
console.debug(`[TRANSCODING] Transcoding ${originalFilePath} to ${outputFilepath} finished`)
return resolve(outputFilepath)
}
const onError = (err) => {
console.error(`[TRANSCODING] Transcoding ${originalFilePath} to ${outputFilepath} failed`, err)
return reject(err)
}
ffmpeg(originalFilePath) ffmpeg(originalFilePath)
.audioBitrate(128) .audioBitrate(options.audioBitrate ?? 128)
.videoBitrate(1024) .videoBitrate(options.videoBitrate ?? 1024)
.videoCodec("libvpx") .videoCodec(options.videoCodec ?? "libvpx")
.audioCodec("libvorbis") .audioCodec(options.audioCodec ?? "libvorbis")
.format("webm") .format(options.format ?? "webm")
.output(outputFilepath) .output(outputFilepath)
.on("error", (err) => { .on("error", onError)
console.log("Error: " + err.message) .on("end", onEnd)
return reject(err)
})
.on("end", () => {
console.log("[Transcoding finished]")
return resolve(outputFilepath)
})
.run() .run()
}) })
} }
@ -122,7 +129,7 @@ export default class FilesController extends Controller {
// check if is video need to transcode // check if is video need to transcode
switch (file.mimetype) { switch (file.mimetype) {
case "video/quicktime": { case "video/quicktime": {
file.filepath = await transcodeVideoToWebM(file.filepath, global.uploadCachePath) file.filepath = await videoTranscode(file.filepath, global.uploadCachePath)
file.newFilename = path.basename(file.filepath) file.newFilename = path.basename(file.filepath)
break break
} }