我试图将sox的输出输送到python程序中,如下所示:
sox <audio file name>.flac --type raw --encoding signed-integer - | python3 <file name>.py | head我对命令行非常陌生,但我知道我必须这样做。我只是不知道如何才能真正地访问我的程序中的数据。当您输入一个程序时,它是通过sys.stdin而不是sys.argv来实现的,所以我尝试做的是:
pcm = sys.stdin.buffer.read().decode('utf-16')但这会引发错误:"UnicodeDecodeError:' UTF-16 -be‘编解码器无法解码位置8598-8599的字节:非法的UTF-16代理程序“。
我也尝试过open(sys.stdin, 'rb'),但这给了我一个类似于“预期的str或ospath类似的对象而不是'_io.TextIOWrapper‘对象的错误”
我希望能够阅读16位组的十六进制输入,但我真的迷路了。希望能在这里提供任何帮助。谢谢!
发布于 2022-04-12 20:59:58
这样做应该很好:
./Generate16BitData | ./Readstdin.py这里的Python代码是:
#!/usr/bin/env python3
import sys
import numpy as np
# Read entire input stream
data = sys.stdin.buffer.read()
print(f'Read {len(data)} bytes')
# Convert into Numpy array of np.uint16
na = np.frombuffer(data, dtype=np.uint16)
print(f'Numpy array shape: {na.shape}, dtype: {na.dtype}')它是这样运行的:
dd if=/dev/urandom bs=1024 count=10 | ./Readstdin.py
10+0 records in
10+0 records out
10240 bytes transferred in 0.000087 secs (117670337 bytes/sec)
Read 10240 bytes
Numpy array shape: (5120,), dtype: uint16https://stackoverflow.com/questions/71848320
复制相似问题