Batching image data based on timestamp contained in filename

I have a dataset of images in keras-style:

main_directory/
...class_a/
......a_image_1.jpg
......a_image_2.jpg
...class_b/
......b_image_1.jpg
......b_image_2.jpg

I create a dataset object by calling

tf.keras.preprocessing.image_dataset_from_directory(train_dir,
                                             shuffle=False,
                                             label_mode='categorical',
                                             batch_size=hyperparameters["BATCH_SIZE"],
                                             image_size=IMG_SIZE)

Then i unbatch it either by applying tf.data.Dataset.unbatch() or tf.data.Dataset.flat_map(lambda x,y: tf.data.Dataset.from_tensor_slices(x))

I need to batch this data in windows of “n” temporally-consecutive elements (for ease, let’s say 3 elements, although the exact number isn’t decided yet). The images come from a set of videos, so i want batches of 3 images, each image 1 second apart. If there’s more than 1 second between at least 2 consecutive frames in the window, i want to drop that whole window
I’ve been trying to figure out how to do it, and have tried a number of approaches, but i have been unable to do what i need
I tried:
use the tf.data.Dataset API and use the .window() method. This creates the dataset of windows, but resulting dataset loses the .file_paths property, meaning there is no way for me to check whether the elements in a window belong together or not
use the .map() function, didn’t manage to make it work with the dataset i have

I have also converted my dataset to TFRecord format, using this script, but i don’t really understand how to load the data and batch it

any help is greatly appreciated