Skip to content

Commit e44f7a4

Browse files
committed
Fix issue with default duration overriding video layer duration
Introduced in #314, which is unreleased.
1 parent aef2d32 commit e44f7a4

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

src/configuration.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,13 @@ export class Configuration {
216216
this.defaults = merge({}, globalDefaults, input.defaults);
217217

218218
this.clips = input.clips.map((clip) => {
219-
const { transition, duration } = merge({}, this.defaults, clip);
220219
let { layers } = clip;
221220

222221
if (layers && !Array.isArray(layers)) layers = [layers]; // Allow single layer for convenience
223222
assert(
224223
Array.isArray(layers) && layers.length > 0,
225224
"clip.layers must be an array with at least one layer.",
226225
);
227-
assert(transition == null || typeof transition === "object", "Transition must be an object");
228226

229227
layers = layers
230228
.map(expandLayerAliases)
@@ -239,7 +237,10 @@ export class Configuration {
239237
);
240238
});
241239

242-
return { transition, duration, layers };
240+
const { transition } = merge({}, this.defaults, clip);
241+
assert(transition == null || typeof transition === "object", "Transition must be an object");
242+
243+
return { transition, layers, duration: clip.duration };
243244
});
244245

245246
// Testing options:

src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ async function Editly(input: ConfigurationOptions): Promise<void> {
4848
customOutputArgs,
4949
isGif,
5050
tmpDir,
51+
defaults,
5152
} = config;
5253

5354
await configureFf(config);
@@ -63,6 +64,7 @@ async function Editly(input: ConfigurationOptions): Promise<void> {
6364
backgroundAudioVolume,
6465
loopAudio,
6566
allowRemoteRequests,
67+
defaults,
6668
});
6769
if (verbose) console.log("Calculated", JSON5.stringify({ clips, arbitraryAudio }, null, 2));
6870

@@ -492,13 +494,19 @@ export async function renderSingleFrame(input: RenderSingleFrameConfig): Promise
492494
verbose,
493495
logTimes,
494496
outPath = `${Math.floor(Math.random() * 1e12)}.png`,
497+
defaults,
495498
} = config;
496499

497500
configureFf(config);
498501

499502
console.log({ clipsIn });
500503

501-
const { clips } = await parseConfig({ clips: clipsIn, arbitraryAudio: [], allowRemoteRequests });
504+
const { clips } = await parseConfig({
505+
clips: clipsIn,
506+
arbitraryAudio: [],
507+
allowRemoteRequests,
508+
defaults,
509+
});
502510
let clipStartTime = 0;
503511
const clip = clips.find((c) => {
504512
if (clipStartTime <= time && clipStartTime + c.duration > time) return true;

src/parseConfig.ts

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import { registerFont } from "canvas";
33
import flatMap from "lodash-es/flatMap.js";
44
import pMap from "p-map";
55
import { basename } from "path";
6+
import { Configuration } from "./configuration.js";
67
import { readDuration, readVideoFileInfo } from "./ffmpeg.js";
78
import { Transition } from "./transition.js";
89
import type {
910
AudioTrack,
1011
CanvasLayer,
11-
Clip,
1212
FabricLayer,
1313
ImageLayer,
1414
ImageOverlayLayer,
@@ -49,13 +49,12 @@ async function validateArbitraryAudio(
4949
}
5050

5151
type ParseConfigOptions = {
52-
clips: Clip[];
53-
backgroundAudioVolume?: string | number;
5452
backgroundAudioPath?: string;
55-
loopAudio?: boolean;
56-
allowRemoteRequests?: boolean;
5753
arbitraryAudio: AudioTrack[];
58-
};
54+
} & Pick<
55+
Configuration,
56+
"clips" | "backgroundAudioVolume" | "loopAudio" | "allowRemoteRequests" | "defaults"
57+
>;
5958

6059
export default async function parseConfig({
6160
clips,
@@ -64,6 +63,7 @@ export default async function parseConfig({
6463
backgroundAudioVolume,
6564
loopAudio,
6665
allowRemoteRequests,
66+
defaults,
6767
}: ParseConfigOptions) {
6868
async function handleLayer(layer: Layer): Promise<Layer | Layer[]> {
6969
// https://github.com/mifi/editly/issues/39
@@ -122,14 +122,8 @@ export default async function parseConfig({
122122
let clipsOut: ProcessedClip[] = await pMap(
123123
clips,
124124
async (clip, clipIndex) => {
125-
const { transition: userTransition, duration, layers } = clip;
126-
127-
const videoLayers = layers.filter((layer) => layer.type === "video");
128-
129-
if (videoLayers.length === 0)
130-
assert(duration, `Duration parameter is required for videoless clip ${clipIndex}`);
131-
132-
const transition = new Transition(userTransition, clipIndex === clips.length - 1);
125+
const { layers } = clip;
126+
const transition = new Transition(clip.transition, clipIndex === clips.length - 1);
133127

134128
let layersOut = flatMap(
135129
await pMap(
@@ -179,13 +173,14 @@ export default async function parseConfig({
179173
),
180174
);
181175

182-
let clipDuration = duration;
176+
let clipDuration = clip.duration;
183177

184-
const firstVideoLayer = layersOut.find(
185-
(layer): layer is VideoLayer => layer.type === "video",
186-
);
187-
if (firstVideoLayer && !duration) clipDuration = firstVideoLayer.layerDuration!;
188-
assert(clipDuration);
178+
if (!clipDuration) {
179+
const video = layersOut.find((layer): layer is VideoLayer => layer.type === "video");
180+
clipDuration = video?.layerDuration ?? defaults.duration;
181+
}
182+
183+
assert(clipDuration, `Duration parameter is required for videoless clip ${clipIndex}`);
189184

190185
// We need to map again, because for audio, we need to know the correct clipDuration
191186
layersOut = (
@@ -229,9 +224,9 @@ export default async function parseConfig({
229224
let speedFactor;
230225

231226
// If user explicitly specified duration for clip, it means that should be the output duration of the video
232-
if (duration) {
227+
if (clipDuration) {
233228
// Later we will speed up or slow down video using this factor
234-
speedFactor = duration / layerDuration;
229+
speedFactor = clipDuration / layerDuration;
235230
} else {
236231
speedFactor = 1;
237232
}

0 commit comments

Comments
 (0)