From a9933887eca7a6885f6f738845f846a5f8f5a114 Mon Sep 17 00:00:00 2001 From: neodarz Date: Tue, 6 Oct 2020 21:43:04 +0200 Subject: Add ability to launch a video via a mpv from firefox It just send the url to an mpv instance, for example launched via mpv --idle --- dotfiles/scripts/ff2mpv.py | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100755 dotfiles/scripts/ff2mpv.py (limited to 'dotfiles/scripts') diff --git a/dotfiles/scripts/ff2mpv.py b/dotfiles/scripts/ff2mpv.py new file mode 100755 index 0000000..acc5d4a --- /dev/null +++ b/dotfiles/scripts/ff2mpv.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 + +import sys +import struct +import json +from subprocess import Popen, DEVNULL + + +def main(): + message = get_message() + url = message.get("url") + + # Need to install playerctl [1] and mpv-mpris [2] + # [1] https://github.com/altdesktop/playerctl + # [2] https://github.com/hoyon/mpv-mpris + # To use with https://addons.mozilla.org/fr/firefox/addon/ff2mpv/ + # Tips: to have mpv in "deamon mode" use mpv --idle + + args = ["playerctl", "--player=mpv", "open", url] + Popen(args, stdin=DEVNULL, stdout=DEVNULL, stderr=DEVNULL) + + args = ["playerctl", "--player=mpv", "play"] + Popen(args, stdin=DEVNULL, stdout=DEVNULL, stderr=DEVNULL) + + # Need to respond something to avoid "Error: An unexpected error occurred" + # in Browser Console. + send_message("ok") + + +# https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Native_messaging#App_side +def get_message(): + raw_length = sys.stdin.buffer.read(4) + if not raw_length: + return {} + length = struct.unpack("@I", raw_length)[0] + message = sys.stdin.buffer.read(length).decode("utf-8") + return json.loads(message) + + +def send_message(message): + content = json.dumps(message).encode("utf-8") + length = struct.pack("@I", len(content)) + sys.stdout.buffer.write(length) + sys.stdout.buffer.write(content) + sys.stdout.buffer.flush() + + +if __name__ == "__main__": + main() -- cgit v1.2.1