From 864b45520acae62962a10f335eab3950ed84c6fc Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Fri, 6 Aug 2021 18:27:44 -0300 Subject: get fractional playback pos & duration from mpv This fixes a side-effect of timestamp files with fractional timestamps: mtui always used to assume it was at .000 of the current second, so it would briefly highlight the previous timestamp before completely passing the second the timestamp starts within. --- players.js | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) (limited to 'players.js') diff --git a/players.js b/players.js index c707494..77f1246 100644 --- a/players.js +++ b/players.js @@ -1,7 +1,13 @@ // stolen from http-music +const { + commandExists, + killProcess, + getTimeStrings, + getTimeStringsFromSec +} = require('./general-util') + const { spawn } = require('child_process') -const { commandExists, killProcess, getTimeStrings } = require('./general-util') const EventEmitter = require('events') const Socat = require('./socat') const fs = require('fs') @@ -88,25 +94,42 @@ class Player extends EventEmitter { } module.exports.MPVPlayer = class extends Player { + // The more powerful MPV player. MPV is virtually impossible for a human + // being to install; if you're having trouble with it, try the SoX player. + getMPVOptions(file, startTime) { - const opts = ['--no-video', file] + const opts = [ + `--term-status-msg='${this.getMPVStatusMessage()}'`, + '--no-video', + file + ] + if (this.isLooping) { opts.unshift('--loop') } + if (this.isPaused) { opts.unshift('--pause') } + if (startTime) { opts.unshift('--start=' + startTime) } + opts.unshift('--volume=' + this.volume * this.volumeMultiplier) + return opts } - playFile(file, startTime) { - // The more powerful MPV player. MPV is virtually impossible for a human - // being to install; if you're having trouble with it, try the SoX player. + getMPVStatusMessage() { + // Note: This function shouldn't include any single-quotes! It probably + // (NOTE: PROBABLY) wouldn't cause any security issues, but it will break + // --term-status-msg parsing and might keep mpv from starting at all. + return '${=time-pos} ${=duration} ${=percent-pos}' + } + + playFile(file, startTime) { this.process = spawn('mpv', this.getMPVOptions(file, startTime).concat(this.processOptions)) let lastPercent = 0 @@ -117,14 +140,14 @@ module.exports.MPVPlayer = class extends Player { } const match = data.toString().match( - /(..):(..):(..) \/ (..):(..):(..) \(([0-9]+)%\)/ + /([0-9.]+) ([0-9.]+) ([0-9.]+)/ ) if (match) { const [ - curHour, curMin, curSec, // ##:##:## - lenHour, lenMin, lenSec, // ##:##:## - percent // ###% + curSecTotal, + lenSecTotal, + percent ] = match.slice(1) if (parseInt(percent) < lastPercent) { @@ -137,7 +160,7 @@ module.exports.MPVPlayer = class extends Player { lastPercent = parseInt(percent) - this.printStatusLine(getTimeStrings({curHour, curMin, curSec, lenHour, lenMin, lenSec})) + this.printStatusLine(getTimeStringsFromSec(curSecTotal, lenSecTotal)) } this.updateVolume(); -- cgit 1.3.0-6-gf8a5