我正在使用使用google语音到文本api的脚本。api需要flac编码的文件,因此脚本的记录部分如下所示:
arecord -q -t wav -d 0 -f S16_LE -r 16000 | flac - -f --best --sample-rate 16000 -s -o "$TEMP_FILE"此命令将记录,直到用户使用ctrl-c退出,而wav记录的格式应该以flac格式传输到flac程序,以便以flac格式输出,然后脚本应该继续。
我遇到的问题是,按ctrl完全结束脚本,并切断一些音频( flac文件仍在输出)。如果在没有管道的情况下运行脚本:
arecord -q -t wav -d 0 -f S16_LE -r 16000 some.wav然后按下ctrl只会结束录音,然后继续按它应该的方式继续脚本。
如何解决这个问题,使ctrl只停止arecord命令,并允许脚本的其余部分(包括管道化的flac编码)完成?
发布于 2014-07-02 17:02:19
我要试试这个方法:
# Create a fifo
FIFO=/tmp/my_fifo
mkfifo $FIFO
# Start arecord in background and redirect its output into the fifo
arecord ... > $FIFO &
# Get the arecord PID
PID=$!
# Trap the SIGINT to send SIGINT to arecord
trap "kill -INT $PID" INT
# Start flac and redirect its input with the fifo.
flac - ... < $FIFO
# The script should be blocked here, and a CTRL+C will run
# the kill -INT to the arecord process only.
# But I don't know how flac will react after ...
# If flac exit correctly, just restore the SIGINT
trap - INThttps://stackoverflow.com/questions/24535707
复制相似问题