Loading a sequence with varying length

Hi,
I have a csv file, each row is a sample (image). The difficulty is that they represent consecutive frames from different video clips, i.e.:

0a 0
0b 0
0c 0
0d 0
0e 0
0f 1
1a 0
1b 0
1c 0
1d 0
1e 1
2a 0
2b 0
2c 1

0, 1, 2 represents 3 distinct video clips with varying lengths (a to f, a to e, a to c). The last number on each row is a tag denoting the last frame of the video clip.

I would like to make sure that

  1. whenever I sample, it does not overlap with another clip.
  2. It never samples the last frame (sample is dismissed if it contains the last frame)
  3. I can sample with a varying length seq_length.

For example, assuming a batch size of 1 (and | denotes the next iteration):
if seq_length=1, it should return: 0a | 0b | 0c | 0d | 0e | 1a | 1b | … | 2b.
if seq_length=2, it should return: 0a - 0b | 0b - 0c | 0c - 0d | 0d - 0e | 1a - 1b | 1b - 1c | 1c - 1d | 2a - 2b.
if seq_length=3, it should return: 0a - 0b - 0c | 0b - 0c - 0d | 1a - 1b - 1c | 1b - 1c - 1d.
if seq_length=4, it should return: 0a - 0b - 0c - 0d | 0b - 0c - 0d - 0e | 1a - 1b - 1c - 1d.

For seq_length=1 it is easy to do, but I am getting confused in how to write it properly when seq_length>1. It’s also difficult to compute the length of the dataset beforehand. I would appreciate any help.