Skip to content

Commit

Permalink
update: argv, adjust cli
Browse files Browse the repository at this point in the history
  • Loading branch information
chase-moskal committed May 23, 2024
1 parent c414608 commit e4b96f6
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 33 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
"s"
],
"scripts": {
"build": "run-s clean compile",
"build": "run-s clean compile perms",
"clean": "rm -rf x && mkdir x",
"compile": "tsc",
"perms": "chmod -R +x x",
"start": "run-p watch",
"watch": "tsc -w",
"test": "rm -rf x/assets && node x/cli.js --in assets --out x/assets --format jpg && if [ ! -f 'x/assets/house.jpg' ]; then echo 'expected to find house.jpg, but it was not found'; exit 1; else exit 0; fi"
},
"dependencies": {
"@benev/argv": "^0.2.1",
"@benev/argv": "^0.3.0",
"ffmpeg-static": "^5.2.0",
"globby": "^14.0.1",
"shelljs": "^0.8.5",
Expand Down
5 changes: 4 additions & 1 deletion s/batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ deathWithDignity()
await cli(process.argv, {
name: "batch",
commands: {audio: {m4a}},
help: `convert and compress media files en mass.`,
help: `
convert and compress media files en mass.
powered by ffmpeg, of course.
`,
}).execute()

36 changes: 25 additions & 11 deletions s/commands/audio/common/params.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@

import {param, validators} from "@benev/argv"
import {asType, number, param, string, validators} from "@benev/argv"

const date = asType({
name: "date",
coerce: string => new Date(string),
})

export const audioParams = {
indir: param.required(String),
outdir: param.required(String),
date: param.required(date),

indir: param.required(string, {help: `
source directory from where to load audio files.
`}),

kbps: param.required(Number, {
outdir: param.required(string, {help: `
target directory to write new audio files to.
`}),

kbps: param.required(number, {
validate: validators.integer_between(16, 320),
help: `
output bitrate in kilobits per second. eg, --kbps=192
Expand All @@ -17,12 +29,14 @@ export const audioParams = {
you should consider lowering the bitrate kbps by about a third, to get similar fidelity as stereo.
`}),

suffix: param.optional(String, {
help: `
insert a string into the output filename. eg,
--suffix=potato
"coolsound.m4a" becomes "coolsound.potato.m4a"
`,
}),
suffix: param.optional(string, {help: `
insert a string into the output filename. eg,
--suffix=potato
"coolsound.m4a" becomes "coolsound.potato.m4a"
`}),

"dry-run": param.flag("d", {help: `
don't actually write any files.
`}),
}

49 changes: 34 additions & 15 deletions s/commands/audio/m4a.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,56 @@ import path from "path"
import shell from "shelljs"
import {globby} from "globby"
import ffmpeg from "ffmpeg-static"
import {command} from "@benev/argv"
import {color, command, list, param, string} from "@benev/argv"

import {audioParams} from "./common/params.js"
import {replaceExtension} from "./common/replace-extension.js"

export const m4a = command({
help: `convert audio files from indir, outputting them into outdir.`,
args: [],
params: audioParams,
params: {
...audioParams,
accept: param.default(list(string), "wav,mp3,m4a,ogg", {
help: `sniff out files with these extensions from the input directory.`,
validate: extensions => {
if (extensions.length === 0)
throw new Error("you need at least one")
return extensions
},
}),
},
execute: async({params}) => {
const patterns = ["**/*.{wav,mp3,m4a,aac}"]
const patterns = [`**/*.{${params.accept.join(",")}}`]
const cwd = path.resolve(params.indir)
const filepaths = await globby(patterns, {cwd})

if (params["dry-run"]) {
console.log(color.yellow("dry-run"))
}

for (const filepath of filepaths) {
const infile = path.join(params.indir, filepath)
const outfile = replaceExtension(
path.join(params.outdir, filepath),
"m4a",
params.suffix,
)
shell.mkdir("-p", path.dirname(outfile))
console.log(`writing "${outfile}"`)
await $`
${ffmpeg} \\
-i ${infile} \\
-c:a aac \\
-b:a ${params.kbps}k \\
${params.mono ? ["-ac", "1"] : []} \\
-y \\
-loglevel quiet \\
${outfile}
`
console.log(` in ${color.blue(infile)}`)
console.log(`out ${color.green(outfile)}`)
if (!params["dry-run"]) {
shell.mkdir("-p", path.dirname(outfile))
await $`
${ffmpeg} \\
-i ${infile} \\
-c:a aac \\
-b:a ${params.kbps}k \\
${params.mono ? ["-ac", "1"] : []} \\
-y \\
-loglevel quiet \\
${outfile}
`
}
}
},
})
Expand Down

0 comments on commit e4b96f6

Please sign in to comment.