-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdups.php
More file actions
314 lines (278 loc) · 10.4 KB
/
dups.php
File metadata and controls
314 lines (278 loc) · 10.4 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/php5
<?php
$arr=array();
$table=false;
$normal=false;
$verbose=false;
$audio=false;
$bash=false;
$divide_by=1;
$store_fp=false;
$random=false;
$fpext='fp';
$md5arr=array();
$sizes=array();
$durations=array();
$fprints=array();
$i=0;
$files=0;
declare(ticks = 10);
$run = true;
pcntl_signal(SIGINT, function($signo) {
global $run;
$run = false;
});
foreach ($argv as $k => $arg){
if ($k!=0){
if (substr($arg,0,1)=="-"){
if ($arg=="-h" || $arg=="--help") help();
if ($arg=="-t" || $arg=="--table") $table=true;
if ($arg=="-n" || $arg=="--normal") $normal=true;
if ($arg=="-v" || $arg=="--verbose") $verbose=true;
if ($arg=="-a" || $arg=="--audio") $audio=true;
if ($arg=="-b" || $arg=="--bash") $bash=true;
if ($arg=="-r" || $arg=="--random") $random=true;
if ($arg=="-fp") $store_fp=true;
if ($arg=="-d2") $divide_by=2;
if ($arg=="-d3") $divide_by=3;
if ($arg=="-d4") $divide_by=4;
if ($arg=="-d5") $divide_by=5;
if ($arg=="-d6") $divide_by=6;
if ($arg=="-d7") $divide_by=7;
if ($arg=="-d8") $divide_by=8;
if ($arg=="-d9") $divide_by=9;
if ($arg=="-d10") $divide_by=10;
if ($arg=="-d12") $divide_by=12;
if ($arg=="-d14") $divide_by=14;
if ($arg=="-d16") $divide_by=16;
if ($arg=="-d18") $divide_by=18;
if ($arg=="-d20") $divide_by=20;
if ($arg=="-d24") $divide_by=24;
if ($arg=="-d28") $divide_by=28;
if ($arg=="-d30") $divide_by=30;
} else {
$arr[] = $arg;
}
}
}
if (count($arr)==0){
$arr[]=".";
}
$total=count($arr);
if ($verbose) fwrite(STDERR, "Step: 1/6\tDefining functions.\n");
function mglob($arg){
global $arr, $normal, $i, $verbose, $total, $random;
$j=$i;
$tmp=glob($arg);
if ($random){
shuffle($tmp);
}
foreach ($tmp as $tmpitem){
if (substr($tmpitem,-3)!="/.." && substr($tmpitem,-2)!="/." && !in_array($tmpitem,$arr)){
if ($normal && count($arr)>$j){
$j++;
array_splice($arr,$j,0,$tmpitem);
} else {
$arr[]=$tmpitem;
}
$total++;
if ($verbose) fwrite(STDERR, ".");
}
}
}
function help(){
global $argv;
echo "Usage: php ".$argv[0]." [options]... [directorys]...
This php script searches duplicate files in the current directory or the one specified on the command line and show them to the standard output together with their md5sum.
-h, --help\t\tShows This and exits.
-t, --table\t\tShow results as table, otherwise the output is shown in groups.
-n, --normal\tScan in normal recursive mode, default is to scan first all files on current folder and then the files of direct subfolders and so on.
-a, --audio\t\tUse Audio Fingerprint to compare audio files, needs fpcalc, mediainfo and file to be installed. Bee carefull with this option its much slower and can make mistakes.
-b, --bash\t\tWrite 'rm' before all but the first file in a group (you can write 'php ".$argv[0]." | bash')
-v, --verbose\tBe more verbose. Output is sent to the stderr so you don't need to worry about pipes.
-fp\t\t\tAllows me to store fingerprints from files in other files with the same name but adding some extra extention.
directory\t\tThe absolute or relative path to some directory without the last slash \"/\" default is \".\" (current directory).
";
die();
}
//http://php.net/manual/en/function.filesize.php#106569
function human_filesize($bytes, $decimals = 2) {
$sz = 'BKMGTP';
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor];
}
function human_time($time){
$s=$time%60;
$m=floor($time/60)%60;
$h=floor($time/3600);
return $h.":".$m.":".$s;
}
//http://php.net/manual/en/function.shuffle.php#94697
//this is just for fun
function shuffle_assoc(&$array) {
$new = array();
$keys = array_keys($array);
shuffle($keys);
foreach($keys as $key) {
$new[$key] = $array[$key];
}
$array = $new;
return true;
}
function msubstr($str,$len){
if (strlen($str)<=$len) return $str;
$len = floor(($len - 3)/2);
return substr($str,0,$len)."...".substr($str,-$len);
}
if ($verbose) fwrite(STDERR, "\nStep: 2/6\tStarting file scan.\n");
$run = true;
while (isset($arr[$i]) && $run){
$item = $arr[$i];
if ($verbose) fwrite(STDERR, round(($files/$total)*100)."% ".$files."/".$total."\t".$item."");
if (filetype($item)=="file"){
$files++;
if (substr($item,-strlen($fpext))==$fpext){
if ($verbose) fwrite(STDERR, " skipping fingerprint file ");
} else {
$size = filesize($item);
$sizes[$size][] = $item;
if ($audio){
//$type=exec('file '.str_replace("?","\\?",escapeshellarg($item)));
$type=exec('file '.escapeshellarg($item));
if (strpos($type,"Audio")!==false ||
strpos($type,"audio")!==false ||
strpos($type,"Microsoft ASF")!==false ||
strpos($type,"MPEG v4 system")!==false){
$duration = exec('mediainfo --Inform="General;%Duration%" '.escapeshellarg($item).''); // General outputs always in ms, audio outputs in s if duration%1000==0
$duration = floor($duration/1000); // ms to s
$duration = floor($duration/$divide_by); // to match more files while comparing durations.
$duration = $duration * $divide_by;
$durations[$duration][] = $item;
if ($verbose) fwrite(STDERR, "\tAudio ".human_time($duration));
}
}
}
} else if (filetype($item)=="dir"){
mglob($item."/*");
mglob($item."/.*");
mglob($item."/*[*");
mglob($item."/*]*");
} else if ($verbose){
fwrite(STDERR, "Filetype ".filetype($item)." isn't file nor dir, skipping.");
}
if ($verbose) fwrite(STDERR, "\n");
$i++;
}
if ($verbose) fwrite(STDERR, "\n(3 / 6)\tComparing sizes.\n");
foreach ($sizes as $size => $files){
if (count($files)==1){
unset($sizes[$size]);
}
}
if ($random){
shuffle_assoc($sizes);
}
$total=count($sizes);
$i=0;
$run = true;
foreach ($sizes as $size => $sizdups){
if (!$run) break;
$i++;
if ($verbose) fwrite(STDERR, round(($i/$total)*100)."% ".$i."/".$total."\t".human_filesize($size)."\n");
foreach ($sizdups as $file){
$md5 = md5_file($file);
$md5arr[$md5][] = $file;
if ($verbose) fwrite(STDERR, "\tmd5: ".msubstr($md5,18)." ".$file."\n");
}
}
$run = true;
if ($audio){
if ($verbose) fwrite(STDERR, "\n(4 / 6)\tComparing durations.\n");
foreach ($durations as $duration => $files){
if (count($files)==1){
unset($durations[$duration]);
}
}
if ($random){
shuffle_assoc($durations);
}
$total=count($durations);
$i=0;
foreach ($durations as $duration => $duration_dups){
if (!$run) break;
$i++;
if ($verbose) fwrite(STDERR, round(($i/$total)*100)."% ".$i."/".$total."\t".human_time($duration)."\n");
foreach ($duration_dups as $file){
unset($output);
if (file_exists($file.'.'.$fpext)){
$output = file_get_contents($file.'.'.$fpext);
$fprint_md5=md5_file($file.'.'.$fpext);
} else {
exec('fpcalc -length '.$duration.' '.str_replace("?","\\?",escapeshellarg($file)),$output);
$output = implode(" ",$output);
$output = explode("=",$output);
$output = end($output);
$fprint_md5 = md5($output);
if ($store_fp){
file_put_contents($file.'.'.$fpext, $output);
}
}
if (strlen($output)<=6){//invalid output: 'AQAAAA' or ''
if ($verbose) fwrite(STDERR, "\tSkipping ".$file."\n");
}else {
$fprints[$fprint_md5][] = array($file,$output);
if ($verbose) fwrite(STDERR, "\tfprint_md5: ".msubstr($fprint_md5,18)." ".$file."\n");
}
}
}
}
if ($verbose) fwrite(STDERR, "\n(5 / 6)\tComparing md5.\n");
//ksort($md5arr);
$total=count($md5arr);
$i=0;
$run = true;
foreach ($md5arr as $md5 => $dups){
if (!$run) break;
$i++;
if (count($dups)>1){
if ($verbose) fwrite(STDERR, round(($i/$total)*100)."% ".$i."/".$total."\tFollowing files have same md5: ");
if (!$table) {
if ($bash) echo "#".$md5."\n";
else echo $md5."\n";
}
if ($bash){
$j=0;
foreach ($dups as $dup){
$dup=escapeshellarg($dup);
if ($j==0) echo "# ".$dup."\n";
else echo "rm ".$dup."\n";
$j++;
}
} else {
foreach ($dups as $dup){
if ($table) echo $md5." ".$dup."\n";
else echo "\t".$dup."\n";
}
}
}
}
$run = true;
if ($audio){
if ($verbose) fwrite(STDERR, "\n(6 / 6)\tComparing Audio fingerprints.\n");
//ksort($fprints);
$total=count($fprints);
$i=0;
foreach ($fprints as $fprint_md5 => $fprint_dups){
if (!$run) break;
$i++;
if (count($fprint_dups)>1){
if ($verbose) fwrite(STDERR, "\n".round(($i/$total)*100)."% ".$i."/".$total."\tFollowing files have same fingerprint_md5: ");
if (!$table) echo $fprint_md5."\n";
foreach ($fprint_dups as $dup){
if ($table) echo $fprint_md5." ".$dup[0]."\t".msubstr($dup[1],43)."\n";
else echo "\t".$dup[0]."\t".msubstr($dup[1],43)."\n";
}
}
}
}
if ($verbose) fwrite(STDERR, "Done!\n");