-
Notifications
You must be signed in to change notification settings - Fork 104
/
exiftool2desc.js
executable file
·136 lines (115 loc) · 3.05 KB
/
exiftool2desc.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
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
#!/usr/bin/env node
/* jslint node: true */
/* eslint-disable no-console */
'use strict';
// :TODO: Make this it's own sep tool/repo
const exiftool = require('exiftool');
const fs = require('graceful-fs');
const moment = require('moment');
const TOOL_VERSION = '1.0.0.0';
// map fileTypes -> handlers
const FILETYPE_HANDLERS = {};
['AIFF', 'APE', 'FLAC', 'OGG', 'MP3'].forEach(
ext => (FILETYPE_HANDLERS[ext] = audioFile)
);
[
'PDF',
'DOC',
'DOCX',
'DOCM',
'ODB',
'ODC',
'ODF',
'ODG',
'ODI',
'ODP',
'ODS',
'ODT',
].forEach(ext => (FILETYPE_HANDLERS[ext] = documentFile));
['PNG', 'JPEG', 'GIF', 'WEBP', 'XCF'].forEach(
ext => (FILETYPE_HANDLERS[ext] = imageFile)
);
['MP4', 'MOV', 'AVI', 'MKV', 'MPG', 'MPEG', 'M4V', 'WMV'].forEach(
ext => (FILETYPE_HANDLERS[ext] = videoFile)
);
function audioFile(metadata) {
// nothing if we don't know at least the author or title
if (!metadata.author && !metadata.title) {
return;
}
let desc = `${metadata.artist || 'Unknown Artist'} - ${
metadata.title || 'Unknown'
} (`;
if (metadata.year) {
desc += `${metadata.year}, `;
}
desc += `${metadata.audioBitrate})`;
return desc;
}
function videoFile(metadata) {
return `${metadata.fileType} video(${metadata.imageSize}px, ${metadata.duration}, ${metadata.audioBitsPerSample}/${metadata.audioSampleRate} audio)`;
}
function documentFile(metadata) {
// nothing if we don't know at least the author or title
if (!metadata.author && !metadata.title) {
return;
}
let result = metadata.author || '';
if (result) {
result += ' - ';
}
result += metadata.title || 'Unknown Title';
return result;
}
function imageFile(metadata) {
let desc = `${metadata.fileType} image (`;
if (metadata.animationIterations) {
desc += 'Animated, ';
}
desc += `${metadata.imageSize}px`;
const created = moment(metadata.createdate);
if (created.isValid()) {
desc += `, ${created.format('YYYY')})`;
} else {
desc += ')';
}
return desc;
}
function main() {
const argv = (exports.argv = require('minimist')(process.argv.slice(2), {
alias: {
h: 'help',
v: 'version',
},
}));
if (argv.version) {
console.info(TOOL_VERSION);
return 0;
}
if (0 === argv._.length || argv.help) {
console.info('usage: exiftool2desc.js [--version] [--help] PATH');
return 0;
}
const path = argv._[0];
fs.readFile(path, (err, data) => {
if (err) {
return -1;
}
exiftool.metadata(data, (err, metadata) => {
if (err) {
return -1;
}
const handler = FILETYPE_HANDLERS[metadata.fileType];
if (!handler) {
return -1;
}
const info = handler(metadata);
if (!info) {
return -1;
}
console.info(info);
return 0;
});
});
}
return main();