videoinfo.sh

Browser view only. Use the download link if you want to save the file.

Back to Toolbox
#!/bin/bash
source ./launch_random_terminal.sh

# Get the current terminal width
current_width=$(tput cols)

# Check if the width is less than 135
if [ "$current_width" -lt 135 ]; then
    echo "Current terminal width is $current_width. Resizing to 135..."
    printf '\e[8;%d;%dt' "$(tput lines)" 135  # Resize the terminal to 128 columns
else
    echo "Terminal width is already $current_width."
fi

# Enable case-insensitive globbing and nullglob to handle unmatched patterns
shopt -s nocaseglob nullglob

# Check if ffprobe is installed
if ! command -v ffprobe &> /dev/null; then
    echo "Error: ffprobe is required but not found."
    exit 1
fi

# Function to format the duration as XXhYYmZZs
format_duration() {
    local total_seconds=$1
    local hours=$((total_seconds / 3600))
    local minutes=$(( (total_seconds % 3600) / 60 ))
    local seconds=$((total_seconds % 60))

    printf "%01dh %02dm %02ds" $hours $minutes $seconds
}

# Function to apply colored background based on file type
apply_codec_color() {
    local codec=$1
    local filename=$2
    local color_code=""
    
    # Choose color based on file extension
    case "${filename##*.}" in
        mts|m2ts) color_code="\033[43;97m" ;;  # xxx background, white text
        mkv) color_code="\033[41;97m" ;;  # Red background, white text
        mp4) color_code="\033[42;97m" ;;  # Green background, white text
        mpg|mpeg1) color_code="\033[44;97m" ;;  # Blue background, white text
        mp3) color_code="\033[45;97m" ;;  # Magenta background, white text
        *) color_code="\033[47;30m" ;;  # Default: white background, black text
    esac

    # Return codec with colored background and an extra space on both sides
    printf "${color_code} %-10s \033[0m" "$codec"
}

# Collect all video files in the current directory with specified extensions
declare -a filenames=(*.mp4 *.mkv *.mov *.mts *.mpg *.m2ts)

# Check if any video files are found
if [ ${#filenames[@]} -eq 0 ]; then
    echo "No video files found in the current directory with extensions: .mp4, .mkv, .mov, .mts, .mpg, .m2ts"
    exit 1
fi

echo
echo "$0 : Stats about all video files in this folder"
echo

echo "Hang on a sec while I examine those files..."
echo

declare -a widths heights DARS samplerates durations framerates video_codecs audio_codecs prettydurations

# Process each file
for index in "${!filenames[@]}"; do
    filename="${filenames[$index]}"
    if [[ ! -f "$filename" ]]; then
        echo "Warning: File '$filename' does not exist."
        continue
    fi

    # Get video stats using ffprobe
    widths[$index]=$(ffprobe -v error -select_streams v:0 -show_entries stream=width -of default=noprint_wrappers=1:nokey=1 "$filename" | head -n 1)
    heights[$index]=$(ffprobe -v error -select_streams v:0 -show_entries stream=height -of default=noprint_wrappers=1:nokey=1 "$filename" | head -n 1)
    DARS[$index]=$(ffprobe -v error -select_streams v:0 -show_entries stream=display_aspect_ratio -of default=noprint_wrappers=1:nokey=1 "$filename" | head -n 1)
    samplerates[$index]=$(ffprobe -v error -select_streams a:0 -show_entries stream=sample_rate -of default=noprint_wrappers=1:nokey=1 "$filename" | head -n 1)
    durations[$index]=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$filename" | head -n 1)
    framerates[$index]=$(ffprobe -v error -select_streams v:0 -show_entries stream=avg_frame_rate -of default=noprint_wrappers=1:nokey=1 "$filename" | head -n 1)
    video_codecs[$index]=$(ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "$filename" | head -n 1)
    audio_codecs[$index]=$(ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 "$filename" | head -n 1)
        
    # Convert duration to XXhYYmZZs format
    prettydurations[$index]=$(format_duration "${durations[$index]%%.*}")
done


# Output file stats
echo "Files found:"
for index in "${!filenames[@]}"; do
    filename="${filenames[$index]}"
    if [[ -n "${widths[$index]}" ]]; then
        # First line: Filename with codec information, codec padded to 10 spaces and colored
        printf "    %-35s " "$filename"
        apply_codec_color "${video_codecs[$index]}" "$filename"
        printf "  %4dx%-4d, %10s fps, %s aspect, %s audio (%-6s), %s\n" \
            "${widths[$index]}" "${heights[$index]}" "${framerates[$index]}" \
            "${DARS[$index]}" "${samplerates[$index]}" "${audio_codecs[$index]}" "${prettydurations[$index]}"
    fi
done


echo
echo "Done."
echo "Press any key to exit..."
read -n1  # This will wait for a single keypress
kill -- "$PPID"