-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmpv_remote.py
More file actions
405 lines (339 loc) · 15.3 KB
/
Copy pathmpv_remote.py
File metadata and controls
405 lines (339 loc) · 15.3 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import os
import sys
import json
import socket
import random
import subprocess
import urllib.parse
from http.server import HTTPServer, BaseHTTPRequestHandler
# Configuration file
CONFIG_FILE = 'config.json'
def load_config():
# Check if config file exists
if not os.path.exists(CONFIG_FILE):
print(f"ERROR: Configuration file '{CONFIG_FILE}' not found.")
sys.exit(1)
try:
with open(CONFIG_FILE, 'r') as f:
config = json.load(f)
if os.name == 'nt':
required_keys = ["WINDOWS_media_dir", "allowed_extensions", "port", "WINDOWS_mpv_executable", "audio_device"]
else:
required_keys = ["LINUX_media_dir", "allowed_extensions", "port", "LINUX_mpv_executable", "audio_device"]
missing = [key for key in required_keys if key not in config]
if missing:
print(f"ERROR: Missing keys in JSON: {', '.join(missing)}")
sys.exit(1)
if os.name == 'nt':
config["media_dir"] = config["WINDOWS_media_dir"]
config["mpv_executable"] = config["WINDOWS_mpv_executable"]
config["ipc_socket"] = "\\\\.\\pipe\\mpvpipe"
config["screenshot_file"] = ".\\mpv_screenshot.jpg"
else:
config["media_dir"] = config["LINUX_media_dir"]
config["mpv_executable"] = config["LINUX_mpv_executable"]
config["ipc_socket"] = "/tmp/mpv-socket"
config["screenshot_file"] = "/tmp/mpv_screenshot.jpg"
# Optional autoplay settings: list of extensions to auto-queue and maximum items
# `autoplay_extensions` can be like [".mp4", ".mkv"] or ["mp4","mkv"] in config.json
config["autoplay_extensions"] = config.get("autoplay_extensions", [])
if not isinstance(config["autoplay_extensions"], list):
config["autoplay_extensions"] = []
# Normalize extensions to start with a dot and be lowercase
config["autoplay_extensions"] = [(e.lower() if e.startswith('.') else f'.{e.lower()}') for e in config["autoplay_extensions"]]
config["autoplay_max"] = int(config.get("autoplay_max", 20))
return config
except json.JSONDecodeError as e:
print(f"ERROR: '{CONFIG_FILE}' is not a valid JSON file.")
print(f"Details: {e}")
sys.exit(1)
except Exception as e:
print(f"ERROR: Unexpected error while loading configuration: {e}")
sys.exit(1)
# Initialize configuration
CONF = load_config()
# Configuration Shortcuts
MEDIA_DIR = CONF['media_dir']
ALLOWED_EXTENSIONS = set(CONF['allowed_extensions'])
PORT = CONF['port']
MPV_EXE = CONF['mpv_executable']
IPC_SOCKET = CONF['ipc_socket']
SCREENSHOT_TEMP = CONF['screenshot_file']
AUDIO_DEVICE = CONF['audio_device']
NORMALIZE_AUDIO = CONF['normalize_audio']
START_FULLSCREEN = CONF['start_fullscreen']
def send_mpv_command(command):
msg = json.dumps({"command": command}) + "\n"
# print(f"MPV command: {command}")
try:
if os.name == 'nt':
# Implementazione specifica per Windows Named Pipes
with open(IPC_SOCKET, 'r+b', buffering=0) as pipe:
pipe.write(msg.encode())
res = pipe.readline()
# print(f"MPV res: {res}")
return json.loads(res.decode())
else:
client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
client.settimeout(0.2)
client.connect(IPC_SOCKET)
client.send(msg.encode())
res = client.recv(4096)
client.close()
# print(f"MPV res: {res}")
return json.loads(res.decode())
except Exception as e:
# print(f"\n[Socket Error] {e}")
return {"offline": "offline"}
def old_send_mpv_command(command):
client = None
try:
print(f"sendmpv: {command}")
# Estrazione IP e porta dal config
ip, port = IPC_SOCKET.split(':')
# Creazione socket TCP
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.settimeout(2) # Timeout rapido: se non risponde subito, mpv è spento
client.connect((ip, int(port)))
msg = json.dumps({"command": command}) + "\n"
client.send(msg.encode())
res = client.recv(4096)
print(res)
return json.loads(res.decode())
# except (ConnectionRefusedError, ConnectionAbortedError, socket.timeout, OSError):
except Exception as e:
# MPV non sta girando o la connessione è stata rifiutata
print(f"\n[Socket Error] {e}")
return {"error": "offline", "data": None}
finally:
if client:
client.close()
class MPVRemoteHandler(BaseHTTPRequestHandler):
def log_message(self, format, *args):
# Create standard log message
message = format % args
# print(f"[Realtime Log] {message}".ljust(80), flush=True)
def do_GET(self):
url = urllib.parse.urlparse(self.path)
params = urllib.parse.parse_qs(url.query)
path = url.path
# Index
if path == "/":
self.serve_file('./template/index.html', 'text/html')
# Static files
elif os.path.isfile("./template/" + path.lstrip('/')):
self.serve_file("./template/" + path.lstrip('/'))
# API: List Files
elif path == "/api/files":
req_path = params.get('path', [''])[0]
full_path = os.path.normpath(os.path.join(MEDIA_DIR, req_path))
if not os.path.exists(full_path):
self.send_json([])
return
items = []
current_folder_thumb = None
if os.path.exists(os.path.join(full_path, "folder.jpg")):
current_folder_thumb = f"/api/thumb?path={urllib.parse.quote(os.path.join(req_path, 'folder.jpg'))}"
for entry in os.scandir(full_path):
# Otteniamo i dati della entry (incluso mtime)
stats = entry.stat()
mtime = stats.st_mtime
if entry.is_dir():
thumb = None
if os.path.exists(os.path.join(entry.path, "folder.jpg")):
rel = os.path.relpath(entry.path, MEDIA_DIR)
thumb = f"/api/thumb?path={urllib.parse.quote(os.path.join(rel, 'folder.jpg'))}"
items.append({
"name": entry.name, "is_dir": True,
"rel_path": os.path.relpath(entry.path, MEDIA_DIR),
"thumb": thumb, "mtime": mtime
})
else:
ext = os.path.splitext(entry.name)[1].lower()
if ext in ALLOWED_EXTENSIONS:
items.append({
"name": entry.name, "is_dir": False,
"rel_path": os.path.relpath(entry.path, MEDIA_DIR),
"thumb": None, "ext": ext, "mtime": mtime
})
# Restituiamo la lista grezza (l'ordinamento lo farà il client)
self.send_json({
"items": items,
"current_thumb": current_folder_thumb
})
# API: Thumbnail
elif path == "/api/thumb":
rel_path = params.get('path', [''])[0]
full_path = os.path.abspath(os.path.join(MEDIA_DIR, rel_path))
if os.path.exists(full_path) and full_path.startswith(os.path.abspath(MEDIA_DIR)):
self.serve_file(full_path)
else:
self.send_error(404)
# API: Status
elif path == "/api/status":
response = send_mpv_command(["get_property", "path"])
# Se mpv è offline, restituiamo uno stato vuoto coerente
if "offline" in response:
status = {
"pos": 0,
"duration": 0,
"volume": 0,
"paused": True,
"title": "Select a file to play...",
"filename": "",
"folder": "",
"extension": "",
"playlist_pos": "",
"playlist_count": ""
}
else:
full_path = response.get("data", "")
file_name = ""
folder_rel = ""
extension = ""
if full_path:
# Extract the file name
file_name = os.path.basename(full_path)
# Extract the extension
extension = os.path.splitext(file_name)[1]
# Calcoliamo la cartella relativa rispetto a MEDIA_DIR
try:
# Rimuoviamo il prefisso MEDIA_DIR per avere solo la sottocartella
folder_rel = os.path.dirname(os.path.relpath(full_path, os.path.abspath(MEDIA_DIR)))
if folder_rel == ".":
folder_rel = ""
except:
folder_rel = ""
status = {
"pos": send_mpv_command(["get_property", "time-pos"]).get("data", 0),
"duration": send_mpv_command(["get_property", "duration"]).get("data", 0),
"volume": send_mpv_command(["get_property", "volume"]).get("data", 100),
"paused": send_mpv_command(["get_property", "pause"]).get("data", False),
"title": send_mpv_command(["get_property", "media-title"]).get("data", "Select a file"),
"filename": file_name,
"folder": folder_rel,
"extension": extension,
"playlist_pos": send_mpv_command(["get_property", "playlist-pos-1"]).get("data", ""),
"playlist_count": send_mpv_command(["get_property", "playlist-count"]).get("data", "")
}
self.send_json(status)
# API: Screenshot
elif path == "/api/screenshot":
response = send_mpv_command(["screenshot-to-file", SCREENSHOT_TEMP, "video"])
if "offline" in response:
self.serve_file("./template/offline.jpg", 'image/jpeg')
else:
if os.path.exists(SCREENSHOT_TEMP):
self.serve_file(SCREENSHOT_TEMP, 'image/jpeg')
else:
self.send_error(404)
else:
self.send_error(404)
def do_POST(self):
if self.path == "/api/control":
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
data = json.loads(post_data)
cmd = data.get('cmd')
params = data.get('params', [])
if cmd == "play_file":
# Resolve absolute path of requested file
rel_requested = params[0]
file_path = os.path.abspath(os.path.join(MEDIA_DIR, rel_requested))
# Stop any running mpv instance (best-effort)
if os.name == 'nt':
subprocess.run(["taskkill", "/F", "/IM", os.path.basename(MPV_EXE), "/T"], stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)
else:
subprocess.run(["pkill", "-9", os.path.basename(MPV_EXE)])
# Build playlist: start from requested file and include up to autoplay_max files
playlist = [file_path]
try:
autoplay_exts = set(CONF.get('autoplay_extensions', []))
max_items = int(CONF.get('autoplay_max', 20))
if autoplay_exts:
folder = os.path.dirname(file_path)
# List files in same folder, sorted
entries = sorted([f for f in os.listdir(folder) if os.path.isfile(os.path.join(folder, f))], key=lambda s: s.lower())
# Keep only files matching autoplay extensions
matches = [os.path.join(folder, e) for e in entries if os.path.splitext(e)[1].lower() in autoplay_exts]
# Find index of requested file in matches
idx = -1
try:
idx = matches.index(file_path)
except ValueError:
base = os.path.basename(file_path)
for i, p in enumerate(matches):
if os.path.basename(p) == base:
idx = i
break
if idx != -1:
playlist = matches[idx: idx + max_items]
except Exception:
# On any error, fall back to single-file playback
playlist = [file_path]
# Create the MPV command line
options = [f'--input-ipc-server={IPC_SOCKET}',
'--idle',
'--volume=80',
f'--audio-device={AUDIO_DEVICE}'
]
if NORMALIZE_AUDIO == "True":
options.append('--af=dynaudnorm')
if START_FULLSCREEN == "True":
options.append('--fullscreen')
cmd = [MPV_EXE]
cmd.extend(options)
cmd.extend(playlist)
# cmd = [MPV_EXE, options, playlist]
subprocess.Popen(cmd)
# You can add here "IPC_notes" fragments, for study and debug purpose.
self.send_json({"status": "ok"})
else:
res = send_mpv_command([cmd] + params)
self.send_json(res)
def serve_file(self, file_path, content_type=None):
try:
with open(file_path, 'rb') as f:
self.send_response(200)
if content_type:
self.send_header('Content-type', content_type)
self.end_headers()
self.wfile.write(f.read())
except:
self.send_error(404)
def send_json(self, data):
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(data).encode())
if __name__ == '__main__':
if not os.path.exists(MEDIA_DIR):
print("Media path doesn't exist.")
sys.exit(1)
# Recupero Hostname
hostname = socket.gethostname()
# Recupero IP Locale (metodo robusto che non richiede connessione internet attiva)
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
local_ip = s.getsockname()[0]
s.close()
except Exception:
local_ip = "127.0.0.1"
print(f"-------------------------")
print(f"--- MPV Remote Server ---")
print(f"-------------------------")
print(f"Server started on:")
print(f" http://localhost:{PORT}")
print(f" http://{local_ip}:{PORT}")
print(f" http://{hostname}.local:{PORT} (if mDNS is active)")
print()
httpd = HTTPServer(('0.0.0.0', PORT), MPVRemoteHandler)
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nShutting down server...")
httpd.server_close()
except Exception as e:
print(f"\nServer error: {e}")
httpd.server_close()