Write a python code that lists all audio files in a directory in seconds.
- User
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.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →