Saturday, August 9, 2025

 Regaining chronicle in video indexing.

The previous articles examined various cloud service APIs and dedicated web services to extract gps coordinates (latitude, longitude) with varying degrees of success. This article describes how to regain the timestamps post video indexing so that it can be added as a time field for extracted frames and uploaded in the document to a vector store.

def repeat_video_index(access_token, video_id):

    """Retrieve the index/insights for a video by its ID."""

    url = f"{video_indexer_endpoint}/{video_indexer_region}/Accounts/{video_indexer_account_id}/Videos/{video_id}/ReIndex?accessToken={access_token}"

    response = requests.put(url)

    if response.status_code == 200:

        return response

    return get_video_insights(access_token, video_id)

def get_video_insights(access_token, video_id):

    url = f"{video_indexer_endpoint}/{video_indexer_region}/Accounts/{video_indexer_account_id}/Videos/{video_id}/Index?accessToken={access_token}"

    count = 0

    while True:

        response = requests.get(url)

        data = response.json()

        if "state" in data and data['state'] == 'Processed':

            return data

        count+=1

        if count%10 == 0:

            print(data)

        print("Sleeping for ten seconds...")

        time.sleep(10) # Wait 10 seconds before checking again

def get_timestamps(access_token, video_id):

    # Main workflow

    # access_token = get_access_token()

    insights = get_video_insights(access_token, video_id)

    #pprint(insights)

    timestamps=[]

    for keyframe in insights['videos'][0]['insights']['shots'][0]['keyFrames']:

        timestamps+=[(keyframe['instances'][0]['start'], keyframe['instances'][0]['end'])]

    print(timestamps)

    return timestamps

# [('0:00:00.2219828', '0:00:00.2570043'), ('0:00:00.5030307', '0:00:00.5379849'), ('0:00:00.8780307', '0:00:00.9249731')]


No comments:

Post a Comment