webd

module
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 4, 2024 License: MIT

README

Webd - CLI Tool for image conversion

A fast image converter built in Go. Webd processes all images in a given directory, converting only targeted formats to desired formats. This tool is helpful for bulk conversions and can be potentially automated using scripts. Support for more formats coming soon!

Demo (v0.1.2)

Demo GIF Optimizer

Features

  • Fast conversions powered by concurrency
  • PNG → WebP
  • WebP → PNG
  • Optional cleanup - Delete original files after conversion
  • Verbose logging - Track conversion progress and file operations
  • Targeted conversion - Only processes specified file types, leaving others untouched

Table of Contents

Installation

Prerequisites
  • Go version 1.22 or higher
Quick Install
go install github.com/v4n1lla-1ce/webd/cmd/webd@latest

Usage

# you MUST specify a conversion flag i.e. -webp2png
webd <flags> <directory>
Examples
# Help Menu (both works)
webd -h
webd

# Convert all WebP files to PNG in current directory
webd --webp2png .

# Convert all WebP files to PNG in a specific directory whilst deleting all original files
# Setting --verbose will turn on logs
webd --webp2png -d --verbose /path/to/images

# Convert all PNG files to WebP in parent directory
webd --png2webp ../
Troubleshooting

If you get a "command not found" error after installation, you need to add Go's bin directory to your PATH:

Linux/macOS

Add this line to your ~/.bashrc, ~/.zshrc, or equivalent:

export PATH=$PATH:$(go env GOPATH)/bin

Then restart your terminal or run:

source ~/.bashrc  # or source ~/.zshrc
Windows
  1. Open System Properties → Advanced → Environment Variables
  2. Under "User variables", find "Path"
  3. Click "Edit" and add: %USERPROFILE%\go\bin
  4. Click "OK" to save
  5. Restart your terminal

Documentation

Command Line Options
Options Description
-h Display help menu
-d Delete original files after conversion
-v Display version information
--verbose Logs the files converted/deleted depending on options selected
--webp2png Converts Webp files to PNG in the directory specified. Other files will be left untouched
--png2webp Converts PNG files to Webp in the directory specified. Other files will be left untouched.



Case Study: Pipeline Concurrency Pattern in WebD

This project demonstrates the pipeline concurrency pattern in Go. Here's how WebD processes multiple images efficiently using concurrent stages.

The Problem

Converting multiple WebP images to PNG format presents several challenges:

  1. CPU-intensive image decoding/encoding
  2. I/O operations for reading/writing files
  3. Processing multiple files efficiently

Traditional sequential processing would be slow, especially for large directories.

The Solution: Pipeline Pattern

WebD implements a concurrent pipeline where each processing stage runs independently:

func NewPipeline[I any, O any](input <-chan I, process func(I) O) <-chan O {
    out := make(chan O)
    go func() {
        defer close(out)
        for in := range input {
            out <- process(in)
        }
    }()
    return out
}
Pipeline Architecture
Load Files → Decode WebP → Encode PNG → Save to Disk
   ↓            ↓            ↓            ↓
[webp imgs] → [images] → [png bytes] → [saved files]

Data flows through the pipeline using a custom structure:

type PipelineData struct {
    Value      any    // Current processing data
    SourcePath string // Original file path
    Directory  string // Working directory
    BaseName   string // Original filename
}
Implementation

The pipeline is constructed in stages:

// This is a simplified version
files := LoadPipeline(directory)          // Find WebP files
decoded := NewPipeline(files, DecodeWebp) // Decode WebP
encoded := NewPipeline(decoded, EncodePng) // Encode PNG
saved := NewPipeline(encoded, SaveToDisk)  // Save files
Benefits
  1. Concurrent Processing

    • Multiple images processed simultaneously
    • Efficient CPU utilization
    • Reduced total processing time
  2. Memory Efficiency

    • Works like a conveyor belt, processes one image at a time
    • Only loads a few images at once, not your entire folder
    • Automatically slows down if your computer gets busy
  3. Maintainable Code

    • Clear separation of concerns
    • Easy to add new processing stages
Visualisation

pipeline

License

This project is licensed under the MIT License - see the LICENSE file for details.

Directories

Path Synopsis
cmd
internal
cli

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL