Compress by size
This commit is contained in:
parent
5fe20f47de
commit
4b062470dc
|
@ -1,4 +1,6 @@
|
|||
## CHANGELOG
|
||||
* 0.21.0 - Compress by quality
|
||||
* 0.20.0 - Keep original dates and format conversion
|
||||
* 0.19.0 - Rust migration
|
||||
* 0.18.0 - Fixed Windows build + libcaesium 0.9.3
|
||||
* 0.17.0 - libcaesium 0.9.2
|
||||
|
|
|
@ -22,7 +22,7 @@ CaesiumCLT - Command Line Tools for image compression
|
|||
|
||||
```
|
||||
USAGE:
|
||||
caesiumclt [FLAGS] [OPTIONS] --output <output> --quality <quality> [FILE]...
|
||||
caesiumclt [FLAGS] [OPTIONS] --max-size <max-size> --output <output> --quality <quality> [FILE]...
|
||||
|
||||
FLAGS:
|
||||
-d, --dry-run do not compress files but just show output paths
|
||||
|
@ -38,6 +38,7 @@ FLAGS:
|
|||
OPTIONS:
|
||||
--height <height> height of the output image, if width is not set will preserve aspect ratio
|
||||
[default: 0]
|
||||
--max-size <max-size> set the expected maximum output size in bytes
|
||||
-o, --output <output> output folder
|
||||
--output-format <output-format> convert the image to the selected format (jpg, png, webp, tiff) [default:
|
||||
none]
|
||||
|
|
34
src/main.rs
34
src/main.rs
|
@ -1,6 +1,7 @@
|
|||
use std::fs;
|
||||
use std::path::Path;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use caesium::SupportedFileTypes;
|
||||
use filetime::{FileTime, set_file_times};
|
||||
use human_bytes::human_bytes;
|
||||
|
@ -30,7 +31,7 @@ struct CompressionResult {
|
|||
|
||||
struct OutputFormat {
|
||||
pub file_type: SupportedFileTypes,
|
||||
pub extension: String
|
||||
pub extension: String,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
@ -43,6 +44,8 @@ fn main() {
|
|||
let convert = output_format.file_type != SupportedFileTypes::Unkn;
|
||||
let keep_dates = opt.keep_dates;
|
||||
|
||||
let compress_by_size = opt.max_size.is_some();
|
||||
|
||||
if opt.quiet {
|
||||
verbose = 0;
|
||||
}
|
||||
|
@ -65,15 +68,20 @@ fn main() {
|
|||
let (base_path, files) = scanfiles::scanfiles(args, opt.recursive);
|
||||
|
||||
let mut compression_parameters = caesium::initialize_parameters();
|
||||
if opt.quality == 0 {
|
||||
|
||||
if opt.quality.is_some() {
|
||||
let quality = opt.quality.unwrap();
|
||||
if quality == 0 {
|
||||
compression_parameters.optimize = true;
|
||||
compression_parameters.png.force_zopfli = opt.zopfli;
|
||||
} else {
|
||||
compression_parameters.jpeg.quality = opt.quality;
|
||||
compression_parameters.png.quality = opt.quality;
|
||||
compression_parameters.gif.quality = opt.quality;
|
||||
compression_parameters.webp.quality = opt.quality;
|
||||
compression_parameters.jpeg.quality = quality;
|
||||
compression_parameters.png.quality = quality;
|
||||
compression_parameters.gif.quality = quality;
|
||||
compression_parameters.webp.quality = quality;
|
||||
}
|
||||
}
|
||||
|
||||
compression_parameters.keep_metadata = opt.exif;
|
||||
|
||||
if opt.width > 0 {
|
||||
|
@ -173,6 +181,8 @@ fn main() {
|
|||
if !dry_run {
|
||||
let result = if convert {
|
||||
caesium::convert(input_full_path.to_string(), output_full_path_str.to_string(), &compression_parameters, output_format.file_type)
|
||||
} else if compress_by_size {
|
||||
caesium::compress_to_size(input_full_path.to_string(), output_full_path_str.to_string(), &mut compression_parameters.clone(), opt.max_size.unwrap() as usize, true)
|
||||
} else {
|
||||
caesium::compress(input_full_path.to_string(), output_full_path_str.to_string(), &compression_parameters)
|
||||
};
|
||||
|
@ -223,8 +233,6 @@ fn main() {
|
|||
}
|
||||
compression_result.compressed_size = final_output_size;
|
||||
if compression_result.result && keep_dates {
|
||||
|
||||
|
||||
match set_file_times(final_output_full_path, input_atime, input_mtime) {
|
||||
Ok(_) => {}
|
||||
Err(_) => {
|
||||
|
@ -309,23 +317,23 @@ fn map_output_format(format: String) -> OutputFormat {
|
|||
match format.to_lowercase().as_str() {
|
||||
"jpg|jpeg" => OutputFormat {
|
||||
file_type: SupportedFileTypes::Jpeg,
|
||||
extension: format
|
||||
extension: format,
|
||||
},
|
||||
"png" => OutputFormat {
|
||||
file_type: SupportedFileTypes::Png,
|
||||
extension: format
|
||||
extension: format,
|
||||
},
|
||||
"webp" => OutputFormat {
|
||||
file_type: SupportedFileTypes::WebP,
|
||||
extension: format
|
||||
extension: format,
|
||||
},
|
||||
"tiff|tif" => OutputFormat {
|
||||
file_type: SupportedFileTypes::Tiff,
|
||||
extension: format
|
||||
extension: format,
|
||||
},
|
||||
_ => OutputFormat {
|
||||
file_type: SupportedFileTypes::Unkn,
|
||||
extension: "".to_string()
|
||||
extension: "".to_string(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,8 +19,12 @@ arg_enum! {
|
|||
#[structopt(name = "", about = "CaesiumCLT - Command Line Tools for image compression")]
|
||||
pub struct Opt {
|
||||
/// sets output file quality between [0-100], 0 for optimization
|
||||
#[structopt(short = "q", long)]
|
||||
pub quality: u32,
|
||||
#[structopt(short = "q", long, required_unless="max-size")]
|
||||
pub quality: Option<u32>,
|
||||
|
||||
/// set the expected maximum output size in bytes
|
||||
#[structopt(long = "max-size", required_unless="quality")]
|
||||
pub max_size: Option<u32>,
|
||||
|
||||
/// keeps EXIF info during compression
|
||||
#[structopt(short = "e", long)]
|
||||
|
|
Loading…
Reference in New Issue