From e249bda854212d9ba29015b0c895b72aa2ee3cad Mon Sep 17 00:00:00 2001 From: Florrie Date: Mon, 12 Feb 2018 19:45:52 -0400 Subject: Add --track-display-file option for meme OBS livestreams --- man/http-music-play.1 | 5 +++++ src/loop-play.js | 29 ++++++++++++++++++++++------- src/play.js | 24 +++++++++++++++++++++++- 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 @@ -192,6 +192,11 @@ Sets the track to begin playback from. Especially useful when using an ordered sort; for example, this option could be used to start a long album part way through. (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. 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. -- cgit 1.3.0-6-gf8a5