up
This commit is contained in:
parent
3c645dc893
commit
55b437d830
@ -1,6 +1,5 @@
|
|||||||
using LibHeifSharp;
|
using LibHeifSharp;
|
||||||
using SixLabors.ImageSharp;
|
using SixLabors.ImageSharp;
|
||||||
using SixLabors.ImageSharp.Processing;
|
|
||||||
using ShellProgressBar;
|
using ShellProgressBar;
|
||||||
using SixLabors.ImageSharp.Metadata;
|
using SixLabors.ImageSharp.Metadata;
|
||||||
using SixLabors.ImageSharp.PixelFormats;
|
using SixLabors.ImageSharp.PixelFormats;
|
||||||
@ -9,7 +8,7 @@ namespace Comic_Compressor
|
|||||||
{
|
{
|
||||||
internal class AvifCompressor
|
internal class AvifCompressor
|
||||||
{
|
{
|
||||||
internal static void CompressImages(string sourceImagePath, string targetStoragePath)
|
internal static void CompressImages(string sourceImagePath, string targetStoragePath, int threadCount)
|
||||||
{
|
{
|
||||||
LibHeifSharpDllImportResolver.Register();
|
LibHeifSharpDllImportResolver.Register();
|
||||||
// Step 1: Get all subdirectories and store them in a list
|
// Step 1: Get all subdirectories and store them in a list
|
||||||
@ -31,13 +30,13 @@ namespace Comic_Compressor
|
|||||||
foreach (string subdirectory in subdirectories)
|
foreach (string subdirectory in subdirectories)
|
||||||
{
|
{
|
||||||
// Step 3: Process each directory
|
// Step 3: Process each directory
|
||||||
ProcessDirectory(subdirectory, sourceImagePath, targetStoragePath, progressBar);
|
ProcessDirectory(subdirectory, sourceImagePath, targetStoragePath, progressBar, threadCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.WriteLine("All directories processed successfully.");
|
Console.WriteLine("All directories processed successfully.");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ProcessDirectory(string subdirectory, string sourceImagePath, string targetStoragePath, ShellProgressBar.ProgressBar progressBar)
|
private static void ProcessDirectory(string subdirectory, string sourceImagePath, string targetStoragePath, ShellProgressBar.ProgressBar progressBar, int threadCount)
|
||||||
{
|
{
|
||||||
// Get the relative path of the subdirectory
|
// Get the relative path of the subdirectory
|
||||||
string relativePath = Path.GetRelativePath(sourceImagePath, subdirectory);
|
string relativePath = Path.GetRelativePath(sourceImagePath, subdirectory);
|
||||||
@ -52,7 +51,7 @@ namespace Comic_Compressor
|
|||||||
// Set up ParallelOptions to limit the number of concurrent threads
|
// Set up ParallelOptions to limit the number of concurrent threads
|
||||||
ParallelOptions options = new()
|
ParallelOptions options = new()
|
||||||
{
|
{
|
||||||
MaxDegreeOfParallelism = 2 // Adjust this value to set the number of concurrent threads
|
MaxDegreeOfParallelism = threadCount // Adjust this value to set the number of concurrent threads
|
||||||
};
|
};
|
||||||
|
|
||||||
// Process each image file in parallel
|
// Process each image file in parallel
|
||||||
@ -82,7 +81,7 @@ namespace Comic_Compressor
|
|||||||
private static string[] GetImageFiles(string directoryPath)
|
private static string[] GetImageFiles(string directoryPath)
|
||||||
{
|
{
|
||||||
// Get all image files supported by ImageSharp
|
// Get all image files supported by ImageSharp
|
||||||
string[] supportedExtensions = ["*.jpg", "*.jpeg", "*.png", "*.bmp", "*.gif", "*.tiff"];
|
string[] supportedExtensions = ["*.jpg", "*.jpeg", "*.png", "*.bmp", "*.gif", "*.tiff", "*.gif"];
|
||||||
List<string> allFiles = [];
|
List<string> allFiles = [];
|
||||||
|
|
||||||
foreach (string extension in supportedExtensions)
|
foreach (string extension in supportedExtensions)
|
||||||
|
@ -25,6 +25,10 @@ namespace Comic_Compressor
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Console.WriteLine("处理线程数:");
|
||||||
|
int threadCount = int.Parse(Console.ReadLine() ?? "2");
|
||||||
|
Console.WriteLine($"处理线程数设定:{threadCount}");
|
||||||
|
|
||||||
Console.WriteLine("请选择压缩模式:0 - 压缩成webp,1 - 压缩成avif");
|
Console.WriteLine("请选择压缩模式:0 - 压缩成webp,1 - 压缩成avif");
|
||||||
string? modeInput = Console.ReadLine();
|
string? modeInput = Console.ReadLine();
|
||||||
if (modeInput == null)
|
if (modeInput == null)
|
||||||
@ -36,10 +40,12 @@ namespace Comic_Compressor
|
|||||||
switch (modeInput)
|
switch (modeInput)
|
||||||
{
|
{
|
||||||
case "0":
|
case "0":
|
||||||
WebpCompressor.CompressImages(sourceImagePath, targetStoragePath);
|
WebpCompressor.CompressImages(sourceImagePath, targetStoragePath, threadCount);
|
||||||
|
GetCompressorResult(sourceImagePath, targetStoragePath);
|
||||||
break;
|
break;
|
||||||
case "1":
|
case "1":
|
||||||
AvifCompressor.CompressImages(sourceImagePath, targetStoragePath);
|
AvifCompressor.CompressImages(sourceImagePath, targetStoragePath, threadCount);
|
||||||
|
GetCompressorResult(sourceImagePath, targetStoragePath);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
Console.WriteLine("不支持的模式");
|
Console.WriteLine("不支持的模式");
|
||||||
@ -63,5 +69,32 @@ namespace Comic_Compressor
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static long GetDirectorySize(string path)
|
||||||
|
{
|
||||||
|
long size = 0;
|
||||||
|
|
||||||
|
// 遍历目录中的所有文件
|
||||||
|
foreach (string file in Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories))
|
||||||
|
{
|
||||||
|
// 获取文件的大小并累加到总大小中
|
||||||
|
FileInfo fileInfo = new(file);
|
||||||
|
size += fileInfo.Length;
|
||||||
|
}
|
||||||
|
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void GetCompressorResult(string source, string target)
|
||||||
|
{
|
||||||
|
long sourceSize = GetDirectorySize(source);
|
||||||
|
long targetSize = GetDirectorySize(target);
|
||||||
|
double reduced = (sourceSize - targetSize) * 1.0 / sourceSize;
|
||||||
|
|
||||||
|
Console.WriteLine($"源目录大小:{sourceSize} 字节");
|
||||||
|
Console.WriteLine($"目标目录大小:{targetSize} 字节");
|
||||||
|
Console.WriteLine($"已减少:{reduced:P}的体积");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ namespace Comic_Compressor
|
|||||||
{
|
{
|
||||||
internal class WebpCompressor
|
internal class WebpCompressor
|
||||||
{
|
{
|
||||||
internal static void CompressImages(string sourceImagePath, string targetStoragePath)
|
internal static void CompressImages(string sourceImagePath, string targetStoragePath, int threadCount)
|
||||||
{
|
{
|
||||||
// Step 1: Get all subdirectories and store them in a list
|
// Step 1: Get all subdirectories and store them in a list
|
||||||
List<string> subdirectories = new(Directory.GetDirectories(sourceImagePath, "*", SearchOption.AllDirectories));
|
List<string> subdirectories = new(Directory.GetDirectories(sourceImagePath, "*", SearchOption.AllDirectories));
|
||||||
@ -28,13 +28,13 @@ namespace Comic_Compressor
|
|||||||
foreach (string subdirectory in subdirectories)
|
foreach (string subdirectory in subdirectories)
|
||||||
{
|
{
|
||||||
// Step 3: Process each directory
|
// Step 3: Process each directory
|
||||||
ProcessDirectory(subdirectory, sourceImagePath, targetStoragePath, progressBar);
|
ProcessDirectory(subdirectory, sourceImagePath, targetStoragePath, progressBar, threadCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.WriteLine("All directories processed successfully.");
|
Console.WriteLine("All directories processed successfully.");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ProcessDirectory(string subdirectory, string sourceImagePath, string targetStoragePath, ShellProgressBar.ProgressBar progressBar)
|
private static void ProcessDirectory(string subdirectory, string sourceImagePath, string targetStoragePath, ShellProgressBar.ProgressBar progressBar, int threadCount)
|
||||||
{
|
{
|
||||||
// Get the relative path of the subdirectory
|
// Get the relative path of the subdirectory
|
||||||
string relativePath = Path.GetRelativePath(sourceImagePath, subdirectory);
|
string relativePath = Path.GetRelativePath(sourceImagePath, subdirectory);
|
||||||
@ -49,7 +49,7 @@ namespace Comic_Compressor
|
|||||||
// Set up ParallelOptions to limit the number of concurrent threads
|
// Set up ParallelOptions to limit the number of concurrent threads
|
||||||
ParallelOptions options = new()
|
ParallelOptions options = new()
|
||||||
{
|
{
|
||||||
MaxDegreeOfParallelism = 2 // Adjust this value to set the number of concurrent threads
|
MaxDegreeOfParallelism = threadCount // Adjust this value to set the number of concurrent threads
|
||||||
};
|
};
|
||||||
|
|
||||||
// Process each image file in parallel
|
// Process each image file in parallel
|
||||||
@ -79,7 +79,7 @@ namespace Comic_Compressor
|
|||||||
private static string[] GetImageFiles(string directoryPath)
|
private static string[] GetImageFiles(string directoryPath)
|
||||||
{
|
{
|
||||||
// Get all image files supported by ImageSharp
|
// Get all image files supported by ImageSharp
|
||||||
string[] supportedExtensions = ["*.jpg", "*.jpeg", "*.png", "*.bmp", "*.gif", "*.tiff"];
|
string[] supportedExtensions = ["*.jpg", "*.jpeg", "*.png", "*.bmp", "*.gif", "*.tiff", "*.gif"];
|
||||||
List<string> allFiles = [];
|
List<string> allFiles = [];
|
||||||
|
|
||||||
foreach (string extension in supportedExtensions)
|
foreach (string extension in supportedExtensions)
|
||||||
|
Loading…
Reference in New Issue
Block a user