I do a lot of video conversions, and often i need to calculate the parameters (bitrate, resolution, etc) for these conversions.
However, even after hours of hard work, I couldn't find an all-in-one tool to do it for me.
So I decided to write my own using a familiar language, xy script.
This was intended to be a simple script for my personal use only, but it's been so useful to me that i decided to share it.
Video Calculator v6
This script used to be a simple bitrate calculator, but it got a lot more features over time.
What it can do:
-Calculate video duration, bitrate or size.
-Calculate new resolution based on current or specified aspect ratio (current AR calculated from original resolution).
-Calculate image aspect ratio (you can even give a name to them).
Since v2:
-Remember last input.
Since v3:
-Round resolution to closest multiple of "x". If "x" is not specified, use (configurable) default value.
Changes in v6:
-Small changes to the input syntax.
-Rewriten to make use of "if" and "while" blocks.
Simply run the script, fill the parameters for the desired operation and hit enter.
And make sure you don't use any ridiculous values.
If you need help, just post your problem here.
It's kinda big, but I won't post it as a file attachment.
Code: Select all
// Video Calculator v6
//
// Settings
// $sep
// set the separator between parameters.
// never use comma(s), slash(es), or a single dot.
// changing the separator *might* break the script.
// the only way to know is testing.
// default = "-"
//
// $delta
// this is used to compare the calculated aspect ratio with those in the $ratios list.
// a match will happen if the difference betwen the calculated ratio and the named ratio is smaller than $delta.
// lower values give more precision, but if it's too low the script will probably never match anything.
// higher values will match more often, but they are more likely to match the wrong name.
// i'm still testing to see what is the best value for this setting.
// for now i'll leave the default at "0.03" (it's enough to separate 3:2 from 14:9).
//
// $ratios
// $names
// comma-separated list of aspect ratios and their names.
// put the ratio in the $ratio list and the name you want for it in the $names list.
// the ratio and it's name must be in the same position in the list.
// if there are 2 or more succesful matches, the one closest to the end of the list will overwrite the others.
// the default lists match some common aspect ratios.
//
// $temppath
// path used to store variables.
//
// $defresdiv
// set the default divisor for output resolution.
// any calculated resolution will be rounded to the closest multiple of this value.
// unless, of course, you specify otherwise in the input dialog.
// the default value is "1" (calculated resolution is not changed).
//
"Video Calculator"
global $input,$sep,$delta,$ratios,$names,$defresdiv;
//the settings
$sep = "-";
$delta = "0.03";
$ratios = "1.33,1.50,1.56,1.66,1.78";
$names = "4:3,3:2,14:9,5:3,16:9";
$temppath = "%temp%";
$defresdiv = 16;
//script start
getkey $lastinput, lastinput, general, "$temppath\videocalc.tmp";
$tips=<<<TIPS
Use * to select the output (only one!)
Duration, Bitrate or Size:
<hh,nn,ss>$sep<kbps>$sep<MB>
Note: "hh," or "hh,nn," are optional
Resize using current aspect ratio:
<orig_width>,<orig_height>$sep<new_width>,<new_height>[,<divisor>]
Use * only on <new_width> or <new_height>
Change/calculate aspect ratio:
<width>,<height>[,<divisor>]$sep<ratio>
Enter <ratio> as a decimal or a proportion (e.g. 1.33 == 4/3)
TIPS;
$input = input("Video Calculator", "$tips", $lastinput, s);
setkey $input, lastinput, general, "$temppath\videocalc.tmp";
replace $input, $input, " ", ""; //remove spaces
$test = gettoken($input, 3, "$sep"); //find type of operation
sub ($test)?(_dbs):(_res);
"_dbs" //get duration, bitrate and size from input, then see which one we have to calculate
global $input,$dur,$bit,$size,$sep;
$dur = gettoken($input, 1, "$sep");
$bit = gettoken($input, 2, "$sep");
$size = gettoken($input, 3, "$sep");
if("$dur"=="*"){sub _duration;}
elseif("$bit"=="*"){sub _bitrate;}
elseif("$size"=="*"){sub _size;}
else{sub _invalid;}
"_res" //get resolutions, divisor and aspect ratio, then decide what to calculate
global $input,$origw,$origh,$neww,$newh,$ar,$sep,$resdiv,$defresdiv;
$original = gettoken($input, 1, "$sep");
$new = gettoken($input, 2, "$sep");
$origw = gettoken($original,1,",");
$origh = gettoken($original,2,",");
$resdiv = gettoken($original,3,",");
$neww = gettoken($new,1,",");
$newh = gettoken($new,2,",");
$resdiv = ($resdiv)?($resdiv):(gettoken($new,3,","));
$resdiv=($resdiv)?($resdiv):($defresdiv);
$ar = eval($new);
if("$new"=="*"){sub _ar;}
elseif("$newh"=="*"){sub _newheight;}
elseif("$origw"=="*"){sub _arwidth;}
elseif("$origh"=="*"){sub _arheight;}
elseif("$neww"=="*"){sub _newwidth;}
else{sub _invalid;}
//sub ("$new"=="*")?(_ar):(("$newh"=="*")?(_newheight):(("$origw"=="*")?(_arwidth):(("$origh"=="*")?(_arheight):(("$neww"=="*")?(_newwidth):(_invalid)))));
"_duration" //calculate clip duration for given bitrate and file size
global $bit,$size,$round;
$sec = ($size*8192/$bit);//calculate total seconds
$min = ($sec/60);//minutes
$hrs = ($min/60);//and hours
$round = "$sec,3"; sub _round; $sec=$round;
$min = gettoken("$min.", 1, ","); //discard redundant time
$hrs = gettoken("$hrs.", 1, ",");
$sec2 = ($sec-$min*60);
$round = "$sec2,3"; sub _round; $sec2 = $round;
$min = ($min-$hrs*60);
strlen $test, $sec2; //fix leading zeros
$sec2 = ($test==1)?(0.$sec2):($sec2);
strlen $test, $min;
$min = ($test==1)?(0.$min):($min);
input("Clip duration in hrs:min:sec (Click OK to see total seconds)", ,"$hrs:$min:$sec2");
input("Clip duration in seconds", ,"$sec"); //done!
"_getduration" //calculate total of seconds based on duration input
global $dur,$total;
$thrs = gettoken($dur, 1, ",");
$tmin = gettoken($dur, 2, ",");
$tsec = gettoken($dur, 3, ",");
if($tmin==""){ //fix if "hh.nn" wasn't entered
$tsec = $thrs;
$tmin = 0;
$thrs = 0;}
elseif($tsec==""){ //fix if "hh" wasn't entered
$tsec = $tmin;
$tmin = $thrs;
$thrs = 0;}
$total = ($tsec+60*$tmin+3600*$thrs); //total seconds
"_bitrate" //calculate bitrate based on duration and file size
global $size,$total,$round;
sub _getduration;
$bitrate = ($size*8192/$total); //this will also round down the result
$round = "$bitrate,0"; sub _round; $bitrate = $round; //this will also round the result
input("Clip bitrate in Kbps", ,"$bitrate"); //done!
"_size" //calculate file size based on duration and bitrate
global $bit,$total,$round;
sub _getduration;
$size = ($bit*$total/8192);
$round = "$size,2"; sub _round; $size = $round; //leave only two decimals
input("File size in MB", ,"$size"); //done!
"_newwidth" //calculate new width for desired height, using current aspect ratio
global $origw,$origh,$neww,$newh,$ar,$round,$resdiv,$newheight,$newwidth;
$newwidth = ($origw/$origh*$newh);
$newheight = $newh;
if($resdiv>1){sub _resdiv;}
//sub ($resdiv>1)?(_resdiv):(_nothing);
$round = "$newwidth,0"; sub _round; $newwidth = $round;
input("New clip resolution (width,height)", ,"$newwidth,$newheight"); //done!
"_newheight" //vice-versa, again using the current aspect ratio
global $origw,$origh,$neww,$newh,$round,$resdiv,$newheight,$newwidth;
$newheight = ($origh/$origw*$neww);
$newwidth = $neww;
if($resdiv>1){sub _resdiv;}
//sub ($resdiv>1)?(_resdiv):(_nothing);
$round = "$newheight,0"; sub _round; $newheight = $round;
input("New clip resolution (width,height)", ,"$newwidth,$newheight"); //done!
"_arwidth" //calculate width using specified aspect ratio
global $origh,$ar,$round,$resdiv,$newheight,$newwidth;
$newwidth = ($ar*$origh);
$newheight = $origh;
if($resdiv>1){sub _resdiv;}
//sub ($resdiv>1)?(_resdiv):(_nothing);
$round = "$newwidth,0"; sub _round; $newwidth = $round;
input("New clip resolution (width,height)", ,"$newwidth,$newheight"); //done!
"_arheight" //calculate height using specified aspect ratio
global $origw,$ar,$round,$resdiv,$newheight,$newwidth;
$newheight = (1/$ar*$origw);
$newwidth = $origw;
if($resdiv>1){sub _resdiv;}
//sub ($resdiv>1)?(_resdiv):(_nothing);
$round = "$newheight,0"; sub _round; $newheight = $round;
input("New clip resolution (width,height)", ,"$newwidth,$newheight"); //done!
"_ar" //calculate aspect ratio of given resolution
global $origw,$origh,$ar,$modar,$delta,$names,$maxcount,$round;
$ar = ($origw/$origh); //calculate ratio
regexreplace $test, $names, "[^,]", ""; //start comparison loop
strlen $maxcount, $test;
$count = 0;
while($count<=$maxcount){
$workar = gettoken("$ratios", ($count+1),","); //get current name and ratio from the list
$workname = gettoken("$names", ($count+1),",");
$modar = (((-$delta<($ar-$workar))==(($ar-$workar)<$delta))==1)?($workname):($modar); //compare with calculated aspect ratio using $delta
$count++;}
$round = "$ar,2"; sub _round; $ar = $round; //leave only two decimals
input(($modar)?("Aspect ratio matched $modar"):("Aspect ratio is:"), ,"$ar:1"); //done!
"_resdiv"
global $resdiv,$newwidth,$newheight,$round;
$round=($newwidth/$resdiv).",0"; sub _round;
$newwidth=($round*$resdiv);
$round=($newheight/$resdiv).",0"; sub _round;
$newheight=($round*$resdiv);
"_round" //global $round; $round="<value_or_variable_to_round>,<output_decimal_places>"; sub _round; <output_variable>=$round;
global $round;
$roundval = gettoken($round,1,","); //get parameters
$roundto = gettoken($round,2,",");
$roundint = gettoken($roundval,1,"."); //separate decimal part
$rounddec = gettoken($roundval,2,".");
if($roundto>0){
substr $outdec, $rounddec,,$roundto; //separate removed decimals
substr $remdec, $rounddec, $roundto;
if("0.$remdec">"0.5"){ //test to round down the decimals
incr $outdec;}
elseif("0.$remdec"=="0.5"){
$outdec = (gettoken(($outdec/2),2, "."))?($outdec+1):($outdec);}
strlen $countdec, $outdec; //fix trailing zeroes
$outdec = "$outdec".strrepeat("0",($roundto-$countdec));}
elseif($roundto==0){
if("0.$rounddec">"0.5"){ //test to round down the decimals
incr $roundint;}
elseif("0.$$rounddec"=="0.5"){
$roundint = (gettoken(($roundint/2),2, "."))?($roundint+1):($roundint);}}
$round = "$roundint".($roundto>0)?(".$outdec"):(""); //output to $round
"_invalid"
msg "Invalid input parameters";
//"_nothing"
XYplorer Beta Club