Files
rpi-tulivision/install.sh

543 lines
13 KiB
Bash
Executable File

#!/bin/bash
# Raspberry Pi Video Player Auto-Start Installation Script
# This script installs and configures the video player system
set -e # Exit on any error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
INSTALL_DIR="/opt/video_player"
CONFIG_DIR="/etc/video_player"
SERVICE_NAME="video-player"
USER="pi"
VIDEO_FOLDER="${VIDEO_FOLDER:-/home/pi/Videos}"
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to check if running as root
check_root() {
if [[ $EUID -ne 0 ]]; then
print_error "This script must be run as root (use sudo)"
exit 1
fi
}
# Function to check if running on Raspberry Pi
check_raspberry_pi() {
if ! grep -q "Raspberry Pi" /proc/cpuinfo 2>/dev/null; then
print_warning "This script is designed for Raspberry Pi. Continue anyway? (y/N)"
read -r response
if [[ ! "$response" =~ ^[Yy]$ ]]; then
exit 1
fi
fi
}
# Function to update system packages
update_system() {
print_status "Updating system packages..."
apt-get update
apt-get upgrade -y
print_success "System packages updated"
}
# Function to install required packages
install_packages() {
print_status "Installing required packages..."
# Essential packages
apt-get install -y \
python3 \
python3-pip \
python3-dev \
python3-venv \
vlc \
vlc-plugin-base \
ffmpeg \
git \
curl \
wget \
unzip \
build-essential \
cmake \
pkg-config \
libasound2-dev \
libpulse-dev \
libavcodec-dev \
libavformat-dev \
libswscale-dev \
libv4l-dev \
libxvidcore-dev \
libx264-dev \
libjpeg-dev \
libpng-dev \
libtiff-dev \
libatlas-base-dev \
gfortran \
libhdf5-dev \
pulseaudio \
pulseaudio-utils \
alsa-utils \
alsa-tools \
i2c-tools \
spi-tools \
python3-rpi.gpio \
python3-smbus \
python3-spidev \
python3-pil \
python3-setuptools \
python3-wheel
print_success "Required packages installed"
}
# Function to install Python dependencies
install_python_dependencies() {
print_status "Installing Python dependencies..."
# Install pip packages
pip3 install --upgrade pip
pip3 install \
python-vlc \
python-dotenv \
psutil \
pathlib \
PyYAML \
RPi.GPIO \
pigpio \
lirc \
requests \
pillow \
numpy \
opencv-python-headless
print_success "Python dependencies installed"
}
# Function to create directories
create_directories() {
print_status "Creating directories..."
# Create installation directory
mkdir -p "$INSTALL_DIR"
# Create configuration directory
mkdir -p "$CONFIG_DIR"
# Create video folder
mkdir -p "$VIDEO_FOLDER"
chown "$USER:$USER" "$VIDEO_FOLDER"
# Create log directory
mkdir -p /var/log
touch /var/log/video_player.log
chown "$USER:$USER" /var/log/video_player.log
print_success "Directories created"
}
# Function to copy application files
copy_files() {
print_status "Copying application files..."
# Copy Python scripts
cp video_player.py "$INSTALL_DIR/"
cp ir_remote.py "$INSTALL_DIR/"
cp config_manager.py "$INSTALL_DIR/"
# Copy USB auto-mount script
cp usb_automount.sh "$INSTALL_DIR/"
chmod +x "$INSTALL_DIR/usb_automount.sh"
# Copy service files
cp video-player.service /etc/systemd/system/
cp usb-automount.service /etc/systemd/system/
# Copy configuration templates
mkdir -p "$CONFIG_DIR/templates"
cp templates/* "$CONFIG_DIR/templates/" 2>/dev/null || true
# Set permissions
chmod +x "$INSTALL_DIR"/*.py
chmod 644 /etc/systemd/system/video-player.service
print_success "Application files copied"
}
# Function to create default configuration
create_default_config() {
print_status "Creating default configuration..."
# Create main configuration
cat > "$CONFIG_DIR/config.json" << EOF
{
"video_folder": "$VIDEO_FOLDER",
"supported_formats": [".mp4", ".avi", ".mkv", ".mov", ".wmv", ".flv", ".webm", ".m4v"],
"default_channel": 1,
"auto_play": true,
"fullscreen": true,
"window_width": 1920,
"window_height": 1080,
"hide_cursor": true,
"audio_device": "default",
"volume": 50,
"mute_on_start": false,
"channel_timeout": 3.0,
"multi_digit_timeout": 1.0,
"channel_display_timeout": 2.0,
"channel_assignment_method": "alphabetical",
"ir_pin": 18,
"ir_protocols": ["NEC", "RC5"],
"ir_repeat_delay": 0.1,
"vlc_options": [
"--fullscreen",
"--no-video-title-show",
"--no-audio-display",
"--no-osd",
"--quiet"
],
"log_level": "INFO",
"log_file": "/var/log/video_player.log",
"log_max_size": 10485760,
"log_backup_count": 5,
"check_interval": 1.0,
"restart_on_crash": true,
"max_restart_attempts": 3
}
EOF
# Create sample channels configuration
cat > "$CONFIG_DIR/channels.json" << EOF
{
"1": {
"number": 1,
"name": "Sample Video 1",
"path": "$VIDEO_FOLDER/sample1.mp4",
"description": "First sample video",
"category": "general",
"enabled": true,
"priority": 0
}
}
EOF
# Create default IR mapping
cat > "$CONFIG_DIR/ir_mapping.json" << EOF
{
"NEC_00FF_00FF": {
"ir_code": "NEC_00FF_00FF",
"command": "power_toggle",
"description": "Power button",
"repeatable": true
},
"NEC_00FF_807F": {
"ir_code": "NEC_00FF_807F",
"command": "channel_0",
"description": "Channel 0",
"repeatable": true
},
"NEC_00FF_40BF": {
"ir_code": "NEC_00FF_40BF",
"command": "channel_1",
"description": "Channel 1",
"repeatable": true
},
"NEC_00FF_C03F": {
"ir_code": "NEC_00FF_C03F",
"command": "channel_2",
"description": "Channel 2",
"repeatable": true
},
"NEC_00FF_20DF": {
"ir_code": "NEC_00FF_20DF",
"command": "channel_3",
"description": "Channel 3",
"repeatable": true
},
"NEC_00FF_A05F": {
"ir_code": "NEC_00FF_A05F",
"command": "channel_4",
"description": "Channel 4",
"repeatable": true
},
"NEC_00FF_609F": {
"ir_code": "NEC_00FF_609F",
"command": "channel_5",
"description": "Channel 5",
"repeatable": true
},
"NEC_00FF_E01F": {
"ir_code": "NEC_00FF_E01F",
"command": "channel_6",
"description": "Channel 6",
"repeatable": true
},
"NEC_00FF_10EF": {
"ir_code": "NEC_00FF_10EF",
"command": "channel_7",
"description": "Channel 7",
"repeatable": true
},
"NEC_00FF_906F": {
"ir_code": "NEC_00FF_906F",
"command": "channel_8",
"description": "Channel 8",
"repeatable": true
},
"NEC_00FF_50AF": {
"ir_code": "NEC_00FF_50AF",
"command": "channel_9",
"description": "Channel 9",
"repeatable": true
},
"REPEAT": {
"ir_code": "REPEAT",
"command": "repeat_last",
"description": "Repeat last command",
"repeatable": true
}
}
EOF
# Set permissions
chown -R "$USER:$USER" "$CONFIG_DIR"
chmod -R 755 "$CONFIG_DIR"
print_success "Default configuration created"
}
# Function to setup USB auto-mounting
setup_usb_automount() {
print_status "Setting up USB auto-mounting..."
# Create USB mount directory
mkdir -p /media/usb
chown "$USER:$USER" /media/usb
chmod 755 /media/usb
# Copy udev rules
cp 99-usb-automount.rules /etc/udev/rules.d/
chmod 644 /etc/udev/rules.d/99-usb-automount.rules
# Reload udev rules
udevadm control --reload-rules
udevadm trigger
# Enable USB auto-mount service
systemctl daemon-reload
systemctl enable usb-automount
print_success "USB auto-mounting configured"
}
# Function to setup GPIO permissions
setup_gpio_permissions() {
print_status "Setting up GPIO permissions..."
# Add user to gpio group
usermod -a -G gpio "$USER"
# Create udev rule for GPIO access
cat > /etc/udev/rules.d/99-gpio.rules << EOF
SUBSYSTEM=="gpio", GROUP="gpio", MODE="0664"
SUBSYSTEM=="gpio*", PROGRAM="/bin/sh -c 'chown -R root:gpio /sys/class/gpio && chmod -R 775 /sys/class/gpio; chown -R root:gpio /sys/devices/virtual/gpio && chmod -R 775 /sys/devices/virtual/gpio'"
EOF
# Reload udev rules
udevadm control --reload-rules
udevadm trigger
print_success "GPIO permissions configured"
}
# Function to enable and start service
setup_service() {
print_status "Setting up systemd service..."
# Reload systemd daemon
systemctl daemon-reload
# Enable service
systemctl enable "$SERVICE_NAME"
print_success "Service configured"
}
# Function to create sample video
create_sample_video() {
print_status "Creating sample video..."
# Check if ffmpeg is available
if command -v ffmpeg &> /dev/null; then
# Create a simple test video
ffmpeg -f lavfi -i testsrc=duration=10:size=1920x1080:rate=30 \
-f lavfi -i sine=frequency=1000:duration=10 \
-c:v libx264 -c:a aac \
-shortest "$VIDEO_FOLDER/sample1.mp4" -y 2>/dev/null || true
if [[ -f "$VIDEO_FOLDER/sample1.mp4" ]]; then
chown "$USER:$USER" "$VIDEO_FOLDER/sample1.mp4"
print_success "Sample video created"
else
print_warning "Could not create sample video (ffmpeg may not be available)"
fi
else
print_warning "ffmpeg not available, skipping sample video creation"
fi
}
# Function to create desktop shortcut
create_desktop_shortcut() {
print_status "Creating desktop shortcut..."
# Create desktop entry
cat > "/home/$USER/Desktop/Video Player.desktop" << EOF
[Desktop Entry]
Version=1.0
Type=Application
Name=Video Player
Comment=Raspberry Pi Video Player with IR Remote
Exec=sudo systemctl start $SERVICE_NAME
Icon=vlc
Terminal=false
Categories=AudioVideo;Player;
EOF
chown "$USER:$USER" "/home/$USER/Desktop/Video Player.desktop"
chmod +x "/home/$USER/Desktop/Video Player.desktop"
print_success "Desktop shortcut created"
}
# Function to create management scripts
create_management_scripts() {
print_status "Creating management scripts..."
# Create start script
cat > /usr/local/bin/video-player-start << 'EOF'
#!/bin/bash
sudo systemctl start video-player
echo "Video Player started"
EOF
# Create stop script
cat > /usr/local/bin/video-player-stop << 'EOF'
#!/bin/bash
sudo systemctl stop video-player
echo "Video Player stopped"
EOF
# Create restart script
cat > /usr/local/bin/video-player-restart << 'EOF'
#!/bin/bash
sudo systemctl restart video-player
echo "Video Player restarted"
EOF
# Create status script
cat > /usr/local/bin/video-player-status << 'EOF'
#!/bin/bash
sudo systemctl status video-player
EOF
# Create log script
cat > /usr/local/bin/video-player-logs << 'EOF'
#!/bin/bash
sudo journalctl -u video-player -f
EOF
# Create USB management script
cat > /usr/local/bin/video-player-usb << 'EOF'
#!/bin/bash
/opt/video_player/usb_automount.sh "$@"
EOF
# Set permissions
chmod +x /usr/local/bin/video-player-*
print_success "Management scripts created"
}
# Function to display installation summary
display_summary() {
print_success "Installation completed successfully!"
echo
echo "Installation Summary:"
echo "===================="
echo "Installation directory: $INSTALL_DIR"
echo "Configuration directory: $CONFIG_DIR"
echo "Video folder: $VIDEO_FOLDER"
echo "Service name: $SERVICE_NAME"
echo
echo "Management Commands:"
echo "==================="
echo "Start: video-player-start"
echo "Stop: video-player-stop"
echo "Restart: video-player-restart"
echo "Status: video-player-status"
echo "Logs: video-player-logs"
echo "USB: video-player-usb {scan|mount-all}"
echo
echo "Next Steps:"
echo "==========="
echo "1. Add video files to: $VIDEO_FOLDER"
echo "2. Insert USB drives - they will auto-mount to: /media/usb/"
echo "3. Configure IR remote codes in: $CONFIG_DIR/ir_mapping.json"
echo "4. Start the service: video-player-start"
echo "5. Check status: video-player-status"
echo
echo "For more information, see the documentation in $INSTALL_DIR"
}
# Main installation function
main() {
echo "Raspberry Pi Video Player Auto-Start Installation"
echo "================================================="
echo
check_root
check_raspberry_pi
print_status "Starting installation..."
update_system
install_packages
install_python_dependencies
create_directories
copy_files
create_default_config
setup_usb_automount
setup_gpio_permissions
setup_service
create_sample_video
create_desktop_shortcut
create_management_scripts
display_summary
}
# Run main function
main "$@"