feat: support create subtitles (.srt)

This commit is contained in:
ddidderr committed 2026-09-05 16:14:16 +02:00
1 parent ca08ad34bc
commit 4d7fcb1c8f
4 files changed
+210 -11

No files matched your search

+25 -10
View File
@@ -1,25 +1,34 @@
#!/usr/bin/env bash
export OPENROUTER_API_KEY
OPENROUTER_API_KEY="$(op read "op://API Keys/openrouter-mai-transcribe-2/credential")"
set -euo pipefail
srt=false
if [[ ${1:-} == --srt ]]; then
srt=true
shift
fi
if [[ $# == 1 && ($1 == -h || $1 == --help) ]]; then
echo "Usage: ${0##*/} AUDIO_FILE"
echo "Usage: ${0##*/} [--srt] AUDIO_FILE"
echo 'Transcribe audio using OPENROUTER_API_KEY; print the text to stdout.'
echo ' --srt Print SRT subtitles in short cues using word timestamps instead.'
exit 0
fi
if [[ $# != 1 ]]; then
echo "Usage: ${0##*/} AUDIO_FILE" >&2
echo "Usage: ${0##*/} [--srt] AUDIO_FILE" >&2
exit 1
fi
if [[ ! -f $1 || ! -r $1 ]]; then
echo "Cannot read audio file: $1" >&2
exit 1
fi
export OPENROUTER_API_KEY
OPENROUTER_API_KEY="$(op read "op://API Keys/openrouter-mai-transcribe-2/credential")"
: "${OPENROUTER_API_KEY:?Set OPENROUTER_API_KEY first}"
for tool in ffmpeg curl jq base64 mktemp; do
dependencies=(ffmpeg curl jq base64 mktemp)
if "$srt"; then
dependencies+=(python3)
fi
for tool in "${dependencies[@]}"; do
command -v "$tool" >/dev/null || {
echo "Missing dependency: $tool" >&2
exit 1
@@ -37,8 +46,10 @@ ffmpeg -nostdin -hide_banner -loglevel error -i "$(realpath -- "$1")" \
# Keep large audio payloads out of shell arguments (which have a size limit).
base64 <"$tmp/audio.mp3" | tr -d '\n\r' >"$tmp/audio.b64"
jq -n --rawfile data "$tmp/audio.b64" \
'{model: "microsoft/mai-transcribe-2", input_audio: {data: $data, format: "mp3"}}' \
jq -n --rawfile data "$tmp/audio.b64" --argjson srt "$srt" \
'{model: "microsoft/mai-transcribe-2", input_audio: {data: $data, format: "mp3"}}
+ (if $srt then {response_format: "verbose_json", timestamp_granularities: ["segment", "word"]}
else {} end)' \
>"$tmp/request.json"
curl --silent --show-error --fail-with-body \
@@ -49,6 +60,10 @@ curl --silent --show-error --fail-with-body \
cat "$tmp/response.json" >&2
exit 1
}
jq -r 'if (.text | type) == "string" then .text
if "$srt"; then
python3 "$(dirname -- "$(realpath -- "$0")")/subtitles.py" "$tmp/response.json"
else
jq -r 'if (.text | type) == "string" then .text
else error("Transcription failed: " + (.error.message // tojson)) end' \
"$tmp/response.json"
"$tmp/response.json"
fi