« get me outta code hell

get fractional playback pos & duration from mpv - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <towerofnix@gmail.com>2021-08-06 18:27:44 -0300
committer(quasar) nebula <towerofnix@gmail.com>2021-08-06 18:27:44 -0300
commit864b45520acae62962a10f335eab3950ed84c6fc (patch)
tree555cbebcb7677f51a8914a5b2e77085179d2e8eb
parent450d751b76574584b1bce70c2108e7fd86510c8f (diff)
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.
-rw-r--r--players.js43
-rw-r--r--todo.txt3
2 files changed, 36 insertions, 10 deletions
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();
diff --git a/todo.txt b/todo.txt
index 3b29b7e..290b8c0 100644
--- a/todo.txt
+++ b/todo.txt
@@ -631,3 +631,6 @@ TODO: "Alphabetize order of groups" order option. Listen to the releases of
 TODO: "Reveal in queue" option in the context menu for tracks that are part of
       the queue! Also, rename existing "Reveal" option to "Reveal in library".
       (Done!)
+
+TODO: Timestamps which have a timestampEnd property (all of them I think?)
+      should display their duration in the right column.