Skip to content

Instantly share code, notes, and snippets.

@bellbind
Last active October 25, 2023 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bellbind/17d112a7a83cd5d4ea4acd354654230b to your computer and use it in GitHub Desktop.
Save bellbind/17d112a7a83cd5d4ea4acd354654230b to your computer and use it in GitHub Desktop.
[deno]10M bps video files concation with x4 speed and x2 volume with ffmpeg command
#!/usr/bin/env -S deno run --allow-read --allow-run
// usage: deno run --allow-read --allow-run movconcat.js /Volumes/UNTITLED/Normal/F/ result.mp4
import * as path from "https://deno.land/std/path/mod.ts";
import * as flags from "https://deno.land/std/flags/mod.ts";
const parsed = flags.parse(Deno.args);
const sources = parsed._.at(-2);
const output = parsed._.at(-1);
const inputs = [];
for await (const {name} of Deno.readDir(sources)) inputs.push(name);
inputs.sort();
const list = inputs.map(f => `file ${path.toFileUrl(path.join(sources, f))}\n`).join(``);
const ffmpeg = Deno.build.os === "darwin" ? [`caffeinate`, `-imd`, `ffmpeg`] : [`ffmpeg`];
const vf = (parsed.g ? `eq=gamma=${parsed.g},` : ``) + `yadif=0:-1:1,fps=60000/1000,setpts=(PTS-STARTPTS)/4.0`;
const options = [
`-protocol_whitelist`, `file,data,pipe,crypto`, `-safe`, `0`, `-f`, `concat`, `-i`, `pipe:0`,
`-movflags`, `+faststart`,
`-c:v`, `libx264`, `-profile:v`, `high`, `-b:v`, `12M`, `-bf`, `2`, `-r`, `60`, `-g`, `30`, `-coder`, `1`, `-vf`, vf,
`-c:a`, `aac`, `-profile:a`, `aac_low`, `-b:a`, `384k`, `-ac`, `2`, `-ar`, `96000`, `-af`, `atempo=2.0,atempo=2.0,volume=2.0`,
];
const cmd = [...ffmpeg, ...options, output];
console.log(cmd.join(` `));
const proc = Deno.run({cmd, stdin: `piped`});
const tes = new TextEncoderStream();
tes.readable.pipeTo(proc.stdin.writable);
const writer = tes.writable.getWriter();
await writer.write(list);
await writer.close();
await proc.status();
#!/usr/bin/env -S deno run --allow-read --allow-run
// usage: deno run --allow-read --allow-run movconcat.js /Volumes/UNTITLED/Normal/F/ result.mp4
import * as path from "https://deno.land/std/path/mod.ts";
const sources = Deno.args.at(-2);
const output = Deno.args.at(-1);
const inputs = [];
for await (const {name} of Deno.readDir(sources)) inputs.push(name);
inputs.sort();
const list = inputs.map(f => `file ${path.toFileUrl(path.join(sources, f))}\n`).join(``);
const ffmpeg = Deno.build.os === "darwin" ? [`caffeinate`, `-imd`, `ffmpeg`] : [`ffmpeg`];
const options = [
`-protocol_whitelist`, `file,data,pipe,crypto`, `-safe`, `0`, `-f`, `concat`, `-i`, `pipe:0`,
`-movflags`, `+faststart`,
`-c:v`, `libx264`, `-profile:v`, `high`, `-b:v`, `10M`, `-bf`, `2`, `-r`, `30`, `-g`, `15`, `-coder`, `1`, `-vf`, `yadif=0:-1:1,setpts=PTS/4.0`,
`-c:a`, `aac`, `-profile:a`, `aac_low`, `-b:a`, `384k`, `-ac`, `2`, `-ar`, `96000`, `-af`, `atempo=2.0,atempo=2.0,volume=2.0`,
];
const cmd = [...ffmpeg, ...options, output];
console.log(cmd.join(" "));
const proc = Deno.run({cmd, stdin: "piped"});
const tes = new TextEncoderStream();
tes.readable.pipeTo(proc.stdin.writable);
const writer = tes.writable.getWriter();
await writer.write(list);
await writer.close();
await proc.status();
@bellbind
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment