Last active
May 22, 2017 18:10
-
-
Save zplizzi/3e8ad7851a45122fd733ef20e0f80f1d to your computer and use it in GitHub Desktop.
Code for aligning two videos with different framerates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
duration = 60*60 | |
num_frames = 30* duration | |
error_percent = .02 | |
a = np.linspace(0, duration, num_frames) | |
b = np.linspace(0, duration, num_frames * (1 - error_percent)) | |
def closest_sorted(l, x): | |
"""Returns the index of the closest value in an ascending sorted list l to x.""" | |
# Get the index of the value on the right side of the zero crossing | |
i = np.searchsorted(l, x) | |
if i > 0 and abs(l[i] - x) > abs(l[i-1] - x): | |
# The item on the left of the zero crossing exists and was smaller | |
return i-1 | |
else: | |
return i | |
def create_alignment_mapping(a, b): | |
"""Given two lists of timestamps, return a new list with the index | |
of the closest item in b for each value in a.""" | |
return [closest_sorted(b, t) for t in a] | |
test = create_alignment_mapping(a, b) | |
print(test[1000]) # should return 980 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment