@marcuth/movie.js 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
+ import ffmpeg from "fluent-ffmpeg";
1
2
  import { Path } from "../utils/resolve-path";
2
3
  import { RenderContext } from "../render-context";
3
4
  import { FFmpegInput } from "../ffmpeg-input";
@@ -20,8 +21,8 @@ export declare class ImageClip<RenderData> extends Clip<RenderData> {
20
21
  readonly path: Path<RenderData>;
21
22
  readonly fadeIn?: number;
22
23
  readonly fadeOut?: number;
23
- readonly width?: number;
24
- readonly height?: number;
24
+ readonly width: number;
25
+ readonly height: number;
25
26
  readonly scroll?: {
26
27
  axis?: "auto" | "x" | "y";
27
28
  direction?: "forward" | "backward";
@@ -29,5 +30,6 @@ export declare class ImageClip<RenderData> extends Clip<RenderData> {
29
30
  };
30
31
  constructor({ duration, path, width, height, fadeIn, fadeOut, scroll }: ImageClipOptions<RenderData>);
31
32
  protected getInput(path: string, inputIndex: number, fps: number): FFmpegInput;
32
- build(data: RenderData, context: RenderContext): void;
33
+ protected getMetadata(path: string): Promise<ffmpeg.FfprobeData>;
34
+ build(data: RenderData, context: RenderContext): Promise<void>;
33
35
  }
@@ -1,9 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ImageClip = void 0;
4
+ const fluent_ffmpeg_1 = require("fluent-ffmpeg");
4
5
  const resolve_path_1 = require("../utils/resolve-path");
6
+ const easing_expr_1 = require("../utils/easing-expr");
5
7
  const clip_1 = require("./clip");
6
- const get_eased_expression_1 = require("../utils/get-eased-expression");
7
8
  class ImageClip extends clip_1.Clip {
8
9
  constructor({ duration, path, width, height, fadeIn, fadeOut, scroll }) {
9
10
  super();
@@ -11,8 +12,8 @@ class ImageClip extends clip_1.Clip {
11
12
  this.path = path;
12
13
  this.fadeIn = fadeIn;
13
14
  this.fadeOut = fadeOut;
14
- this.width = width;
15
- this.height = height;
15
+ this.width = width || -1;
16
+ this.height = height || -1;
16
17
  this.scroll = scroll;
17
18
  }
18
19
  getInput(path, inputIndex, fps) {
@@ -31,12 +32,17 @@ class ImageClip extends clip_1.Clip {
31
32
  ]
32
33
  };
33
34
  }
34
- build(data, context) {
35
- var _a, _b, _c, _d, _e;
35
+ getMetadata(path) {
36
+ return new Promise((resolve, reject) => {
37
+ (0, fluent_ffmpeg_1.ffprobe)(path, (err, data) => err ? reject(err) : resolve(data));
38
+ });
39
+ }
40
+ async build(data, context) {
36
41
  const path = (0, resolve_path_1.resolvePath)({ path: this.path, data: data, index: context.clipIndex });
37
42
  const input = this.getInput(path, context.inputIndex, context.fps);
38
43
  let currentVideoOutput = input.aliases.video;
39
44
  const currentAudioOutput = input.aliases.audio;
45
+ const metadata = await this.getMetadata(path);
40
46
  context.command
41
47
  .input(input.path)
42
48
  .inputOptions(input.options);
@@ -52,37 +58,101 @@ class ImageClip extends clip_1.Clip {
52
58
  inputs: anullSrcLabel,
53
59
  outputs: currentAudioOutput,
54
60
  });
55
- if (this.width !== undefined || this.height !== undefined) {
61
+ const hasCanvas = this.width !== -1 || this.height !== -1;
62
+ if (hasCanvas && this.scroll) {
56
63
  const scaleOutput = `scale${context.inputIndex}`;
64
+ let { axis = "auto" } = this.scroll;
65
+ let scaleOptions = null;
66
+ if (axis === "auto") {
67
+ axis = metadata.format.width > metadata.format.height ? "x" : "y";
68
+ }
69
+ if (axis === "y") {
70
+ scaleOptions = {
71
+ w: this.width,
72
+ h: -1,
73
+ };
74
+ }
75
+ else if (axis === "x") {
76
+ scaleOptions = {
77
+ w: -1,
78
+ h: this.height,
79
+ };
80
+ }
81
+ if (!scaleOptions) {
82
+ throw new Error("Invalid scroll axis");
83
+ }
57
84
  context.filters.push({
58
85
  filter: "scale",
59
- options: { w: (_a = this.width) !== null && _a !== void 0 ? _a : -1, h: (_b = this.height) !== null && _b !== void 0 ? _b : -1 },
86
+ options: scaleOptions,
60
87
  inputs: currentVideoOutput,
61
88
  outputs: scaleOutput,
62
89
  });
63
- currentVideoOutput = scaleOutput;
90
+ const setsarOutput = `setsar${context.inputIndex}`;
91
+ context.filters.push({
92
+ filter: "setsar",
93
+ options: { sar: "1/1" },
94
+ inputs: scaleOutput,
95
+ outputs: setsarOutput,
96
+ });
97
+ currentVideoOutput = setsarOutput;
64
98
  }
65
- if (this.scroll) {
66
- const cropOutput = `crop${context.inputIndex}`;
67
- const easing = (_c = this.scroll.easing) !== null && _c !== void 0 ? _c : "linear";
68
- const totalFrames = this.duration * context.fps;
69
- const p = (0, get_eased_expression_1.getEasedExpression)(`min(n/${totalFrames},1)`, easing);
70
- const xExpr = this.scroll.axis === "x" && this.width
71
- ? `if(gt(iw,${this.width}),min((iw-${this.width})*(${p}),iw-${this.width}),0)`
72
- : "0";
73
- const yExpr = this.scroll.axis === "y" && this.height
74
- ? `if(gt(ih,${this.height}),min((ih-${this.height})*(${p}),ih-${this.height}),0)`
75
- : "0";
99
+ else if (hasCanvas && !this.scroll) {
100
+ const scaleOutput = `scale${context.inputIndex}`;
101
+ context.filters.push({
102
+ filter: "scale",
103
+ options: {
104
+ w: this.width,
105
+ h: this.height,
106
+ force_original_aspect_ratio: "increase",
107
+ },
108
+ inputs: currentVideoOutput,
109
+ outputs: scaleOutput,
110
+ });
111
+ const fitOutput = `fit${context.inputIndex}`;
76
112
  context.filters.push({
77
113
  filter: "crop",
78
114
  options: {
79
- w: (_d = this.width) !== null && _d !== void 0 ? _d : "iw",
80
- h: (_e = this.height) !== null && _e !== void 0 ? _e : "ih",
81
- x: xExpr,
82
- y: yExpr
115
+ w: this.width,
116
+ h: this.height,
117
+ x: "(iw-ow)/2",
118
+ y: "(ih-oh)/2",
83
119
  },
120
+ inputs: scaleOutput,
121
+ outputs: fitOutput,
122
+ });
123
+ currentVideoOutput = fitOutput;
124
+ }
125
+ if (this.scroll) {
126
+ const { axis = "auto", direction = "forward", easing = "linear", } = this.scroll;
127
+ const cropOutput = `scroll${context.inputIndex}`;
128
+ const tNorm = `t/${this.duration}`;
129
+ const eased = (0, easing_expr_1.easingExpr)(easing, tNorm);
130
+ const movement = direction === "backward" ? `1-(${eased})` : eased;
131
+ let cropOptions = null;
132
+ if (axis === "y" || axis === "auto") {
133
+ cropOptions = {
134
+ w: this.width,
135
+ h: this.height,
136
+ x: 0,
137
+ y: `(ih-oh)*(${movement})`,
138
+ };
139
+ }
140
+ if (axis === "x") {
141
+ cropOptions = {
142
+ w: this.width,
143
+ h: this.height,
144
+ x: `(iw-ow)*(${movement})`,
145
+ y: 0,
146
+ };
147
+ }
148
+ if (!cropOptions) {
149
+ throw new Error("Invalid crop options");
150
+ }
151
+ context.filters.push({
152
+ filter: "crop",
153
+ options: cropOptions,
84
154
  inputs: currentVideoOutput,
85
- outputs: cropOutput
155
+ outputs: cropOutput,
86
156
  });
87
157
  currentVideoOutput = cropOutput;
88
158
  }
package/dist/template.js CHANGED
@@ -26,16 +26,16 @@ class Template {
26
26
  video: []
27
27
  },
28
28
  };
29
- // if (this.options.debug) {
30
- // command
31
- // .on("start", (commandLine) => {
32
- // console.log("Spawned Ffmpeg with command: " + commandLine)
33
- // })
34
- // .on("error", (err, stdout, stderr) => {
35
- // console.error("Error: " + err.message)
36
- // console.error("ffmpeg stderr: " + stderr)
37
- // })
38
- // }
29
+ if (this.options.debug) {
30
+ command
31
+ .on("start", (commandLine) => {
32
+ console.log("Spawned Ffmpeg with command: " + commandLine);
33
+ })
34
+ .on("error", (err, stdout, stderr) => {
35
+ console.error("Error: " + err.message);
36
+ console.error("ffmpeg stderr: " + stderr);
37
+ });
38
+ }
39
39
  await this.runBuildWithProgress(data, context);
40
40
  const concatFilter = context.labels.video
41
41
  .map((v, i) => `${v}${context.labels.structuralAudio[i]}`)
@@ -53,18 +53,6 @@ class Template {
53
53
  mixFilter,
54
54
  finalAudioFilter
55
55
  ].filter((filter) => filter !== null);
56
- // console.dir({
57
- // concatFilter,
58
- // finalAudioFilter,
59
- // mixFilter,
60
- // filterComplex,
61
- // context: {
62
- // ...context,
63
- // command: "<command>"
64
- // }
65
- // }, {
66
- // depth: null
67
- // })
68
56
  command.complexFilter(filterComplex);
69
57
  command.outputOptions([
70
58
  "-map [outv]",
@@ -0,0 +1 @@
1
+ export declare function easingExpr(easing: "linear" | "easeIn" | "easeOut" | "easeInOut", t: string): string;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.easingExpr = easingExpr;
4
+ function easingExpr(easing, t) {
5
+ switch (easing) {
6
+ case "linear":
7
+ return `(${t})`;
8
+ case "easeIn":
9
+ return `pow(${t},2)`;
10
+ case "easeOut":
11
+ return `1-pow(1-(${t}),2)`;
12
+ case "easeInOut":
13
+ return `if(lt(${t},0.5),2*pow(${t},2),1-2*pow(1-(${t}),2))`;
14
+ default:
15
+ return `(${t})`;
16
+ }
17
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marcuth/movie.js",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Video template builder",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -1,9 +0,0 @@
1
- export type FontConfigOptions = {
2
- color?: string;
3
- filePath?: string;
4
- };
5
- export type PartialFontOptions = {
6
- size: number;
7
- color?: string;
8
- };
9
- export declare function fontConfig(options: FontConfigOptions): (partialOptions: PartialFontOptions) => TextClipFontOptions;
@@ -1,19 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fontConfig = fontConfig;
4
- function fontConfig(options) {
5
- return (partialOptions) => {
6
- var _a;
7
- const color = (_a = options.color) !== null && _a !== void 0 ? _a : partialOptions.color;
8
- const filePath = options.filePath;
9
- const size = partialOptions.size;
10
- if (!color) {
11
- throw new Error("Font color is required");
12
- }
13
- return {
14
- size: size,
15
- color: color,
16
- filePath: filePath,
17
- };
18
- };
19
- }
@@ -1 +0,0 @@
1
- export declare function getEasedExpression(expr: string, easing: "linear" | "easeIn" | "easeOut" | "easeInOut"): string;
@@ -1,15 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getEasedExpression = getEasedExpression;
4
- function getEasedExpression(expr, easing) {
5
- switch (easing) {
6
- case "easeIn":
7
- return `${expr}*${expr}`;
8
- case "easeOut":
9
- return `1-(1-${expr})*(1-${expr})`;
10
- case "easeInOut":
11
- return `(1-cos(PI*${expr}))/2`;
12
- default:
13
- return expr;
14
- }
15
- }
@@ -1 +0,0 @@
1
- export * from "./font-config";
@@ -1,17 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./font-config"), exports);