Skip to content

Commit f605cf3

Browse files
feat: Add audio transcription function with command line options and error handling
1 parent d12e086 commit f605cf3

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed

.config/bash/bashrc

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,157 @@ extract() {
283283
done
284284
}
285285

286+
# ────────────────────────── Audio Transcription ────────────────────────────
287+
transcribe_audio() {
288+
local input_file=""
289+
local model="medium"
290+
local language="en"
291+
local task="transcribe"
292+
local output_format="txt"
293+
local show_help=false
294+
295+
# Parse command line arguments
296+
while [[ $# -gt 0 ]]; do
297+
case $1 in
298+
-i | --input)
299+
input_file="$2"
300+
shift 2
301+
;;
302+
-m | --model)
303+
model="$2"
304+
shift 2
305+
;;
306+
-l | --language)
307+
language="$2"
308+
shift 2
309+
;;
310+
-t | --task)
311+
task="$2"
312+
shift 2
313+
;;
314+
-f | --format)
315+
output_format="$2"
316+
shift 2
317+
;;
318+
-h | --help)
319+
show_help=true
320+
shift
321+
;;
322+
*)
323+
if [[ -z "$input_file" ]]; then
324+
input_file="$1"
325+
fi
326+
shift
327+
;;
328+
esac
329+
done
330+
331+
# Show help if requested
332+
if [[ "$show_help" == true ]]; then
333+
echo "Usage: transcribe_audio [OPTIONS] INPUT_FILE"
334+
echo ""
335+
echo "Transcribe audio files using FFmpeg and Whisper"
336+
echo ""
337+
echo "Options:"
338+
echo " -i, --input FILE Input audio file (required)"
339+
echo " -m, --model MODEL Whisper model (tiny, base, small, medium, large)"
340+
echo " Default: medium"
341+
echo " -l, --language LANG Language code (en, es, fr, de, etc.)"
342+
echo " Default: en"
343+
echo " -t, --task TASK Task type (transcribe, translate)"
344+
echo " Default: transcribe"
345+
echo " -f, --format FORMAT Output format (txt, json, srt, vtt, tsv)"
346+
echo " Default: txt"
347+
echo " -h, --help Show this help message"
348+
echo ""
349+
echo "Examples:"
350+
echo " transcribe_audio audio.ogg"
351+
echo " transcribe_audio -m large -l es audio.mp3"
352+
echo " transcribe_audio --model base --format srt audio.wav"
353+
return 0
354+
fi
355+
356+
# Check if input file is provided
357+
if [[ -z "$input_file" ]]; then
358+
echo "Error: Input file is required"
359+
echo "Use 'transcribe_audio --help' for usage information"
360+
return 1
361+
fi
362+
363+
# Check if input file exists
364+
if [[ ! -f "$input_file" ]]; then
365+
echo "Error: Input file '$input_file' not found"
366+
return 1
367+
fi
368+
369+
# Check if required tools are available
370+
if ! command -v ffmpeg &>/dev/null; then
371+
echo "Error: ffmpeg is not installed"
372+
echo "Install with: sudo apt install ffmpeg"
373+
return 1
374+
fi
375+
376+
if ! command -v whisper &>/dev/null; then
377+
echo "Error: whisper is not installed"
378+
echo "Install with: pip install openai-whisper"
379+
return 1
380+
fi
381+
382+
# Create temporary directory
383+
local temp_dir=$(mktemp -d)
384+
local temp_audio="$temp_dir/audio.wav"
385+
local temp_output="$temp_dir/audio"
386+
387+
echo "Processing audio file: $input_file"
388+
echo "Model: $model | Language: $language | Task: $task | Format: $output_format"
389+
echo ""
390+
391+
# Convert audio to the format expected by Whisper
392+
echo "Converting audio format..."
393+
if ! ffmpeg -i "$input_file" -ar 16000 -ac 1 "$temp_audio" -loglevel error; then
394+
echo "Error: Failed to convert audio file"
395+
rm -rf "$temp_dir"
396+
return 1
397+
fi
398+
399+
echo "Transcribing audio..."
400+
# Run Whisper transcription
401+
if ! whisper "$temp_audio" \
402+
--model "$model" \
403+
--language "$language" \
404+
--task "$task" \
405+
--output_format "$output_format" \
406+
--output_dir "$temp_dir"; then
407+
echo "Error: Whisper transcription failed"
408+
rm -rf "$temp_dir"
409+
return 1
410+
fi
411+
412+
# Display the result
413+
echo ""
414+
echo "Transcription result:"
415+
echo "────────────────────────────────────────"
416+
417+
local result_file="$temp_output.$output_format"
418+
if [[ -f "$result_file" ]]; then
419+
cat "$result_file"
420+
else
421+
echo "Error: Output file not found"
422+
rm -rf "$temp_dir"
423+
return 1
424+
fi
425+
426+
echo ""
427+
echo "────────────────────────────────────────"
428+
429+
# Cleanup
430+
rm -rf "$temp_dir"
431+
echo "Temporary files cleaned up"
432+
}
433+
434+
# Alias for convenience
435+
alias transcribe='transcribe_audio'
436+
286437
# ────────────────────────── Completions ────────────────────────────
287438
[[ -f /usr/share/bash-completion/bash_completion ]] && source /usr/share/bash-completion/bash_completion
288439
[[ -f ~/.fzf.bash ]] && source ~/.fzf.bash
@@ -319,3 +470,11 @@ export NVM_DIR="$HOME/.nvm"
319470
# ────────────────────────── Launchers ──────────────────────────────
320471

321472
show_system_info
473+
474+
# pnpm
475+
export PNPM_HOME="/home/ranuga/snap/code/194/.local/share/pnpm"
476+
case ":$PATH:" in
477+
*":$PNPM_HOME:"*) ;;
478+
*) export PATH="$PNPM_HOME:$PATH" ;;
479+
esac
480+
# pnpm end

0 commit comments

Comments
 (0)