参考
pipes — 终端管道接口:https://docs.python.org/zh-cn/3/library/pipes.html
用Python管道SoX-替代子流程?:http://www.cocoachina.com/cms/wap.php?action=article&id=107492
管道简介
pipes 定义了一个类用来抽象 pipeline 的概念 — 将数据从一个文件转到另一文件的转换器序列。
由于模块使用了 /bin/sh 命令行,因此要求有 POSIX 或兼容 os.system() 和 os.popen() 的终端程序。
可用性: Unix。 在 VxWorks 上不可用。
pipes 模块定义了以下的类:
class pipes.Template
其中append
函数中可指定输入输出。如下:
-表示参数输入,-也表示参数输出
使用示例
tr 转换
def test():
import pipes
t = pipes.Template()
t.append('tr a-z A-Z', '--')
f = t.open('pipefile', 'w')
f.write('hello world')
f.close()
return open('pipefile').read()
运行结果:将输入的hello world 转换为大写 HELLO WORLD
sox 截取文件
def test_sox():
from subprocess import Popen, PIPE
kwargs = {
'stdin': PIPE, 'stdout': PIPE, 'stderr': PIPE}
pipe = Popen(['sox', '-t', 'wav', '-', 'pipe_sox.wav', 'trim', '0', '1'], **kwargs)
output, errors = pipe.communicate(input=open('tmp_3412.wav', 'rb').read())
if errors:
raise RuntimeError(errors)
运行结果:将输入的tmp_3412.wav文件进行截取0到1秒,生成pipe_sox.wav