
我在 Ubuntu 22.04 平台上。我在 c 中製作了一個簡單的按鈕 GUI t2s
,並將其放置在~/.local/bin
已新增至PATH
環境變數的路徑中。當我按下按鈕時,它正在將麥克風中的語音錄製到臨時檔案中。當我釋放按鈕時 GUI 退出。我運行以下行,該行在終端機中運行良好:
t2s && notify-send -u normal -t 10000 "$( whispercpp -m /home/****/Desktop/2022-10/whisper.cpp/models/ggml-small.bin -nt -l tr -f /dev/shm/mic.wav )"
語音被發送whispercpp
到文字引擎並轉錄為語音。結果顯示在螢幕上的通知中。
但是當我將該行放入文件中並啟動它時,例如:
#!/bin/bash
t2s && notify-send -u normal -t 10000 "$( whispercpp -m /home/****/Desktop/2022-10/whisper.cpp/models/ggml-small.bin -nt -l tr -f /dev/shm/mic.wav )"
exit 0
它只執行GUI按鈕,當釋放按鈕後GUI退出時,它不執行
notify-send -u normal -t 10000 "$( whispercpp -m /home/****/Desktop/2022-10/whisper.cpp/models/ggml-small.bin -nt -l tr -f /dev/shm/mic.wav )"
部分
我究竟做錯了什麼?
編輯:
我也這樣嘗試過:
#!/bin/bash
t2s
TEXT=$( whispercpp -m /home/****/Desktop/2022-10/whisper.cpp/models/ggml-small.bin -nt -l tr -f /dev/shm/mic.wav )"
notify-send -u normal -t 10000 $TEXT
什麼都沒有改變。
編輯:
我注意到它與外殼內部有關。
- https://stackoverflow.com/questions/81520/how-to-suppress-termminate-message-after-killing-in-bash
- https://stackoverflow.com/questions/32551713/how-can-i-silence-the-termerated-message-when-my-command-is-killed-by-timeout
- https://unix.stackexchange.com/questions/65902/why-does-bash-show-termerated-after-killing-a-process
我仍然不知道如何克服它。
答案1
閱讀以下連結後:
我了解到,終止ffmpeg
執行的行會導致 bash shell 在GUIGUI button
之後終止。t2s
我透過在區塊內進行通訊SIGINT
和SIGTERM
訊號來解決這個問題trap
,然後將其餘命令放入其中t2s
:
#!/bin/bash
trap_with_arg() { # from https://stackoverflow.com/a/2183063/804678
local func="$1"; shift
for sig in "$@"; do
trap "$func $sig" "$sig"
done
}
stop() {
trap - SIGINT EXIT
printf '\n%s\n' "received $1, killing child processes"
notify-send -u normal -t 10000 "$(whispercpp -m /home/**/Desktop/2022-10/whisper.cpp/models/ggml-small.bin -nt -l tr -f /dev/shm/mic.wav )"
kill -s SIGINT 0
}
trap_with_arg 'stop' EXIT SIGINT SIGTERM SIGHUP
t2s
exit 0