Skip to content

Instantly share code, notes, and snippets.

@come25136
Created August 11, 2020 01:27
Show Gist options
  • Save come25136/01b3eff8bb8085c2fed5a1ac472fb0b1 to your computer and use it in GitHub Desktop.
Save come25136/01b3eff8bb8085c2fed5a1ac472fb0b1 to your computer and use it in GitHub Desktop.
const path = require('path')
const fS = require('fs')
const fL = require('node-filelist')
const ffmpeg = require('fluent-ffmpeg')
const cliProgress = require('cli-progress')
const exclusion = require('./exclusion.json')
if (process.argv.length < 4) throw new Error(`Args isn't valid.`)
const searchDirectory = path.normalize(process.argv[2])
const encodedDirectory = path.normalize(process.argv[3])
const ext = 'flv'
process.env.FFMPEG_PATH = path.join(__dirname, 'ffmpeg.exe')
function enc(paths) {
let _index = 0
const multiBar =
new cliProgress.MultiBar(
{
clearOnComplete: false,
hideCursor: true,
format: '{bar} {percentage}% | ETA: {eta}s | {filename} | {index}/{length}'
},
cliProgress.Presets.shades_grey
)
async function encode(index) {
const filePath = paths[index]
if (exclusion.includes(filePath)) {
process.nextTick(() => encode(++_index))
} else {
const encodedFilePath = path.join(encodedDirectory, filePath.replace(searchDirectory, '').replace(new RegExp(`${ext}$`), 'mp4'))
fS.mkdirSync(path.dirname(encodedFilePath), { recursive: true })
const bar = multiBar.create(100, 0, { index: index + 1, length: paths.length, filename: encodedFilePath })
ffmpeg()
.input(filePath)
.videoCodec('h264_nvenc')
.videoBitrate(600000)
.output(encodedFilePath)
.on('progress', progress => bar.update(Math.floor(progress.percent * 10) / 10))
.once('error', (e, sO, sE) => {
console.error(e, sO, sE)
if (++_index !== paths.length) process.nextTick(() => encode(_index))
})
.once('end', () => {
bar.update(100)
if (++_index !== paths.length) process.nextTick(() => encode(_index))
})
.run()
}
}
encode(_index)
encode(++_index)
encode(++_index)
}
if (5 <= process.argv.length && process.argv[4] === 'useCache')
enc(require('./pathCaches.json'))
else
fL.read(
[searchDirectory],
{ ext },
results => {
const paths = results.map(d => d.path)
fS.writeFile("./pathCaches.json", JSON.stringify(paths), () => { })
return
enc(paths)
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment