BASH Post Services

Viewing: 1744743048_transgram.sh

Try wget https://bash.commongrounds.cc/uploads/1744743048_transgram.sh from the console

Raw File Link

#!/bin/bash

# Transgram v1.0 by Page Telegram Volunteer Services, 2025

# Function to display usage

usage() {
    echo "Transgram v1.0 by Page Telegram Volunteer Services, 2025 w/ Multithreaded Support:"
    echo "Usage: $0 <input_file_or_url> $1 <#threads>"
    echo "Input can be a local A/V file or a URL (https://)"
    echo "Threads is an optional number of CPU threads (default: 1)"
    exit 1
}

# Check if at least one argument is provided
if [ $# -lt 1 ]; then
    usage
fi

INPUT="$1"
THREADS=1  # Default to 1 thread
OUTPUT_DIR="transgram_output"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
TEMP_DIR="/tmp/transgram_$TIMESTAMP"
WHISPER_MODEL="base"  # Default Whisper model, can be changed (e.g., small, medium, large)

# Validate threads if provided
if [ $# -eq 2 ]; then
    if [[ "$2" =~ ^[1-9][0-9]*$ ]]; then
        THREADS="$2"
    else
        echo "Error: Threads must be a positive integer."
        usage
    fi
fi

# Function to detect package manager
detect_package_manager() {
    if command -v apt-get >/dev/null 2>&1; then
        echo "apt"
    elif command -v dnf >/dev/null 2>&1; then
        echo "dnf"
    elif command -v yum >/dev/null 2>&1; then
        echo "yum"
    elif command -v pacman >/dev/null 2>&1; then
        echo "pacman"
    elif command -v zypper >/dev/null 2>&1; then
        echo "zypper"
    else
        echo "unknown"
    fi
}

# Function to install dependencies
install_dependencies() {
    local pm="$1"
    echo "Installing dependencies using $pm..."

    case "$pm" in
        apt)
            sudo apt-get update
            sudo apt-get install -y ffmpeg python3 python3-pip pipx
            ;;
        dnf|yum)
            sudo $pm install -y ffmpeg python3 python3-pip pipx
            ;;
        pacman)
            sudo pacman -Syu --noconfirm ffmpeg python python-pip python-pipx
            ;;
        zypper)
            sudo zypper install -y ffmpeg python3 python3-pip python3-pipx
            ;;
        *)
            echo "Error: Unsupported package manager. Please install ffmpeg, python3, and pipx manually."
            exit 1
            ;;
    esac

    # Ensure pipx is in PATH
    pipx ensurepath >/dev/null 2>&1

    # Install Python packages using pipx
    pipx install openai-whisper
    pipx install yt-dlp
}

# Function to check and install dependencies
check_dependencies() {
    local pm=$(detect_package_manager)
    local missing_deps=()

    # Check for ffmpeg
    if ! command -v ffmpeg >/dev/null 2>&1; then
        missing_deps+=("ffmpeg")
    fi

    # Check for python3
    if ! command -v python3 >/dev/null 2>&1; then
        missing_deps+=("python3")
    fi

    # Check for pipx
    if ! command -v pipx >/dev/null 2>&1; then
        missing_deps+=("pipx")
    fi

    # Check for yt-dlp
    if ! command -v yt-dlp >/dev/null 2>&1; then
        missing_deps+=("yt-dlp")
    fi

    # Check for openai-whisper
    if ! command -v whisper >/dev/null 2>&1; then
        missing_deps+=("openai-whisper")
    fi

    if [ ${#missing_deps[@]} -gt 0 ]; then
        echo "Missing dependencies: ${missing_deps[*]}"
        if [ "$pm" = "unknown" ]; then
            echo "Unknown package manager. Please install ${missing_deps[*]} manually."
            exit 1
        else
            read -p "Install missing dependencies? (y/n): " answer
            if [ "$answer" = "y" ] || [ "$answer" = "Y" ]; then
                install_dependencies "$pm"
            else
                echo "Dependencies not installed. Exiting."
                exit 1
            fi
        fi
    fi
}

# Function to clean up temporary files
cleanup() {
    if [ -d "$TEMP_DIR" ]; then
        rm -rf "$TEMP_DIR"
        echo "Cleaned up temporary directory: $TEMP_DIR"
    fi
}

# Trap to ensure cleanup on exit
trap cleanup EXIT

# Create output and temp directories
mkdir -p "$OUTPUT_DIR" "$TEMP_DIR"

# Check dependencies
check_dependencies

# Function to process local file
process_local_file() {
    local input_file="$1"
    local base_name=$(basename "$input_file" | sed 's/\.[^.]*$//')
    local temp_audio="$TEMP_DIR/${base_name}.wav"

    # Convert to WAV using ffmpeg
    echo "Converting $input_file to WAV..."
    ffmpeg -i "$input_file" -ar 16000 -ac 1 -c:a pcm_s16le "$temp_audio" -y >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "Error: Failed to convert $input_file to WAV."
        exit 1
    fi

    # Run openai-whisper transcription using CLI for all formats
    echo "Transcribing audio to all formats (txt, vtt, srt, tsv, json) using $THREADS thread(s)..."
    whisper --model "$WHISPER_MODEL" --task transcribe --output_format all --output_dir "$TEMP_DIR" --threads "$THREADS" "$temp_audio" >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "Error: Transcription failed."
        exit 1
    fi

    # Move all transcription files to output directory
    for format in txt vtt srt tsv json; do
        local temp_output="$TEMP_DIR/${base_name}.${format}"
        local final_output="$OUTPUT_DIR/${base_name}_transcription.${format}"
        if [ -f "$temp_output" ]; then
            mv "$temp_output" "$final_output"
            if [ $? -ne 0 ]; then
                echo "Error: Failed to save $format transcription."
                exit 1
            fi
            echo "Transcription saved to $final_output"
        else
            echo "Warning: $format output not generated."
        fi
    done
}

# Function to process URL
process_url() {
    local url="$1"
    local base_name="youtube_$TIMESTAMP"
    local temp_video="$TEMP_DIR/${base_name}.mp4"
    local temp_audio="$TEMP_DIR/${base_name}.wav"

    # Download video using yt-dlp
    echo "Downloading video from $url..."
    yt-dlp -o "$temp_video" "$url" >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "Error: Failed to download video from $url."
        exit 1
    fi

    # Convert to WAV using ffmpeg
    echo "Converting downloaded video to WAV..."
    ffmpeg -i "$temp_video" -ar 16000 -ac 1 -c:a pcm_s16le "$temp_audio" -y >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "Error: Failed to convert video to WAV."
        exit 1
    fi

    # Run openai-whisper transcription using CLI for all formats
    echo "Transcribing audio to all formats (txt, vtt, srt, tsv, json) using $THREADS thread(s)..."
    whisper --model "$WHISPER_MODEL" --task transcribe --output_format all --output_dir "$TEMP_DIR" --threads "$THREADS" "$temp_audio" >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "Error: Transcription failed."
        exit 1
    fi

    # Move all transcription files to output directory
    for format in txt vtt srt tsv json; do
        local temp_output="$TEMP_DIR/${base_name}.${format}"
        local final_output="$OUTPUT_DIR/${base_name}_transcription.${format}"
        if [ -f "$temp_output" ]; then
            mv "$temp_output" "$final_output"
            if [ $? -ne 0 ]; then
                echo "Error: Failed to save $format transcription."
                exit 1
            fi
            echo "Transcription saved to $final_output"
        else
            echo "Warning: $format output not generated."
        fi
    done
}

# Main logic
if [[ "$INPUT" == https://* ]]; then
    process_url "$INPUT"
elif [ -f "$INPUT" ]; then
    process_local_file "$INPUT"
else
    echo "Error: Input file does not exist or is not a valid URL."
    usage
fi

exit 0
BASH to Home