🔧 Where my dotfiles lives in harmony and peace, most of the time
1#!/usr/bin/env bash
2set -euo pipefail
3
4extract_spanish_subs() {
5 local input_file="$1"
6 local output_file="${input_file%.*}.srt"
7
8 # Check if input file exists
9 if [[ ! -f "$input_file" ]]; then
10 echo "Error: Input file '$input_file' does not exist."
11 return 1
12 fi
13
14 echo "Analyzing subtitle tracks in $input_file..."
15
16 # Get subtitle track information
17 local sub_info
18 sub_info=$(ffprobe -v error -select_streams s -show_entries stream=index:stream_tags=language -of csv=p=0 "$input_file")
19
20 # Find Spanish subtitle track index
21 local spa_track=""
22 while IFS="," read -r index lang; do
23 if [[ "$lang" == "spa" || "$lang" == "es" || "$lang" == "spanish" ]]; then
24 spa_track="$index"
25 break
26 fi
27 done <<< "$sub_info"
28
29 # Extract subtitle if Spanish track found
30 if [[ -n "$spa_track" ]]; then
31 echo "Found Spanish subtitle track at index $spa_track. Extracting to $output_file..."
32 ffmpeg -v error -i "$input_file" -map "0:${spa_track}" -c:s srt "$output_file"
33 echo "✅ Spanish subtitles extracted to $output_file"
34 return 0
35 else
36 echo "❌ No Spanish subtitle track found in $input_file."
37 return 1
38 fi
39}
40
41# Run the function if script is executed directly
42if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
43 if [[ $# -eq 0 ]]; then
44 echo "Usage: $(basename "$0") <media_file>"
45 exit 1
46 fi
47
48 extract_spanish_subs "$1"
49fi