updates and backlog
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import os
|
||||
import sys
|
||||
import subprocess
|
||||
import re
|
||||
import shutil
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from typing import Optional, Dict, Set
|
||||
|
||||
def check_dependencies() -> bool:
|
||||
"""Check if required commands are available."""
|
||||
for cmd in ['file', 'avifenc']:
|
||||
if not shutil.which(cmd):
|
||||
print(f"Error: {cmd} command not found")
|
||||
return False
|
||||
return True
|
||||
|
||||
def convert_file(filepath: Path, output_path: Path, quality: int = 60) -> bool:
|
||||
"""Convert file to AVIF using avifenc"""
|
||||
try:
|
||||
subprocess.run(['avifenc', '-q', str(quality), str(filepath), str(output_path)], check=True)
|
||||
return True
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"Error converting {filepath} to AVIF: {e}")
|
||||
return False
|
||||
|
||||
def delete_file(filepath: Path) -> bool:
|
||||
"""Delete file"""
|
||||
try:
|
||||
filepath.unlink()
|
||||
return True
|
||||
except OSError as e:
|
||||
print(f"Error deleting {filepath}: {e}")
|
||||
return False
|
||||
|
||||
def process_directory(directory: Path):
|
||||
"""Process a single directory."""
|
||||
print(f"Processing directory: {directory}")
|
||||
|
||||
# Get all image files in directory
|
||||
image_files = sorted(
|
||||
path for path in directory.iterdir()
|
||||
if path.is_file() and path.suffix.lower() in {'.jpg', '.jpeg', '.png'}
|
||||
)
|
||||
|
||||
for filepath in image_files:
|
||||
print(f"Processing: {filepath.name}")
|
||||
|
||||
# Convert to avif
|
||||
if convert_file(filepath, filepath.with_suffix('.avif')):
|
||||
print(f"Converted {filepath.name} to AVIF")
|
||||
if delete_file(filepath):
|
||||
print(f"Deleted {filepath.name}")
|
||||
else:
|
||||
print(f"Failed to delete {filepath.name}")
|
||||
else:
|
||||
print(f"Failed to convert {filepath.name} to AVIF")
|
||||
|
||||
print(f"Done! Processed {len(image_files)} files in {directory}")
|
||||
|
||||
def main():
|
||||
if not check_dependencies():
|
||||
sys.exit(1)
|
||||
|
||||
# Get directories to process
|
||||
if len(sys.argv) == 1:
|
||||
directories = [Path('.')]
|
||||
else:
|
||||
recursive = sys.argv[1] in {'-r', '--recursive'}
|
||||
start_idx = 2 if recursive else 1
|
||||
|
||||
if recursive:
|
||||
directories = []
|
||||
for dir_arg in sys.argv[start_idx:]:
|
||||
path = Path(dir_arg)
|
||||
if path.is_dir():
|
||||
directories.extend(p for p in path.rglob('.') if p.is_dir())
|
||||
else:
|
||||
directories = [Path(dir_arg) for dir_arg in sys.argv[start_idx:] if Path(dir_arg).is_dir()]
|
||||
|
||||
# Process each directory
|
||||
for directory in directories:
|
||||
try:
|
||||
process_directory(directory)
|
||||
except Exception as e:
|
||||
print(f"Error processing directory {directory}: {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user