how to derive wav file duration to put in a cell

1 reply [Last post]
Last seen: 3 years 5 weeks ago
Joined: 15 Mar 2012
Posts: 3

i'll be switching to libre office from open office soon on one of my main computers. i have a simple spreadsheet on that machine that is, basically, a list of wav files and their descriptions. i'd like to include a column for the file durations.  i already have a column that has the path to the file in the form of a hyperlink so that i can simply click to play the file. how would i derive the file durations and display them in a cell of the row that has each of these files?

for added info, the files are all the same format so that wouldn't be a variable. the only variable, as far as that goes, is that some are stereo and some are mono. when i enter the files into the list, though, i already include a column for mono/stereo so that could be used as a reference for a conditional statement since a stereo file would be twice the size of a mono one.

also, if it adds a lot of complication, the durations needn't be absolutely precise. i'd assume to get really precise numbers might mean drilling deeply into the files to be able to distinguish things like header info from audio data. i don't really care that much about that. i'd guess that, for my purposes, a formula would do. something like: if an hour of stereo audio is 650mb, then a 1.24mb file would be x duration, half that for mono.

thanks in advance,


Last seen: 8 weeks 4 hours ago
Title: ★★★★★
Joined: 11 Mar 2012
Posts: 232
duration of wav

With few lines of Python :

# -*- coding: utf-8 -*-

from __future__ import division
import wave

def wavduration( path ):
    w = path )
    dur = w.getnframes() / w.getframerate()
    return dur

....and some more lines in Basic:

Const URL_Main = "$"
Const URL_Args = "?language=Python&location=document"

Function wavduration( path )
    if fileexists( path ) then
       path = convertfromurl(path)
       sURL = URL_Main & "wavduration" & URL_Args
       oMSP = thisComponent.getScriptProvider()
       oScript = oMSP.getScript(sURL)
       x = oScript.invoke(Array( path ),Array(),Array())
       wavduration = x
       exit function
   end if
   wavduration = "#NA"
end Function can call out from calc:

=WAVDURATION( "path/to/your/file.wav")

it returns the duration in seconds, so for convinience enter:

=TIME(0;0;WAVDURATION( path ))

see attached .ods with embedded Script:


wav_duration_exsample.ods 11.63 KB

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.