Back Forum Reply New

Avisynth bitrate calculator

As the title suggests, this is a bitrate calculator written in my
favorite programming language, Avisynth  .  The function takes in atarget filesize (default 700MB) and an audio bitrate (default 128kb/s)and tells you the target video bitrate or filesize, as well as thebits per pixel.Code:
function BitrateCalc(clip c,float quot;target_sizequot;,float quot;audio_bitratequot;,string quot;containerquot;,float quot;bpp_thresholdquot;)
{
target_size = default(target_size,700) #MB
audio_bitrate = default(audio_bitrate,128) #kB/s
container = default(container,quot;aviquot;)
bpp_threshold = default(bpp_threshold,0.170)
assert((container==quot;aviquot;)||(container==quot;ogmquot;)||(container==quot;mkvquot;), quot;Valid containers are avi, ogm, or mkvquot;)
seconds = c.framecount/c.framerate
audiosize = (audio_bitrate/8.0)*(seconds/1024.0)
overhead =  (container == quot;aviquot;) ? (c.framecount)*(24+24)/(1024*1024+0.0) :        \ ((container == quot;ogmquot;) ? (c.framecount)*(0.069)/(1024+0.0) :        \                         (c.framecount)*(0.013)/(1024+0.0))
videosize = target_size-audiosize-overhead
targetbitrate = videosize*1024*8/seconds
bpp = (targetbitrate*1024)/(c.framerate*c.width*c.height+0.0)
f = c.Subtitle(quot;Audio Size = quot;+String(round(audiosize))+quot; MBquot;,last_frame=1)
f = f.Subtitle(quot;Video Size = quot;+String(round(videosize))+quot; MBquot;+quot;  (quot;+container+quot; overhead  = quot;+String(round(overhead))+quot; MB)quot;,last_frame=1,y=36)
f = f.Subtitle(quot;Target Bitrate = quot;+String(round(targetbitrate))+quot; kB/squot;,last_frame=1,y=72)
f = f.Subtitle(quot;Target Filesize = quot;+String(floor(videosize*1024))+quot; kBquot;,last_frame=1,y=90)
f = bpp gt; bpp_threshold ? f.Subtitle(quot;Bits Per Pixel = quot;+LeftStr(String(bpp),5),last_frame=1,y=108,text_color=$00FF00) :            \ f.Subtitle(quot;Bits Per Pixel = quot;+LeftStr(String(bpp),5),last_frame=1,y=108,text_color=$FF0000)
return f
}
To use it, simply copy the above code into the beginning of your
script, or copy it into a text file and name it something.avsi andput it in your plugins directory.  Example usage:Code:
mpeg2source(quot;D:\something.d2vquot;)
LanczosResize(640,480)
BitrateCalc()
Preview this script in Virtualdub, information will be written on thefirst two frames of the video.  You can also specify a container tocalculate overhead.  I got the formulas for the overhead
computations by searching this forum, please let me know if there areany problems with them.

Why use this?  Well, you don't need to input stuff like resolution,
number of frames, framerate, etc., since avisynth already knows it.
If you use gordian knot, you probably don't need this, but if you're
like me, and use only avisynth and virtualdub to set up your dvd
backups, it might come in handy (well, maybe until xvid 1.0 has a
working bitrate calculator).  Let me know if you use it or have any
problems with it.

Yes, useful function if you don't use GordianKnot (I don't) or don't backup DVDs. What file type is the third overhead, with the formula quot;(c.framecount)*(0.013)/(1024+0.0)quot;? (I suppose it's mkv).

Yep, as the function is right now, the optional input 'container' takes in only one of three strings, quot;aviquot;, quot;ogmquot;, or quot;mkvquot; (matroska), with quot;aviquot; being the default.  I don't exactly know which post I got these calculations from, but I do know that at least for mkv, the calculation is an estimation, since matroska interleaving is very complex.  Of course, if you have a favorite container and you know the overhead calculation for it, it's pretty easy to add it in (one of the reasons that Avisynth functions are so cool  ).

qwerpoi, thanks a lot! great tool
but as I see, it works only with 128CBR MP3, what about VBR? or other formats like Ogg?

Humm, that's a good point, I never really considered other audio formats, I guess I only use cbr mp3 in my encodes.  You can change the bitrate using the 'audio_bitrate' parameter, it's only defaulted to 128 (feel free to change the default to whatever you use most).  As far as vbr or ogg is concerned, as I said before, it's always simple to add in extra modes in a user defined function.  When I have some time I'll go hunting for some equations and add them into the function.  

I'm also going to try to include the option to dump the info to a text file instead (although I'm pretty fond of having the info written to the video directly, it's kinda silly).  I know about nic's call plugin, I'm trying to figure out a way to dump the file into the same directory the as the video without requiring the user to specify the directory name (if anyone knows a way to do this using nicecho.exe or cmd.exe, please share  ).
¥
Back Forum Reply New