-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrename-photo-video.php
More file actions
executable file
·173 lines (143 loc) · 4.32 KB
/
rename-photo-video.php
File metadata and controls
executable file
·173 lines (143 loc) · 4.32 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
<?php
/**
* Author: Bert Slagter <bert@procurios.nl>
* License: MIT
*/
class RenamePhotoVideo
{
/**
* @param string $path
*/
public function rename($path)
{
foreach (new DirectoryIterator($path) as $fileInfo) {
if ($this->shouldSkipItem($fileInfo)) {
continue;
}
if ($fileInfo->isDir()) {
$this->rename($fileInfo->getPathname());
continue;
}
if ($this->isPhoto($fileInfo)) {
$this->processPhoto($fileInfo);
continue;
}
if ($this->isVideo($fileInfo)) {
$this->processVideo($fileInfo);
continue;
}
}
}
/**
* @param DirectoryIterator $fileInfo
* @return bool
*/
private function shouldSkipItem(DirectoryIterator $fileInfo)
{
if ($fileInfo->isDot()) {
return true;
}
if ($fileInfo->isLink()) {
return true;
}
if (substr($fileInfo->getFilename(), 0, 1) === '.') {
return true;
}
return false;
}
/**
* @param DirectoryIterator $fileInfo
* @return bool
*/
private function isPhoto(DirectoryIterator $fileInfo)
{
if (in_array(strtolower($fileInfo->getExtension()), ['jpg', 'jpeg'])) {
return true;
}
return false;
}
/**
* @param DirectoryIterator $fileInfo
*/
private function processPhoto(DirectoryIterator $fileInfo)
{
$date = $this->getDateFromPhoto($fileInfo);
$this->renameFile($fileInfo, $date);
}
/**
* @param DirectoryIterator $fileInfo
* @return DateTimeImmutable
*/
private function getDateFromPhoto(DirectoryIterator $fileInfo)
{
$exif = @exif_read_data($fileInfo->getPathname());
if (!empty($exif['DateTime'])) {
try {
return new DateTimeImmutable($exif['DateTime']);
} catch (Exception $e) {
// fallback to file date
}
}
return DateTimeImmutable::createFromFormat('U', $fileInfo->getMTime());
}
/**
* @param DirectoryIterator $fileInfo
* @return bool
*/
private function isVideo(DirectoryIterator $fileInfo)
{
if (in_array(strtolower($fileInfo->getExtension()), ['mov', 'mp4', 'm4v'])) {
return true;
}
return false;
}
/**
* @param DirectoryIterator $fileInfo
*/
private function processVideo(DirectoryIterator $fileInfo)
{
$date = $this->getDateFromVideo($fileInfo);
$this->renameFile($fileInfo, $date);
}
/**
* @param DirectoryIterator $fileInfo
* @return DateTimeImmutable
*/
private function getDateFromVideo(DirectoryIterator $fileInfo)
{
@exec('ffprobe -show_format -v quiet ' . escapeshellarg($fileInfo->getPathname()) . ' 2>&1', $output);
$output = implode("\n", $output);
if (preg_match('/^TAG:date=(.*?)$/ism', $output, $match)) {
return new DateTimeImmutable($match[1]);
}
if (preg_match('/^TAG:creation_time=(.*?)$/ism', $output, $match)) {
return new DateTimeImmutable($match[1]);
}
return DateTimeImmutable::createFromFormat('U', $fileInfo->getMTime());
}
/**
* @param DirectoryIterator $fileInfo
* @param DateTimeImmutable $date
*/
private function renameFile(DirectoryIterator $fileInfo, DateTimeImmutable $date)
{
$oldFileName = $fileInfo->getPathname();
preg_match('/(\S+)$/is', $fileInfo->getFilename(), $match);
$originalFileName = $match[1];
$parentName = basename($fileInfo->getPath());
$newFileName =
$fileInfo->getPath() .
DIRECTORY_SEPARATOR .
$date->format('Y-m-d') . ' - ' . $parentName . ' - ' . $originalFileName;
if ($oldFileName == $newFileName) {
//echo 'Skipping already renamed file: ' . $oldFileName . "\n";
return;
}
echo "\n" . 'Renaming file' . "\n";
echo 'Old: ' . $oldFileName . "\n";
echo 'New: ' . $newFileName . "\n\n";
rename($oldFileName, $newFileName);
}
}
$worker = new RenamePhotoVideo();
$worker->rename(realpath('.'));