gpt4 book ai didi

python - Winmm.dll 不返回文件长度

转载 作者:行者123 更新时间:2023-11-30 16:08:25 28 4
gpt4 key购买 nike

我正在尝试使用 winmm.dll (ctypes.windll.winmm) 在 python 中播放 .mp3 文件。但是,当我尝试获取某个文件的长度(以毫秒为单位)而不是实际长度(05:23 = 约 323000 毫秒)时,我只得到 3。状态命令获取的时间格式是“m”,并且之后不会改变设置命令。下面是一些说明问题的代码:

from ctypes import windll, c_buffer

fp = 'song.mp3'
alias = 'test'

buf = c_buffer(255)
r = windll.winmm.mciSendStringW(f'open "{fp}" alias {alias}', buf, 254, 0)
print(r)

buf = c_buffer(255)
r = windll.winmm.mciSendStringW(f'status {alias} time format', buf, 254, 0)
print(r, buf.value)

buf = c_buffer(255)
r = windll.winmm.mciSendStringW(f'set {alias} time format milliseconds', buf, 254, 0)
print(r)

buf = c_buffer(255)
r = windll.winmm.mciSendStringW(f'status {alias} time format', buf, 254, 0)
print(r, buf.value)

buf = c_buffer(255)
r = windll.winmm.mciSendStringW(f'status {alias} length', buf, 254, 0)
print(r, buf.value)

及其输出:

0
0 b'm'
0
0 b'm'
0 b'3'

预先感谢您的帮助!

最佳答案

正如我在评论中指出的,这是一个经典的未定义行为示例。检查[SO]: C function called from Python via ctypes returns incorrect value (@CristiFati's answer)更多细节。
此外,您还混合了 8 位16 位 字符串。

列表[Python 3.Docs]: ctypes - A foreign function library for Python .

code00.py:

#!/usr/bin/env python3

import sys
import ctypes as ct
from ctypes import wintypes as wt


def main(*argv):
winmm_dll = ct.WinDLL("Winmm.dll")
mciSendString = winmm_dll.mciSendStringW
mciSendString.argtypes = (wt.LPCWSTR, wt.LPWSTR, wt.UINT, wt.HANDLE)
mciSendString.restype = wt.DWORD

file_name = "./SampleAudio_0.4mb.mp3" # Downloaded from https://www.sample-videos.com/audio/mp3/crowd-cheering.mp3
alias_name = "test00"
status_commands = [
f"status {alias_name} time format",
f"status {alias_name} length",
]

commands = [f"open \"{file_name}\" alias {alias_name}"]
commands.extend(status_commands)
commands.append(f"set {alias_name} time format ms")
commands.extend(status_commands)
commands.append(f"close {alias_name}")

out_buf_len = 0xFF
out_buf = ct.create_unicode_buffer(out_buf_len)
for command in commands:
print(f"Sending {command}...")
res = mciSendString(command, out_buf, out_buf_len, None)
print(f" Result: {res}\n Output: {out_buf.value}")


if __name__ == "__main__":
print("Python {0:s} {1:d}bit on {2:s}\n".format(" ".join(item.strip() for item in sys.version.split("\n")), 64 if sys.maxsize > 0x100000000 else 32, sys.platform))
main(*sys.argv[1:])
print("\nDone.")

输出:

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q059343461]> "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\Scripts\python.exe" code00.py
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] 64bit on win32

Sending open "./SampleAudio_0.4mb.mp3" alias test00...
Result: 0
Output: 1
Sending status test00 time format...
Result: 0
Output: milliseconds
Sending status test00 length...
Result: 0
Output: 27746
Sending set test00 time format ms...
Result: 0
Output:
Sending status test00 time format...
Result: 0
Output: milliseconds
Sending status test00 length...
Result: 0
Output: 27746
Sending close test00...
Result: 0
Output:

Done.

关于python - Winmm.dll 不返回文件长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59343461/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com