Hydrogenaudio Forums

Lossless Audio Compression => FLAC => Topic started by: Database4 on 2009-06-17 07:08:59

Title: FLAC
Post by: Database4 on 2009-06-17 07:08:59
I didn't find bitrate information in the flac stream header, can someone tell me how to calculate the file duration for a given flac stream? can we calcuate it?
Title: FLAC
Post by: MClemo on 2009-06-17 07:40:58
I calculated the bitrate once in a bash script, here's the part of calculating it:

Code: [Select]
number_samples=$(metaflac --show-total-samples "$file")
sample_rate=$(metaflac --show-sample-rate "$file")
seconds=$(($number_samples / $sample_rate))
file_size=$(du -b "$file" | awk '{ print $1 }')
bitrate=$(( (($file_size / $seconds) * 8) / 1000 ))
Title: FLAC
Post by: Database4 on 2009-06-17 11:30:57
Thank you very much, it is working fine.
Title: FLAC
Post by: Database4 on 2009-06-22 10:40:32
Can a FLAC stream exists without streaminfo metadata block?
Can we calcuate the file duration in case of absence of streaminfo metadata block without parsing the entire stream?
Title: FLAC
Post by: Justin Ruggles on 2009-06-22 11:17:05
Can a FLAC stream exists without streaminfo metadata block?
Can we calcuate the file duration in case of absence of streaminfo metadata block without parsing the entire stream?

If the stream is Subset compliant, you don't necessarily need the STREAMINFO.

You could calculate the duration by just parsing the first and last frames in your stream.  If the variable block size flag is set, the frame count in each header is actually the starting sample number.  If it is not set, it is the frame number.

for normal:  ((last frame number - first frame number + 1) * blocksize) / sample rate
for variable:  (last frame sample number + last frame size - first frame sample number) / sample rate
SimplePortal 1.0.0 RC1 © 2008-2020