forked from woodruffw/ff2mpv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathff2mpv.py
More file actions
executable file
·34 lines (27 loc) · 990 Bytes
/
ff2mpv.py
File metadata and controls
executable file
·34 lines (27 loc) · 990 Bytes
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
#!/usr/bin/env python3
import sys, struct, json
from subprocess import Popen, DEVNULL
def main():
message = get_message()
url = message.get('url')
args = ['mpv', '--no-terminal', '--', url]
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()