Python3.2 Ogg and Wav Sound Duration.

Hi All,

I have spent the last few days looking for ways to get the duration/length of an Ogg audio file using Python3.2.

It seems that all the tools to do this were created as far back as 2007, therefore they aren’t designed to support python3.2.

The tools/modules I have tried using are as follows:

Pymad
Mutagen
Audioread

Is there anything available which will give me ogg and wave audio duration/length for python3.2?

Thanks
-Helenclarko

HG1 got Pygame working (by changing dll’s)!
It works with Blender 2.59 and up to Blender 2.63.12 r48053M.
Only sound playback and duration were tested.

here is his packed version of Pygame along with an example blend file.
Just extract the zip file and start the blend file.
http://www.mediafire.com/?wb3l42theyb210d

the dirty way:
load sound into blender as sequencer strip, determine the strip length and calculate duration based on FPS settings. Not sure if this really works though
bpy.data.screens[‘Video Editing’].scene.sequence_editor.active_strip.frame_final_duration

or get some file format specs and write your own header parser to calculate the length

Hi Helenclarko,

not sure about Ogg files.


import bpy
import wave
import contextlib


# for ease i have a speaker with wav file loaded, selected

file_path = bpy.context.object.data.sound.filepath


with contextlib.closing(wave.open(file_path,'r')) as f:
    frames = f.getnframes()
    rate = f.getframerate()
    duration = frames / float(rate)
    print(duration)

Thank you both for your prompt replies.

I will test and get back to you with the results.

Thanks
-Helenclarko

Hi Again,

I am guessing there is no easy way to get the duration of a sound file using python3.2.

batFINGER, your solution would probably work. However it looks as though it will be loading the sound clips into a speaker before it can get the duration from it… is that right?

Thanks for your help.
-helenclarko

batFinger, your solution doesn’t seem to work on wave files…

I get an error message stating that the “file does not start with a RIFF id”, which I can only assume is the information at the start of a wave file.

Do you know of a way to load an ogg file through this?

Thanks again for your help.

How about this?

http://lists.xiph.org/pipermail/vorbis-dev/2003-July/007533.html

 time = (double)inf->lastgranulepos / inf->vi.rate;
 minutes = (long)time / 60;
 seconds = (long)time - minutes*60;
seconds100 = (long)(time - floor(time));

look at the OGG format specs to figure out where the last granule pos and the rate can be found.

Thanks Codeman, ill let you know how I get on.

one note to consider:

every codec with variable compression has no option to get the length/duration

  • it always needs to scan thru the whole file
  • there are settings to use time-stamps inside the stream, but this will only work if a re-winding can catch up with the last mark.

yeah, variable compression is a pain for duration calc, but there might be an average rate field which could give an idea about the length

I’m starting to come to the conclusion that this may not be possible.

test-dr, the following seems to be exactly as you had said:

I am using aud to play sound through blender, but it doesn’t seem to have any way to find the duration/length of the file it is using.

Thanks,
-Helenclarko

You don’t need the speaker just put in your own filepath to your wav file.

The code works ok for me with wav files on win XP.

like BatFinger is using wav-codec with “fixed compression” - you may have the possibility to use a conversion of the sound-file to another codec.
In linux with “sox” (is it available for windows?) its easy to convert from one sound-codec to another:
sox input.mp3 output.wav
and there are options to specify codecs not automatically identified.

with external applications involved, wouldn’t it be easier to read out the duration from a media info tool instead of converting?

http://mediainfo.sourceforge.net

Ogg duration supported:
http://mediainfo.sourceforge.net/de/Support/Formats

Is there any way to get info from an OGG file using Audaspace?

“hsaudiotag” can calculate the duration, but it only works with pure audio ogg files.

Is there any way to have it calculate all ogg files or is this too difficult due to the different compressions being used?

Thanks,
-helenclarko

HG1 got Pygame working (by changing dll’s)!
It works with Blender 2.59 and up to Blender 2.63.12 r48053M.
Only sound playback and duration were tested.

here is his packed version of Pygame along with an example blend file.
Just extract the zip file and start the blend file.