Skip to content
This repository was archived by the owner on Apr 11, 2018. It is now read-only.

Scripts

chocolateboy edited this page Dec 29, 2010 · 22 revisions

PMSEncoder is designed to make it easy to configure PS3 Media Server (PMS) web video streaming for a wide variety of sites. This is done by writing short configuration files called scripts.

Here's a short example:

script {
    profile ('Politiek 24') {
        pattern {
            match { $URI == 'http://livestreams.omroep.nl/nos/politiek24-bb' }
        }

        action {
            // grab the .asx file and extract the first stream into $URI
            scrape '<Ref\\s+href="(?<URI>[^"]+)"\\s*/>'
        }
    }
}

Scripts are written in Groovy using a simple domain-specific language (DSL) tailored to common web-scraping and transcoder configuration tasks within PMS. The syntax should be easy to pick up for anyone familiar with JavaScript, Java, PHP &c.. In addition, a basic familiarity with regular expressions may be required for some sites.

Syntax

Scripts use a pattern-action DSL that takes a combination of input arguments and transforms it into a PMS command. The keywords within this DSL are methods, many of which take blocks (i.e. closures) as arguments. A number of global variables can be accessed within scripts. Note: keywords and variable names are case-sensitive. Also note that keywords (methods) and the blocks that correspond to them (the blocks passed to those methods) are used interchangeably in the following description e.g. "profile" and "profile block" refer to the same entity (a profile is defined in a profile block).

Each mapping from a set of inputs to a command is defined in a profile block, and multiple profiles can be defined within a script block. A profile consists of a pattern and an action. A pattern consists of one or more match statements, and an action consists of one or more action statements. If a pattern block matches the input parameters, then the corresponding action block is executed. This can happen multiple times, i.e. multiple different profiles can match a given input. Any changes in the input performed in one matching profile are passed through to subsequent profiles.

The DSL is not limited to the following keywords and their arguments. Arbitrary Groovy statements can appear anywhere inside any of these blocks. These can define and modify local and global variables and perform arbitrary calculations e.g..

script {
    def nbcores = $PMS.getConfiguration().getNumberOfCpuCores() // set a local variable
    $DEFAULT_TRANSCODER_ARGS = [ '-lavcopts', "threads=$nbcores" ] // set a global variable (this particular one is set by default)

    profile('Example Profile') {
        // ...
    }
}

Variables and Methods

Variables are scoped to a block and any blocks it contains. Unless otherwise stated, methods are scoped only to their enclosing block.

script

A script block is the top-level of a script. It contains one or more profiles.

Example:

script {
    profile('Empty Profile') { }
}

Variables

The following variables are available within PMSEncoder scripts. Each variable name is followed by its scope (i.e. the topmost block in which it is defined) in parentheses.

$ARGS (Profile)

The list of MEncoder args for this request. By default this is a copy of $DEFAULT_TRANSCODER_ARGS.

$DEFAULT_TRANSCODER_ARGS (Script)

This is a list of the default MEncoder arguments. The current value is defined in the [[builtin pmsencoder.conf|http://github.com/chocolateboy/pmsencoder/blob/plugin/src/main/resources/pmsencoder.groovy]] as :

    def nbcores = $PMS.getConfiguration().getNumberOfCpuCores()

    // the default MEncoder args - these can be redefined in a custom script
    $DEFAULT_TRANSCODER_ARGS = [
        '-prefer-ipv4',
        '-oac', 'lavc',
        '-of', 'lavf',
        '-lavfopts', 'format=dvd',
        '-ovc', 'lavc',
        '-lavcopts', "vcodec=mpeg2video:vbitrate=4096:threads=${nbcores}:acodec=ac3:abitrate=128",
        '-ofps', '25',
        '-cache', '16384', // default cache size; default minimum percentage is 20%
        '-vf', 'harddup'
    ]
Example

Globally double the cache size:

script {
    $DEFAULT_TRANSCODER_ARGS -= [ '-cache', '16384' ]
    $DEFAULT_TRANSCODER_ARGS += [ '-cache', '32768' ]
    // ...
}

$MATCHES

$TRANSCODER_OUT (Profile)

This is the path to the FIFO read by PMS. It should only be necessary to consult this if setting a custom transcoder via $TRANSCODER.

Example

$PMS (Script)

This is an instance of PMS. See the PMS documentation/source for available methods.

Example:

script {
    def nbcores = $PMS.getConfiguration().getNumberOfCpuCores()
    
    profile ('YouTube Extreme HD') {
        pattern {
            match { nbcores > 4 }
        }

        action { ... }
    }
}

$URI (Profile)

The URI of the current web page or video. This is the videostream URI from WEB.conf or the enclosure URI from the Web feed.

Example
script {
    profile ('Example') {
        pattern {
            match { $URI == 'http://www.example.com' }
        }

        action { ... }
    }
}

$YOUTUBE_ACCEPT (Script)

A list of integers corresponding to YouTube formats (full list here).

Example