Go Back   CodingForums.com > :: Client side development > JavaScript programming > Post a JavaScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 2.50 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-27-2003, 08:40 PM   PM User | #1
Jeff Mott
Regular Coder

 
Join Date: Sep 2003
Posts: 290
Thanks: 0
Thanked 0 Times in 0 Posts
Jeff Mott is an unknown quantity at this point
DivX Calculator: Video Bitrate/Audio Bitrate/Resolution

Code:
/*
 * var divx_video = new DivXVideo();
 */
function DivXVideo()
{
    /*
     * default values
     */
    this.VideoLength = 0;
    this.AspectRatio = 16 / 9;
    this.Width = 720;
    this.Height = 480;
    this.CroppingTop = 0;
    this.CroppingRight = 0;
    this.CroppingBottom = 0;
    this.CroppingLeft = 0;
    this.FramesPerSecond = 23.976;
    this.OutputFileSize = 703.125;
    this.ResXModulus = 4;
    this.ResYModulus = 2;
    this.AudioBitrateModulus = 16;
    
    /*
     * videoLength();
     * videoLength( seconds );
     * videoLength( minutes, seconds );
     * videoLength( hours, minutes, seconds );
     */
    this.videoLength = function()
    {
        if (arguments.length > 2)
            this.VideoLength = arguments[0] * 3600 + arguments[1] * 60 + arguments[2];
        else if (arguments.length > 1)
            this.VideoLength = arguments[0] * 60 + arguments[1];
        else if (arguments.length)
            this.VideoLength = arguments[0];
        return this.VideoLength;
    }
    
    /*
     * aspectRatio( [ ratio ] );
     */
    this.aspectRatio = function()
    {
        if (arguments.length)
            this.AspectRatio = arguments[0];
        return this.AspectRatio;
    }
    
    /*
     * width( [ n ] );
     */
    this.width = function()
    {
        if (arguments.length)
            this.Width = arguments[0];
        return this.Width;
    }
    
    /*
     * height( [ n ] );
     */
    this.height = function()
    {
        if (arguments.length)
            this.Height = arguments[0];
        return this.Height;
    }
    
    /*
     * croppingTop( [ n ] );
     */
    this.croppingTop = function()
    {
        if (arguments.length)
            this.CroppingTop = arguments[0];
        return this.CroppingTop;
    }
    
    /*
     * croppingRight( [ n ] );
     */
    this.croppingRight = function()
    {
        if (arguments.length)
            this.CroppingRight = arguments[0];
        return this.CroppingRight;
    }
    
    /*
     * croppingBottom( [ n ] );
     */
    this.croppingBottom = function()
    {
        if (arguments.length)
            this.CroppingBottom = arguments[0];
        return this.CroppingBottom;
    }
    
    /*
     * croppingLeft( [ n ] );
     */
    this.croppingLeft = function()
    {
        if (arguments.length)
            this.CroppingLeft = arguments[0];
        return this.CroppingLeft;
    }
    
    /*
     * framesPerSecond( [ fps ] );
     */
    this.framesPerSecond = function()
    {
        if (arguments.length)
            this.FramesPerSecond = arguments[0];
        return this.FramesPerSecond;
    }
    
    /*
     * outputFileSize( [ MB ] );
     */
    this.outputFileSize = function()
    {
        if (arguments.length)
            this.OutputFileSize = arguments[0];
        return this.OutputFileSize;
    }
    
    /*
     * resXModulus( [ modulus ] );
     */
    this.resXModulus = function()
    {
        if (arguments.length)
            this.ResXModulus = arguments[0];
        return this.ResXModulus;
    }
    
    /*
     * resYModulus( [ modulus ] );
     */
    this.resYModulus = function()
    {
        if (arguments.length)
            this.ResYModulus = arguments[0];
        return this.ResYModulus;
    }
    
    /*
     * audioBitrateModulus( [ modulus ] );
     */
    this.audioBitrateModulus = function()
    {
        if (arguments.length)
            this.AudioBitrateModulus = arguments[0];
        return this.AudioBitrateModulus;
    }
    
    /*
     * audioBitrate();
     *
     * Assume MP3 compression.
     * Variable dependencies:
     *  - outputFileSize
     *  - videoLength
     *  - audioBitrateModulus
     */
    this.audioBitrate = function()
    {
        var kbps = Math.round(this.OutputFileSize * 8192 / (this.VideoLength * 7.09375));
        kbps -= kbps % this.AudioBitrateModulus;
        return kbps > 128 ? 128 : kbps;
    }
    
    /*
     * videoBitrate();
     *
     * Variable dependencies:
     *  - outputFileSize
     *  - videoLength
     *  - audioBitrateModulus
     */
    this.videoBitrate = function()
    {
        return Math.floor((this.OutputFileSize - this.kbps2MB(this.audioBitrate(), this.VideoLength)) * 8192 / this.VideoLength);
    }
    
    /*
     * resX();
     *
     * Variable dependencies:
     *  - width
     *  - croppingLeft
     *  - croppingRight
     *  - height
     *  - croppingTop
     *  - croppingBottom
     *  - aspectRatio
     *  - outputFileSize
     *  - videoLength
     *  - audioBitrateModulus
     *  - framesPerSecond
     *  - resXModulus
     */
    this.resX = function()
    {
        var x = this.Width - this.CroppingLeft - this.CroppingRight;
        var y = this.Height - this.CroppingTop - this.CroppingBottom;
        if (Math.round(this.Width / this.AspectRatio) > this.Height)
            x /= this.Width / Math.round(this.Height * this.AspectRatio)
        else
            y /= this.Height / Math.round(this.Width / this.AspectRatio);
        var optimum_res_x = Math.sqrt(1024 * this.videoBitrate() * (x / y) / (.2 * this.FramesPerSecond));
        if (optimum_res_x < x)
            x = optimum_res_x;
        x = Math.round(x);
        x -= x % this.ResXModulus;
        return x;
    }
    
    /*
     * resY();
     *
     * Variable dependencies:
     *  - width
     *  - croppingLeft
     *  - croppingRight
     *  - height
     *  - croppingTop
     *  - croppingBottom
     *  - aspectRatio
     *  - outputFileSize
     *  - videoLength
     *  - audioBitrateModulus
     *  - framesPerSecond
     *  - resXModulus
     *  - resYModulus
     */
    this.resY = function()
    {
        var x = this.Width - this.CroppingLeft - this.CroppingRight;
        var y = this.Height - this.CroppingTop - this.CroppingBottom;
        if (Math.round(this.Width / this.AspectRatio) > this.Height)
            x /= this.Width / Math.round(this.Height * this.AspectRatio)
        else
            y /= this.Height / Math.round(this.Width / this.AspectRatio);
        y = Math.round(this.resX() / (x / y));
        y -= y % this.ResYModulus;
        return y
    }
    
    /*
     * kbps2MB( bitrate, seconds );
     *
     * Convert kbps for a given number of seconds to file size in MB.
     */
    this.kbps2MB = function(bitrate, seconds)
    {
        return bitrate * seconds / 8192;
    }
}

Last edited by Jeff Mott; 09-29-2003 at 04:09 PM..
Jeff Mott is offline   Reply With Quote
Old 11-25-2003, 06:43 PM   PM User | #2
Jeff Mott
Regular Coder

 
Join Date: Sep 2003
Posts: 290
Thanks: 0
Thanked 0 Times in 0 Posts
Jeff Mott is an unknown quantity at this point
Updated.
  • Better organized
  • Follows the naming conventions for OO programming
  • Includes support for an initial compressibility test
Code:
function DivXVideo()
{
    var videoLength = 0;
    var aspectRatio = 1.7777777777777777777777777777778;
    var width = 720;
    var height = 480;
    var croppingTop = 0;
    var croppingRight = 0;
    var croppingBottom = 0;
    var croppingLeft = 0;
    var framesPerSecond = 23.976023976023976023976023976024;
    var outputFileSize = 703.125;
    var audioBitrateLimit = 128;
    var resXModulus = 4;
    var resYModulus = 2;
    var audioBitrateModulus = 16;
    var qualityFactor = 0.21559718335619570187471422039323;
    
    this.getAudioBitrate = function() {
        var kbps = Math.round(outputFileSize * 8192 / (videoLength * (1 + 780 / audioBitrateLimit)) / audioBitrateModulus) * audioBitrateModulus;
        return kbps > audioBitrateLimit ? audioBitrateLimit : kbps;
    }
    
    this.getVideoBitrate = function() {
        return Math.floor(outputFileSize * 8192 / videoLength) - this.getAudioBitrate();
    }
    
    this.getWidthAfterCrop = function() {
        return width - croppingLeft - croppingRight;
    }
    
    this.getHeightAfterCrop = function() {
        return height - croppingTop - croppingBottom;
    }
    
    this.getResX = function() {
        var x = this.getWidthAfterCrop();
        if (Math.round(width / aspectRatio) > height)
            x /= width / Math.round(height * aspectRatio);
        return Math.round(x);
    }
    
    this.getResXAtModulus = function() {
        var x = Math.round(this.getResX() / resXModulus) * resXModulus;
        x = x > this.getWidthAfterCrop() ? this.getWidthAfterCrop() - this.getWidthAfterCrop() % resXModulus : x;
        while (Math.round(x / (this.getResX() / this.getResY()) / resYModulus) * resYModulus > this.getHeightAfterCrop())
            x -= resXModulus;
        return x;
    }
    
    this.getResY = function() {
        var y = this.getHeightAfterCrop();
        if (Math.round(height * aspectRatio) > width)
            y /= height / Math.round(width / aspectRatio);
        return Math.round(y);
    }
    
    this.getResYAtModulus = function() {
        return Math.round(this.getResXAtModulus() / (this.getResX() / this.getResY()) / resYModulus) * resYModulus;
    }
    
    this.getOptimizedResX = function() {
        var x = Math.round(Math.sqrt(1024 * this.getVideoBitrate() * (this.getResX() / this.getResY()) / (qualityFactor * framesPerSecond)));
        return x > this.getWidthAfterCrop() ? this.getWidthAfterCrop() : x;
    }
    
    this.getOptimizedResXAtModulus = function() {
        var x = Math.round(this.getOptimizedResX() / resXModulus) * resXModulus;
        return x > this.getResXAtModulus() ? this.getResXAtModulus() : x;
    }
    
    this.getOptimizedResY = function() {
        return Math.round(this.getOptimizedResX() / (this.getResX() / this.getResY()));
    }
    
    this.getOptimizedResYAtModulus = function() {
        return Math.round(this.getOptimizedResXAtModulus() / (this.getResX() / this.getResY()) / resYModulus) * resYModulus;
    }
    
    this.getOptimizedQualityFactor = function(bytes) {
        return bytes * 8 / (this.getResXAtModulus() * this.getResYAtModulus() * framesPerSecond * videoLength);
    }
    
    this.getCdCapacity = function(minutes) {
        return minutes * 8.7890625;
    }
    
    this.getVideoLength = function() { return videoLength }
    this.getAspectRatio = function() { return aspectRatio }
    this.getWidth = function() { return width }
    this.getHeight = function() { return height }
    this.getCroppingTop = function() { return croppingTop }
    this.getCroppingRight = function() { return croppingRight }
    this.getCroppingBottom = function() { return croppingBottom }
    this.getCroppingLeft = function() { return croppingLeft }
    this.getFramesPerSecond = function() { return framesPerSecond }
    this.getOutputFileSize = function() { return outputFileSize }
    this.getAudioBitrateLimit = function() { return audioBitrateLimit }
    this.getResXModulus = function() { return resXModulus }
    this.getResYModulus = function() { return resYModulus }
    this.getAudioBitrateMouduls = function() { return audioBitrateModulus }
    this.getQualityFactor = function() { return qualityFactor }
    
    this.setVideoLength = function() {
        if (arguments.length == 3)
            videoLength = 3600 * arguments[0] + 60 * arguments[1] + arguments[2];
        else if (arguments.length == 2)
            videoLength = 60 * arguments[0] + arguments[1];
        else
            videoLength = arguments[0];
    }
    this.setAspectRatio = function(a) { aspectRatio = a }
    this.setWidth = function(w) { width = w }
    this.setHeight = function(h) { height = h }
    this.setCroppingTop = function(c) { croppingTop = c }
    this.setCroppingRight = function(c) { croppingRight = c }
    this.setCroppingBottom = function(c) { croppingBottom = c }
    this.setCroppingLeft = function(c) { croppingLeft = c }
    this.setFramesPerSecond = function(f) { framesPerSecond = f }
    this.setOutputFileSize = function(s) { outputFileSize = s }
    this.setAudioBitrateLimit = function(b) { audioBitrateLimit = b }
    this.setResXModulus = function(m) { resXModulus = m }
    this.setResYModulus = function(m) { resYModulus = m }
    this.setAudioBitrateModulus = function(m) { audioBitrateModulus = m }
    this.setQualityFactor = function(q) { qualityFactor = q }
}
And attached in an HTML document implementing this class.
Attached Files
File Type: txt divx calc.html.txt (7.4 KB, 524 views)

Last edited by Jeff Mott; 11-25-2003 at 06:50 PM..
Jeff Mott is offline   Reply With Quote
Old 12-08-2003, 06:03 AM   PM User | #3
kwhubby
Regular Coder

 
Join Date: Nov 2002
Location: Carmel California
Posts: 471
Thanks: 0
Thanked 1 Time in 1 Post
kwhubby is an unknown quantity at this point
Internet Explorer errors in IE

Works for moz, not for IE 6. gives the error:
Line: 14
Error: 'DivXVideo' is undefined
__________________
Kris Hubby
kwhubby site
kwhubby is offline   Reply With Quote
Old 12-08-2003, 07:00 AM   PM User | #4
Jeff Mott
Regular Coder

 
Join Date: Sep 2003
Posts: 290
Thanks: 0
Thanked 0 Times in 0 Posts
Jeff Mott is an unknown quantity at this point
I've been using it almost exclusively in IE. Note that the DivXVideo class is not within the HTML doc. The class must be placed within a local DivXVideo.js file, which is then imported into the HTML doc.
Jeff Mott is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:58 AM.


Advertisement
Log in to turn off these ads.