Files
personal_development/video_transcription/ai_transcriber_v1/extract_audio.sh
T

90 lines
2.6 KiB
Bash
Executable File

#!/bin/bash
# Function to handle single file conversion
convert_file() {
local INPUT_FILE="$1"
# Check if file exists
if [ ! -f "$INPUT_FILE" ]; then
echo "Warning: File '$INPUT_FILE' not found. Skipping."
return
fi
# Get path and filename info
local DIRNAME=$(dirname -- "$INPUT_FILE")
local FILENAME=$(basename -- "$INPUT_FILE")
local FILENAME_NO_EXT="${FILENAME%.*}"
# Define output filename in the same directory
local OUTPUT_FILE="${DIRNAME}/${FILENAME_NO_EXT}.wav"
# Check if we are trying to convert a wav to wav (avoid redundant work or loops)
if [[ "$INPUT_FILE" == *.wav ]]; then
echo "Skipping .wav file: $INPUT_FILE"
return
fi
echo "Processing '$INPUT_FILE' -> '$OUTPUT_FILE'..."
# Run ffmpeg command
# -y overwrites output files without asking
# -v error -stats reduces output verbosity but keeps progress/errors
ffmpeg -i "$INPUT_FILE" -ar 16000 -ac 1 -c:a pcm_s16le -y "$OUTPUT_FILE" < /dev/null -v error -stats
if [ $? -eq 0 ]; then
echo -e "\nSuccess: '$OUTPUT_FILE' created."
else
echo -e "\nError converting '$INPUT_FILE'."
fi
echo "----------------------------------------"
}
# Check if at least one input is provided
if [ -z "$1" ]; then
echo "Usage: $0 <file_or_folder> [file_or_folder2 ...]"
exit 1
fi
# Loop through all provided arguments
for ARG in "$@"; do
# Check for SMB/Network URIs which standard shell tools don't support directly
if [[ "$ARG" == smb://* ]]; then
echo "Error: Network URI '$ARG' detected."
echo "This script works on filesystem paths. Please mount the network share first."
echo " - Linux (GVFS): Check /run/user/\$UID/gvfs/"
echo " - macOS: Check /Volumes/"
echo " - Windows (WSL): Mount the drive to a letter or /mnt/"
continue
fi
if [ -d "$ARG" ]; then
# It's a directory: find files recursively
echo "Scanning directory '$ARG' for video files..."
# Find common video files (case insensitive)
# Using -print0 and while read loop handles filenames with spaces correctly
find "$ARG" -type f \( \
-iname "*.mp4" -o \
-iname "*.mkv" -o \
-iname "*.mov" -o \
-iname "*.avi" -o \
-iname "*.webm" -o \
-iname "*.flv" -o \
-iname "*.wmv" -o \
-iname "*.m4v" -o \
-iname "*.mpg" -o \
-iname "*.mpeg" -o \
-iname "*.3gp" -o \
-iname "*.ts" \
\) -print0 | while IFS= read -r -d '' FOUND_FILE; do
convert_file "$FOUND_FILE"
done
elif [ -f "$ARG" ]; then
# It's a single file
convert_file "$ARG"
else
echo "Warning: '$ARG' is not a valid file or directory."
fi
done