-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
84 lines (72 loc) · 2.8 KB
/
Program.cs
File metadata and controls
84 lines (72 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.IO;
using System.Diagnostics;
namespace SimpleStickerResizer
{
internal static class Program
{
private static readonly string[] SupportedFormats = { ".png", ".jpg", ".jpeg", ".gif", ".webp" };
static void Main()
{
Console.Write("Enter the path to the stickers: ");
string inputDir = Console.ReadLine();
if (!Directory.Exists(inputDir))
{
Console.WriteLine("Error: Directory does not exist.");
return;
}
string outputDir = Path.Combine(inputDir, "resized");
Directory.CreateDirectory(outputDir);
foreach (string file in Directory.GetFiles(inputDir, "*.*", SearchOption.TopDirectoryOnly))
{
string ext = Path.GetExtension(file).ToLower();
if (Array.Exists(SupportedFormats, e => e == ext))
{
string outputFile = Path.Combine(outputDir, Path.GetFileName(file));
if (ext == ".webp" && IsAnimated(file))
{
outputFile = Path.Combine(outputDir, Path.GetFileNameWithoutExtension(file) + ".gif");
Console.WriteLine($"Converting animated WebP: {file} to GIF...");
RunFFmpeg($"-i \"{file}\" -vf scale=160:160 \"{outputFile}\"");
}
else
{
Console.WriteLine($"Resizing image: {file}");
RunFFmpeg($"-i \"{file}\" -vf scale=160:160 \"{outputFile}\"");
}
}
}
Console.WriteLine("Job Done!");
}
private static void RunFFmpeg(string args)
{
var psi = new ProcessStartInfo
{
FileName = "ffmpeg",
Arguments = args,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
using var process = Process.Start(psi);
process?.WaitForExit();
}
private static bool IsAnimated(string filePath)
{
var psi = new ProcessStartInfo
{
FileName = "ffprobe",
Arguments = $"\"{filePath}\"",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
using var process = Process.Start(psi);
string errorOutput = process?.StandardError.ReadToEnd() ?? string.Empty;
process?.WaitForExit();
return !errorOutput.Contains("Duration: N/A");
}
}
}