Skip to main content

Overview

The converter package provides the main conversion orchestrator for the Camera Workflow application. It handles parallel processing of images and videos, tracks conversion statistics, manages worker pools, and provides comprehensive progress reporting.

Types

Converter

The main converter type that orchestrates the entire conversion process.
type Converter struct {
    config        *config.Config
    logger        *logger.Logger
    security      *security.SecurityChecker
    stats         *ConversionStats
    ffmpegCommand []string
    ffmpegMessage string
    accelOnce     sync.Once
    accelInfo     VideoAccelerationInfo
    hasher        checksum.Hasher
    tracker       *checksum.Tracker
}
config
*config.Config
Configuration settings for the conversion process
logger
*logger.Logger
Logger instance for output and file logging
security
*security.SecurityChecker
Security checker for file validation and disk space verification
stats
*ConversionStats
Tracks conversion statistics (processed files, sizes, timings)

ConversionStats

Tracks detailed statistics about the conversion process.
type ConversionStats struct {
    mu              sync.Mutex
    totalFiles      int
    processedFiles  int
    failedFiles     int
    skippedFiles    int
    recoveredFiles  int
    cleanedFiles    int
    verifiedFiles   int
    startTime       time.Time
    totalSizeMB     float64
    processedSizeMB float64
    outputSizeMB    float64
    totalS3Cost     float64
    savedSizeMB     float64
}

Functions

NewConverter

Creates a new Converter instance with the given configuration and logger.
func NewConverter(cfg *config.Config, log *logger.Logger) *Converter
cfg
*config.Config
required
Configuration for the conversion process
log
*logger.Logger
required
Logger instance for output
converter
*Converter
A new Converter instance ready to process files

Example

import (
    "github.com/Azilone/Camera-Workflow/internal/config"
    "github.com/Azilone/Camera-Workflow/internal/converter"
    "github.com/Azilone/Camera-Workflow/internal/logger"
)

cfg := config.NewConfig()
log, _ := logger.NewLogger("/path/to/conversion.log")

conv := converter.NewConverter(cfg, log)

Methods

Convert

Executes the main conversion process. Automatically switches between copy mode and conversion mode based on configuration.
func (c *Converter) Convert() error
error
error
Returns an error if the conversion process fails

Example

if err := conv.Convert(); err != nil {
    log.Fatalf("Conversion failed: %v", err)
}

Process Flow

  1. Recovery Phase: Cleanup abandoned files and recover incomplete conversions
  2. Disk Space Check: Verify sufficient space (unless skipped)
  3. Safety Test: Test conversion on a sample file
  4. File Discovery: Walk source directory and categorize files
  5. Parallel Conversion: Process files with worker pools
  6. Final Report: Display statistics and cost estimations

findFiles (internal)

Scans the source directory to find all convertible media files.
func (c *Converter) findFiles() ([]string, []string, error)
photoFiles
[]string
List of photo file paths found
videoFiles
[]string
List of video file paths found
error
error
Returns an error if directory traversal fails

convertFiles (internal)

Processes a batch of files using parallel worker pools.
func (c *Converter) convertFiles(files []string, fileType string) error
files
[]string
required
List of file paths to convert
fileType
string
required
Type of files (“photo” or “video”)

Worker Pool Behavior

  • Photos: Uses configured MaxJobs workers
  • Videos: Limited to 2 workers (or adaptive workers if enabled)
  • Adaptive Workers: Dynamically adjusts worker count based on CPU/memory usage

Key Features

Safety Guarantees

  • Triple verification before deleting originals
  • Processing markers track active conversions with PID/timestamp
  • Automatic recovery from crashed processes
  • Corruption detection validates existing files

Progress Tracking

  • Real-time progress bars with ETA calculations
  • Comprehensive statistics (files, sizes, savings)
  • S3 storage cost estimations
  • JSON mode for programmatic integration

Adaptive Worker Management

When enabled, the converter dynamically adjusts worker count based on:
  • CPU usage (scales down when CPU > 80%)
  • Memory availability (scales down when available memory < 20%)
  • Configurable min/max worker limits
  • Config - Configuration management
  • Security - File validation and safety checks
  • Logger - Logging functionality
  • Checksum - File integrity verification

Build docs developers (and LLMs) love