What does lipsync-audio do?
Makes one vocal window per shot for a lip-sync model. The rules come from Seedance experiments; other models are not verified. The tool uses only the Python standard library plus ffmpeg and ffprobe on PATH. Script messages are in English; report to the user in the user's language.
Run
- Tool:
scripts\lipsync_audio.pynext to this file. - Python: 3.7 or later, standard library only (tested with 3.14). On Windows a bare
pythoncan be the Microsoft Store stub;py -3is the python.org launcher. Run with-I -B -X utf8:-Ikeeps the work folder andPYTHON*variables out of the import path,-Bwrites no__pycache__,-X utf8keeps non-ASCII output readable on PowerShell 5.1. - Command:
py -3 -I -B -X utf8 <skill>\scripts\lipsync_audio.py --vocal <vocal wav> --shots <shots.json> --out <folder> [--mix <full mix wav>] [--dry-run] - Options:
--lead 0.7(seconds before the first sound),--noise -35(silence threshold in dB),--min-silence 0.2,--search 1.0,--tail 0.1,--pre-keep 0.05,--fade 0.02(0 is an instant mute). - Exit codes: 0 success (also with warnings); 2 input error; 1 an output file could not be written. Input errors include a shot with a detected line but no qualifying line sample that remains nonzero in the final vocal window (capped at 15 s, checked before any files are written and in a dry-run), an output path that would overwrite an input or another output (hard links too; checked in a dry-run as well when
--outis given), shot ids that collide after file-name conversion (ignoring case), astartafter the end of the audio, audio that ffmpeg cannot read or that makes ffmpeg exit non-zero (--mixtoo, also in a dry-run), and JSONtrue,falseor quoted strings as times. If qualifying source samples lie beyond the capped window, reduce the lead or split the shot. If the full line is below the threshold, check the shot times,--noiseand--min-silence. If the fade alone removes the remaining sound, reduce--fade. When ffmpeg exits 0 but prints decoding error lines, the run does not stop and reportsDECODE_ERRORS.
Shots JSON
For each shot give the rough start and end (seconds) of the lines sung in that shot. Add lead only to change the lead for one shot.
The sound inside [start, end) is the shot's line. Sound that crosses a boundary joins the line only when it ends (or starts) within --search of the boundary and is not inside another shot's range; otherwise it is cut at the given time. Where sound is continuous the given time is the cut, so when there is silence between lines, put the times inside that silence. The full rule is in README.md, "How a line is chosen".
{"shots": [
{"id": "v1", "start": 20.1, "end": 27.9},
{"id": "c1", "start": 62.1, "end": 70.0, "lead": 0.8}
]}
Order of work
- Find the lead vocal stem of the song (a separated or exported vocal track, wav preferred).
- Write the shots JSON from the rough start and end seconds of the lyric lines. A window is at most 15 s and the 0.7 s lead is inside it, so keep one shot's line (from first sound to line end) at 14.3 s or less.
- Run with
--dry-runfirst and read the warnings in the JSON. No files are written. - If short silences between lines are read as sound, raise
--noiseto -30 or -25 and dry-run again. - When the warnings are settled, run with
--outto cut. Add--mixto get the same windows from the full mix for compositing.
Output: <id>_vocal.wav (muted outside the line, exactly length_s seconds), <id>_mix.wav with --mix, and windows.json.
Warning codes and what to do
NO_LEAD_SILENCE: there was sound in the lead region before the first sound (less than 0.5 s of silence), and it was muted. Check that the previous line did not leak into the shot.LINE_END_IN_SOUND: sound continues past the line end for longer than--search, or into another shot's line, so it was cut at the given end. Recheck the end time, or keep it if intended.TOO_LONG: the line was really cut at the 15 s window end (reported even for one sample), and at least one qualifying line sample remains nonzero in the output. Split the shot in two.OTHER_LINE_IN_WINDOW: sound of another shot's line (ranges not overlapping) is inside this window and was muted. The message gives the times of the muted sound.OVERLAP_LINES: the[start, end)ranges of two shots share at least one sample. Sound in the shared part is not muted, but it is not heard if the window ends at 15 s or the shot isNO_SOUND. The message estimates audible spans in both shots and gives the times of the other line's rest when it is muted here. An individual shared sample can round to zero in a fade. Fix the boundaries unless you mean to use the same line in two shots.NO_ONSET_FOUND:startis inside continuous sound (that sound began more than--searchearlier, or in another shot's line), so it was cut atstart. Listen for a clipped first word and fixstart.ONSET_AMBIGUOUS: the start of the sound crossingstartwas taken as the first sound, so the window opens beforestart, but another sound starts inside the given range. If the earlier sound is the last word of the previous line, movestartto the time in the warning (then the later sound becomes the first sound).ONSET_FAR: the first sound is more than--searchafter the given start. Fixstartor adjust--noise.NO_SOUND: no detected sound interval inside the line, or the range is shorter than one sample and holds none. The output is all silence (sound below the threshold is muted too). Recheck the times or--noise. This shot gets no lead or length warnings.DECODE_ERRORS: ffmpeg exited 0 but printed decoding error lines. It is a file-level warning: it is in the top-levelwarningsofwindows.jsonand of the dry-run JSON, and on stderr asWARN - DECODE_ERRORS. Junk bytes at the end of a file (seen in some MP3 exports) lose no samples and can be ignored; damaged frames lose samples and shift every later time. Read the error lines and re-export the file if needed.PAST_END,NO_LEAD,LEAD_SHORT,LEAD_SHORTENED,LEAD_OUT_OF_RANGE: seeREADME.md.LEAD_SHORTmeans the window opens at the file start and the lead is short (the first sound is earlier than 0.5 s, the requested lead is under 0.5 s, and similar cases where a 0.5 s minimum lead cannot be kept).LEAD_SHORTENEDmeans the usable silence was shorter than the requested lead (0.05 s is kept from the end of an earlier sound), so the real lead is shorter; with a line that starts at 0 s it comes withNO_LEAD.
In windows.json the sample numbers under each shot's samples, with the top-level sample_rate, are authoritative. Second values are "sample number / sample rate" written with 6 decimals for reading. given_start and given_end in seconds are the values from the shots JSON rounded to 6 decimals, so read their sample numbers from samples.
Limits
- Only the first audio track is used. It is decoded once to 16-bit samples in memory, and time is "sample number / sample rate" (a given time t is the first sample at or after t). For wav this equals playback time. Files with timestamp gaps, a non-zero start time, encoder delay in compressed formats, or a sample-rate change inside the track can differ from playback time (see
README.md, "Time is the decoded sample"). - The whole input is kept in memory: about 144 MiB peak for a 5-minute 48 kHz stereo vocal, about 199 MiB with a
--mixof the same length. - The first sound is found by level only (the silencedetect rule computed directly on the samples). Breaths or reverb that meet the threshold count as sound, and detection joins silences shorter than
--min-silenceinto sound intervals. The final window must contain a sample within both the line and[keep_from, keep_to)whose source value is nonzero and meets the threshold in magnitude, and whose value in the same channel stays nonzero after the fade and 16-bit rounding. - Not verified by feeding the output to Seedance; only the synthetic audio tests pass (
tests\run_tests.py, silence checked on the output PCM samples). - On real lead vocal stems the default
--noise -35finds few silences between lines, so window accuracy is in practice the accuracy of the given times. With -25 the first sound was seen to be detected 0.2-0.3 s late in places (an observation, not confirmed by listening). - Seedance can drop about 0.3 s of leading silence and sing slower; matching that after generation is outside this tool.