如何使用 python 在 windows 10 中获取当前播放媒体的标题

How can I get the title of the currently playing media in windows 10 with python

只要在 windows10 中播放音频,无论是来自 Spotify、Firefox 还是游戏。当你调音量时,windows 的角落里有一个东西,上面写着歌曲艺术家、标题和正在播放的应用程序,如下图所示(有时它只说如果游戏正在播放声音)

我想通过 python 以某种方式获取该数据。我的最终目标是在应用程序播放我不喜欢的内容(例如广告)时将其静音。

我在 python 中没有如何执行此操作的示例,但我可以为您指明正确的方向。你想要的是使用 winrt python module https://github.com/microsoft/xlang/blob/master/src/package/pywinrt/projection/readme.md to access WinRT api https://docs.microsoft.com/en-us/uwp/api/windows.media?view=winrt-19041 。没有很多关于 winrt python 模块的文档,因此您可能需要深入研究 win rt api 文档才能弄清楚如何操作。祝你好运!

我正在获取 windows 的标题以获取歌曲信息。通常,应用名称会显示在标题中,但在播放歌曲时,会显示歌曲名称。这是一个 returns 所有 window 标题列表的函数。

from __future__ import print_function
import ctypes
def get_titles(): 
    EnumWindows = ctypes.windll.user32.EnumWindows
    EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
    GetWindowText = ctypes.windll.user32.GetWindowTextW
    GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW
    IsWindowVisible = ctypes.windll.user32.IsWindowVisible
    
    titles = []
    def foreach_window(hwnd, lParam):
        if IsWindowVisible(hwnd):
            length = GetWindowTextLength(hwnd)
            buff = ctypes.create_unicode_buffer(length + 1)
            GetWindowText(hwnd, buff, length + 1)
            titles.append(buff.value)
        return True
    EnumWindows(EnumWindowsProc(foreach_window), 0)
    return titles

事实证明这是可能的没有解决方法并且通过使用Windows运行时直接访问此信息API (winrt).

显示的所有代码都使用 Python 3 和通过 pip 安装的 winrt

收集媒体/'Now Playing'信息

以下代码允许您使用 Windows 运行时 API 的 winrt 包装器收集 Windows 可用的媒体信息字典。它 不像这里的其他答案那样依赖 window 的 title/application 名称更改

import asyncio

from winrt.windows.media.control import \
    GlobalSystemMediaTransportControlsSessionManager as MediaManager


async def get_media_info():
    sessions = await MediaManager.request_async()

    # This source_app_user_model_id check and if statement is optional
    # Use it if you want to only get a certain player/program's media
    # (e.g. only chrome.exe's media not any other program's).

    # To get the ID, use a breakpoint() to run sessions.get_current_session()
    # while the media you want to get is playing.
    # Then set TARGET_ID to the string this call returns.

    current_session = sessions.get_current_session()
    if current_session:  # there needs to be a media session running
        if current_session.source_app_user_model_id == TARGET_ID:
            info = await current_session.try_get_media_properties_async()

            # song_attr[0] != '_' ignores system attributes
            info_dict = {song_attr: info.__getattribute__(song_attr) for song_attr in dir(info) if song_attr[0] != '_'}

            # converts winrt vector to list
            info_dict['genres'] = list(info_dict['genres'])

            return info_dict

    # It could be possible to select a program from a list of current
    # available ones. I just haven't implemented this here for my use case.
    # See references for more information.
    raise Exception('TARGET_PROGRAM is not the current media session')


if __name__ == '__main__':
    current_media_info = asyncio.run(get_media_info())

current_media_info 将是以下格式的字典,然后可以根据需要在程序中访问信息:

{
    'album_artist': str,
    'album_title': str,
    'album_track_count': int, 
    'artist': str,
    'genres': list,
    'playback_type': int,
    'subtitle': str, 
    'thumbnail': 
        <_winrt_Windows_Storage_Streams.IRandomAccessStreamReference object at ?>, 
    'title': str,
    'track_number': int,
}

控制媒体

正如 OP 所说,他们的最终目标是控制媒体,这应该可以通过相同的库实现。可能请参阅此处以获取更多信息(在我的情况下我不需要这个):

(获取媒体缩略图)

实际上也可以 'scrape' 当前正在播放的媒体的专辑 art/media 缩略图(显示在 OP 屏幕截图的右侧)(尽管 OP 没有要求这样做)但有人可能想这样做):

from winrt.windows.storage.streams import \
    DataReader, Buffer, InputStreamOptions


async def read_stream_into_buffer(stream_ref, buffer):
    readable_stream = await stream_ref.open_read_async()
    readable_stream.read_async(buffer, buffer.capacity, InputStreamOptions.READ_AHEAD)


# create the current_media_info dict with the earlier code first
thumb_stream_ref = current_media_info['thumbnail']

# 5MB (5 million byte) buffer - thumbnail unlikely to be larger
thumb_read_buffer = Buffer(5000000)

# copies data from data stream reference into buffer created above
asyncio.run(read_stream_into_buffer(thumb_stream_ref, thumb_read_buffer))

# reads data (as bytes) from buffer
buffer_reader = DataReader.from_buffer(thumb_read_buffer)
byte_buffer = buffer_reader.read_bytes(thumb_read_buffer.length)

with open('media_thumb.jpg', 'wb+') as fobj:
    fobj.write(bytearray(byte_buffer))

这会将 media_thumb.jpg 保存到当前工作目录 (cwd),然后可以在其他地方使用。

文档和参考资料:

可能从多个可用媒体流中选择?

请注意,我尚未对此进行测试或尝试,仅供可能想要进行实验的任何人参考:

相对于目前使用的