But so what is the thing holding you back?
My strategy would be to have a preprocessing step that collects information like length in a CSV or so and then you can sort on the length column. Personally, I like to lazily use pandas for doing things with the metadata. If your data has all the same sample frequency you can just use file size and scan your files when initializing the dataset.
With that information, you can group the data to form batches.