Mpv: Cycle between different commands with one keybinding

Created on 2 Oct 2019  ·  7Comments  ·  Source: mpv-player/mpv

Sorry to post a support question here. I thoroughly searched the documentation and some of the official users scripts from the Wiki, but still couldn't find an answer.

I know about the cycle and cycle-values, which only work for a unique command, and binding more than one command with ;, which executes multiple commands in a row with one key press.

However, I'm specifically looking for a way to cycle between different commands with one keybinding, as a way of limiting the number of keybindings (incredibly useful when you use an IR remote). The closest I've came across is cycle_messages. However I can't make it work with commands, due to my lacking Lua scripting abilities.

Dummy user case:

# input.conf
# show-clock is a third-party script command

i cycle-commands show-text "${filename/no-ext}" show-progress show-clock
option-param-conf scripting

All 7 comments

You can obtain the desired behaviour with a script like this:

// save it with the extension .js 

var state = 0

var commands = [
    'show-text "${filename/no-ext}"',
    'show-progress',
    'show-clock'
]

function perform_action() {
    mp.command(commands[state])
    state = (state + 1) % commands.length
}

mp.add_key_binding('i', 'your-multiple-action', perform_action)

cycle is only for properties. The syntax you're trying doesn't make sense. You probably need to write for anything more advanced.

@viniciusbm Thank you so much for your kindness. Not wanting to abuse it and for the sake of completion, what about concatenating different commands? Can your script be adapted to show all the commands in one line, in the order they're set in the script, separated by a symbol definition, eg. separator = ' | ', like so:

Les Diaboliques | 00:00:30 / 01:30:00 (1%) | 23:30

Edit: I can add the filename and progress info, but not external commands (as far as I know) to the OSD message:

# mpv.conf

osd-msg3="${filename/no-ext} | ${time-pos} / ${duration} (${percent-pos})"

I don't know if you can get the output of some external command, but you don't need a separate script just to get the current time. The following script produces the line you want:

var separator = ' | '

function format_timestamp(t) {
    if (t === null)
        return '??:??:??'
    t = Math.floor(t)
    s = t % 60; t = Math.floor(t / 60)
    m = t % 60; h = Math.floor(t / 60)
    h = (h < 10 ? '0' : '') + h
    m = (m < 10 ? '0' : '') + m
    s = (s < 10 ? '0' : '') + s
    return h + ':' + m + ':' + s
}

function get_time() {
    d = new Date()
    h = d.getHours();   h = (h < 10 ? '0' : '') + h
    m = d.getMinutes(); m = (m < 10 ? '0' : '') + m
    return h + ':' + m
}

function show_things() {
    var title   = mp.get_property('filename/no-ext')
    var current = mp.get_property('playback-time')
    var total   = mp.get_property('duration')
    var percent = mp.get_property('percent-pos') || 0
    var message =
        title +
        separator +
        format_timestamp(current) +
        ' / ' +
        format_timestamp(total) +
        ' (' + Math.floor(percent) + '%)' +
        separator +
        get_time()
    mp.osd_message(message)
}

mp.add_key_binding('a', 'show-things', show_things)

Man, this is pretty hardcore.

Thanks again for your dedication to provide some support to noobs like me. Let me play with this over the weekend, tweak a little and yeah, maybe I won't even need the third-party scripts, after all.

I'll get back to you with a thumbs-up when I'm done. Cheers, man.

@viniciusbm Thanks again, man. With your help, I was able to replace 4 show information triggers with a single keybinding. Hope this issue is helpful to someone else, as well. Cheers.

You're welcome!
I think the syntax of input.conf would become too complicated if it supported multiple commands at once, but fortunately your idea can be implemented with a script.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yuvadm picture yuvadm  ·  3Comments

WoLpH picture WoLpH  ·  3Comments

422658476 picture 422658476  ·  3Comments

xanadupark picture xanadupark  ·  3Comments

szg0000 picture szg0000  ·  3Comments