Skip to content

Instantly share code, notes, and snippets.

@petzku
Last active November 19, 2021 11:43
Show Gist options
  • Save petzku/7a70f992e05a9053353d131c537df326 to your computer and use it in GitHub Desktop.
Save petzku/7a70f992e05a9053353d131c537df326 to your computer and use it in GitHub Desktop.
""" Diff video sources to catch infoboxes and other signs.
Usage: $ python diff.py <clean> <hardsubbed> [start-offset]
`start-offset` should be the number of frames to drop from start to align the two video sources.
Positive values drop frames from `clean`, negative from `hardsubbed`.
If left out, automatically deduce the proper offset.
Outputs frame ranges to stdout, and a diffing clip to "out.y4m".
Requires vapoursynth and lvsfunc, both available from PyPi (shouldn't require others? idk)
"""
import vapoursynth as vs
import lvsfunc
import sys
core = vs.core
print(f"!! args: {sys.argv}")
if len(sys.argv) < 3:
raise ValueError("Give at least the two filenames!")
sys.exit(1)
fclean, fsigns, sync, *_ = sys.argv[1:] + [None]
clean = lvsfunc.src(sys.argv[1])
signs = lvsfunc.src(sys.argv[2])
# verify that sources line up...
# assume the only thing to sync is removing frames at start
if sync is None:
# try to align automatically
sync = len(clean) - len(signs)
print(f"!! automatically syncing: {sync} ({len(clean)} - {len(signs)})")
if sync < 0:
signs = signs[-sync:]
else:
clean = clean[sync:]
# fix resolution differences, if applicable, by rescaling to a common resolution
clean = clean.resize.Bicubic(signs.width, signs.height, signs.format)
print(clean)
print(signs)
# tweak `thr` to adjust threshold. higher catches smaller diffs
# 72 should catch everything, but going up to ~96 can help (but will start to catch artifacts)
comp, ranges = lvsfunc.comparison.diff(clean, signs, return_ranges=True, thr=72)
print("differences on ranges: {}".format(", ".join(f"{a}-{b}" for a,b in ranges)))
# comp.set_output()
with open('out.y4m', 'wb') as fo:
comp.output(fo, y4m=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment