-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
279 lines (231 loc) · 11.1 KB
/
Copy pathProgram.cs
File metadata and controls
279 lines (231 loc) · 11.1 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
using System.Text;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using CommandLine;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Threading;
namespace Album_Compress_Helper
{
class Program
{
const string input_identifier = "{in}";
const string output_identifier = "{out}";
public class Options
{
[Option("src", Required = true, HelpText = "Path to source directory.")]
public string Source { get; set; }
[Option("dst", Required = true, HelpText = "Path to destination directory, create if not exist.")]
public string Destination { get; set; }
[Option("argff", Required = false, HelpText = "Arguments pass to FFMPEG, use "+input_identifier+" for input file path, "+output_identifier+" for output file path")]
public string FFArgument { get; set; }
[Option("argexif", Required = false, HelpText = "Arguments pass to ExifTool, use "+input_identifier+" for input file path, "+output_identifier+" for output file path")]
public string ExifArgument { get; set; }
[Option('c',"comment", Required = false, HelpText = "Ignore file with spicific comment tag from input, otherwise write comment to output file.")]
public string Comment { get; set; }
[Option('i',"ignore", Required = false, HelpText = "Ignore if files already exist in destination.")]
public bool Ignore { get; set; }
[Option("ext",Separator = ',', Required = true, HelpText = "File extensions filter such as jpg or mp4, use',' to seperate multiple extensions.")]
public IEnumerable<string> Extensions { get; set; }
[Option('t', "thread", Required = false, HelpText = ".",Default = 1)]
public int Thread { get; set; }
[Option('d', "date", Required = false, HelpText = "Modify last write and creation time, available options:\n copy:copy from original \n min or max: select minimum or maximum datetime, then write to both last write and creation time.")]
public string Date { get; set; }
[Option('k', "keep", Required = false, HelpText = "Copy original file if compressed version is larger.")]
public bool Keep { get; set; }
[Option('v', "verbose", Required = false, HelpText = "Show verbose info.")]
public bool Verbose { get; set; }
[Option("vvv", Required = false, HelpText = "Show more verbose info, mostly from ffmpeg.")]
public bool VerboseMore { get; set; }
}
public static Options Option {get;set;}
static async Task Main(string[] args)
{
Parser.Default.ParseArguments<Options>(args)
.WithParsed(o=>Option=o)
.WithNotParsed(o=>Environment.Exit(-1));
Option.FFArgument = Option.FFArgument?.Replace("\\","");
Option.ExifArgument = Option.ExifArgument?.Replace("\\","");
List<string> ExtensionFilters = Option.Extensions.ToList();
var FilePaths = Directory.GetFiles(Option.Source, "*", SearchOption.AllDirectories)
.Where(f=>ExtensionFilters.Any(e=>f.EndsWith(e,StringComparison.CurrentCultureIgnoreCase)))
.ToList();
int Process_cnt=0,Ignored_cnt=0,Keep_cnt=0;
var TaskList = new List<Task>();
foreach (var FilePath in FilePaths)
{
var SavePath = FilePath.Replace(Option.Source,Option.Destination);
//multi thread
if (TaskList.Count>=Option.Thread)
{
Task FinishedTask = await Task.WhenAny(TaskList);
TaskList.Remove(FinishedTask);
Interlocked.Increment(ref Process_cnt);
}
//Check Ignore
if(Option.Ignore && File.Exists(SavePath))
{
Console.WriteLine($"File exist, ignored:{SavePath}");
Interlocked.Increment(ref Ignored_cnt);
continue;//skip
}
Directory.CreateDirectory(Path.GetDirectoryName(SavePath));
Task t = Task.Factory.StartNew(()=>{
bool retry = true;
while(retry)
{
//Check Comment
if(String.IsNullOrEmpty(Option.Comment)==false &&
Option.Comment == ReadComment(FilePath))
{
Console.WriteLine($"File already has comment, ignored:{SavePath}");
Interlocked.Increment(ref Ignored_cnt);
return;//skip
}
if(String.IsNullOrEmpty(Option.FFArgument)==false)
{
RunFFMPEG(FilePath,SavePath);
if(File.Exists(SavePath)==false)
{
Console.WriteLine($"FFMPEG failed, retry:{SavePath}");
continue;
}
}
if(String.IsNullOrEmpty(Option.ExifArgument)==false)
RunExifTool(FilePath,SavePath);
if(String.IsNullOrEmpty(Option.Date)==false)
CopyDate(FilePath,SavePath);
if(Option.Keep && CheckFileSize(FilePath,SavePath))
{
File.Copy(FilePath, SavePath, true);
Console.WriteLine($"Output file is larger, copy original:{SavePath}");
Interlocked.Increment(ref Keep_cnt);
}
retry = false;
}
},TaskCreationOptions.LongRunning);
TaskList.Add(t);
Console.WriteLine($"Processing:{SavePath}");
WriteOnBottomLine($"File Count:{FilePaths.Count}, Processed:{Process_cnt.ToString()}, Ignored:{Ignored_cnt.ToString()}, Keep:{Keep_cnt.ToString()}");
}
while(TaskList.Count!=0)
{
Task FinishedTask = await Task.WhenAny(TaskList);
TaskList.Remove(FinishedTask);
Interlocked.Increment(ref Process_cnt);
}
WriteOnBottomLine($"File Count:{FilePaths.Count}, Processed:{Process_cnt.ToString()}, Ignored:{Ignored_cnt.ToString()}, Keep:{Keep_cnt.ToString()}");
}
static string ReadComment(string FilePath)
{
string comment=null;
var Info = new ProcessStartInfo
{
FileName = @"exiftool",
Arguments = $"-comment {FilePath}",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
};
using(Process proc = Process.Start(Info))
{
var str = proc.StandardOutput.ReadToEnd();
if(String.IsNullOrEmpty(str)==false)
{
var ary = str.Split(':').ToArray();
if(ary.Length>1)
comment = ary[1].Replace("\n", "")
.Replace("\r", "")
.TrimStart()
.TrimEnd();
}
proc.WaitForExit();
}
return comment;
}
static void RunFFMPEG(string FilePath,string SavePath)
{
string arg = new string(Option.FFArgument)
.Replace(input_identifier,$"\"{FilePath}\"")
.Replace(output_identifier,$"\"{SavePath}\"");
ProcessStartInfo ff_procinfo = new ProcessStartInfo()
{
FileName = "ffmpeg",
Arguments= arg,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = !Option.VerboseMore
};
if(Option.Verbose)
Console.WriteLine($"{ff_procinfo.FileName} {ff_procinfo.Arguments}");
using(Process proc = Process.Start(ff_procinfo))
proc.WaitForExit();
}
static void RunExifTool(string FilePath,string SavePath)
{
string arg = new string(Option.ExifArgument)
.Replace(input_identifier,$"\"{FilePath}\"")
.Replace(output_identifier,$"\"{SavePath}\"");
if(String.IsNullOrEmpty(Option.Comment)==false)
arg+=$" -comment={Option.Comment} -usercomment={Option.Comment}";
ProcessStartInfo exif_procinfo = new ProcessStartInfo()
{
FileName = "exiftool",
Arguments=arg,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = !Option.VerboseMore
};
if(Option.Verbose)
Console.WriteLine($"{exif_procinfo.FileName} {exif_procinfo.Arguments}");
using(Process proc = Process.Start(exif_procinfo))
proc.WaitForExit();
}
static void CopyDate(string FilePath,string SavePath)
{
var CreationTime = File.GetCreationTime(FilePath);
var LastWriteTime = File.GetLastWriteTime(FilePath);
var Min = CreationTime>LastWriteTime?LastWriteTime:CreationTime;
var Max = CreationTime<LastWriteTime?LastWriteTime:CreationTime;
switch(Option.Date)
{
case "copy":
File.SetCreationTime(SavePath,CreationTime);
File.SetLastWriteTime(SavePath,LastWriteTime);
break;
case "min":
File.SetCreationTime(SavePath,Min);
File.SetLastWriteTime(SavePath,Min);
break;
case "max":
File.SetCreationTime(SavePath,Max);
File.SetLastWriteTime(SavePath,Max);
break;
}
}
static bool CheckFileSize(string FilePath,string SavePath)
{
if(File.Exists(FilePath) && File.Exists(SavePath))
{
var file_length = new FileInfo(FilePath).Length;
var save_length = new FileInfo(SavePath).Length;
if (save_length>file_length)
{
return true;
}
}
return false;
}
static void WriteOnBottomLine(string text)
{
int x = Console.CursorLeft;
int y = Console.CursorTop;
Console.CursorTop = Console.WindowTop + Console.WindowHeight - 1;
Console.Write(text);
Console.SetCursorPosition(x, y);
}
}
}