Mart R
Mart R

Reputation: 126

How to make MatLab to write a txt with a list of files and their durations?

I have a folder containing several wav files and wanted to make MatLab to go through all of them and write their duration on a txt file. Anyone has a clue how to do it? I already know how to get the the duration for one single file:

[w,fs] = wavread('filename.wav');
length = length(w)/fs;

but I cannot figure out how to make the loop to read all files on a folder.

Any help appreciated! Thanks!

Upvotes: 1

Views: 77

Answers (1)

IKavanagh
IKavanagh

Reputation: 6187

I haven't tested this but something like this should help get you started or most of the way.

fid = fopen('durations.txt'); % File to save durations to
files = dir('*.wav'); % Creates a struct containing details of each file

for i = 1:length(files)
    [w,fs] = wavread(files(i).name); % files(i).name is the filename
    duration = length(w)/fs;
    fprintf(fid, 'File %s is %.4f seconds long\n', files(i).name, duration); % Writes one line to the file
end

Upvotes: 4

Related Questions