mpv

package module
v0.0.0-...-9a41887 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 31, 2022 License: MIT Imports: 11 Imported by: 0

README

mpv (remote) control library

This library provides everything needed to (remote) control the mpv media player.

It provides an easy api, a json api and rpc functionality.

Usecases: Remote control your mediaplayer running on a raspberry pi or laptop or build a http interface for mpv

Usage

$ go get github.com/blang/mpv

Note: Always vendor your dependencies or fix on a specific version tag.

Start mpv:

$ mpv --idle --input-ipc-server=/tmp/mpvsocket

Remote control:

import github.com/blang/mpv

ipcc := mpv.NewIPCClient("/tmp/mpvsocket") // Lowlevel client
c := mpv.NewClient(ipcc) // Highlevel client, can also use RPCClient

c.LoadFile("movie.mp4", mpv.LoadFileModeReplace)
c.SetPause(true)
c.Seek(600, mpv.SeekModeAbsolute)
c.SetFullscreen(true)
c.SetPause(false)

pos, err := c.Position()
fmt.Printf("Position in Seconds: %.0f", pos)

Also check the GoDocs.

Features

  • Low-Level and High-Level API
  • RPC Server and Client (fully transparent)
  • HTTP Handler exposing lowlevel API (json)

Contribution

Feel free to make a pull request. For bigger changes create a issue first to discuss about it.

License

See LICENSE file.

Documentation

Index

Constants

View Source
const (
	LoadFileModeReplace    = "replace"
	LoadFileModeAppend     = "append"
	LoadFileModeAppendPlay = "append-play" // Starts if nothing is playing
)

Mode options for Loadfile

View Source
const (
	SeekModeRelative = "relative"
	SeekModeAbsolute = "absolute"
)

Mode options for Seek

View Source
const (
	LoadListModeReplace = "replace"
	LoadListModeAppend  = "append"
)

Mode options for LoadList

View Source
const (
	EventStartFile       = "start-file"
	EventTracksChanged   = "tracks-changed"
	EventMetadataUpdate  = "metadata-update"
	EventAudioReconfig   = "audio-reconfig"
	EventVideoReconfig   = "video-reconfig"
	EventFileLoaded      = "file-loaded"
	EventPlayBackRestart = "playback-restart"
	EventEndFile         = "end-file"
	EventSeek            = "seek"
	EventShutDown        = "shutdown"
	EventLogMessage      = "log-message"
	EventIdle            = "idle"
	EventClientMessage   = "client-message"
	EventPropertyChange  = "property-change"
)

Variables

View Source
var (
	ErrTimeoutSend = errors.New("Timeout while sending command")
	ErrTimeoutRecv = errors.New("Timeout while receiving response")
)

Timeout errors while communicating via IPC

View Source
var ErrInvalidType = errors.New("Invalid type")

ErrInvalidType is returned if the response data does not match the methods return type. Use GetProperty or find matching type in mpv docs.

Functions

func HTTPServerHandler

func HTTPServerHandler(client LLClient) http.Handler

HTTPServerHandler returns a http.Handler to access a client via a lowlevel json-api. Register as route on your server:

http.Handle("/mpv", mpv.HTTPHandler(lowlevelclient)

Use api:

POST http://host/lowlevel Body: { "command": ["get_property", "fullscreen"] }

Result:

{"error":"success","data":false}

Types

type Client

type Client struct {
	LLClient
}

Client is a more comfortable higher level interface to LLClient. It can use any LLClient implementation.

func NewClient

func NewClient(llClient LLClient) *Client

NewClient creates a new highlevel client based on a lowlevel client.

func (*Client) AudioBitrate

func (c *Client) AudioBitrate() int

Get Audio-bitrate

func (*Client) CurerentFileWithPath

func (c *Client) CurerentFileWithPath() string

Path returns the currently playing path

func (*Client) CurrentFile

func (c *Client) CurrentFile() string

Filename returns the currently playing filename

func (*Client) CurrentSpeed

func (c *Client) CurrentSpeed() float64

Speed returns the current playback speed.

func (*Client) CurrentVolume

func (c *Client) CurrentVolume() int

Volume returns the current volume level.

func (*Client) Duration

func (c *Client) Duration() float64

Duration returns the duration of the currently playing file.

func (*Client) FileLoop

func (c *Client) FileLoop() error

loop-file

func (*Client) FileSize

func (c *Client) FileSize() int

Get file-size

func (*Client) FileUnLoop

func (c *Client) FileUnLoop() error

Unloop-file

func (*Client) Format

func (c *Client) Format() string

Get file-format

func (*Client) Fullscreen

func (c *Client) Fullscreen() error

SetFullscreen activates/deactivates the fullscreen mode.

func (*Client) GetBoolProperty

func (c *Client) GetBoolProperty(name string) (bool, error)

GetBoolProperty reads a bool property and returns the data as a boolean.

func (*Client) GetFloatProperty

func (c *Client) GetFloatProperty(name string) (float64, error)

GetFloatProperty reads a float property and returns the data as a float64.

func (*Client) GetProperty

func (c *Client) GetProperty(name string) string

GetProperty reads a property by name and returns the data as a string.

func (*Client) IsFileLoop

func (c *Client) IsFileLoop() bool

Return fileloop status

func (*Client) IsFullscreen

func (c *Client) IsFullscreen() bool

Fullscreen returns true if the player is in fullscreen mode.

func (*Client) IsIdle

func (c *Client) IsIdle() bool

Idle returns true if the player is idle

func (*Client) IsMute

func (c *Client) IsMute() bool

Mute returns true if the player is muted.

func (*Client) IsPause

func (c *Client) IsPause() bool

Pause returns true if the player is paused

func (*Client) IsPlayLoop

func (c *Client) IsPlayLoop() bool

Return playloop status

func (*Client) IsShuffle

func (c *Client) IsShuffle() bool

Return shuffle status

func (*Client) LoadFile

func (c *Client) LoadFile(path string, mode string) error

Loadfile loads a file, it either replaces the currently playing file (LoadFileModeReplace), appends to the current playlist (LoadFileModeAppend) or appends to playlist and plays if nothing is playing right now (LoadFileModeAppendPlay)

func (*Client) LoadList

func (c *Client) LoadList(path string, mode string) error

LoadList loads a playlist from path. It can either replace the current playlist (LoadListModeReplace) or append to the current playlist (LoadListModeAppend).

func (*Client) MediaTitle

func (c *Client) MediaTitle() string

Get media-title

func (*Client) Mute

func (c *Client) Mute() error

SetMute mutes or unmutes the player.

func (*Client) Pause

func (c *Client) Pause() error

SetPause pauses or unpauses the player

func (*Client) PercentPosition

func (c *Client) PercentPosition() float64

PercentPosition returns the current playback position in percent.

func (*Client) PlayClear

func (c *Client) PlayClear() error

Clear Playlist (keep the playing)

func (*Client) PlayIndex

func (c *Client) PlayIndex(n int) error

Play the specified item

func (*Client) PlayIndexRemove

func (c *Client) PlayIndexRemove(n int) error

Remove the specified playlistitem

func (*Client) PlayLoop

func (c *Client) PlayLoop() error

loop-playlist

func (*Client) PlayNext

func (c *Client) PlayNext() error

PlaylistNext plays the next playlistitem or NOP if no item is available.

func (*Client) PlayPos

func (c *Client) PlayPos() int

Return Playlist Current Pos

func (*Client) PlayPrev

func (c *Client) PlayPrev() error

PlaylistPrevious plays the previous playlistitem or NOP if no item is available.

func (*Client) PlayRemove

func (c *Client) PlayRemove() error

Remove current Playlist

func (*Client) PlayShuffle

func (c *Client) PlayShuffle() error

Shuffle the playlist

func (*Client) PlayUnLoop

func (c *Client) PlayUnLoop() error

Unloop-playlist

func (*Client) PlayUnShuffle

func (c *Client) PlayUnShuffle() error

UnShuffle the playlist

func (*Client) Playlist

func (c *Client) Playlist() []string

Return Playlist

func (*Client) PlaylistCount

func (c *Client) PlaylistCount() int

Return the playlist-count

func (*Client) Position

func (c *Client) Position() float64

Position returns the current playback position in seconds.

func (*Client) Quit

func (c *Client) Quit() error

Quit

func (*Client) RegisterEvent

func (c *Client) RegisterEvent(eventName string, handle func())

Register Event HandFunc

func (*Client) Seek

func (c *Client) Seek(n int) error

Seek seeks to a position in the current file.

func (*Client) SetProperty

func (c *Client) SetProperty(name string, value interface{}) error

SetProperty sets the value of a property.

func (*Client) Shuffle

func (c *Client) Shuffle() error

Playlist shuffle

func (*Client) Speed

func (c *Client) Speed(n float64) error

Set playback speed

func (*Client) Stop

func (c *Client) Stop() error

Stop and clear playlist

func (*Client) TimeRemaining

func (c *Client) TimeRemaining() float64

time-remaining

func (*Client) VideoBitrate

func (c *Client) VideoBitrate() int

Get Video-bitrate

func (*Client) Volume

func (c *Client) Volume(level int) error

Set Volume level

type IPCClient

type IPCClient struct {
	// contains filtered or unexported fields
}

IPCClient is a low-level IPC client to communicate with the mpv player via socket.

func NewIPCClient

func NewIPCClient(socket string) *IPCClient

NewIPCClient creates a new IPCClient connected to the given socket.

func (*IPCClient) Exec

func (c *IPCClient) Exec(command ...interface{}) (*Response, error)

Exec executes a command via ipc and returns the response. A request can timeout while sending or while waiting for the response. An error is only returned if there was an error in the communication. The client has to check for `response.Error` in case the server returned an error.

func (*IPCClient) RegisterEvent

func (c *IPCClient) RegisterEvent(name string, fn func())

Register Event Handle Function

type JSONRequest

type JSONRequest struct {
	Command []interface{} `json:"command"`
}

JSONRequest send to the server.

type JSONResponse

type JSONResponse struct {
	Err  string      `json:"error"`
	Data interface{} `json:"data"` // May contain float64, bool or string
}

JSONResponse send from the server.

type LLClient

type LLClient interface {
	Exec(command ...interface{}) (*Response, error)
	RegisterEvent(name string, handle func())
}

LLClient is the most low level interface

type RPCClient

type RPCClient struct {
	// contains filtered or unexported fields
}

RPCClient represents a LLClient over RPC.

func NewRPCClient

func NewRPCClient(client *rpc.Client) *RPCClient

NewRPCClient creates a new RPCClient based on rpc.Client

func (*RPCClient) Exec

func (s *RPCClient) Exec(command ...interface{}) (*Response, error)

Exec executes a command over rpc.

func (*RPCClient) RegisterEvent

func (s *RPCClient) RegisterEvent(name string, handle func())

type RPCServer

type RPCServer struct {
	// contains filtered or unexported fields
}

RPCServer publishes a LLClient over RPC.

func NewRPCServer

func NewRPCServer(client LLClient) *RPCServer

NewRPCServer creates a new RPCServer based on lowlevel client.

func (*RPCServer) Exec

func (s *RPCServer) Exec(args *[]interface{}, res *Response) error

Exec exposes llclient.Exec via RPC. Not intended to be used directly.

type Response

type Response struct {
	Err       string      `json:"error"`
	Data      interface{} `json:"data"` // May contain float64, bool or string
	Event     string      `json:"event"`
	RequestID int         `json:"request_id"`
}

Response received from mpv. Can be an event or a user requested response.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL