2025-08-11 08:55:28 +01:00

82 lines
2.1 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# --- args ---
if [[ $# -lt 1 ]]; then
echo "Usage: $0 <file.mp3|file.mp4|URL> [--language <code>] [--model <name>]" >&2
exit 1
fi
INPUT="$1"; shift
LANGUAGE="en"
MODEL="medium"
# Parse flags
while [[ $# -gt 0 ]]; do
case "$1" in
--language) LANGUAGE="$2"; shift 2 ;;
--model) MODEL="$2"; shift 2 ;;
*) echo "Unknown option: $1" >&2; exit 1 ;;
esac
done
# --- helpers ---
lower_ext() {
local f="$1"
local p="${f%%\?*}" # strip query params if any
p="${p%%\#*}" # strip fragments
local e="${p##*.}" # extension
printf '%s' "${e,,}" # lowercase
}
is_url() {
[[ "$1" =~ ^https?:// ]]
}
# --- temp workspace & cleanup ---
WORKDIR="$(mktemp -d)"
trap 'rm -rf "$WORKDIR"' EXIT
SOURCE_PATH="" # where the audio/video comes from (local file after download if URL)
MP3_PATH="" # final mp3 well feed to whisper
# --- stage input (download if URL) ---
if is_url "$INPUT"; then
# Decide ext from URL (we only promise mp3/mp4 support as requested)
ext="$(lower_ext "$INPUT")"
case "$ext" in
mp3|mp4) : ;;
*) # default to mp3 if no/unknown extension (keeps your original intent)
ext="mp3"
;;
esac
SOURCE_PATH="$WORKDIR/input.$ext"
echo "Downloading: $INPUT"
wget -q --show-progress -O "$SOURCE_PATH" "$INPUT"
else
# Local file
if [[ ! -f "$INPUT" ]]; then
echo "Error: file not found: $INPUT" >&2
exit 1
fi
SOURCE_PATH="$INPUT"
ext="$(lower_ext "$SOURCE_PATH")"
fi
# --- transcode if needed ---
case "$ext" in
mp3)
MP3_PATH="$SOURCE_PATH"
;;
mp4)
MP3_PATH="$WORKDIR/audio.mp3"
echo "Transcoding MP4 → MP3 with ffmpeg..."
# -vn: drop video; -q:a 2 ~ VBR high quality (use -b:a 128k if you prefer CBR)
ffmpeg -y -i "$SOURCE_PATH" -vn -acodec libmp3lame -q:a 2 "$MP3_PATH" >/dev/null 2>&1
;;
*)
echo "Error: unsupported extension '$ext'. Only .mp3 or .mp4 are handled." >&2
exit 1
;;
esac
# --- run whisper ---
echo "Running whisper on: $MP3_PATH"
"$WHISPER_EXEC" "$MP3_PATH" --model "$MODEL" --device cuda --language "$LANGUAGE"