-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageMetadata.js
102 lines (95 loc) · 3.13 KB
/
ImageMetadata.js
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
// Add/modify date metadata of images based on the file name
const fs = require('fs')
const { exec } = require('child_process')
const path = require('path')
const os = require('os')
const directory = 'C:\\Media\\Images\\Camera'
function extractDateTime(filename) {
const parts = filename.split('_')
if (parts.length < 3) {
console.error(`Invalid filename format: ${filename}`)
return null
}
const dateString = parts[1]
const timeString = parts[2].split('.')[0]
const year = dateString.slice(0, 4)
const month = dateString.slice(4, 6)
const day = dateString.slice(6, 8)
const hour = timeString.slice(0, 2)
const minute = timeString.slice(2, 4)
const second = timeString.slice(4, 6)
let datetime = new Date(Date.UTC(year, month - 1, day, hour, minute, second))
if (filename.includes('_1.')) datetime.setSeconds(datetime.getSeconds() + 1)
return datetime
}
function modifyMetadata(filepath, datetime) {
return new Promise((resolve, reject) => {
const command = `exiftool -overwrite_original -DateTimeOriginal="${datetime.toISOString()}" -CreateDate="${datetime.toISOString()}" "${filepath}"`
exec(command, (error, stdout, stderr) => {
if (error) {
reject(error)
} else {
resolve()
}
})
})
}
function updateFileModificationTime(filepath, datetime) {
fs.utimes(filepath, datetime, datetime, (err) => {
if (err) {
console.error(
`Error updating file modification time for ${filepath}:`,
err
)
} else {
console.log(`File modification time updated for ${filepath}`)
}
})
}
function updateFileCreationTime(filepath, tempFilePath, datetime) {
fs.copyFile(filepath, tempFilePath, (err) => {
if (err) {
console.error(`Error copying file for ${tempFilePath}:`, err)
} else {
fs.utimes(tempFilePath, datetime, datetime, (err) => {
if (err) {
console.error(
`Error updating file creation time for ${tempFilePath}:`,
err
)
} else {
fs.rename(tempFilePath, filepath, (err) => {
if (err) {
console.error(`Error renaming file for ${filepath}:`, err)
} else {
console.log(`File creation time updated for ${filepath}`)
}
})
}
})
}
})
}
async function processFiles(directory) {
try {
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'image-metadata-'))
const files = fs.readdirSync(directory)
for (const file of files) {
const filepath = path.join(directory, file)
const stat = fs.statSync(filepath)
if (stat.isFile()) {
const datetime = extractDateTime(file)
const tempFilePath = path.join(tempDir, file)
await modifyMetadata(filepath, datetime)
updateFileModificationTime(filepath, datetime)
updateFileCreationTime(filepath, tempFilePath, datetime)
console.log(`Modified metadata for ${file}`)
}
}
console.log('All files processed successfully.')
fs.rmdirSync(tempDir, { recursive: true })
} catch (error) {
console.error('Error processing files:', error)
}
}
processFiles(directory)