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