Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrating SMA with FFmpeg in Docker #1718

Open
VampiricAlien opened this issue Jul 23, 2024 · 0 comments
Open

Integrating SMA with FFmpeg in Docker #1718

VampiricAlien opened this issue Jul 23, 2024 · 0 comments

Comments

@VampiricAlien
Copy link

The project has been discussed previously without viable solutions at the time. Over the past few days, I have been working on a script to process media files. The process_videos.sh script monitors the input folder every 120 seconds to process any files found, moving them to the output folder after processing. This is a proof of concept and does not yet fully support VAAPI or integration with Sonarr

Docker file

# Use the latest Ubuntu 22.04 image
FROM ubuntu:22.04

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV TZ=America/New_York

# Install necessary system packages including timezone data
RUN apt-get update && \
    apt-get install -y \
    tzdata \
    software-properties-common && \
    add-apt-repository ppa:deadsnakes/ppa && \
    apt-get update && \
    apt-get install -y \
    python3.10 \
    python3.10-distutils \
    python3.10-venv \
    python3-pip \
    ffmpeg \
    vainfo \
    libva-dev \
    libva-drm2 \
    libva-x11-2 \
    mesa-va-drivers \
    i965-va-driver \
    gcc \
    libffi-dev \
    libssl-dev \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# Set the timezone
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# Upgrade pip, setuptools, and wheel to the latest version
RUN python3.10 -m pip install --upgrade pip setuptools wheel

# Set the working directory
WORKDIR /app

# Copy the SMA source code into the container
COPY sma /app

# Copy the monitoring script into the container
COPY process_videos.sh /usr/local/bin/process_videos.sh

# Install Python dependencies for SMA
RUN pip install --no-cache-dir -r /app/requirements.txt

# Make the monitoring script executable
RUN chmod +x /usr/local/bin/process_videos.sh

# Set the entrypoint to the monitoring script
ENTRYPOINT ["/usr/local/bin/process_videos.sh"]

process_videos.sh

#!/bin/bash

# Set environment variables
export INPUT_DIR="/data/input"
export OUTPUT_DIR="/data/output"
export SMA_DIR="/app"
export LOG_DIR="$SMA_DIR/logs"
export LOG_FILE="$LOG_DIR/video_processing.log"
export ERROR_LOG="$LOG_DIR/error.log"

# Ensure log directory exists
mkdir -p "$LOG_DIR"

# Redirect stderr to the error log
exec 2>>"$ERROR_LOG"

# Function to log messages
log_message() {
    local timestamp=$(date "+%Y-%m-%d %H:%M:%S")
    # Log to both the log file and stdout
    echo "[$timestamp] $1" | tee -a "$LOG_FILE"
}

log_message "Video processing script started"
log_message "Script is running in $(pwd)"

# Function to process a single video file
process_video() {
    local input_file="$1"
    local filename=$(basename "$input_file")
    local start_time=$(date "+%Y-%m-%d %H:%M:%S")

    log_message "Starting to process file: $filename"

    # Execute the Python script with the input file
    log_message "Executing command: python3 $SMA_DIR/manual.py -a -i $input_file"
    
    if timeout 900 python3 "$SMA_DIR/manual.py" -a -i "$input_file"; then
        log_message "Successfully processed file: $filename"
        
        # Move the processed file to the output directory
        if mv "$input_file" "$OUTPUT_DIR/"; then
            log_message "Moved $filename to output directory"
        else
            log_message "Error moving $filename to output directory"
        fi
    else
        log_message "Error processing file: $filename or timeout occurred."
    fi

    local end_time=$(date "+%Y-%m-%d %H:%M:%S")
    log_message "Finished processing $filename. Start: $start_time, End: $end_time"

    # Sleep for 120 seconds before processing the next file
    log_message "Sleeping for 120 seconds before processing the next file..."
    sleep 120
}

# Infinite loop to monitor the input directory
while true; do
    log_message "Checking for files in $INPUT_DIR"
    found_file=false
    for file in "$INPUT_DIR"/*; do
        if [[ -f "$file" ]]; then
            found_file=true
            log_message "Found file: $file"
            process_video "$file"  # Process one file at a time
        fi
    done

    if ! $found_file; then
        log_message "No files found in $INPUT_DIR to process."
    fi

    log_message "Sleeping for 5 seconds before checking again..."
    sleep 5  # Check every 5 seconds
done

docker build -t ffmpeg-sma-processor:latest .

docker run -d --name video-processor \
    --device /dev/dri:/dev/dri \
    -v /path/on/host/iinput:/data/input \
    -v /path/on/host/i/output:/data/output \
    -v /path/on/host/sma/config/autoProcess.ini:/app/sma/config/autoProcess.ini \
    ffmpeg-sma-processor

The image after build is around 1GB.

@mdhiggins What are your thoughts on a AIO SMA docker container?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant