Skip to content

Make videos on the computer you already own. FreeVideo runs MiniMax H3 in as little as 8 GB of VRAM and 16 GB of RAM, and adapts its acceleration path to your hardware.

Editor: imported clips can be extended like generated ones

Bowen Xuec190d9ce11ae
diff --git a/freevideo_engine/agent/editor_runtime.py b/freevideo_engine/agent/editor_runtime.py
index 3d4c42c..e247014 100644
--- a/freevideo_engine/agent/editor_runtime.py
+++ b/freevideo_engine/agent/editor_runtime.py
@@ -166,12 +166,15 @@ globalThis.__generation = text => {
};
try {
const record = model.parse(request.project);
+ const generation = __load('editor_generation');
if (request.mode === 'request') {
const value = model.generationRequest(record, request.ids, {text: request.text, paths: request.paths});
- return JSON.stringify({ok: true, blocks: value.blocks.map(({before, ...block}) => block)});
+ // Parts that continue finished footage are made at its shape, as on the page.
+ const follow = generation.continuedSize(model.generationBefore(record, request.ids), new Map(Object.entries(request.sizes || {})));
+ return JSON.stringify({ok: true, blocks: value.blocks.map(({before, ...block}) => block), size: follow});
}
- const store = __load('editor_store'), generation = __load('editor_generation');
- const size = generation.generationSize(record), target = request.target, words = request.text;
+ const store = __load('editor_store');
+ const size = request.size || generation.generationSize(record), target = request.target, words = request.text;
const reply = request.capability, ready = reply && (!reply.status || reply.status === 'ready');
const limits = ready ? store.capabilityPoints(reply, {...record, ...size})
: {maxSeconds: 15, maxFollowSeconds: 15, maxRequestFrames: null};
@@ -354,7 +357,7 @@ class EditorRuntime(ScriptRuntime):
return self._entry('__batch', dict(project=project, operations=operations))
def generation(self, **request):
- """mode='plan' (project, target, text, capability) or mode='request' (project, ids, text, paths)."""
+ """mode='plan' (project, target, text, capability[, size]) or mode='request' (project, ids, text, paths, sizes)."""
return self._entry('__generation', request)
def deliver(self, **request):
diff --git a/freevideo_engine/agent/projects.py b/freevideo_engine/agent/projects.py
index 2dd233f..ddcb13e 100644
--- a/freevideo_engine/agent/projects.py
+++ b/freevideo_engine/agent/projects.py
@@ -325,10 +325,18 @@ class Projects:
if any(info['missing'] or not info.get('path') for info in found.values()):
raise ProjectError('source_missing', 'The part this generation continues from is not available.')
request = rules.generation(mode='request', project=plan['project'], ids=plan['ids'], text=text,
- paths={key: info['path'] for key, info in found.items()})
+ paths={key: info['path'] for key, info in found.items()},
+ sizes={key: dict(width=info['width'], height=info['height']) for key, info in found.items()})
if not request['ok']:
self._rejected(request['error'], stored, '/target', target)
- limit = plan['max_request_frames']
+ size, limit = plan['size'], plan['max_request_frames']
+ if request.get('size') and request['size'] != size:
+ # A part continuing footage of another size has that size's limit, as on the page.
+ size = request['size']
+ reply = capability(size) if capability is not None else None
+ if isinstance(reply, dict) and reply.get('status', 'ready') == 'ready':
+ limit = rules.generation(mode='plan', project=stored, target=target, text=text, capability=reply,
+ size=size).get('max_request_frames')
longest = max(block['frames'] for block in request['blocks'])
if limit is not None and longest > limit:
raise _invalid('/target', 'parts of at most %d frames at this size and quality; choose a shorter range'
@@ -338,8 +346,8 @@ class Projects:
prompt = text.strip() or next((b['text'] for b in blocks if b['text'].strip()), '') or 'Continue the video.'
frames = sum(block['frames'] for block in blocks)
spec = dict({k: v for k, v in settings.items() if k in ('quality', 'sampling', 'loras', 'force_regenerate')},
- kind='generate', prompt=prompt, seed=seed, width=plan['size']['width'],
- height=plan['size']['height'], seconds=min(60., max(1.625, frames / 24)),
+ kind='generate', prompt=prompt, seed=seed, width=size['width'],
+ height=size['height'], seconds=min(60., max(1.625, frames / 24)),
label='Editing project ' + identity)
saved = self._save(plan['project']) # The reservation: the page's revision check orders it.
editor = dict(project=identity, ids=plan['ids'], blocks=blocks, revision=saved['revision'])
diff --git a/freevideo_engine/comfy_editor.py b/freevideo_engine/comfy_editor.py
index d915d2c..619b9b2 100644
--- a/freevideo_engine/comfy_editor.py
+++ b/freevideo_engine/comfy_editor.py
@@ -242,7 +242,7 @@ def validate(record):
raise ValueError('Duplicate versions')
clip = dict(id=identity, start=int(raw['start']), frames=int(raw['frames']), length=int(raw['length']),
source=source, link=link, text=raw.get('text', ''), part=raw.get('part'), pending=pending,
- reason=raw.get('reason'), job=raw.get('job'), extend=raw.get('extend', source is not None and source['kind'] != 'import'),
+ reason=raw.get('reason'), job=raw.get('job'), extend=raw.get('extend', source is not None),
versions=history, **{'in': int(raw['in'])})
clip.update(speed_fields)
clip.update(clip_fields(raw, kind))
@@ -646,6 +646,9 @@ def sources(root, keys):
if source['kind'] in ('work', 'generated'):
relative = path.relative_to(Path(root).resolve()).as_posix()
info['path'] = relative
+ elif path.suffix == '.mp4':
+ # An imported video is continued from its upright 24 fps copy; it is still shown through the import.
+ info['path'] = path.relative_to(Path(root).resolve()).as_posix()
if path.stat().st_size == 0:
raise ValueError('Empty media')
if source['kind'] == 'work':
diff --git a/web/editor_generation.js b/web/editor_generation.js
index 4bf818d..dbc2730 100644
--- a/web/editor_generation.js
+++ b/web/editor_generation.js
@@ -13,11 +13,12 @@ export function generationSize(project, sizes = []) {
|| Math.abs(a.width * a.height - area) - Math.abs(b.width * b.height - area));
return {width: candidates[0].width, height: candidates[0].height};
}
-// A part that continues finished footage is made at that footage's size, whatever the edit's own: the size all the
-// footage a request continues has in common. null when a video in the request starts fresh or the sizes differ; the
-// edit's size then stands.
+// A part that continues finished footage is made at that footage's shape, whatever the edit's own: the size all the
+// footage a request continues has in common (an imported video off the 32-pixel grid gets the nearest size of its
+// shape). null when a video in the request starts fresh or the sizes differ; the edit's size then stands.
export function continuedSize(before, sources) {
- const sizes = before.map(key => key && sources.get(key));
+ const sizes = before.map(key => key && sources.get(key)).map(size =>
+ valid(size) ? size : [size?.width, size?.height].every(side => Number.isSafeInteger(side) && side > 0) ? generationSize(size) : null);
if (!sizes.length || !sizes.every(valid)) return null;
const {width, height} = sizes[0];
return sizes.every(size => size.width === width && size.height === height) ? {width, height} : null;
diff --git a/web/editor_model.js b/web/editor_model.js
index 0c80ecf..ac2acf7 100644
--- a/web/editor_model.js
+++ b/web/editor_model.js
@@ -161,7 +161,7 @@ export function parse(value) {
const versions = item.versions.map(value => versionData(value, kind)), versionKeys = versions.map(key);
check(new Set(versionKeys).size === versions.length, 'versions');
const clip = {id: item.id, start: item.start, frames: item.frames, in: item.in, length: item.length,
- source, link, text, part, pending, reason, job, extend: item.extend, versions};
+ source, link, text, part, pending, reason, job, extend: item.extend || (source?.kind === 'import' && item.length >= TAIL_FRAMES), versions};
if (kind === 'video') { Object.assign(clip, pictureFields(item)); clip.fit = pictureFit(item.fit); }
else check(item.fit === undefined, 'fit');
if (kind === 'audio') { check(typeof item.volume === 'number' && Number.isFinite(item.volume) && item.volume >= 0 && item.volume <= 2, 'volume'); clip.volume = item.volume; }
@@ -321,10 +321,12 @@ function audioTrack(record, id = 'a1') {
return trackBy(record, id);
}
const freshId = record => `c${++record.serial}`;
+// A continued video needs this many frames for its encoded tail.
+const TAIL_FRAMES = 39;
function newClip(record, track, {source = null, start, frames, part = null, text = ''}) {
const clip = {id: freshId(record), start, frames, in: 0, length: frames, source, link: null,
text, part, pending: source ? null : 'draft', reason: null, job: null,
- extend: source ? source.kind !== 'import' : true, versions: []};
+ extend: source?.kind !== 'import' || frames >= TAIL_FRAMES, versions: []};
if (track.kind === 'audio') clip.volume = 1;
track.clips.push(clip); return clip;
}
@@ -911,7 +913,7 @@ export function prepare(record, ids) {
const spare = Math.min(...members.map(clip => clip.length - clip.in - clip.frames));
const first = find(next, clips[0].id);
// Leave at least one visible frame in every planned segment.
- const credit = spare < first.frames ? spare : 0;
+ const credit = spare > 0 && spare < first.frames ? spare : 0;
if (credit) {
busy(members);
for (const clip of members) clip.frames += credit;
@@ -939,6 +941,12 @@ export function generationBefore(record, ids) {
return generationPlan(record, clips).filter(row => !row.before || !clips.some(clip => clip.id === row.before.id))
.map(row => row.before ? key(row.before.source) : null);
}
+// An imported video is continued from the copy the editor plays, which is upright and at 24 fps.
+function continuedPath(source, path) {
+ if (source.kind !== 'import') return sourceData({kind: 'generated', id: path}).id;
+ check(path === `FreeVideo/.editor/imports/${source.id}/preview.mp4`, 'source');
+ return path;
+}
export function generationRequest(record, ids, {text, paths = {}} = {}) {
check(object(paths), 'source');
if (text !== undefined) check(validText(text), 'text');
@@ -947,7 +955,7 @@ export function generationRequest(record, ids, {text, paths = {}} = {}) {
let after = null;
if (row.before) {
if (clips.some(clip => clip.id === row.before.id)) after = row.before.id;
- else { const path = paths[key(row.before.source)]; check(typeof path === 'string' && path.length > 0, 'source', 'missing source path'); after = {work: sourceData({kind: 'generated', id: path}).id}; }
+ else { const path = paths[key(row.before.source)]; check(typeof path === 'string' && path.length > 0, 'source', 'missing source path'); after = {work: continuedPath(row.before.source, path)}; }
}
blocks.push({id: row.clip.id, part: row.clip.part ?? row.clip.id, after, before: null, text: text ?? row.clip.text, frames: row.request_frames});
plan[row.clip.id] = {target_frames: row.target_frames, net_frames: row.net_frames, request_frames: row.request_frames, audio: row.clip.link};
@@ -996,7 +1004,7 @@ export function fill(record, id, {source, frames, width, height, skip_frames = 0
const take = numberTakes(clip) + 1;
remember(clip, source); clip.source = source; clip.in = skip_frames; clip.length = frames; clip.take = take;
if (clip.text) clip.made_text = clip.text; else delete clip.made_text;
- clip.pending = clip.reason = clip.job = null; clip.extend = source.kind !== 'import';
+ clip.pending = clip.reason = clip.job = null; clip.extend = true;
delete clip.splice;
if (skip_frames || skip_audio_samples) clip.splice = {frames: skip_frames, audio_samples: skip_audio_samples};
clearPlan(clip);
@@ -1040,7 +1048,7 @@ export function restoreVersion(record, id, index, {by = 'user'} = {}) {
remember(clip, version); clip.source = {kind: version.kind, id: version.id}; clip.in = version.in; clip.length = version.length;
delete clip.take; delete clip.made_text; madeFields(version, clip); touchedBy(clip, by);
// Placement belongs to this timeline part, including when its media changes.
- clip.pending = clip.reason = clip.job = null; clip.extend = version.kind !== 'import'; clearPlan(clip);
+ clip.pending = clip.reason = clip.job = null; clip.extend = version.kind !== 'import' || version.length >= TAIL_FRAMES; clearPlan(clip);
delete clip.splice; if (version.splice) clip.splice = version.splice;
}
return finish(next);