[英]How to get 16bit arguments/inputs piped into a python program?
我正在尝试将 pipe sox 的 output 转换为 python 程序,如下所示:
sox <audio file name>.flac --type raw --encoding signed-integer - | python3 <file name>.py | head
我对命令行很陌生,但我知道我必须这样做。 我只是不知道如何实际访问我正在输送到我的程序中的数据。 IIRC 当你 pipe 进入它通过 sys.stdin 而不是 sys.argv 的程序时,所以我尝试做的是:
pcm = sys.stdin.buffer.read().decode('utf-16')
但这会引发错误:“UnicodeDecodeError:‘utf-16-be’编解码器无法解码 position 8598-8599 中的字节:非法 UTF-16 代理项”
我也试过open(sys.stdin, 'rb')
但这给了我一个错误,类似于“预期的 str 或 ospath,如 object 而不是 '_io.TextIOWrapper' 对象”
我希望能够读取 16 位组中的十六进制输入,但我真的迷路了。 在这里将不胜感激。 谢谢!
这应该可以正常工作:
./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: uint16
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.