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.

Voice change: dubbed lines keep the original pace and loudness: the tempo change closes a line...

Bowen Xue1496a1b5019f
diff --git a/freevideo_engine/agent/voice.py b/freevideo_engine/agent/voice.py
index ffd3174..fc3602d 100644
--- a/freevideo_engine/agent/voice.py
+++ b/freevideo_engine/agent/voice.py
@@ -471,6 +471,14 @@ def _measure(audio, first, last):
return (low + span[0]) / RATE, (low + span[1]) / RATE
+def _level(audio, first, last):
+ """RMS of the sound in [first, last) of a mono 44.1 kHz file: the loudness the new voice is to come back with."""
+ import numpy as np
+ low, high = max(0, int(first * RATE)), min(len(audio), int(last * RATE))
+ part = np.asarray(audio[low:high], dtype=np.float64)
+ return round(float(np.sqrt(np.mean(np.square(part)))), 6) if len(part) else None
+
+
def _windows(every, todo, spans, base, limit):
"""Where each changed line's new voice goes: the original line plus a little quiet each side, and how far it may reach."""
windows, failures = {}, {}
@@ -571,6 +579,9 @@ def run_voice_replace(jobs, row, *, poll=.25):
window = windows[line['id']]
made = dict(key=line['id'], text=line['text'].strip(), seconds=round(length, 3),
onset=round(window['onset'] - window['first'], 3), offset=round(offset - window['first'], 3))
+ level = _level(mono, windows[line['id']]['onset'], windows[line['id']]['offset'])
+ if level:
+ made['level'] = level
if own and line['end'] - line['start'] >= 1. and line['original_text'].strip():
made['reference'] = dict(file=str(original_wav), text=line['original_text'].strip(),
start=base + line['start'], end=base + line['end'])
diff --git a/freevideo_engine/dubbing/align.py b/freevideo_engine/dubbing/align.py
index 5c3ec0d..355b04c 100644
--- a/freevideo_engine/dubbing/align.py
+++ b/freevideo_engine/dubbing/align.py
@@ -180,14 +180,20 @@ def fit(synth, target, *, onset=0., offset=None, tolerance=TOLERANCE, max_stretc
tried.append((speed, voice, rate, seconds(voice, rate), edges))
return tried[-1]
+ # A line that is too long is first closed by the tempo change, which keeps the voice natural; the model
+ # speed only takes what the tempo change cannot (a faster model speed costs naturalness quickly).
+ # A line that is too short is slowed by the tempo change in the same way, inside the same range.
+ def aim(length):
+ return min(max(length, wanted / (max_stretch * .96)), wanted * max_stretch * .96) # inside this range no model speed is needed
+
def off(entry):
- return abs(entry[3] - wanted) / wanted
+ return abs(entry[3] - aim(entry[3])) / wanted
speed = 1.0
entry = run(speed)
while off(entry) > tolerance and len(tried) < 3:
# the length scales with 1/speed, so the next speed is the measured ratio
- speed = min(max(speed * entry[3] / wanted, low), high)
+ speed = min(max(speed * entry[3] / aim(entry[3]), low), high)
if speed == tried[-1][0]:
break
entry = run(speed)
diff --git a/freevideo_engine/dubbing/service.py b/freevideo_engine/dubbing/service.py
index a0ae7b2..0c36c65 100644
--- a/freevideo_engine/dubbing/service.py
+++ b/freevideo_engine/dubbing/service.py
@@ -90,6 +90,11 @@ def request(value):
raise ValueError('invalid_request')
if line.get('reference') is not None:
row['reference'] = reference_of(line['reference'], required=True)
+ level = line.get('level')
+ if level is not None:
+ if isinstance(level, bool) or not isinstance(level, (int, float)) or not 0 < level <= 1:
+ raise ValueError('invalid_request')
+ row['level'] = float(level)
instruction = line.get('instruction')
if instruction not in (None, ''):
if not isinstance(instruction, str) or len(instruction) > 200:
diff --git a/freevideo_engine/dubbing/worker.py b/freevideo_engine/dubbing/worker.py
index 8bca359..7ce212e 100644
--- a/freevideo_engine/dubbing/worker.py
+++ b/freevideo_engine/dubbing/worker.py
@@ -181,11 +181,13 @@ def run(request, model_dir, device='cuda'):
used = []
match = {} # per line with its own reference: the voiced RMS the line is to come back with
for index, line in enumerate(lines):
+ if line.get('level'):
+ match[index] = float(line['level'])
own = line.get('reference')
if own:
try:
used.append(('line',) + prepare(own, 'line%d' % index) + (cut['reference-line%d.wav' % index], None))
- match[index] = levels.get('line%d' % index)
+ match.setdefault(index, levels.get('line%d' % index))
continue
except ValueError as error:
if str(error) != 'reference_short' or job_ref is None: