AyrA icon

__frametools.js

AyrA | PRO | 11/02/17 01:23:33 AM UTC | 0 ⭐ | 508 👁️ | Never ⏰ | []
JavaScript |

2.75 KB

|

None

|

0 👍

/

0 👎

"use strict";
 
//maximum filesize in bytes
var MAX_SIZE=3000000;
//Percentage of bitrate to subtract (use at least 2)
var SPACE=2;
//Factor of bitrate (125 converts bytes to kbits (x*8/1000)
var FACTOR=125;
//Unit of bitrate (for display purposes only)
var UNIT="k";
//Upper [0] and lower [1] bitrate cap.
var CAP=[1000,100];
 
//You can set this to true or false. The default is to check for "-v" argument
var VERBOSE=process.argv.slice(2).map(function(v){return v.toLowerCase();}).indexOf("-v")>=0;
 
//////////////////////
//                  //
// no changes below //
//                  //
//////////////////////
 
var FILENAME=process.argv.slice(2).filter(function(v){return v.toLowerCase()!="-v";})[0];
 
if(!FILENAME)
{
    console.log("node",nameOnly(__filename),"<filename> [-v]");
    process.exit(2);
}
 
function nameOnly(x)
{
    return x=x.split('/').slice(-1)[0].split('\\').slice(-1)[0];
}
 
function r(x,d)
{
    var factor=Math.pow(10, d);
    var tempNumber=x*factor;
    var roundedTempNumber=Math.round(tempNumber);
    return roundedTempNumber/factor;
}
 
function niceSize(o,f)
{
    var units=["Bytes","KB","MB","GB","TB"];
    
    if(!f)
    {
        f=1000;
    }
    
    var current=o;
    var index=0;
    while(current>=f && index++<units.length-1)
    {
        current/=f;
    }
    return r(current,1)+" "+units[index];
}
 
var path=require("path");
var spawn=require("child_process").spawn;
var proc=spawn(path.join(__dirname,"../ffprobe"),[
"-show_entries",
"format=duration",
"-of",
"default=noprint_wrappers=1:nokey=1",
"-v",
"quiet",
process.argv[2]
]);
 
SPACE=(100-SPACE)/100;
if(SPACE>1.0 || SPACE<=0.0)
{
    console.log("Invalid percentage (must be in the range 0-99)");
    process.exit(1);
}
 
proc.stdout.on("data",function(data)
{
    var Duration=+data.toString();
    if(Duration)
    {
        var Bitrate=MAX_SIZE/Duration/FACTOR*SPACE|0;
        if(VERBOSE)
        {
            console.log("Duration    :",Duration,"seconds");
            console.log("Raw bitrate :",Bitrate+"k");
        }
        if(Bitrate>CAP[0])
        {
            Bitrate=CAP[0];
            if(VERBOSE)
            {
                console.log("Bitrate too big. Capped to",CAP[0]);
                console.log(
                    "File size will be",niceSize(Bitrate*Duration*FACTOR),
                    "instead of",niceSize(MAX_SIZE),
                    "("+r(Bitrate*Duration*FACTOR/MAX_SIZE*100,1)+"% of original)");
            }
        }
        else if(Bitrate<CAP[1])
        {
            Bitrate=CAP[1];
            if(VERBOSE)
            {
                console.log("Bitrate too small. Capped to",CAP[1]);
                console.log(
                    "File size will be",niceSize(Bitrate*Duration*FACTOR),
                    "instead of",niceSize(MAX_SIZE),
                    "("+r(Bitrate*Duration*FACTOR/MAX_SIZE*100,1)+"% of original)");
            }
        }
        console.log("Recommended :",Bitrate+UNIT);
    }
    else
    {
        console.log("Error estimating bitrate. Invalid input file (maybe image)?");
    }
});

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎