« get me outta code hell

Add --track-display-file option for meme OBS livestreams - http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-02-12 19:45:52 -0400
committerFlorrie <towerofnix@gmail.com>2018-02-12 19:45:52 -0400
commite249bda854212d9ba29015b0c895b72aa2ee3cad (patch)
tree8e092cc2329c8b0c7eaa2def041df4987b86f342
parentfe65f1777f130ec9d61c5ce06532a551b5dcc899 (diff)
Add --track-display-file option for meme OBS livestreams
-rw-r--r--man/http-music-play.15
-rw-r--r--src/loop-play.js29
-rwxr-xr-xsrc/play.js24
3 files changed, 50 insertions, 8 deletions
diff --git a/man/http-music-play.1 b/man/http-music-play.1
index 7c77c5b..604691d 100644
--- a/man/http-music-play.1
+++ b/man/http-music-play.1
@@ -193,6 +193,11 @@ Especially useful when using an ordered sort; for example, this option could be
 (See also \fB\-\-sort\fR.)
 
 .TP
+.BR \-\-track\-display\-file ", " \-\-display\-track\-file " \fIfilePath\fR"
+Sets the file to output the current track's path to every time a track is played.
+This is mostly useful for interfacing tools like OBS with http-music, for example so that you can display the name/path of the track that is currently playing during a live stream.
+
+.TP
 .BR \-w ", " \-\-write\-playlist ", " \-\-write ", " \-\-save " \fIfilePath\fR"
 Writes the active playlist to a file.
 This file can later be used with \fB\-\-open\fR; you won't need to stick in all the filtering options again.
diff --git a/src/loop-play.js b/src/loop-play.js
index 34ac4d5..e82e77e 100644
--- a/src/loop-play.js
+++ b/src/loop-play.js
@@ -11,9 +11,13 @@
 const { spawn } = require('child_process')
 const FIFO = require('fifo-js')
 const EventEmitter = require('events')
+const fs = require('fs')
+const util = require('util')
 const killProcess = require('./kill-process')
 const { HistoryController, generalPicker } = require('./pickers')
 
+const writeFile = util.promisify(fs.writeFile)
+
 const {
   getDownloaderFor, byName: downloadersByName, makeConverter
 } = require('./downloaders')
@@ -345,14 +349,19 @@ class DownloadController extends EventEmitter {
 }
 
 class PlayController extends EventEmitter {
-  constructor(player, playlist, historyController, downloadController) {
+  constructor({
+    player, playlist, historyController, downloadController,
+    useConverterOptions = true,
+    trackDisplayFile = null // File to output current track path to.
+  }) {
     super()
 
     this.player = player
     this.playlist = playlist
     this.historyController = historyController
     this.downloadController = downloadController
-    this.useConverterOptions = true
+    this.useConverterOptions = useConverterOptions
+    this.trackDisplayFile = trackDisplayFile
 
     this.currentTrack = null
     this.nextTrack = null
@@ -440,6 +449,10 @@ class PlayController extends EventEmitter {
       ])
 
       if (next) {
+        if (this.trackDisplayFile) {
+          await writeFile(this.trackDisplayFile, getItemPathString(this.currentTrack))
+        }
+
         await this.playFile(next)
 
         // Now that we're done playing the file, we should delete it.. unless
@@ -643,7 +656,8 @@ module.exports = async function startLoopPlay(
     pickerOptions, playerCommand, converterCommand,
     useConverterOptions = true,
     disablePlaybackStatus = false,
-    startTrack = null
+    startTrack = null,
+    trackDisplayFile = null
   }
 ) {
   // Looping play function. Takes a playlist and an object containing general
@@ -686,11 +700,12 @@ module.exports = async function startLoopPlay(
     historyController.timeline.push(startTrack)
   }
 
-  const playController = new PlayController(
-    player, playlist, historyController, downloadController
-  )
+  const playController = new PlayController({
+    player, playlist, historyController, downloadController,
+    trackDisplayFile
+  })
 
-  Object.assign(playController, {playerCommand, useConverterOptions})
+  Object.assign(playController, {useConverterOptions})
 
   const promise = playController.loopPlay()
 
diff --git a/src/play.js b/src/play.js
index 75099f9..a3481ea 100755
--- a/src/play.js
+++ b/src/play.js
@@ -103,6 +103,9 @@ async function main(args) {
   // keybinding files.
   let mayTrustShellCommands = true
 
+  // The file to output the playlist path to the current file.
+  let trackDisplayFile
+
   const keybindings = [
     [['space'], 'togglePause'],
     [['left'], 'seek', -5],
@@ -571,6 +574,24 @@ async function main(args) {
 
     '-hide-playback-status': util => util.alias('-disable-playback-status'),
 
+    '-track-display-file': async function(util) {
+      // --track-display-file  (alias: --display-track-file)
+      // Sets the file to output the current track's path to every time a new
+      // track is played. This is mostly useful for using tools like OBS to
+      // interface with http-music, for example so that you can display the
+      // name/path of the track that is currently playing in a live stream.
+      const file = util.nextArg()
+      try {
+        await writeFile(file, 'Not yet playing.')
+      } catch (error) {
+        console.log(`Failed to set track display file to "${file}".`)
+        return
+      }
+      trackDisplayFile = file
+    },
+
+    '-display-track-file': util => util.alias('-track-display-file'),
+
     '-trust-shell-commands': function(util) {
       // --trust-shell-commands  (alias: --trust)
       // Lets keybindings run shell commands. Only use this when loading
@@ -648,7 +669,8 @@ async function main(args) {
         willUseConverterOptions === null && shouldUseConverterOptions
       ),
       disablePlaybackStatus,
-      startTrack
+      startTrack,
+      trackDisplayFile
     })
 
     // We're looking to gather standard input one keystroke at a time.