[英]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.