calculate average bitrate

Hi. I recently tried to create .nfos with some homemade template heavily borrowing from existing scripts since scripting isn't my area of expertise :smiley:

One thing I can't make work and I have no clue why is calculating average bitrate. I stole the "looping add and divide" formula from old scripts found in this forum section, the addition part works, not the division...

ie this is supposed to display the average number :
$loop(%_filename_ext%)$puts(Vbitrate,$add($get(Vbitrate),%_bitrate%))$puts(cnt1,$add($get(cnt1),1))$loopend()$div($get(Vbitrate),get(cnt1))

but all I get is numbers around of above 10000 for standard flac 16-44 or typically 3200 for say a ten tracks album @ 320k (it's sure useless to calculate average for CBR by definition, it's just to show what's calculated)
so either the division function must be called differently or the cnt1 number/divider is stuck to one...

what am I missing ?

ah, forget it, found the missing character, it's $get(cnt1) at the end, not get(cnt1)

me again...

the formula above is "okayish", but I soon noticed variations, sometiimes huge ones, with bitrate indicated by foobar for instance. and I (think I) understand it has to do with track lengths. Foobar takes that into account (as it should) while the script above doesn't care and just divides by track numbers. So if you have say 2 indexes, 1mn interlude @ 300k, then one 20mn piece @ 900k, the script will give average 600k while foobar will more accurately report something much closer to 900...

so in short : is there a way to script an average bitrate taking track timings into accounts ?
meaning
-each track bitrate should be multiplied with individual length in seconds
-added to previous one till last track (that part doesn"t change)
then the whole should be divided by global timing in seconds.

Hmm, you may do such mathematics within an export script, which can create for example a CSV text file, which provides all the related data like filename and the other calculated data, for later import.

As a starting point ... to calculate average bitrate ...

$filename($getEnv('USERPROFILE')'\Desktop\Mp3tag.Report.Bitrate.txt',UTF-8)

$loop(%_path%)$puts(SUMBITRATE,$add($get(SUMBITRATE),%_bitrate%))$puts(SUMTRACKS,$if(%_bitrate%,$add($get(SUMTRACKS),1),$get(SUMTRACKS)))$loopend()
'Max Tracks : '%_max_counter%
'Sum Bitrate : '$get(SUMBITRATE)
'Sum Tracks (BR): '$get(SUMTRACKS)
'Average Bitrate: '$div($get(SUMBITRATE),$get(SUMTRACKS))

DD.20171202.1500.CET

1 Like

can't we just do it within the loop without export->import ??

I see you mentionned %_length_seconds% in another thread, can't we just use this for each track "mul"tiplication and add it to the global counter ? (cnt1 becoming total length instead of total tracks and used for final division) I now zilch about scripting but I don't see any obstacle, just clueless about the required syntax...
it's just (bitrate-x1seconds+bitrate-x2seconds+bitrate-x3*seconds, etc, loop till last track)/(total seconds)
if %_length_seconds% can be used as numeric value, where is the obstacle ?

Ok I did it myself, %_length_seconds% works wonders, nothing to export/re-import
you just have to replace the average bitrate formula this way :

OLD formula

$loop(%_filename_ext%)
$puts(Vbitrate,$add($get(Vbitrate),%_bitrate%))
$puts(cnt1,$add($get(cnt1),1))
$loopend()
$div($get(Vbitrate),$get(cnt1))

NEW formula

$loop(%_filename_ext%)
$puts(Vbitrate,$add($get(Vbitrate),$mul(%_bitrate%,%_length_seconds%)))
$puts(cnt1,$add($get(cnt1),%_length_seconds%))
loopend()
$div($get(Vbitrate),$get(cnt1))