Created
February 4, 2024 08:40
-
-
Save bkbilly/c997b9584825f54bb1fba1b9625dc00a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64 | |
from datetime import datetime | |
import asyncio | |
from winsdk.windows.media.control import GlobalSystemMediaTransportControlsSessionManager as MediaManager | |
from winsdk.windows.storage.streams import Buffer, InputStreamOptions | |
async def get_media_info(): | |
sessions = await MediaManager.request_async() | |
current_session = sessions.get_current_session() | |
thumbnail = bytearray(b"") | |
info = { | |
"title": "", | |
"artist": "", | |
"album": "", | |
"status": "idle", | |
"volume": None, | |
"position": None, | |
"duration": None, | |
} | |
if current_session: | |
session_playing = await current_session.try_get_media_properties_async() | |
session_playback = current_session.get_playback_info() | |
session_timeline = current_session.get_timeline_properties() | |
position = session_timeline.position.seconds | |
if session_playback.playback_status.name == "PLAYING": | |
position += int(datetime.now().timestamp() - session_timeline.last_updated_time.timestamp()) | |
info = { | |
"title": session_playing.title, | |
"artist": session_playing.artist, | |
"album": "", | |
"status": session_playback.playback_status.name.lower(), | |
"volume": None, | |
"position": position, | |
"duration": session_timeline.end_time.seconds, | |
} | |
# Get Thumbnail img | |
buffer = Buffer(5 * 1024 * 1024) | |
readable_stream = await session_playing.thumbnail.open_read_async() | |
await readable_stream.read_async(buffer, buffer.capacity, InputStreamOptions.READ_AHEAD) | |
thumbnail = bytearray(buffer) | |
return info, thumbnail | |
async def media_action(action): | |
sessions = await MediaManager.request_async() | |
current_session = sessions.get_current_session() | |
if current_session: | |
if action == "playpause": | |
await current_session.try_toggle_play_pause_async() | |
elif action == "previous": | |
await current_session.try_skip_previous_async() | |
elif action == "next": | |
await current_session.try_skip_next_async() | |
if __name__ == '__main__': | |
info, thumbnail = asyncio.run(get_media_info()) | |
# print(base64.b64encode(thumbnail).decode()) | |
print(info) | |
with open('media_thumb.jpg', 'wb+') as fobj: | |
fobj.write(thumbnail) | |
# asyncio.run(media_action("playpause")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment