prompter: Write a python code that lists all audio files in a directory in seconds.

assistant: Here's a python code that lists all audio files in a directory and displays their length in seconds:

import os
import wave
import contextlib

def get_audio_duration(filename):
    with contextlib.closing(wave.open(filename, 'r')) as f:
        frames = f.getnframes()
        rate = f.getframerate()
        duration = frames / float(rate)
        return duration

directory = '/path/to/your/directory'
for filename in os.listdir(directory):
    if filename.endswith(".wav") or filename.endswith(".mp3"):
        filepath = os.path.join(directory, filename)
        duration = get_audio_duration(filepath)
        print(f"{filename}: {duration:.2f} seconds")

This code loops through all the files in the directory and prints the filename and its duration for audio files with the .wav or .mp3 extension.

prompter: I get an error when I have mp3 files but it works for wav files. Can you make it work for mp3 files too?

assistant: Sure, you can first list all the files then select the audio files.

Here is how to list all the files (the runtime will depend on how many files you have):

```python
from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
```

Now you can filter `onlyfiles` to only keep audio files, to do that tell me which extensions you expect (such as ".mp3", ".wav", etc.)

prompter: Can this be applied to any extention such as a jpg, bmp, txt? Or just music?

assistant: It can be applied to any set of files you wish as long as you know the extension. So if a text file has the somewhat more rare used extension asc, then you will not find it, if you are only looking for ".txt".

assistant: import glob

# list all mp3 files in a directory. The file extension can be changed to find other types of audio files too such as wav and ogg.
path = "/directory/*.mp3"
files = glob.glob(path)
print(files)

prompter: Can you make a more general code for UNIX systems, where files can have no extensions?

assistant: Here's a more general Python script that lists all audio files in a directory, including files with no extensions, on UNIX systems:

```python
import subprocess
import os

def list_audio_files(directory):
    audio_files = []
    for root, dirs, files in os.walk(directory):
        for file in files:
            full_path = os.path.join(root, file)
            result = subprocess.run(['file', '--mime-type', '-b', full_path], stdout=subprocess.PIPE)
            mime_type = result.stdout.decode().strip()
            if mime_type in ['audio/mpeg', 'audio/x-wav', 'audio/mp4', 'audio/x-m4a']:
                audio_files.append(full_path)
    return audio_files

directory = '/path/to/directory'
print(list_audio_files(directory))
```

This script uses the `os.walk` function to traverse the directory tree and the `os.path.join` function to construct the full path to each file. It then uses the `subprocess` module to run the `file` command, which returns the MIME type of the file. If the MIME type is one of the audio types listed, the script appends the full path to the audio file to the `audio_files` list. Finally, the function returns the list of audio files.

prompter: Add documentation to the code

assistant: ```python
import subprocess
import os

def list_audio_files(directory):
    """
    Lists all audio files in a directory.
    :param directory: the directory where files are stored.
    :returns: the list of files paths
    """
    audio_files = []
    for root, dirs, files in os.walk(directory):  # os.walk will iterate through all the folders recursively
        for file in files:
            full_path = os.path.join(root, file)
            result = subprocess.run(['file', '--mime-type', '-b', full_path], stdout=subprocess.PIPE)  # this calls a system command
                                                                                                       # returning the MIME type of the file
            mime_type = result.stdout.decode().strip()  # this cleans the MIME type for comparison
            if mime_type in ['audio/mpeg', 'audio/x-wav', 'audio/mp4', 'audio/x-m4a']:  # here are some frequent MIME type for audio files
                audio_files.append(full_path)
    return audio_files

directory = '/path/to/directory'
print(list_audio_files(directory))
```

assistant: ```
import glob
from ffprobe import FFProbe

# list all files in a directory.
path = "/directory/*"
files = glob.glob(path)

for file in files:
    # extract metadata from file
    metadata=FFProbe(file)

    for stream in metadata.streams:
        if stream.is_audio():
            print('Stream contains {} frames.'.format(stream.frames()))
```