Skip to content

Commit

Permalink
fix: add tests + handle custom video tags
Browse files Browse the repository at this point in the history
  • Loading branch information
rprieto committed Sep 10, 2022
1 parent 5d0c34e commit 681fbe1
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 9 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,15 @@ opts = { height: 100, width: 100 }
opts = { quality: 80 }
```

##### Preserve EXIF Data
##### Preserving metadata

By default, all metadata is removed from downsized images.
This option will keep all EXIF / IPTC / XMP metadata in the target files.

When setting this option to true, images will no longer be auto-rotated.

```js
// note: this will no longer auto-orient images according to its EXIF data
opts = { preserveExif: true }
opts = { keepMetadata: true }
```

##### Watermark
Expand Down Expand Up @@ -227,10 +231,13 @@ opts = { framerate: 60 }
opts = { framerate: 0 } // preserve the original source video's FPS
```

##### Preserve Metadata
##### Preserving Metadata

By default, all metadata is removed from the target video.
This option preserves the metadata.

```js
opts = { preserveMetadata: true }
opts = { keepMetadata: true }
```

##### Conversion progress
Expand Down
2 changes: 1 addition & 1 deletion lib/image/gmagick.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports.prepare = function (source, options) {
// start processing with GraphicsMagick
const image = gm(source)

if (!options.preserveExif) {
if (!options.keepMetadata) {
// read baked-in orientation info, and output a rotated image with orientation=0
// note: this strips EXIF data
image.autoOrient()
Expand Down
11 changes: 8 additions & 3 deletions lib/video/ffargs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const ENCODER_CRF = {
}

exports.prepare = function (source, target, options) {
// output framerate
// overwrite the file if already exists
const args = ['-y']

// IF VAAPI acceleration needs to go before -i input files
Expand All @@ -30,8 +30,13 @@ exports.prepare = function (source, target, options) {
// misc options
args.push('-vsync', '2', '-movflags', '+faststart')

if (options.preserveMetadata) {
args.push('-map_metadata', 0)
// remove metadata if required
if (options.keepMetadata) {
// the second argument is required
// to keep non-standard tags like <com.apple.quicktime.location.ISO6709>
args.push('-map_metadata', '0', '-movflags', 'use_metadata_tags')
} else {
args.push('-map_metadata', -1)
}

// audio bitrate
Expand Down
Binary file added test-data/input/images/metadata.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test-data/input/videos/metadata.mp4
Binary file not shown.
11 changes: 11 additions & 0 deletions test/integration/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ exports.video = function (args, done) {
})
}

exports.metadata = function (type, args, done) {
const input = `test-data/input/${args.input}`
const actual = `test-data/actual/${args.input}`
convert[type](input, actual, args.options, (err) => {
const output = childProcess.execSync(`exiftool -j -g ${actual}`)
const fields = JSON.parse(output)[0]
if (err) throw err
done(err, fields)
})
}

function compareImage (expected, actual, done) {
const isGif = actual.match(/\.gif$/i)
// test metadata
Expand Down
25 changes: 25 additions & 0 deletions test/integration/image.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const should = require('should/as-function')
const diff = require('./diff')

describe('image', () => {
Expand Down Expand Up @@ -156,6 +157,30 @@ describe('image', () => {
}, done)
})

it('removes all metadata by default', done => {
diff.metadata('image', {
input: 'images/metadata.jpg',
options: { height: 150 }
}, (err, fields) => {
should(err).be.undefined()
should(fields).not.have.properties(['EXIF', 'XMP', 'IPTC'])
done()
})
})

it('can optionally keep metadata', done => {
diff.metadata('image', {
input: 'images/metadata.jpg',
options: { height: 150, keepMetadata: true }
}, (err, fields) => {
should(err).be.undefined()
should(fields).have.propertyByPath('EXIF', 'ImageDescription').eql('Red bike')
should(fields).have.propertyByPath('XMP', 'Subject').eql('Bike')
should(fields).have.propertyByPath('IPTC', 'Keywords').eql('bike')
done()
})
})

const ORIENTATIONS = [1, 2, 3, 4, 5, 6, 7, 8]

ORIENTATIONS.forEach((orientation) => {
Expand Down
25 changes: 25 additions & 0 deletions test/integration/video.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const should = require('should/as-function')
const assert = require('assert')
const diff = require('./diff')
const convert = require('../../lib/index')
Expand Down Expand Up @@ -65,6 +66,30 @@ describe('video', () => {
}, done)
})

it('removes metadata by default', done => {
diff.metadata('video', {
input: 'videos/metadata.mp4',
options: {}
}, (err, fields) => {
should(err).be.null()
should(fields).not.have.propertyByPath('QuickTime', 'Title')
should(fields).not.have.propertyByPath('QuickTime', 'GPSCoordinates')
done()
})
})

it('can optionally keep metadata', done => {
diff.metadata('video', {
input: 'videos/metadata.mp4',
options: { keepMetadata: true }
}, (err, fields) => {
should(err).be.null()
should(fields).have.propertyByPath('QuickTime', 'Title').eql('Cool video')
should(fields).have.propertyByPath('QuickTime', 'GPSCoordinates').eql('60 deg 0\' 0.00" N, 10 deg 0\' 0.00" E, 0 m Above Sea Level')
done()
})
})

it('can report progress when processing videos', done => {
const report = []
const input = 'test-data/input/videos/big_buck_bunny.mp4'
Expand Down

0 comments on commit 681fbe1

Please sign in to comment.