Recursively read (audio) files from directories


I am working with the AudioServer media loader, and am looking for a way to load files from a directory recursively. My setup closely follows this example, but has the audio files stored in sub directories.
Is there any way to load the files from there, recursively (via their file extension)?

Generally, I found this function to be important, so might be a good feature anyway!

Answering my own question:
The solution lies in using a combination of plain lists and the fetch_media preprocessor:

    def get_stream_custom():
        files = [{"audio": file_path, "meta": {"file": file_path}} for file_path in
                 glob.glob(os.path.join(source, "**/*.flac"), recursive=True)]
        stream = fetch_media(files, ["audio"])
        for eg in stream:
            eg["options"] = [
                {"id": "CAR", "text": "car"},
                {"id": "PLANE", "text": "PLANE"},
                {"id": "OTHER", "text": "Other / Unclear"}
            yield eg

This function replaces the get_stream() function in the example I linked.

1 Like

Happy to hear you found a solution!