-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathff2mpv.py
executable file
·64 lines (50 loc) · 2.11 KB
/
ff2mpv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
import json
import os
import platform
import struct
import sys
import subprocess
def main():
message = get_message()
url = message.get("url")
options = message.get("options") or []
args = ["mpv", "--no-terminal", *options, "--", url]
kwargs = {}
# https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Native_messaging#Closing_the_native_app
if platform.system() == "Windows":
kwargs["creationflags"] = subprocess.CREATE_BREAKAWAY_FROM_JOB
# HACK(ww): On macOS, graphical applications inherit their path from `launchd`
# rather than the default path list in `/etc/paths`. `launchd` doesn't include
# Homebrew in its default list, which means that any installations
# of MPV and/or youtube-dl under that prefix aren't visible when spawning
# from, say, Firefox. The real fix is to modify `launchd.conf`, but that's
# invasive and maybe not what users want in the general case.
# Hence this nasty hack.
if platform.system() == "Darwin":
path = os.environ.get("PATH")
os.environ["PATH"] = f"/opt/homebrew/bin:/usr/local/bin:{path}"
subprocess.Popen(args, **kwargs)
# 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):
# https://stackoverflow.com/a/56563264
# https://docs.python.org/3/library/json.html#basic-usage
# To get the most compact JSON representation, you should specify
# (',', ':') to eliminate whitespace.
content = json.dumps(message, separators=(",", ":")).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()