File size: 2,616 Bytes
65d68f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { spawn } from "node:child_process";
import fs from "node:fs";
import path from "node:path";
import process from "node:process";
import { fileURLToPath } from "node:url";

const rootDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");

function parseArgs(argv) {
  const args = {};
  for (let index = 0; index < argv.length; index += 1) {
    const current = argv[index];
    if (current.startsWith("--")) {
      args[current.slice(2)] = argv[index + 1];
      index += 1;
    }
  }
  return args;
}

function fail(message) {
  console.error(message);
  process.exit(1);
}

const args = parseArgs(process.argv.slice(2));
const propsPath = args.props ? path.resolve(rootDir, args.props) : null;
const outputPath = args.output ? path.resolve(rootDir, args.output) : null;

if (!propsPath || !fs.existsSync(propsPath)) {
  fail("Missing or unreadable --props JSON file.");
}

if (!outputPath || path.extname(outputPath).toLowerCase() !== ".mp4") {
  fail("Missing --output MP4 path.");
}

const props = JSON.parse(fs.readFileSync(propsPath, "utf8"));
fs.mkdirSync(path.dirname(outputPath), { recursive: true });
const preview = String(args.preview || "").toLowerCase() === "true";
const scale = args.scale ? String(args.scale) : null;

const remotionCli = path.join(
  rootDir,
  "node_modules",
  "@remotion",
  "cli",
  "remotion-cli.js",
);

if (!fs.existsSync(remotionCli)) {
  fail("Local Remotion CLI was not found. Run npm install first.");
}

const plan = props.plan ?? props;
if (preview && plan && typeof plan === "object") {
  plan.preview_quality = true;
  fs.writeFileSync(propsPath, JSON.stringify(props, null, 2));
}
const format = (plan?.video_format || plan?.format || "").toLowerCase().trim();
const compositionId =
  format === "16:9" || format === "landscape" || format === "16-9"
    ? "ExplainerVideo"
    : format === "9:16" || format === "vertical" || format === "9-16"
    ? "ExplainerVideoVertical"
    : plan?.video_format === "9:16" ||
      plan?.visual_style === "anime_visual_explainer" ||
      plan?.style === "anime_visual_explainer"
    ? "ExplainerVideoVertical"
    : "ExplainerVideo";

const renderArgs = [
  remotionCli,
  "render",
  "src/remotion/GeneratedVideoRoot.tsx",
  compositionId,
  outputPath,
  "--props",
  propsPath,
];

if (scale) {
  renderArgs.push("--scale", scale);
}

const child = spawn(
  process.execPath,
  renderArgs,
  {
    cwd: rootDir,
    shell: false,
    stdio: "inherit",
  },
);

child.on("exit", (code) => {
  process.exit(code ?? 1);
});

child.on("error", (error) => {
  console.error(error);
  process.exit(1);
});