簡體   English   中英

我可以在bash腳本中與OSX“say”命令的輸出進行交互嗎?

[英]Can I interact with the output of the OSX `say` command in a bash script?

我在終端中運行以下行:

say "hello, this is the computer talking" --interactive

當我運行此命令時,計算機會在引號中說出單詞並突出顯示單詞。 我想做的是獲得每個口語的時間。 例如:

  • 00.00你好
  • 01.23這個
  • 01.78是
  • 02.10
  • 02.70電腦
  • 03.30說話

我想知道是否有任何方法可以編寫一個與該行輸出交互的bash腳本。

這是一個幾乎完全符合你想要的Zsh腳本。

#!/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 datetimestamp=$(gdate +%s.%N) )。

順便說一句,如果您不想看到逗號,可以將其過濾掉。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM