CRICKET / code /sync_videos.py
d9993r's picture
Add files using upload-large-folder tool
14c58d8 verified
Raw History Blame Contribute Delete
1.67 kB
#!/usr/bin/env python3
"""Find the time offset between two recordings of the same session from their sound
(e.g. two phones filming one nets session; clap once at the start). Records the offset
so multi-view work can line the videos up (standard section 2: synchronisation).
python code/sync_videos.py datasets/phone/behind.mp4 datasets/phone/side.mp4"""
import json
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent))
from audio import av_offset # noqa: E402
from common import absp, load_config, rel # noqa: E402
def main():
if len(sys.argv) != 3:
raise SystemExit(__doc__)
a, b = (Path(x) if Path(x).is_absolute() else absp(x) for x in sys.argv[1:])
res = av_offset(a, b)
if res is None:
raise SystemExit("one of the videos has no audio track")
shift = -res["offset_s"] # time in A = time in B + shift
res["b_to_a_shift_s"] = round(shift, 3)
when = "after" if shift >= 0 else "before"
print(f"{rel(b)} started recording {abs(shift):.3f} s {when} {rel(a)} (confidence {res['confidence']})")
print(f" to line them up: time in {rel(a)} = time in {rel(b)} {'+' if shift >= 0 else '-'} {abs(shift):.3f} s")
if res["confidence"] < 0.3:
print(" ! low confidence: record a sharp clap visible to both cameras and try again")
R = load_config()["_paths"]["results"]
p = R / "registry" / "sync_offsets.json"
data = json.load(open(p)) if p.exists() else {}
data[f"{rel(a)}|{rel(b)}"] = res
p.parent.mkdir(parents=True, exist_ok=True)
json.dump(data, open(p, "w"), indent=2)
if __name__ == "__main__":
main()