-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: split out code into files + separate image/video processing
- Loading branch information
Showing
13 changed files
with
129 additions
and
102 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const gm = require('gm') | ||
|
||
const DEFAULT_PHOTO_QUALITY = 90 // percent | ||
|
||
exports.prepare = function (source, options) { | ||
// start processing with GraphicsMagick | ||
const image = gm(source) | ||
|
||
// read baked-in orientation info, and output a rotated image with orientation=0 | ||
image.autoOrient() | ||
|
||
// optional watermark | ||
const cropping = options.width && options.height | ||
if (options.watermark && !cropping) { | ||
image.composite(options.watermark.file) | ||
if (options.watermark.position === 'Repeat') { | ||
image.tile(options.watermark.file) | ||
} else if (typeof options.watermark.position === 'string') { | ||
image.gravity(options.watermark.position) | ||
} else { | ||
image.gravity('SouthEast') | ||
} | ||
} | ||
|
||
// resize if necessary | ||
if (cropping) { | ||
// crop to the exact height and weight | ||
image.resize(options.width, options.height, '^') | ||
image.gravity('Center') | ||
image.crop(options.width, options.height) | ||
image.out('+repage') | ||
} else if (options.height) { | ||
// resize to a maximum height | ||
image.resize(null, options.height, '>') | ||
} else if (options.width) { | ||
// resize to a maximum width | ||
image.resize(options.width, null, '>') | ||
} | ||
|
||
// default quality, for typical web-friendly sizes | ||
image.quality(options.quality || DEFAULT_PHOTO_QUALITY) | ||
|
||
// apply custom post-processing arguments (sharpen, brightness...) | ||
exports.addRawArgs(image, options.args) | ||
|
||
return image | ||
} | ||
|
||
/* | ||
Applies an array of GraphicsMagick string arguments to a <gm> instance | ||
e.g. apply(image, ['--modulate 120']) | ||
*/ | ||
exports.addRawArgs = function (image, args) { | ||
if (args && args.length) { | ||
args.forEach(arg => { | ||
const index = arg.indexOf(' ') | ||
if (index === -1) { | ||
image.out(arg) | ||
} else { | ||
const command = arg.substr(0, index) | ||
const values = arg.substr(index + 1) | ||
image.out(command, values) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const path = require('path') | ||
|
||
const DEFAULT_AUDIO_BITRATE = '96k' | ||
const DEFAULT_VIDEO_FPS = 25 | ||
// const ENCODER_CRF_MAX = { h264: 51, vpx: 63 } | ||
|
||
exports.prepare = function (source, target, options) { | ||
// source file | ||
const args = ['-i', source] | ||
|
||
// output framerate | ||
args.push('-r', DEFAULT_VIDEO_FPS) | ||
|
||
// misc options | ||
args.push('-vsync', '2', '-movflags', '+faststart') | ||
|
||
// audio bitrate | ||
args.push('-ab', DEFAULT_AUDIO_BITRATE) | ||
|
||
// output to mp4 or webm which are well read on the web | ||
if (options.format === 'webm') { | ||
args.push('-f', 'webm', '-vcodec', 'libvpx-vp9', '-strict', '-2') | ||
} else { | ||
args.push('-f', 'mp4', '-vcodec', 'libx264') | ||
} | ||
|
||
// set average video bitrate or perceptual quality | ||
if (options.bitrate) { | ||
args.push('-b:v', options.bitrate) | ||
} else { | ||
// TODO: add configurable CRF value | ||
args.push('-b:v', 0, '-crf', 20) | ||
} | ||
|
||
// AVCHD/MTS videos need a full-frame export to avoid interlacing artefacts | ||
if (path.extname(source).toLowerCase() === '.mts') { | ||
args.push('-vf', 'yadif=1') | ||
} | ||
|
||
// target filename | ||
args.push('-y', target) | ||
|
||
return args | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters