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.

Say why a video with the most parts cannot be extended as soon as it is chosen, and check it...

Bowen Xueb0dd5fad7156
diff --git a/freevideo_engine/chain_runner.py b/freevideo_engine/chain_runner.py
index 7011b01..dfdde18 100644
--- a/freevideo_engine/chain_runner.py
+++ b/freevideo_engine/chain_runner.py
@@ -121,6 +121,8 @@ class ChainRunner:
self.step_lock = threading.Lock()
self.wake = threading.Event()
self.closed = threading.Event()
+ # The listed records stay bounded; which receipt made a saved video is indexed on first use, over every receipt on disk.
+ self.work_receipts = None
self.records = {row['id']: row for row in store.load_all(store_dir)}
chain_leads.prune(store_dir, self.records)
self.starts, self.success, self.cancel_sent, self.progress_values = {}, {}, set(), {}
@@ -147,6 +149,25 @@ class ChainRunner:
def _save(self, record):
record['updated'] = self.wall()
store.save(self.store_dir, record)
+ self._index_receipt(record, latest=True)
+
+ def _receipts(self):
+ if self.work_receipts is None:
+ self.work_receipts = {}
+ store.load_all(self.store_dir, visitor=self._index_receipt) # newest first: an older receipt never replaces one
+ return self.work_receipts
+
+ def _index_receipt(self, record, latest=False):
+ if self.work_receipts is None:
+ return # the first lookup reads every receipt, this one included
+ output = record.get('output')
+ if output:
+ target = self.work_receipts.__setitem__ if latest else self.work_receipts.setdefault
+ target(_work_key(output), (record['id'], 'output'))
+ for block in record.get('blocks', ()):
+ if block.get('output'):
+ target = self.work_receipts.__setitem__ if latest else self.work_receipts.setdefault
+ target(_work_key(block['output']), (record['id'], 'block'))
def _current(self, record):
executing = next((row for row in record['blocks'] if row['state'] in ('queued', 'running')), None)
@@ -303,18 +324,44 @@ class ChainRunner:
def _work_lineage(self, work, seen):
key = _work_key(work)
- for record in list(self.records.values()):
- if record['id'] in seen:
- continue
- if record.get('output') and _work_key(record['output']) == key:
+ receipt = self._receipts().get(key)
+ if receipt and receipt[0] not in seen:
+ identity, kind = receipt
+ record = self.records.get(identity)
+ outside = record is None
+ if outside:
+ # A receipt older than the listed ones is only read, never listed or protected as a recent chain.
+ rows = store.load_all(self.store_dir, identity=identity)
+ if not rows:
+ return 1, False
+ record = rows[0]
+ if kind == 'output' and record.get('output') and _work_key(record['output']) == key:
+ if 'output_parts' in record and 'output_short' in record:
+ return record['output_parts'], record['output_short']
names = [row['id'] for row in store.leading_blocks(record)[:record.get('output_blocks') or None]]
else:
- names = [row['id'] for row in record['blocks'] if row['output'] and _work_key(row['output']) == key]
+ names = [row['id'] for row in record['blocks'] if row.get('output') and _work_key(row['output']) == key]
if names:
- lineage = self._lineage(record, seen)
+ try:
+ lineage = self._lineage(record, seen)
+ except (KeyError, ValueError):
+ if not outside:
+ raise
+ return 1, False # its own sources are older still and cannot be read: counted as before
return max(lineage[name][0] for name in names), any(lineage[name][1] for name in names)
return 1, False
+ def work_lineage(self, work):
+ """Return (parts, short, known) for an output-relative saved work path."""
+ if not isinstance(work, str) or not work or '\x00' in work:
+ raise ValueError('A work path is required')
+ key = _work_key(work)
+ with self.lock:
+ if key not in self._receipts():
+ return 1, False, False
+ parts, short = self._work_lineage(work, ())
+ return parts, short, True
+
def _check_parts(self, record):
"""Refuses, before anything is written or queued, a chain with a continuing part too short or a video
continued through too many parts."""
@@ -669,7 +716,9 @@ class ChainRunner:
return final_state
if len(blocks) == 1 and record.get('original_audio') is None:
output = blocks[0]['output']
- record.update(output=output, output_blocks=1 if output else 0, output_state='done', output_error=None)
+ parts, short = self._lineage(record)[blocks[0]['id']]
+ record.update(output=output, output_blocks=1 if output else 0, output_state='done', output_error=None,
+ output_parts=parts, output_short=short)
return final_state
if record['output_state'] == 'done' and record['output_blocks'] == len(blocks):
return final_state
@@ -966,8 +1015,13 @@ class ChainRunner:
record.update(state=final_state, output_state='failed', output_error=failure)
logging.warning('[FreeVideo] Chain joining failed: %s', identity)
else:
- record.update(state=final_state, output=output, output_blocks=len(store.leading_blocks(snapshot)),
- output_state='done', output_error=None, output_seams=dict(expected=promised, kept=kept))
+ # Only the blocks the joined video is made of count, as `_work_lineage` counts them for a record without these fields.
+ lineage, leading = self._lineage(snapshot), store.leading_blocks(snapshot)
+ output_parts = max((lineage[row['id']][0] for row in leading), default=1)
+ output_short = any(lineage[row['id']][1] for row in leading)
+ record.update(state=final_state, output=output, output_blocks=len(leading),
+ output_state='done', output_error=None, output_seams=dict(expected=promised, kept=kept),
+ output_parts=output_parts, output_short=output_short)
if record['state'] not in store.ACTIVE:
self._release_active(identity)
self._save(record)
diff --git a/freevideo_engine/chain_store.py b/freevideo_engine/chain_store.py
index 6177fc8..42d1028 100644
--- a/freevideo_engine/chain_store.py
+++ b/freevideo_engine/chain_store.py
@@ -254,7 +254,7 @@ def save(directory, record):
temporary.unlink(missing_ok=True)
-def load_all(directory, limit=200, *, identity=None):
+def load_all(directory, limit=200, *, identity=None, visitor=None):
if identity is not None and (not isinstance(identity, str) or not re.fullmatch(r'c-[0-9a-f]{12}', identity)):
return []
records = []
@@ -265,7 +265,9 @@ def load_all(directory, limit=200, *, identity=None):
except OSError:
logging.warning('[FreeVideo] Skipping inaccessible chain file: %s', path.name)
paths.sort(key=lambda row: (row[0], row[1].name), reverse=True)
- for _, path in paths[:limit]:
+ for position, (_, path) in enumerate(paths):
+ if limit is not None and position >= limit and visitor is None:
+ break
try:
record = json.loads(path.read_text(encoding='utf-8'))
if not isinstance(record, dict) or record.get('version') != 1:
@@ -283,6 +285,11 @@ def load_all(directory, limit=200, *, identity=None):
if (type(record['output_blocks']) is not int or not 0 <= record['output_blocks'] <= len(record['blocks'])
or record['output'] is None and record['output_blocks'] != 0):
raise ValueError('Invalid chain output block count')
+ if ('output_parts' in record) != ('output_short' in record):
+ raise ValueError('Invalid output lineage')
+ if 'output_parts' in record and (type(record['output_parts']) is not int or record['output_parts'] < 1
+ or type(record['output_short']) is not bool):
+ raise ValueError('Invalid output lineage')
split = record.setdefault('split', None)
if split is not None and (not isinstance(split, dict) or set(split) != {'state', 'reason'}
or split['state'] not in SPLIT_STATES
@@ -361,7 +368,10 @@ def load_all(directory, limit=200, *, identity=None):
for key in ('created', 'updated'):
if type(record[key]) not in (int, float) or not math.isfinite(record[key]):
raise ValueError('Invalid chain timestamp')
- records.append(record)
+ if visitor is not None:
+ visitor(record)
+ if limit is None or position < limit:
+ records.append(record)
except (OSError, ValueError, TypeError, KeyError, OverflowError):
# Keep the file for inspection; never log its workflow or sentences.
logging.warning('[FreeVideo] Skipping invalid chain file: %s', path.name)
diff --git a/freevideo_engine/comfy_chain.py b/freevideo_engine/comfy_chain.py
index 210fcc5..5d15cfb 100644
--- a/freevideo_engine/comfy_chain.py
+++ b/freevideo_engine/comfy_chain.py
@@ -413,6 +413,24 @@ def register(host_factory=None):
except ValueError as failure:
return response({'error': str(failure), 'code': 'invalid'}, 400)
+ @server.routes.post('/freevideo/chain/lineage')
+ async def lineage(request):
+ try:
+ value = await body(request)
+ works = value.get('works')
+ if set(value) != {'works'} or not isinstance(works, list) or len(works) > 16:
+ raise ValueError('works must be an array of at most 16 work references')
+ result = []
+ for work in works:
+ if not isinstance(work, dict) or set(work) != {'work'} or not isinstance(work['work'], str):
+ raise ValueError('Each work reference must contain a work path')
+ chain_store.reference(work)
+ parts, short, known = runner.work_lineage(work['work'])
+ result.append(dict(parts=parts, short=short, known=known))
+ return response({'lineage': result})
+ except ValueError as failure:
+ return response({'error': str(failure)}, 400)
+
@server.routes.post('/freevideo/chain')
async def create(request):
try:
diff --git a/web/editor.js b/web/editor.js
index 954043f..e3598cf 100644
--- a/web/editor.js
+++ b/web/editor.js
@@ -50,9 +50,11 @@ export const wellFormed = text => typeof text === 'string' ? text.replace(/[\ud8
export const characters = text => Array.from(text).length;
export const textLongText = (t, limit) => t('The description is too long. Shorten it to %d characters or fewer.', '描述过长,请缩短到 %d 字以内。').replace('%d', limit);
// The engine's limits on continuing parts, refused before a generation starts; the engine says the same.
-export const PARTS_LIMIT_CODES = ['chain_too_long', 'chain_part_too_short', 'chain_short_parts'];
+export const PARTS_LIMIT_CODES = ['chain_too_long', 'chain_part_too_short', 'chain_short_parts', 'chain_source_full'];
export function partsLimitText(code, t = en => en, limit = null) {
const {followSeconds: least, fullSeconds: full, maxParts, shortMaxParts} = model.RULES;
+ if (code === 'chain_source_full') return t('This video already has the maximum of ' + maxParts + '\u00a0parts and cannot be extended.',
+ '这个视频已达 ' + maxParts + '\u00a0段上限,无法再接着生成。');
if (code === 'chain_part_too_short') return t(`Each continued part must be at least ${least} seconds. Make it longer.`, `接着生成的每一段至少 ${least} 秒,请加长这一段。`);
if (code === 'chain_short_parts') { const most = limit ?? shortMaxParts; return t(`With a part shorter than ${full} seconds, one continuous shot can have up to ${most} parts. Make every part at least ${full} seconds long, or use fewer parts.`,
`有短于 ${full} 秒的段时,一个连续镜头最多 ${most} 段。请把每段加长到至少 ${full} 秒,或减少段数。`); }
@@ -834,6 +836,7 @@ export function openEditor({api, app, from = null, panel = {}, components: given
});
}
const metadata = store.sources.get(sourceKey(value.source));
+ if (metadata?.path && value.source?.kind !== 'import') void store.lineage([{key: sourceKey(value.source), reference: {work: metadata.path}}]).then(() => hintFullSource()).catch(() => {});
try { state.apply(t('Insert clip', '放入素材'), record => model.insertSource(record, {
...value, width: metadata?.width ?? value.width, height: metadata?.height ?? value.height, text: value.text ?? metadata?.text,
at: value.at ?? model.duration(record)}), {clearRange: state.selection !== initialSelection});
@@ -963,9 +966,18 @@ export function openEditor({api, app, from = null, panel = {}, components: given
const paths = Object.fromEntries([...store.sources].map(([key, value]) => [key, value.path]));
const request = model.generationRequest(prepared, ids, {text: texts ? undefined : text, paths, queue: true});
// What the engine would refuse for its continuing parts is said here, before anything is sent.
- const problem = model.partsProblem(prepared, ids);
- if (problem) throw Object.assign(new Error(problem.code), {code: problem.code, limit: problem.limit});
- const beforeKeys = model.generationBefore(prepared, ids), sizes = new Map(store.sources);
+ const beforeKeys = model.generationBefore(prepared, ids);
+ const lineageWorks = beforeKeys.filter(key => key && !key.startsWith('chain:')).map(key => {
+ const metadata = store.sources.get(key);
+ return metadata?.path ? {key, reference: {work: metadata.path}} : null;
+ }).filter(Boolean);
+ if (lineageWorks.length) await store.lineage(lineageWorks);
+ const problem = model.partsProblem(prepared, ids, {sourceLineage: store.knownLineage()});
+ if (problem) {
+ const full = problem.code === 'chain_too_long' && beforeKeys.some(key => store.knownLineage()[key]?.parts >= model.RULES.maxParts);
+ throw Object.assign(new Error(full ? 'chain_source_full' : problem.code), {code: full ? 'chain_source_full' : problem.code, limit: problem.limit});
+ }
+ const sizes = new Map(store.sources);
for (const key of beforeKeys.filter(key => key?.startsWith('chain:'))) {
const job = await store.chain(key.split(':')[1]);
sizes.set(key, job?.generation_size);
@@ -1327,7 +1339,22 @@ export function openEditor({api, app, from = null, panel = {}, components: given
updateTransport();
const stopSources = store.subscribeSources(() => { player.refresh(); tracks?.refresh?.(); pictureTools.update(); updateTop(); });
let capabilitySize = `${state.record.width}x${state.record.height}`;
+ // The reason, said as soon as the selected continuation follows a video that already has the most parts, once per selection.
+ let hintedFor = '';
+ function hintFullSource() {
+ const ids = state.selection?.kind === 'clip' ? state.selection.ids : [];
+ const signature = ids.join(',');
+ if (!ids.length) { hintedFor = ''; return; }
+ if (signature === hintedFor) return;
+ try {
+ const known = store.knownLineage();
+ if (model.partsProblem(state.record, ids, {sourceLineage: known})?.code !== 'chain_too_long'
+ || !model.generationBefore(state.record, ids).some(key => known[key]?.parts >= model.RULES.maxParts)) return;
+ } catch { return; }
+ hintedFor = signature; genbar?.say(partsLimitText('chain_source_full', t));
+ }
const unsubscribe = state.subscribe(event => {
+ if (event.type === 'selection') hintFullSource();
if (event.type === 'record') {
const size = `${state.record.width}x${state.record.height}`;
if (!initializing && size !== capabilitySize) {
@@ -1494,6 +1521,10 @@ export function openEditor({api, app, from = null, panel = {}, components: given
if (from?.work) {
const work = from.work;
const id = work.id || work.video?.replace(/^FreeVideo\//, '').replace(/\/video\.mp4$/, '');
+ void store.getSources([`work:${id}`]).then(() => {
+ const source = store.sources.get(`work:${id}`);
+ if (source?.path) void store.lineage([{key: `work:${id}`, reference: {work: source.path}}]);
+ }).catch(() => {});
record = await store.byWork(id);
if (!record) {
const key = `work:${id}`; await store.getSources([key]); const metadata = store.sources.get(key), g = work.geometry || {};
@@ -1579,6 +1610,25 @@ export function openEditor({api, app, from = null, panel = {}, components: given
if (from?.work && from.extend) {
const id = from.work.id || from.work.video?.replace(/^FreeVideo\//, '').replace(/\/video\.mp4$/, '');
const epoch = projectEpoch;
+ const showFullSourceHint = () => {
+ const key = `work:${id}`;
+ void store.getSources([key]).then(() => {
+ const metadata = store.sources.get(key);
+ return metadata?.path ? store.lineage([{key, reference: {work: metadata.path}}]) : null;
+ }).then(values => {
+ if (closing || disposed || epoch !== projectEpoch) return;
+ if (!values) return;
+ const history = values.get(key);
+ if (!history || history.known !== true || history.parts < model.RULES.maxParts) return;
+ const clips = state.record.tracks[0].clips, source = clips.find(clip => sourceKey(clip.source) === key);
+ if (!source) return;
+ const after = clips.filter(clip => clip.pending === 'draft' && clip.start >= source.start + source.frames
+ && model.continuesBefore(state.record, clip.id));
+ if (!after.length) return;
+ const problem = model.partsProblem(state.record, after.map(clip => clip.id), {sourceLineage: store.knownLineage()});
+ if (problem?.code === 'chain_too_long') genbar?.say(partsLimitText('chain_source_full', t));
+ }).catch(() => {});
+ };
// Selects the work and, when nothing follows it yet, adds one minimum draft. Runs once; when the
// generation probe failed this time it runs after a later probe succeeds.
const enter = (select = true) => {
@@ -1590,11 +1640,12 @@ export function openEditor({api, app, from = null, panel = {}, components: given
const after = state.record.tracks[0].clips.filter(other => other.id !== clip.id && other.start >= edge);
const waiting = after.find(other => other.pending === 'draft');
if (select) state.select({kind: 'clip', ids: model.pair(state.record, (waiting || clip).id).map(member => member.id)});
- if (after.length || !clip.extend || !clip.source || store.sources.get(sourceKey(clip.source))?.missing) return true;
+ if (after.length || !clip.extend || !clip.source || store.sources.get(sourceKey(clip.source))?.missing) { showFullSourceHint(); return true; }
if (!store.generationAvailable) return false;
const spare = Math.min(...model.pair(state.record, clip.id).map(member => member.length - member.in - member.frames));
tracks.extendClip(clip.id, spare + model.defaultContinueSeconds(capability.maxFollowSeconds,
{short: store.continueSeconds != null && store.continueSeconds < model.RULES.fullSeconds}) * model.FPS, {silent: true});
+ showFullSourceHint();
return true;
};
await store.generationReady();
diff --git a/web/editor_model.js b/web/editor_model.js
index 8439c36..a61d40b 100644
--- a/web/editor_model.js
+++ b/web/editor_model.js
@@ -1078,12 +1078,15 @@ function madeLineage(record, clip) {
// part shorter than followSeconds, a video continued through more than maxParts parts, or one with a short
// continuing part through more than partsLimit allows. Parts made before the request count as the timeline shows
// them. Returns {code, parts, limit} for the first problem, or null.
-export function partsProblem(record, ids, {firstFrame = record.settings?.first_frame_reference === true} = {}) {
+export function partsProblem(record, ids, {firstFrame = record.settings?.first_frame_reference === true, sourceLineage = {}} = {}) {
const rows = generationPlan(record, generationClips(record, ids));
if (rows.some(row => row.before && row.request_frames < MIN_PART_FRAMES)) return {code: 'chain_part_too_short', parts: null, limit: RULES.followSeconds};
const lineage = new Map();
for (const row of rows) {
- const previous = !row.before ? {parts: 0, short: false} : lineage.get(row.before.id) ?? madeLineage(record, row.before);
+ const inherited = row.before?.source ? sourceLineage[`${row.before.source.kind}:${row.before.source.id}`] : null;
+ const known = inherited && inherited.known !== false && Number.isInteger(inherited.parts) && inherited.parts > 0;
+ const previous = !row.before ? {parts: 0, short: false} : lineage.get(row.before.id)
+ ?? (known ? {parts: inherited.parts, short: inherited.short === true} : madeLineage(record, row.before));
lineage.set(row.clip.id, {parts: previous.parts + 1, short: previous.short || !!row.before && row.request_frames < FULL_PART_FRAMES});
}
const values = [...lineage.values()], parts = Math.max(...values.map(value => value.parts));
diff --git a/web/editor_store.js b/web/editor_store.js
index c95a28a..8762297 100644
--- a/web/editor_store.js
+++ b/web/editor_store.js
@@ -174,7 +174,7 @@ export function mediaURL(api, value) {
}
export function createEditorStore(api, {t = en => en, notify = () => {}, onGenerationReady = () => {}, onGenerationChecking = () => {}, clock = clockDefault, pollMs = 2000, standIn: injected = null} = {}) {
- const sources = new Map(), sourceReads = new Map(), watchers = new Set(), pollers = new Set(), once = new Set();
+ const sources = new Map(), sourceReads = new Map(), lineage = new Map(), lineageReads = new Map(), watchers = new Set(), pollers = new Set(), once = new Set();
let recovered = null, capabilityPoll = null;
// The length a new continuation starts with where the engine names one (a device that continues in short parts).
let continueSeconds = null;
@@ -381,6 +381,34 @@ export function createEditorStore(api, {t = en => en, notify = () => {}, onGener
}
await sourceReads.get(signature); return sources;
},
+ lineage(works) {
+ const unique = new Map(works.filter(work => work && typeof work.key === 'string' && work.reference && !lineage.has(work.key))
+ .map(work => [work.key, work]));
+ const entries = [...unique.values()];
+ if (!entries.length) return Promise.resolve(lineage);
+ const pending = entries.filter(work => !lineageReads.has(work.key));
+ for (let offset = 0; offset < pending.length; offset += 16) {
+ const batch = pending.slice(offset, offset + 16), keys = batch.map(work => work.key);
+ const task = (async () => {
+ try {
+ const value = await json('/freevideo/chain/lineage', post({works: batch.map(work => work.reference)}), {lineage: []});
+ batch.forEach((work, index) => {
+ const item = value?.lineage?.[index];
+ lineage.set(work.key, item && typeof item.known === 'boolean'
+ ? {parts: item.parts, short: item.short, known: item.known}
+ : {parts: 1, short: false, known: false});
+ });
+ } catch {
+ for (const work of batch) lineage.set(work.key, {parts: 1, short: false, known: false});
+ /* Unknown history preserves the editor's existing preflight behavior. */
+ }
+ finally { for (const key of keys) lineageReads.delete(key); }
+ })();
+ for (const key of keys) lineageReads.set(key, task);
+ }
+ return Promise.all(entries.map(work => lineageReads.get(work.key)).filter(Boolean)).then(() => lineage);
+ },
+ knownLineage() { return Object.fromEntries(lineage); },
subscribeSources(fn) { watchers.add(fn); return () => watchers.delete(fn); },
async waveform(key, points = 200) {
return (await json('/freevideo/editor/waveform?' + new URLSearchParams({key, points}), undefined, {peaks: []})).peaks || [];