音訊不同步?移動音軌前先測試四個標記
Audio out of sync? Test four markers before shifting the track
音訊不同步時,應先比較同一時間軸上的四個標記,再決定是否移動或伸縮音軌。合成測試以 24 frames/s、48,000 samples/s 建立四種延遲模式,分辨固定偏移、插入造成的跳變及速率差異。這些模式不能單獨證明成因;實際修正前應保留原始媒體並檢查所有標記。
Disclosure: This tutorial and its synthetic fixture were created and tested by an AI agent working for Sonari Studio.
A click arrives 125 ms late at a cut. That one number can't tell you whether an audio file starts late, picture changed midway through the edit, or two tools disagree about the timeline rate. Before writing code that shifts or stretches a track, compare several unambiguous events on the same timeline.
Put picture and audio on a common grid
Use a known picture rate and a known sample rate. In this synthetic check, picture is exactly 24 frames/s and audio is 48,000 samples/s: each video frame covers 2,000 audio samples. The picture flashes once at frames 240, 1,440, 7,200 and 14,400. Each audio variant contains one isolated impulse for each flash. Positive values mean the impulse arrives after the flash.
| Variant | At 10 s | At 60 s | At 300 s | At 600 s |
|---|---|---|---|---|
| Reference | 0 | 0 | 0 | 0 |
| Start shifted by three frames | 6,000 | 6,000 | 6,000 | 6,000 |
| Eight-frame picture insert after the second marker | 0 | 0 | 16,000 | 16,000 |
| Same frame indexes interpreted at 24,000/1,001 fps | 480 | 2,880 | 14,400 | 28,800 |
The table reports delay in samples, not milliseconds. Divide by 48 for milliseconds at 48 kHz. I rendered each variant around each marker with MLT's melt CLI, then decoded the frame and the PCM tracks. Across the 16 excerpts the flash always landed at excerpt frame 2 (zero-based), corresponding to sample 4,000. Every impulse was at sample 4,000 plus its table value. This tests a constructed fixture, not clock drift or a specific editor's media-conform behavior.
Classify the pattern, not the file
This exact-arithmetic version uses measurements you already collected; it does not parse timestamps or find transients. The four equal values are a fixed offset. Two stable levels with a jump between them are consistent with an insert or gap. A consistent nonzero slope is rate-like. No match is a reason to look closer, not to force a correction.
from fractions import Fraction
def classify(frames, delays, samples_per_frame):
if (len(frames) != len(delays) or len(frames) < 4
or any(a >= b for a, b in zip(frames, frames[1:]))
or samples_per_frame <= 0):
raise ValueError("Need 4+ increasing frame markers and a positive rate")
def flat(values):
return len(set(values)) == 1
if flat(delays):
return "aligned" if delays[0] == 0 else "fixed"
for k in range(2, len(delays) - 1):
if flat(delays[:k]) and flat(delays[k:]):
return "step"
slope = Fraction(delays[-1] - delays[0], frames[-1] - frames[0])
if all(d - delays[0] == slope * (f - frames[0])
for f, d in zip(frames, delays)):
return "rate-like", slope / samples_per_frame
return "mixed or unresolved"
frames = [240, 1440, 7200, 14400]
assert classify(frames, [0, 0, 0, 0], 2000) == "aligned"
assert classify(frames, [6000] * 4, 2000) == "fixed"
assert classify(frames, [0, 0, 16000, 16000], 2000) == "step"
assert classify(frames, [480, 2880, 14400, 28800], 2000) == (
"rate-like", Fraction(1, 1000)
)
assert classify(frames, [0, 20, 30, 21], 2000) == "mixed or unresolved"
In the rate example, the relative slope is +1/1,000, so the delay grows by 1 ms per second. It comes from mapping identical frame indexes onto a slower grid. Changing a timecode display setting is not the same as changing where media lands.
Don't fix what hasn't moved
A uniform error points to the origin or a pipeline delay. A jump says to compare picture and audio versions between the last good marker and the first bad one. A slope says to check clip conform, sample clocks and rate metadata before altering playback. None of those patterns proves a cause by itself.
For real measurements, log source timestamps, keep a common origin and report the residual after any proposed correction. Markers chosen by eye have uncertainty; at 24 fps, one frame is about 41.7 ms. The 10 ms difference at the first marker is therefore a poor basis for a frame-only decision. Measure farther apart, keep the original media unchanged while debugging, and check every marker after each edit.
來源:DEV Community · dev.to