MPV Shim for Plex (Cast to MPV on Linux/Windows)

MPV Shim for Plex

MPV Shim is a simple and lightweight Plex client, with support for Windows and Linux. Think of it as an open source Chromecast for Plex. You can cast almost anything from Plex and it will Direct Play. Subtitles are fully supported, and there are tools to manage them like no other Plex client.

Source Repository: https://github.com/iwalton3/plex-mpv-shim

Getting Started

If you are on Windows, simply download the binary. If you are using Linux or OSX, please see the Linux Installation or OSX Installation sections below.

To use the client, simply launch it and cast your media from another Plex application. The mobile and web applications are supported. You do not have to log in to the client or set it up in any other way.

If you want to use the web application to cast, you must either be on the same network as a Plex server (an empty one will work) or install the MPV Shim Local Connection user script.

The application runs with a notification icon by default. You can use this to view the application log, open the config folder, and open the application menu.

Advanced Features

Menu

To open the menu, press c on your computer or home within the Plex mobile apps.

The menu enables you to:

  • Adjust video transcoding quality.
  • Change the default transcoder settings.
  • Change subtitles or audio, while knowing the track names.
  • Change subtitles or audio for an entire series at once.
  • Mark the media as unwatched and quit.

On your computer, use the arrow keys, enter, and escape to navigate. On your phone, use the arrow buttons, ok, back, and home to navigate. (The option for remote controls is shown next to the name of the client when you select it from the cast menu.)

Please also note that the on-screen controller for MPV (if available) cannot change the audio and subtitle track configurations for transcoded media. It also cannot load external subtitles. You must either use the menu or the application you casted from.

Keyboard Shortcuts

This program supports most of the keyboard shortcuts from MPV. The custom keyboard shortcuts are:

  • < > to skip episodes
  • q to close player
  • w to mark watched and skip
  • u to mark unwatched and quit
  • c to open the menu

Here are the notable MPV keyboard shortcuts:

  • space - Pause/Play
  • left/right - Seek by 5 seconds
  • up/down - Seek by 1 minute
  • s - Take a screenshot
  • S - Take a screenshot without subtitles
  • f - Toggle fullscreen
  • ,/. - Seek by individual frames
  • [/] - Change video speed by 10%
  • {/} - Change video speed by 50%
  • backspace - Reset speed
  • m - Mute
  • d - Enable/disable deinterlace
  • Ctrl+Shift+Left/Right - Adjust subtitle delay.

Configuration

The configuration file is located in different places depending on your platform. You can open the configuration folder using the systray icon. When you launch the program on Linux or OSX from the terminal, the location of the config file will be printed. The locations are:

  • Windows - %appdata%\plex-mpv-shim\conf.json
  • Linux - ~/.config/plex-mpv-shim/conf.json
  • Mac OSX - Library/Application Support/plex-mpv-shim/conf.json
  • CygWin - ~/.config/plex-mpv-shim/conf.json

Transcoding

You can adjust the basic transcoder settings via the menu.

  • always_transcode - This will tell the client to always transcode, without asking. Default: false
    • This may be useful if you are using limited hardware that cannot handle advanced codecs.
    • You may have some luck changing client_profile in the configuration to a more restrictive one.
  • auto_transcode - This will ask the server to determine if transcoding is suggested. Default: true
    • transcode_kbps - Transcode bandwidth to request. Default: 2000
    • direct_limit - Also limit direct play to transcode_kbps. Default: false
    • Note that direct_limit cannot be overriden without changing transcode_kbps.
    • If direct_limit is not set, the player assumes the server will set the limit.
  • adaptive_transcode - Tell the server to adjust the quality while streaming. Default: false

Shell Command Triggers

You can execute shell commands on media state using the config file:

  • media_ended_cmd - When all media has played.
  • pre_media_cmd - Before the player displays. (Will wait for finish.)
  • stop_cmd - After stopping the player.
  • idle_cmd - After no activity for idle_cmd_delay seconds.

Subtitle Visual Settings

All of these settings apply to direct play and are adjustable through the controlling app. Note that some may not work depending on the subtitle codec. Subtitle position and color are not available for transcodes.

  • subtitle_size - The size of the subtitles, in percent. Default: 100
  • subtitle_color - The color of the subtitles, in hex. Default: #FFFFFFFF
  • subtitle_position - The position (top, bottom, middle). Default: bottom

External MPV

The client now supports using an external copy of MPV, including one that is running prior to starting the client. This may be useful if your distribution only provides MPV as a binary executable (instead of as a shared library), or to connect to MPV-based GUI players. Please note that SMPlayer exhibits strange behaviour when controlled in this manner. External MPV is currently the only working backend for media playback on OSX.

  • mpv_ext - Enable usage of the external player by default. Default: false
    • The external player may still be used by default if libmpv1 is not available.
  • mpv_ext_path - The path to the mpv binary to use. By default it uses the one in the PATH. Default: null
    • If you are using Windows, make sure to use two backslashes. Example: C:\\path\\to\\mpv.exe
  • mpv_ext_ipc - The path to the socket to control MPV. Default: null
    • If unset, the socket is a randomly selected temp file.
    • On Windows, this is just a name for the socket, not a path like on Linux.
  • mpv_ext_start - Start a managed copy of MPV with the client. Default: true
    • If not specified, the user must start MPV prior to launching the client.
    • MPV must be launched with --input-ipc-server=[value of mpv_ext_ipc].

Other Configuration Options

  • player_name - The name of the player that appears in the cast menu. Initially set from your hostname.
  • http_port - The TCP port to listen on for Plex to control the player. Default: 3000
  • enable_play_queue - Enable play queue support. Default: true
    • If you disable this, the application will queue media based on the series.
    • This is a legacy feature. It is not regularly tested.
  • client_uuid - The identifier for the client. Set to a random value on first run.
  • audio_ac3passthrough - Does not work. Currently only changes transcoder settings. Default: false
  • audio_dtspassthrough - Does not work. Currently only changes transcoder settings. Default: false
  • allow_http - Allow insecure Plex server connections. Default: false
    • This may be useful if you are using a Plex server offline or not signed in.
  • client_profile - The client profile for transcoding. Default: Plex Home Theater
    • It may be useful to change this on limited hardware.
    • If you change this, it should be changed to a profile that supports hls streaming.
  • sanitize_output - Prevent Plex tokens from being printed to the console. Default: true
  • fullscreen - Fullscreen the player when starting playback. Default: true
  • enable_gui - Enable the system tray icon and GUI features. Default: true
  • media_key_seek - Use the media next/prev keys to seek instead of skip episodes. Default: false
  • enable_osc - Enable the MPV on-screen controller. Default: true
    • It may be useful to disable this if you are using an external player that already provides a user interface.
  • log_decisions - Log the full playback URLs. Default: false
  • mpv_log_level - Log level to use for mpv. Default: info
    • Options: fatal, error, warn, info, v, debug, trace

MPV Configuration

You can configure mpv directly using the mpv.conf and input.conf files. (It is in the same folder as conf.json.) This may be useful for customizing video upscaling, keyboard shortcuts, or controlling the application via the mpv IPC server.

Development

If you’d like to run the application without installing it, run ./run.py. The project is written entierly in Python 3. There are no closed-source components in this project. It is fully hackable.

The project is dependent on python-mpv, python-mpv-jsonipc, and requests. If you are using Windows and would like mpv to be maximize properly, pywin32 is also needed. The GUI component uses pystray and tkinter, but there is a fallback cli mode.

If you are using a local firewall, you’ll want to allow inbound connections on TCP 3000 and UDP 32410, 32412, 32413, and 32414. The TCP port is for the web server the client uses to recieve commands. The UDP ports are for the GDM discovery protocol.

This project is based on https://github.com/wnielson/omplex, which is available under the terms of the MIT License. The project was ported to python3, modified to use mpv as the player, and updated to allow all features of the remote control api for video playback.

Linux Installation

If you are on Linux, you can install via pip. You’ll need libmpv1 or mpv installed.

sudo pip3 install --upgrade plex-mpv-shim

If you would like the GUI and systray features, also install pystray and tkinter:

sudo pip3 install pystray
sudo apt install python3-tk

You can build mpv from source to get better codec support. Execute the following:

sudo pip3 install --upgrade python-mpv
sudo apt install autoconf automake libtool libharfbuzz-dev libfreetype6-dev libfontconfig1-dev libx11-dev libxrandr-dev libvdpau-dev libva-dev mesa-common-dev libegl1-mesa-dev yasm libasound2-dev libpulse-dev libuchardet-dev zlib1g-dev libfribidi-dev git libgnutls28-dev libgl1-mesa-dev libsdl2-dev cmake wget python g++ libluajit-5.1-dev
git clone https://github.com/mpv-player/mpv-build.git
cd mpv-build
echo --enable-libmpv-shared > mpv_options
./rebuild -j4
sudo ./install
sudo ldconfig

OSX Installation

Currently on OSX only the external MPV backend seems to be working. I cannot test on OSX, so please report any issues you find.

To install the CLI version:

  1. Install brew. (Instructions)
  2. Install python3 and mpv. brew install python mpv
  3. Install jellyfin-mpv-shim. pip3 install --upgrade plex-mpv-shim
  4. Run plex-mpv-shim.

If you’d like to install the GUI version, you need a working copy of tkinter.

  1. Install pyenv. (Instructions)
  2. Install TK and mpv. brew install tcl-tk mpv
  3. Install python3 with TK support. FLAGS="-I$(brew --prefix tcl-tk)/include" pyenv install 3.8.1
  4. Set this python3 as the default. pyenv global 3.8.1
  5. Install jellyfin-mpv-shim and pystray. pip3 install --upgrade plex-mpv-shim pystray
  6. Run plex-mpv-shim.

Building on Windows

There is a prebuilt version for Windows in the releases section. When following these directions, please take care to ensure both the python and libmpv libraries are either 64 or 32 bit. (Don’t mismatch them.)

  1. Install Python3 with PATH enabled. Install 7zip.
  2. After installing python3, open cmd as admin and run pip install --upgrade pyinstaller python-mpv requests pywin32 pystray python-mpv-jsonipc.
  3. Download libmpv.
  4. Extract the mpv-1.dll from the file and move it to the plex-mpv-shim folder.
  5. Open a regular cmd prompt. Navigate to the plex-mpv-shim folder.
  6. Run pyinstaller -wF --add-binary "mpv-1.dll;." --add-binary "plex_mpv_shim\systray.png;." --icon media.ico run.py.
16 Likes

Thank you, this is godo stuff!

Congratulations!
I’m not sure what took you so long? :wink:
Seriously though, that was a quick turn around, even if you built on an already existing base. Very impressive.

1 Like

Seems great, but I’m having the same problem as someone on your git page about object required, not str
I fixed it, I made a pull request. It might not be the best solution, but it works

The issue should be fixed now. I also went through the pycharm inspections, so hopefully there are no more issues that I didn’t fix from the python3 porting process.

1 Like

This is fantastic, thanks!

Plex MPV Shim v1.1.1
This is the most recent bugfix release of Plex MPV Shim, which has been tested on Windows and Linux. It includes fixes for video progress saving and also causes the client’s activity to show up in the Plex dashboard. If you haven’t updated since the first revision of Plex MPV Shim, there have been a lot of stability improvements as well.

Install on Windows: Download the Binary

Install on Linux (Debian/Ubuntu):

sudo pip3 install --upgrade plex-mpv-shim

Please note that the Linux version does not bundle libmpv1. You can install it with sudo apt install libmpv1, although support for the on-screen display may require compilation from source. (See the original post for details.)

Plex MPV Shim v1.1.2
This version fixes an issue when casting from the Plex Web Player that causes playback to fail.

Install on Windows: Download the Binary

Install on Linux (requires libmpv1):

sudo pip3 install --upgrade plex-mpv-shim
1 Like

Plex MPV Shim v1.2.0
Plex-MPV-Shim 1.2.0 introduces revamped transcoding support. It will automatically ask the server to see if transcoding is suggested, which enables Plex-MPV-Shim to play more of your library on the go. You can configure this or switch to the old local transcode decision system. It also now supports changing audio and subtitle tracks for transcoded video and avoids issues where playback could fail due to bandwidth limits.

Install on Windows: Download the Binary

Install on Linux (requires libmpv1):

sudo pip3 install --upgrade plex-mpv-shim

Plex MPV Shim v1.6.0
This release contains a few user experience improvements.

  • Make closing the window stop playback (instead of crashing).
  • Prevent window closing between episodes.
  • Bring mpv to the front on windows when launching.
  • Allow disabling launching the player fullscreen.

Plex MPV Shim v1.5.2
This release contains both bug fixes and new features. The new features are:

  • Add support for subtitle color, position, and size settings.
  • Show media title in interface and titlebar.
  • Add new setting to locally limit bandwidth for remote direct play. (Disabled by default.)

Bug fixes:

  • Ensure transcode sessions are properly created and terminated.
  • Improve handling of subtitles transcodes.
  • Remove legacy remote_transcode setting.
  • Prevent tokens being printed to the console by default. (Configurable.)
  • Properly clean up threads on exit.
  • Only show mass subtitle update for episodes.
  • Fix menu that was broken by refactor. (hotfix v1.5.1)
  • Upgrade version of mpv in the Windows build. (hotfix v1.5.2)

Plex MPV Shim v1.4.0
This version introduces the ability to play external subtitles. There are also some performance and reliability improvements. Specifically, the status of the player and user actions are less likely to freeze if a client goes offline or the Plex server is responding slowly to requests.

Plex MPV Shim v1.3.0
Plex-MPV-Shim 1.3 introduces the interactive menu. This allows for features that
would be impossible to support through the remote control interface normally, such as:

  • Change subtitle or audio tracks. The full titles from the media file are shown.
  • Automatically change the subtitle and audio streams for an entire series at once.
  • Adjust video transcoding quality without restarting.
  • Change transcoding preferences without editing the config file.
  • Quit the player, marking the current video unwatched.

The interactive menu supports both the keyboard and the remote control. To open the menu
from a phone, press the ok or home button. Press back to go back or close the menu. The
keyboard shortcut to open the menu is c. Use the arrow keys, escape, and enter/space
to navigate the menu.

Install on Windows: Download the Binary

Install on Linux (requires libmpv1):

sudo pip3 install --upgrade plex-mpv-shim

Plex MPV Shim v1.7.13
Changes:

  • Fix a bug where external MPV was not starting on Windows.
  • Enable volume control support.
  • Log output from MPV.
  • Add more logging configuration options.

Plex MPV Shim v1.7.12
This release:

  • Make external MPV playback more tolerant of startup failures.
  • Add logging for external MPV troubleshooting.

Previous (non-windows point releases):

  • Fix an issue where older MPV versions do not work with external MPV.
  • Isolate pystray into separate process to avoid Segmentation Fault on Linux.
  • Set external MPV as the default player backend on OSX.
  • Enable “forkserver” fix for OSX crashes.

Plex MPV Shim v1.7.8
This release adds support for using an external MPV instead of libmpv1 . If libmpv1 is not available, the player will automatically try to use an external copy of MPV. You can also configure the player to control another instance of MPV that isn’t directly managed. (Please note that this does not work as expected with SMPlayer. You can control the player, but there is a lot of unusual behavior.)

Plex MPV Shim v1.7.2
This release adds a GUI! The application now runs entirely in the background with a system tray icon. You can access the log messages, configuration directory, and integrated menu from the system tray icon.

Install on Windows: Download the Binary

Install on Linux (requires libmpv1 or mpv):

sudo pip3 install --upgrade plex-mpv-shim

Optional to enable the GUI:

sudo pip3 install pystray
sudo apt install python3-tk
1 Like

Thank you for Plex MPV Shim. Your app is the only way yo watch movies on my Z3850 CPU Windows tablet without problems.

It’s awesome.
Finally, I can use the latest MPV with the full features that Emby users already can.

In light of the apparent deprecation of the Plex Media Player Desktop view, I have created a user script called MPV Shim Local Connection. This script allows you to use the Plex Web App to cast to MPV Shim running on the same computer, even when you are not on the same network as a Plex Media Server. You’ll need Greasemonkey (Firefox) or Tampermonkey (Chrome) for the script to work.

Thanks for creating this, I really appreciate it. When I send a video to mpv via this or jellyfin-mpv-shim it always starts at the beginning of the file even if I had partial watch status on the video. Is that expected behavior?

MPV Shim for Plex v1.7.15
Changes:

  • Don’t send the timeline after video playback ends. (Prevents constant refreshing of web client.)

Plex MPV Shim v1.7.14
Changes:

  • Don’t reset font sizes when closing menu.
  • Fix resuming videos with external MPV.

Install on Windows: Download the Binary

Install on Linux (requires libmpv1 or mpv):

sudo pip3 install --upgrade plex-mpv-shim

Optional to enable the GUI:

sudo pip3 install pystray
sudo apt install python3-tk

Thanks for the update, getting the following error on linux.


2020-04-06 15:47:49,645 [ WARNING] Could not find libmpv1.
2020-04-06 15:47:49,647 [    INFO] Using external mpv playback backend.
2020-04-06 15:47:49,648 [   DEBUG] Staring MPV from mpv.
2020-04-06 15:47:49,648 [   DEBUG] Using IPC socket /tmp/mpv96756337668950 for MPV.
2020-04-06 15:47:49,851 [   DEBUG] Found MPV socket.
2020-04-06 15:47:49,859 [   ERROR] EventHandler caught exception from (<function MPV.__init__.<locals>.event_handler at 0x7fa7abfe6b70>, ({'event': 'property-change', 'id': 1, 'name': 'eof-reached'},)).
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/python_mpv_jsonipc.py", line 341, in run
    event[0](*event[1])
  File "/usr/local/lib/python3.6/dist-packages/python_mpv_jsonipc.py", line 420, in event_handler
    self.event_handler.put_task(self.property_bindings[data["id"]], data["name"], data["data"])
KeyError: 'data'
2020-04-06 15:47:49,875 [    INFO] Started GDM service
2020-04-06 15:47:49,875 [    INFO] Started HTTP server

I have patched the jsonipc library. There may be more issues, as this is most likely because your mpv version is older than the one I test against.

sudo pip3 install --upgrade python-mpv-jsonipc
1 Like

Ended up fixing my issue by changing the port from 3000 to 3001. No idea why that was needed, I don’t see any port conflict with “sudo lsof -i -P -n | grep LISTEN”.

Yeah that doesn’t make sense, python-mpv-jsonipc uses a Unix Socket and is not related at all to the http server port. If you have troubles in the future, you can also try installing libmpv1, as that uses a separate control system and cannot be subject to that exception you posted. (It also has slightly better performance.)