Python入門トップページ


目次

  1. 仮想環境を構築し,watchdog をインストールする
  2. Watchdog でフォルダを監視する
  3. 新しいファイルを検知する
  4. 検知した新しいファイルを待ち行列フォルダに移動する
  5. 文字起こし処理用のワーカープログラムのひな形を作成する
  6. ワーカープログラムを常駐化する
  7. フォルダ監視とワーカープログラムでパイプライン処理する
  8. 処理用フォルダを設置してそこで処理を行う
  9. ファイルサイズの変化を監視する
  10. サイズの大きなファイルのコピーに対応する
  11. タイムスタンプを表示する
  12. Subprocess で外部コマンドを起動する
  13. WhisperX の設定を行う
  14. ワーカープログラムで音声データからの文字起こしを実行する
  15. 文字起こし結果を完了用フォルダに移動する
  16. ログを保存し例外を処理する
  17. サブフォルダに結果を格納する
  18. Ollama をインストールする
  19. Python から Ollama にプロンプトを送る
  20. Ollama 処理を関数化する
  21. 文字起こしデータから議事録を作成する
  22. サイズの大きなテキストファイルにも対応する
  23. ワーカープログラムで文字起こし結果から議事録を生成する
  24. 議事録をマークダウンファイルに出力する
  25. 文字コードを強制的に指定する

フォルダ監視により,音声ファイルから文字起こしと議事録生成を自動化しよう

Subprocess で外部コマンドを起動する

最終的にはワーカープログラムで文字起こしや生成AIのプログラムを起動することを考えます.その第一歩として標準ライブラリである subprocess モジュールを使って,Python コードの中から外部のコマンドを実行してみましょう.まず,watch.py については前のページから変更はありません.ワーカープログラムについては次のとおり編集します.具体的には subprocess をインポートし,echo コマンドを用いて画面に hello watchdog と表示させます.なお,Windows コマンドプロンプトの echo コマンドはこちら,Bash の echo コマンドはこちらを参照してください.また,PowerShell では Write-Output のエイリアス(別名)が echo であることに注意してください.

worker.py
from pathlib import Path
import shutil
import time
import datetime
import subprocess

BASE_DIR = Path(__file__).parent
QUEUE_DIR = BASE_DIR / "queue"
WORKING_DIR = BASE_DIR / "working"
DONE_DIR = BASE_DIR / "done"

# ディレクトリを作っておく
QUEUE_DIR.mkdir(parents=True, exist_ok=True)
WORKING_DIR.mkdir(parents=True, exist_ok=True)
DONE_DIR.mkdir(parents=True, exist_ok=True)

try:
    while True:
        for src in QUEUE_DIR.glob("*.m*"): # 監視するファイルの拡張子
            print(f"[{datetime.datetime.now().strftime('%H:%M:%S.%f')}] 処理開始: {src.name}")
            working_file = WORKING_DIR / src.name
            shutil.move(src, working_file)
            print(f"[{datetime.datetime.now().strftime('%H:%M:%S.%f')}] 作業開始: {working_file}")
            print(f"[{datetime.datetime.now().strftime('%H:%M:%S.%f')}] 処理中...")
            subprocess.run(["echo", "hello watchdog"])
            time.sleep(25)
            dst = DONE_DIR / src.name
            shutil.move(working_file, dst)
            print(f"[{datetime.datetime.now().strftime('%H:%M:%S.%f')}] 完了: {dst}")
        time.sleep(1)

except KeyboardInterrupt:
    print(f"[{datetime.datetime.now().strftime('%H:%M:%S.%f')}] 停止します")

ターミナルを2個起動し,それぞれで watch.py と worker.py を実行します.

(py313watchdog) PS C:\Users\rinsaka\Documents\python\watchdog> python watch.py ⏎

監視用 watch フォルダに4つのマークダウンファイルをまとめて設置しました.

(py313watchdog) PS C:\Users\rinsaka\Documents\python\watchdog> python watch.py
[16:43:40.030951] 新しいファイルを検出: C:\Users\rinsaka\Documents\python\watchdog\watch\sample1.md
[16:43:40.031187] 新しいファイルの名前: sample1.md
[16:43:50.032255] ファイルの受取りが完了しました
[16:43:50.032457] ファイルの移動開始: C:\Users\rinsaka\Documents\python\watchdog\queue\sample1.md
[16:43:50.033379] ファイルの移動完了: C:\Users\rinsaka\Documents\python\watchdog\queue\sample1.md
[16:43:50.033607] 新しいファイルを検出: C:\Users\rinsaka\Documents\python\watchdog\watch\sample2.md
[16:43:50.033762] 新しいファイルの名前: sample2.md
[16:44:00.034908] ファイルの受取りが完了しました
[16:44:00.035040] ファイルの移動開始: C:\Users\rinsaka\Documents\python\watchdog\queue\sample2.md
[16:44:00.047893] ファイルの移動完了: C:\Users\rinsaka\Documents\python\watchdog\queue\sample2.md
[16:44:00.048251] 新しいファイルを検出: C:\Users\rinsaka\Documents\python\watchdog\watch\sample3.md
[16:44:00.048562] 新しいファイルの名前: sample3.md
[16:44:10.049526] ファイルの受取りが完了しました
[16:44:10.049801] ファイルの移動開始: C:\Users\rinsaka\Documents\python\watchdog\queue\sample3.md
[16:44:10.050873] ファイルの移動完了: C:\Users\rinsaka\Documents\python\watchdog\queue\sample3.md
[16:44:10.051079] 新しいファイルを検出: C:\Users\rinsaka\Documents\python\watchdog\watch\sample4.md
[16:44:10.051177] 新しいファイルの名前: sample4.md
[16:44:20.052861] ファイルの受取りが完了しました
[16:44:20.053156] ファイルの移動開始: C:\Users\rinsaka\Documents\python\watchdog\queue\sample4.md
[16:44:20.073478] ファイルの移動完了: C:\Users\rinsaka\Documents\python\watchdog\queue\sample4.md
Ctrl + C
[16:45:37.676945] 停止します
(py313watchdog) PS C:\Users\rinsaka\Documents\python\watchdog>

ワーカープログラムでは「hello watchdog」と表示されていることが分かりました.

(py313watchdog) PS C:\Users\rinsaka\Documents\python\watchdog> python worker.py
[16:43:50.649040] 処理開始: sample1.md
[16:43:50.649617] 作業開始: C:\Users\rinsaka\Documents\python\watchdog\working\sample1.md
[16:43:50.649762] 処理中...
hello watchdog
[16:44:15.662985] 完了: C:\Users\rinsaka\Documents\python\watchdog\done\sample1.md
[16:44:16.663507] 処理開始: sample2.md
[16:44:16.664295] 作業開始: C:\Users\rinsaka\Documents\python\watchdog\working\sample2.md
[16:44:16.664362] 処理中...
hello watchdog
[16:44:41.673980] 完了: C:\Users\rinsaka\Documents\python\watchdog\done\sample2.md
[16:44:41.674235] 処理開始: sample3.md
[16:44:41.675430] 作業開始: C:\Users\rinsaka\Documents\python\watchdog\working\sample3.md
[16:44:41.675557] 処理中...
hello watchdog
[16:45:06.688436] 完了: C:\Users\rinsaka\Documents\python\watchdog\done\sample3.md
[16:45:07.689160] 処理開始: sample4.md
[16:45:07.689734] 作業開始: C:\Users\rinsaka\Documents\python\watchdog\working\sample4.md
[16:45:07.689833] 処理中...
hello watchdog
[16:45:32.700563] 完了: C:\Users\rinsaka\Documents\python\watchdog\done\sample4.md
Ctrl + C
[16:45:38.428107] 停止します
(py313watchdog) PS C:\Users\rinsaka\Documents\python\watchdog>

このように外部のコマンドが正しく動作することが分かりました.

目次に戻る