Example with MPV Lua - andy3471/GBD GitHub Wiki
Background
MPV is a free, open source, and cross-platform media player. It is a fork of MPlayer and mplayer2 of which it shares some features while introducing many more.
MPV includes support for loading Lua scripts at program start. For example, Lua scripts can be passed via the
--script
CLI option,
or installed in one of the standard MPV configuration directories e.g. ~/.config/mpv/scripts/
.
These scripts can be used to control MPV in a fashion similar to user control via input/slave mode.
See MPV LUA Scripting.
simple-mpv-webui builds on the MPV/Lua backend to provide a web based interface. It employs the lua-socket framework which is a TCP/UDP socket library for the Lua language that includes support for HTTP -- as well as other application layer protocols such as SMTP and FTP.
Installation
Install mpv(1)
. On Debian/Ubuntu:
$ sudo apt-get install mpv
Install the lua-socket dependency for simple-mpv-webui. On Debian/Ubuntu:
$ sudo apt-get install lua-socket
Then obtain simple-mpv-webui, e.g.:
$ git clone https://github.com/open-dynaMIX/simple-mpv-webui
and install its Lua script and webpages for the MPV framework, e.g.:
$ mkdir ~/.config/mpv/scripts
$ cd simple-mpv-webui/
$ cp -a webui.lua webui-page ~/.config/mpv/scripts/
Testing
Assuming that the GBD server is already running, i.e.:
$ gbdserver -p 7777
(see GBD Quick Start)
and that a gbd
ALSA PCM device definition exists in ~/.asoundrc
(see ALSA Configuration),
place a bunch of mp3 files in some directory, e.g. ~/tracks
. For instance
$ ls ~/tracks
bar.mp3 foo.mp3 oof.mp3 rab.mp3
Then launch mpv(1)
from within that directory, i.e.:
$ cd ~/tracks
$ mpv --idle --pause -audio-device=alsa/gbd --loop-playlist *mp3 [-v] [--no-audio-display]
-
NOTE: For older versions of MPV i.e.
mpv --version
less than v0.23.0:$ mpv --idle --pause -ao alsa:device=gbd --loop=inf *mp3 [-v]
(to upgrade MPV on Ubuntu, see for example Ubuntu Handbook, MPV Upgrade)
where:
--loop-playlist
instructsmpv(1)
to loop back to the first track after it plays the last mp3 file in~/tracks
--pause
launchesmpv(1)
in pause state. This can be useful if startingmpv(1)
as a systemd service at boot--idle
makesmpv(1)
wait idly instead of quitting when there is no file to play. This is mostly useful in input mode (a.k.a slave mode), wherempv(1)
can be controlled through input commands.--no-audio-display
or--audio-display=no
prevents the CLI mode MPV from popping up the cover art image (if available) of the audio file
If the -v
switch was specified, the following information (or similar) should get displayed on the mpv(1)
launch terminal:
[cplayer] Command line options: '--idle' '--pause' '-audio-device=alsa/gbd' '--loop-playlist' 'bar.mp3' 'foo.mp3' 'oof.mp3' 'rab.mp3' '-v'
...
[webui] loading mp.defaults
[global] user path: '/home/$USER/.config/mpv/scripts/webui.lua' -> '/home/$USER/.config/mpv/scripts/webui.lua'
[webui] loading file /home/$USER/.config/mpv/scripts/webui.lua
...
[cplayer] Run command: show-text, flags=0, args=[[webui] Serving on ::0 and 0.0.0.0 port 8080, 5000, 0]
[statusline] [webui] Serving on ::0 and 0.0.0.0 port 8080
...
[cplayer] Done loading /home/$USER/.config/mpv/scripts/webui.lua.
...
[file] Opening bar.mp3
[file] Stream opened successfully.
...
[ao] Trying audio driver 'alsa'
[ao] Using preferred device 'gbd'
...
[ao/alsa] period size: 1024 samples
...
[cplayer] AO: [alsa] 44100Hz stereo 2ch s16
[cplayer] AO: Description: ALSA audio output
[statusline] (Paused) A: 00:00:00 / 00:03:44 (0%)
In other words, mpv(1)
starts up according to the settings in ~/.config/mpv/scripts/webui.lua
.
In this scenario, it instantiates a web server instance listening on port 8080, i.e.:
$ cat ~/.config/mpv/scripts/webui.lua
...
local options = {
port = 8080,
disable = false,
logging = false,
ipv4 = true,
ipv6 = true,
}
and,
$ sudo netstat -ltpn | egrep '(^Proto|8080)'
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 17811/mpv
tcp6 0 0 :::8080 :::* LISTEN 17811/mpv
Also notice how mpv(1)
:
- starts in the paused state.
- uses the expected ALSA PCM configuration space (44.1KHz, stereo, 1024 ALSA period size, and S16 sample format --
see ALSA Configuration) courtesy of the
gbd
ALSA PCM device definition.
Now point a web browser to http://$IPADDR:8080/
. The web server instance should serve
the browser with the HTML, CSS, and Javascript under ~/.config/mpv/scripts/webui-page/
.
The following webpage should get displayed:
At the very least, volume settings (second slider control from the top) should be exactly at 100% (default and as shown in the screenshot; maximum range is 130%). The reason for this requirement was already explained in Configuring Applications for GBD.
Pressing the green button should toggle the (remote) mpv(1)
instance's state between paused and
playing.
Test the blue buttons for:
- forward and reverse seeking for the currently playing track,
- selecting the next and previous tracks in the (remote)
~/tracks
directory.
and test toggling the playlist view. For instance:
NOTES:
- The violet (
Next sub
,Next audio
), yellow (Sub delay*
,Audio delay*
) and gray (Fullscreen
,Audio device
) buttons are currently not used with the GBD audio framework. - Older MPV versions (e.g. less than v0.23.0) might not display playlist info and/or the currently loaded track's metadata (Song, Artist, and Album) info. To upgrade MPV on Ubuntu, see for example Ubuntu Handbook, MPV Upgrade.