Es6 refactor, more to come. Will be re-structuring v2f.js to handle multiple formats and better manage files in different locations.
This commit is contained in:
parent
36c4875d1b
commit
ff8d0a5f93
166
v2f.js
166
v2f.js
|
@ -1,103 +1,119 @@
|
||||||
var exec = require('child_process').exec,
|
/*jshint strict: true, esversion:6, node: true, asi: true*/
|
||||||
fs = require('fs'),
|
|
||||||
_tmp = './temp';
|
|
||||||
|
|
||||||
//var frame_height = 7.61;
|
'use strict'
|
||||||
//var frame_height = 7.49;
|
const exec = require('child_process').exec
|
||||||
var frame_height = 7.62;
|
const fs = require('fs')
|
||||||
var frame_padding = 0;
|
const _tmp = './temp'
|
||||||
|
|
||||||
var frame_dimensions = function (dpi) {
|
//var frame_height = 7.61
|
||||||
var h = Math.round(frame_height * (dpi / 25.4)),
|
//var frame_height = 7.49
|
||||||
w = Math.round(10.5 * (dpi / 25.4)),
|
let frame_height = 7.62
|
||||||
o = Math.round(16 * (dpi / 25.4));
|
let frame_padding = 0
|
||||||
return {h: h, w: w, o: o, dpi: dpi};
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
class Dimensions{
|
||||||
convert() - Turn video into sheet of images
|
constructor (film, dpi) {
|
||||||
|
const IN = dpi / 25.4
|
||||||
|
this.h = Math.round(film.frame_height * IN)
|
||||||
|
this.w = Math.round(film.a * IN)
|
||||||
|
this.o = Math.round(film.b * IN)
|
||||||
|
this.dpi = dpi
|
||||||
|
this.film = film
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@param: path - file path (absolute)
|
/** *
|
||||||
@param: dpi - target printing dpi
|
* Turn video into sheet of images
|
||||||
@param: length - strip length in frames
|
|
||||||
|
|
||||||
|
* @function
|
||||||
|
* @param {String} path file path (absolute)
|
||||||
|
* @param {Integer} dpi target printing dpi
|
||||||
|
* @param {Integer} length strip length in frames
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
var convert = function (path, dpi) {
|
function convert (path, dpi) {
|
||||||
'use strict';
|
const film = { frame_height: frame_height, a : 10.5, b : 16}
|
||||||
var dim = frame_dimensions(dpi),
|
const dim = new Dimensions(film, dpi)
|
||||||
file = path.split('/').pop(),
|
const file = path.split('/').pop()
|
||||||
loc = _tmp + '/',
|
const loc = _tmp + '/'
|
||||||
execStr = 'avconv -i "' + path + '" -s ' + dim.w + 'x' + dim.h + ' -qscale 1 "' + loc + 'sequence_%04d.jpg"';
|
const execStr = `avconv -i "${path}" -s ${dim.w}x${dim.h} -qscale 1 "${loc}sequence_%04d.jpg"`
|
||||||
|
|
||||||
console.log('Converting ' + file + '...');
|
console.log(`Converting ${file}...`)
|
||||||
console.log('Exporting all frames with aspect ratio: ' + (dim.w / dim.h) + '...');
|
console.log(`Exporting all frames with aspect ratio: ${dim.w / dim.h} ...`)
|
||||||
|
|
||||||
fs.mkdirSync(_tmp);
|
fs.mkdirSync(_tmp)
|
||||||
|
|
||||||
exec(execStr, function (ste, std) {
|
exec(execStr, (ste, std) => {
|
||||||
if (ste) {
|
if (ste) {
|
||||||
return errorHandle(ste);
|
return errorHandle(ste)
|
||||||
}
|
}
|
||||||
console.log('Frames exported successfully!');
|
console.log('Frames exported successfully!')
|
||||||
stitch(loc.substring(0, loc.length - 1), dim);
|
stitch(loc.substring(0, loc.length - 1), dim)
|
||||||
});
|
})
|
||||||
};
|
}
|
||||||
|
|
||||||
var stitch = function (loc, dim) {
|
/** *
|
||||||
'use strict';
|
* Stitch rendered frames into strips
|
||||||
var length = Math.floor((11 * dim.dpi) / dim.h) - 1,
|
|
||||||
width = Math.floor((8.5 * dim.dpi / dim.o)) - 1,
|
* @function
|
||||||
page = 0,
|
* @param {String} loc Path of folder containing frames
|
||||||
pageCount = 0,
|
* @param {Object} dim Dimensions object
|
||||||
cmd = 'find "' + loc + '" -type f -name "sequence_*.jpg"',
|
*
|
||||||
find_cb = function (ste, std) {
|
*/
|
||||||
|
|
||||||
|
function stitch (loc, dim) {
|
||||||
|
const length = Math.floor((11 * dim.dpi) / dim.h) - 1
|
||||||
|
const width = Math.floor((8.5 * dim.dpi / dim.o)) - 1
|
||||||
|
let page = 0
|
||||||
|
let pageCount = 0
|
||||||
|
let cmd = `find "${loc}" -type f -name "sequence_*.jpg"`
|
||||||
|
function find_cb (ste, std) {
|
||||||
if (ste) {
|
if (ste) {
|
||||||
return errorHandle(ste);
|
return errorHandle(ste)
|
||||||
}
|
}
|
||||||
var frames = std.split('\n'),
|
const frames = std.split('\n')
|
||||||
execStr = 'montage ',
|
let execStr = 'montage '
|
||||||
montage_cb = function (stee, stdd) {
|
function montage_cb (stee, stdd) {
|
||||||
if (stee) {
|
if (stee) {
|
||||||
return errorHandle(ste);
|
return errorHandle(ste)
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('Created page_' + pageCount + '.jpg!');
|
console.log(`Created page_${pageCount}.jpg!`)
|
||||||
pageCount++;
|
pageCount++
|
||||||
if (pageCount === page) {
|
if (pageCount === page) {
|
||||||
console.log('Cleaning up...');
|
console.log('Cleaning up...');
|
||||||
setTimeout(function () {
|
setTimeout(() => {
|
||||||
exec('find "' + loc + '" -type f -name "sequence_*.jpg" -delete', function () {
|
exec(`find "${loc}" -type f -name "sequence_*.jpg" -delete`, () => {
|
||||||
fs.rmdirSync(_tmp);
|
fs.rmdirSync(_tmp)
|
||||||
console.log('Done!');
|
console.log('Done!')
|
||||||
});
|
})
|
||||||
}, 1000);
|
}, 1000)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
for (var i = 0; i < frames.length; i++) {
|
for (let i = 0; i < frames.length; i++) {
|
||||||
execStr += frames[i] + ' ';
|
execStr += frames[i] + ' '
|
||||||
if ((i + 1) % (width * length) === 0 || i === frames.length - 1) {
|
if ((i + 1) % (width * length) === 0 || i === frames.length - 1) {
|
||||||
execStr += '\ -tile 1x' + length + ' -geometry ' + dim.w + 'x' + dim.h + '+' + Math.round((dim.o - dim.w) / 2) + '+0 miff:- |\ \nmontage - -geometry +0+0 -tile ' + width + 'x1 -density ' + dim.dpi + ' "./page_' + page + '.jpg"';
|
execStr += '\ -tile 1x' + length + ' -geometry ' + dim.w + 'x' + dim.h + '+' + Math.round((dim.o - dim.w) / 2) + '+0 miff:- |\ \nmontage - -geometry +0+0 -tile ' + width + 'x1 -density ' + dim.dpi + ' "./page_' + page + '.jpg"'
|
||||||
exec(execStr, montage_cb);
|
exec(execStr, montage_cb)
|
||||||
execStr = 'montage ';
|
execStr = 'montage '
|
||||||
page++;
|
page++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
loc = _tmp;
|
|
||||||
|
|
||||||
console.log('Stitching frames into sheets...');
|
|
||||||
console.log('Sheets will contain ' + width + 'x' + length + ' frames...');
|
|
||||||
exec(cmd, find_cb);
|
|
||||||
};
|
|
||||||
var errorHandle = function (err) {
|
|
||||||
'use strict';
|
|
||||||
if (process.argv.indexOf('-v') !== -1 || process.argv.indexOf('--verbose') !== -1){
|
|
||||||
console.error(err);
|
|
||||||
} else {
|
|
||||||
console.error('Error running program. Run in verbose mode for more info (-v,--verbose)');
|
|
||||||
}
|
}
|
||||||
process.exit(1);
|
|
||||||
|
loc = _tmp
|
||||||
|
|
||||||
|
console.log('Stitching frames into sheets...')
|
||||||
|
console.log(`Sheets will contain ${width}x${length} frames...`)
|
||||||
|
exec(cmd, find_cb)
|
||||||
|
};
|
||||||
|
|
||||||
|
var errorHandle = function (err) {
|
||||||
|
if (process.argv.indexOf('-v') !== -1 || process.argv.indexOf('--verbose') !== -1){
|
||||||
|
console.error(err)
|
||||||
|
} else {
|
||||||
|
console.error('Error running program. Run in verbose mode for more info (-v,--verbose)')
|
||||||
|
}
|
||||||
|
process.exit(1)
|
||||||
};
|
};
|
||||||
|
|
||||||
process.on('uncaughtException', function (err) {
|
process.on('uncaughtException', function (err) {
|
||||||
|
|
Loading…
Reference in New Issue