Skip to content

Scripting

stax76 edited this page Apr 30, 2019 · 33 revisions

Here is a list of scripts that users of mpv.net have published, adding functionality that is not part of the core mpv.net player. Anyone can add their own script by editing this wiki.

mpv.net supports Lua, JavaScript, Python, C# and PowerShell.

Scripts are placed in:

<config folder>\scripts

If you don't know where the config folder is read here:

https://github.com/stax76/mpv.net#settings

https://mpv.io/manual/master/#lua-scripting

https://mpv.io/manual/master/#files-on-windows

The config folder can also be opened with the context menu at:

Settings > Open Config Folder

Links

mpv Lua and JavaScript documentation: https://mpv.io/manual/master/#lua-scripting

mpv user scripts: https://github.com/mpv-player/mpv/wiki/User-Scripts

C#

Draw text

The following script draws text on the screen when the full screen property changes.

using mpvnet;

class Script
{
    public Script()
    {
        var fs = mp.get_property_string("fullscreen");
        mp.commandv("show-text", "fullscreen: " + fs);
        mp.observe_property_bool("fullscreen", FullscreenChange);
    }

    void FullscreenChange(bool val)
    {
        mp.commandv("show-text", "fullscreen: " + val.ToString());
    }
}

Update context menu

The following script shows how to add track info dynamically to the context menu.

using mpvnet;
using System.ComponentModel;
using System.Linq;

class Script
{
    MainForm MainForm;

    public Script()
    {
        MainForm = mpvnet.MainForm.Instance;
        MainForm.ContextMenu.Opening += ContextMenu_Opening;
    }

    void ContextMenu_Opening(object sender, CancelEventArgs e)
    {
        // please note that you have to edit the input.conf
        // file and add an 'Edition' menu item there
        MenuItem menuItem = MainForm.FindMenuItem("Edition");
        if (menuItem == null) return;
        menuItem.DropDownItems.Clear();
        var editionTracks = mp.MediaTracks.Where(track => track.Type == "e");

        foreach (MediaTrack track in editionTracks)
        {
            MenuItem mi = new MenuItem(track.Text);
            mi.Action = () => { mp.commandv("set", "edition", track.ID.ToString()); };
            mi.Checked = MainForm.MpvEdition == track.ID;
            menuItem.DropDownItems.Add(mi);
        }
    }
}

Python

Display the position and duration while seeking

The following script is used by mpv.net to display the position and duration while seeking. It is located in mpv.net's startup (install) directory.

# when seeking displays position and
# duration like so: 70:00 / 80:00
# which is different from mpv which
# uses 01:10:00 / 01:20:00

import math

def seek():
    pos = mp.get_property_number("time-pos")
    dur = mp.get_property_number("duration")

    if pos > dur:
        pos = dur

    mp.commandv('show-text', format(pos) + " / " + format(dur))

def format(f):
    sec = round(f)
    
    if sec < 0:
        sec = 0
    
    pos_min_floor = math.floor(sec / 60)
    sec_rest = sec - pos_min_floor * 60
    return add_zero(pos_min_floor) + ":" + add_zero(sec_rest)

def add_zero(val):
    val = round(val)
    return "" + str(int(val)) if (val > 9) else "0" + str(int(val))

mp.register_event("seek", seek) # or use: mp.Seek += seek

JavaScript

Display the position and duration while seeking

The following script is used by mpv.net to display the position and duration while seeking.

// when seeking displays position and
// duration like so: 70:00 / 80:00
// which is different from mpv which
// uses 01:10:00 / 01:20:00

function add_zero(val)
{
    val = Math.round(val);
    return val > 9 ? "" + val : "0" + val;
}

function format(val)
{
    var sec = Math.round(val);

    if (sec < 0)
        sec = 0;

    pos_min_floor = Math.floor(sec / 60);
    sec_rest = sec - pos_min_floor * 60;
    return add_zero(pos_min_floor) + ":" + add_zero(sec_rest);
}

function on_seek(_)
{
    mp.commandv("show-text",
                format(mp.get_property_number("time-pos")) + " / " +
                format(mp.get_property_number("duration")));
}

mp.register_event("seek", on_seek);

OSC always on for audio files

function OnFileLoaded() {
    if (mp.get_property_number("dwidth") == undefined) {
        mp.commandv("script-message", "osc-visibility", "always");
    }
}

mp.register_event("file-loaded", OnFileLoaded);

PowerShell

$position = [mp]::get_property_number("time-pos");
[mp]::commandv("show-text", $position.ToString() + " seconds")

Please note that PowerShell is EXTREMELY slow and don't allow assigning to events and mpv.net uses as workaround a matching script filename, a list of available events can be found in the mpv manual or in the file mp.cs in the mpv.net source code.

You can’t perform that action at this time.