mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-09 18:44:16 +00:00
update defaults
This commit is contained in:
parent
09c349f66e
commit
71eac66b1f
@ -3,86 +3,96 @@ import path from "path"
|
|||||||
const ffmpeg = require("fluent-ffmpeg")
|
const ffmpeg = require("fluent-ffmpeg")
|
||||||
|
|
||||||
const defaultParams = {
|
const defaultParams = {
|
||||||
audioBitrate: 128,
|
audioBitrate: 128,
|
||||||
videoBitrate: 1024,
|
videoBitrate: 1024,
|
||||||
videoCodec: "libvpx",
|
videoCodec: "libvpx",
|
||||||
audioCodec: "libvorbis",
|
audioCodec: "libvorbis",
|
||||||
format: "webm",
|
format: "mp4",
|
||||||
}
|
}
|
||||||
|
|
||||||
const maxTasks = 5
|
const maxTasks = 5
|
||||||
|
|
||||||
export default (input, params = defaultParams) => {
|
export default (input, params = defaultParams) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!global.ffmpegTasks) {
|
if (!global.ffmpegTasks) {
|
||||||
global.ffmpegTasks = []
|
global.ffmpegTasks = []
|
||||||
}
|
}
|
||||||
|
|
||||||
if (global.ffmpegTasks.length >= maxTasks) {
|
if (global.ffmpegTasks.length >= maxTasks) {
|
||||||
return reject(new Error("Too many transcoding tasks"))
|
return reject(new Error("Too many transcoding tasks"))
|
||||||
}
|
}
|
||||||
|
|
||||||
const outputFilename = `${path.basename(input).split(".")[0]}_ff.${params.format ?? "webm"}`
|
const outputFilename = `${path.basename(input).split(".")[0]}_ff.${params.format ?? "webm"}`
|
||||||
const outputFilepath = `${path.dirname(input)}/${outputFilename}`
|
const outputFilepath = `${path.dirname(input)}/${outputFilename}`
|
||||||
|
|
||||||
console.debug(`[TRANSCODING] Transcoding ${input} to ${outputFilepath}`)
|
console.debug(`[TRANSCODING] Transcoding ${input} to ${outputFilepath}`)
|
||||||
|
|
||||||
const onEnd = async () => {
|
const onEnd = async () => {
|
||||||
console.debug(`[TRANSCODING] Finished transcode ${input} to ${outputFilepath}`)
|
console.debug(
|
||||||
|
`[TRANSCODING] Finished transcode ${input} to ${outputFilepath}`,
|
||||||
|
)
|
||||||
|
|
||||||
return resolve({
|
return resolve({
|
||||||
filename: outputFilename,
|
filename: outputFilename,
|
||||||
filepath: outputFilepath,
|
filepath: outputFilepath,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const onError = (err) => {
|
const onError = (err) => {
|
||||||
console.error(`[TRANSCODING] Transcoding ${input} to ${outputFilepath} failed`, err)
|
console.error(
|
||||||
|
`[TRANSCODING] Transcoding ${input} to ${outputFilepath} failed`,
|
||||||
|
err,
|
||||||
|
)
|
||||||
|
|
||||||
return reject(err)
|
return reject(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
let exec = null
|
let exec = null
|
||||||
|
|
||||||
const commands = {
|
const commands = {
|
||||||
input: input,
|
input: input,
|
||||||
...params,
|
...params,
|
||||||
preset: "ultrafast",
|
output: outputFilepath,
|
||||||
output: outputFilepath,
|
outputOptions: ["-preset veryfast"],
|
||||||
}
|
}
|
||||||
|
|
||||||
// chain methods
|
// chain methods
|
||||||
for (let key in commands) {
|
for (let key in commands) {
|
||||||
if (exec === null) {
|
if (exec === null) {
|
||||||
exec = ffmpeg(commands[key])
|
exec = ffmpeg(commands[key])
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if (key === "extraOptions" && Array.isArray(commands[key])) {
|
if (key === "extraOptions" && Array.isArray(commands[key])) {
|
||||||
for (const option of commands[key]) {
|
for (const option of commands[key]) {
|
||||||
exec = exec.inputOptions(option)
|
exec = exec.inputOptions(option)
|
||||||
}
|
}
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof exec[key] !== "function") {
|
if (key === "outputOptions" && Array.isArray(commands[key])) {
|
||||||
console.warn(`[TRANSCODING] Method ${key} is not a function`)
|
for (const option of commands[key]) {
|
||||||
return false
|
exec = exec.outputOptions(option)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(commands[key])) {
|
continue
|
||||||
exec = exec[key](...commands[key])
|
}
|
||||||
} else {
|
|
||||||
exec = exec[key](commands[key])
|
|
||||||
}
|
|
||||||
|
|
||||||
continue
|
if (typeof exec[key] !== "function") {
|
||||||
}
|
console.warn(`[TRANSCODING] Method ${key} is not a function`)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
exec
|
if (Array.isArray(commands[key])) {
|
||||||
.on("error", onError)
|
exec = exec[key](...commands[key])
|
||||||
.on("end", onEnd)
|
} else {
|
||||||
.run()
|
exec = exec[key](commands[key])
|
||||||
})
|
}
|
||||||
|
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
exec.on("error", onError).on("end", onEnd).run()
|
||||||
|
})
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user