Skip to content

Instantly share code, notes, and snippets.

@kepoorz
Created April 8, 2025 05:25
Show Gist options
  • Save kepoorz/bad6a231040f34ae9d51100234496ae6 to your computer and use it in GitHub Desktop.
Save kepoorz/bad6a231040f34ae9d51100234496ae6 to your computer and use it in GitHub Desktop.
whisper.cppとkotoba-whisper-v2.0をシェルから実行するスクリプト
#!/bin/bash
# whisper_tools.sh
#
# このスクリプトは以下の処理を提供します。
#
# --to-wav (-w)
# 入力ファイル(音声/動画)の絶対パスを取得し、16kHz・モノラル・16bit PCM のwavファイルに変換します。
# 出力ファイルは元ファイルの拡張子が.wavに置換されたものとなります。
#
# --transcript (-t)
# 絶対パスのwavファイルを kotoba-whisper (ggml-kotoba-whisper-v2.0.bin)
# で文字起こしし、出力テキストを同じディレクトリに出力します。(日本語認識)
#
# ※ いずれのフラグも指定がなかった場合は、動画ファイルを入力とみなし、
# 音声抽出→文字起こしの処理(動画から直接処理)を実行します。
#
# whisper.cpp のディレクトリパスは以下で指定しています。必要に応じて変更してください。
whisper_dir="PATH_TO_CPP"
############################################
# ヘルプメッセージの表示
############################################
print_help() {
cat <<EOF
Usage: $0 [OPTION] <input_file>
Options:
-w, --to-wav
入力ファイル(音声/動画)を16kHzのwavに変換します。
-t, --transcript
絶対パスのwavファイルを kotoba-whisper (ggml-kotoba-whisper-v2.0.bin)
で文字起こしします。(日本語認識)
-h, --help
このヘルプメッセージを表示します.
※ いずれのフラグも指定しない場合は、動画ファイルとみなし、
音声抽出から文字起こしまでの処理を実行します.
Examples:
$0 --to-wav /path/to/input.mp3
$0 --transcript /path/to/input.wav
$0 /path/to/video.mp4 (フラグなしの場合は動画ファイルとして処理)
EOF
}
############################################
# 関数: convert_to_16k_wav
# 入力ファイルを絶対パスに変換し、16kHz, 1チャンネル, 16bit PCM のwavに変換します。
############################################
convert_to_16k_wav() {
if [ "$#" -ne 1 ]; then
echo "Usage: convert_to_16k_wav <input_file>"
return 1
fi
local abs_input
abs_input=$(realpath "$1")
if [ ! -f "$abs_input" ]; then
echo "入力ファイルが存在しません: $abs_input"
return 1
fi
local output_file="${abs_input%.*}.wav"
echo "変換中: ${abs_input} -> ${output_file}"
ffmpeg -i "$abs_input" -ar 16000 -ac 1 -c:a pcm_s16le "$output_file"
}
############################################
# 関数: run_whisper_transcript
# 絶対パスのwavファイルを kotoba-whisper (ggml-kotoba-whisper-v2.0.bin)
# で文字起こしし、出力ファイルは入力ファイルと同じディレクトリに生成します。
############################################
run_whisper_transcript() {
if [ "$#" -ne 1 ]; then
echo "Usage: run_whisper_transcript <wav_file>"
return 1
fi
local abs_input
abs_input=$(realpath "$1")
if [ ! -f "$abs_input" ]; then
echo "wavファイルが存在しません: $abs_input"
return 1
fi
local output_base="${abs_input%.*}"
echo "文字起こし実行中: ${abs_input}"
echo "出力先: ${output_base}"
pushd "$whisper_dir" > /dev/null
./build/bin/whisper-cli -f "$abs_input" -m models/ggml-kotoba-whisper-v2.0.bin -l ja -otxt -of "$output_base"
popd > /dev/null
}
############################################
# 関数: run_video_pipeline
# フラグ指定がない場合は動画ファイルとみなし、
# 音声抽出 (convert_to_16k_wav) → 文字起こし (run_whisper_transcript) の処理を実行します。
############################################
run_video_pipeline() {
if [ "$#" -ne 1 ]; then
echo "Usage: run_video_pipeline <video_file>"
return 1
fi
local abs_video
abs_video=$(realpath "$1")
if [ ! -f "$abs_video" ]; then
echo "動画ファイルが存在しません: $abs_video"
return 1
fi
echo "動画ファイルから音声抽出開始: ${abs_video}"
convert_to_16k_wav "$abs_video"
local wav_file="${abs_video%.*}.wav"
if [ ! -f "$wav_file" ]; then
echo "wav変換に失敗しました: $wav_file が存在しません。"
return 1
fi
run_whisper_transcript "$wav_file"
}
############################################
# メイン処理
############################################
if [ "$#" -lt 1 ]; then
print_help
exit 1
fi
case "$1" in
-w|--to-wav)
shift
if [ "$#" -ne 1 ]; then
echo "Usage: $0 --to-wav <input_file>"
exit 1
fi
convert_to_16k_wav "$1"
;;
-t|--transcript)
shift
if [ "$#" -ne 1 ]; then
echo "Usage: $0 --transcript <wav_file>"
exit 1
fi
run_whisper_transcript "$1"
;;
-h|--help)
print_help
;;
*)
# 何もフラグが指定されない場合は、動画ファイルとして処理
if [ "$#" -eq 1 ]; then
run_video_pipeline "$1"
else
echo "不正な引数です。"
print_help
exit 1
fi
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment