Commit 5df6a984f3
Unsigned
Layout: unified 路 split
start_video.py +31
| @@ -19,4 +19,35 @@ def start_video(link, salt:str, args:list=[]): | |||
| 19 | # client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) | 19 | # client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 20 | # client.connect(socket_path) | 20 | # client.connect(socket_path) |
| 21 | 21 | ||
| 22 | def send_command(ipc_socket_path, command): | ||
| 23 | """ | ||
| 24 | Sends a command to the MPV IPC socket and returns the response. | ||
| 25 | """ | ||
| 26 | with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s: | ||
| 27 | s.connect(ipc_socket_path) | ||
| 28 | command_str = json.dumps({"command": command}) + "\n" | ||
| 29 | s.sendall(command_str.encode()) | ||
| 30 | response = s.recv(4096).decode() | ||
| 31 | |||
| 32 | try: | ||
| 33 | response_data = json.loads(response) | ||
| 34 | if 'data' in response_data: | ||
| 35 | return response_data['data'] | ||
| 36 | except json.JSONDecodeError: | ||
| 37 | return None | ||
| 38 | return None | ||
| 39 | |||
| 40 | def get_percentage_watched(ipc_socket_path): | ||
| 41 | """ | ||
| 42 | Calculates the percentage watched of the currently playing video. | ||
| 43 | """ | ||
| 44 | # Get current playback time and total duration | ||
| 45 | current_time = send_command(ipc_socket_path, ["get_property", "time-pos"]) | ||
| 46 | duration = send_command(ipc_socket_path, ["get_property", "duration"]) | ||
| 47 | |||
| 48 | if current_time is not None and duration is not None and duration > 0: | ||
| 49 | percentage_watched = (current_time / duration) * 100 | ||
| 50 | return percentage_watched | ||
| 51 | return None | ||
| 52 | |||
| 22 | # start_video("https://video.wixstatic.com/video/36bbae_bef5207b465447f19c0f9366f3ee1c27/720p/mp4/file.mp4", "10", ['--start=100'])\ No newline at end of file | 53 | # start_video("https://video.wixstatic.com/video/36bbae_bef5207b465447f19c0f9366f3ee1c27/720p/mp4/file.mp4", "10", ['--start=100'])\ No newline at end of file |