enigma-bbs/util/exiftool2desc.js

137 lines
3.1 KiB
JavaScript
Raw Permalink Normal View History

#!/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) {
2018-06-30 04:39:36 +00:00
// nothing if we don't know at least the author or title
if (!metadata.author && !metadata.title) {
2018-06-30 04:39:36 +00:00
return;
}
let desc = `${metadata.artist || 'Unknown Artist'} - ${
metadata.title || 'Unknown'
} (`;
if (metadata.year) {
2018-06-30 04:39:36 +00:00
desc += `${metadata.year}, `;
}
desc += `${metadata.audioBitrate})`;
return desc;
}
2017-08-27 22:25:32 +00:00
function videoFile(metadata) {
2018-06-30 04:39:36 +00:00
return `${metadata.fileType} video(${metadata.imageSize}px, ${metadata.duration}, ${metadata.audioBitsPerSample}/${metadata.audioSampleRate} audio)`;
2017-08-27 22:25:32 +00:00
}
function documentFile(metadata) {
2018-06-30 04:39:36 +00:00
// nothing if we don't know at least the author or title
if (!metadata.author && !metadata.title) {
2018-06-30 04:39:36 +00:00
return;
}
let result = metadata.author || '';
if (result) {
2018-06-30 04:39:36 +00:00
result += ' - ';
}
result += metadata.title || 'Unknown Title';
return result;
}
function imageFile(metadata) {
2018-06-30 04:39:36 +00:00
let desc = `${metadata.fileType} image (`;
if (metadata.animationIterations) {
2018-06-30 04:39:36 +00:00
desc += 'Animated, ';
}
desc += `${metadata.imageSize}px`;
const created = moment(metadata.createdate);
if (created.isValid()) {
2018-06-30 04:39:36 +00:00
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) {
2018-06-30 04:39:36 +00:00
console.info(TOOL_VERSION);
return 0;
}
if (0 === argv._.length || argv.help) {
2018-06-30 04:39:36 +00:00
console.info('usage: exiftool2desc.js [--version] [--help] PATH');
return 0;
}
const path = argv._[0];
fs.readFile(path, (err, data) => {
if (err) {
2018-06-30 04:39:36 +00:00
return -1;
}
exiftool.metadata(data, (err, metadata) => {
if (err) {
2018-06-30 04:39:36 +00:00
return -1;
}
const handler = FILETYPE_HANDLERS[metadata.fileType];
if (!handler) {
2018-06-30 04:39:36 +00:00
return -1;
}
const info = handler(metadata);
if (!info) {
2018-06-30 04:39:36 +00:00
return -1;
}
console.info(info);
return 0;
});
});
}
return main();