我可以在bash脚本中与OSX`say`命令的输出进行交互吗? [英] Can I interact with the output of the OSX `say` command in a bash script?
问题描述
我在终端中运行以下行:
I have the following line that I run in terminal:
say "hello, this is the computer talking" --interactive
当我运行此命令时,计算机会说出引号中的单词,并在说出单词时突出显示这些单词.我想做的是获取每个口语的时间.例如:
When I run this command, the computer speaks the words in quotes and highlights the words as they are spoken. What I would like to do is get the time of each spoken word. For example:
- 00.00你好
- 01.23这个
- 01.78是
- 02.10的
- 02.70计算机
- 03.30谈话
我想知道是否有任何方法可以编写可以与该行的输出进行交互的bash脚本.
I am wondering if there is any way to write a bash script that would interact with the output of the line.
推荐答案
这是一个Zsh脚本,几乎可以完全满足您的要求.
Here is a Zsh script that almost does exactly what you want.
#!/bin/zsh
zmodload zsh/datetime
say --interactive "hello, this is the computer talking" | {
counter=0
while IFS= read -r -d $'\r' line; do
(( counter++ )) || continue # first line in the output of `say --interactive` suppresses the cursor; discard this line
timestamp=$EPOCHREALTIME
(( counter == 2 )) && offset=$timestamp # set the timestamp of the actual first line at the offset
(( timestamp -= offset ))
printf '%05.2f %s\n' $timestamp ${${line%$'\e[m'*}#*$'\e[7m'}
done
}
示例输出:
00.00 hello
00.26 ,
00.52 this
00.65 is
00.78 the
01.36 computer
02.04 talking
如果要将其转换为bash,则需要在诸如bc
之类的外部命令中执行浮点运算,并且要获得准确的时间戳,您将需要coreutils date
(timestamp=$(gdate +%s.%N)
).
If you want to convert this to bash, then floating point arithmetic needs to be done in external commands like bc
, and to get a precise timestamp you would need coreutils date
(timestamp=$(gdate +%s.%N)
).
顺便说一句,如果您不想看到逗号,则可以将其过滤掉.
By the way, if you don't want to see the comma, you can just filter it out.
这篇关于我可以在bash脚本中与OSX`say`命令的输出进行交互吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!