Skip to content

Commit

Permalink
feat: support 3gp
Browse files Browse the repository at this point in the history
  • Loading branch information
klieber committed Aug 31, 2023
1 parent c0d4010 commit 2d973cb
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ WORKDIR /usr/src/app

COPY package*.json ./

RUN apt-get update && apt-get install -y libvips && apt-get clean
RUN apt-get update && apt-get install -y libvips ffmpeg && apt-get clean
RUN npm ci --platform=linux --arch=x64 --only=production --ignore-scripts
RUN npm uninstall sharp && npm install --platform=linux --arch=x64 sharp

Expand Down
5 changes: 4 additions & 1 deletion default-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
"target": "/media/photo"
},
{
"extensions": ["mp4", "mov"],
"extensions": ["mp4", "mov", "3gp"],
"conversions": {
"3gp": "mp4"
},
"target": "/media/video"
}
]
Expand Down
40 changes: 38 additions & 2 deletions lib/support/file-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ const DropboxContentHasher = require('../provider/dropbox/dropbox-content-hasher
const logger = require('./logger').create('lib/support/file-utils');
const config = require('../config');
const sharp = require('sharp');
const ffmpeg = require('fluent-ffmpeg');

const CONVERTERS = {
jpg: convertImage,
mp4: convertVideo
};

async function createFileInfo(sourceFile, targetDirectory) {
const timestamp = await extractDate(sourceFile);
Expand Down Expand Up @@ -80,8 +86,13 @@ async function moveFile(sourceFile, targetDirectory, conversions) {
if (!config.dry_run) {
await mkdirp(target.dirname);
if (target.conversion) {
let buffer = await fs.promises.readFile(sourceFile);
await sharp(buffer).toFormat(target.extension).toFile(target.name);
let convert = CONVERTERS[target.extension];

if (!convert) {
throw new Error(`conversion not supported: ${target.extension}`);
}

await convert(sourceFile, target);
} else {
await fs.promises.copyFile(sourceFile, target.name);
}
Expand All @@ -106,6 +117,31 @@ async function hash(filename) {
return hasher.digest('hex');
}

async function convertImage(sourceFile, target) {
let buffer = await fs.promises.readFile(sourceFile);
await sharp(buffer).toFormat(target.extension).toFile(target.name);
}

async function convertVideo(sourceFile, target) {
await new Promise((resolve, reject) => {
ffmpeg({ source: sourceFile })
.on('progress', (progress) => {
logger.verbose(`[ffmpeg] ${JSON.stringify(progress)}`);
})
.on('error', (err) => {
logger.verbose(`[ffmpeg] error: ${err.message}`);
reject(err);
})
.on('end', () => {
logger.verbose('[ffmpeg] finished');
resolve();
})
.withAudioCodec('libmp3lame')
.toFormat(target.extension)
.saveToFile(target.name);
});
}

module.exports = {
createFileInfo: createFileInfo,
moveFile: moveFile
Expand Down
49 changes: 45 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"dropbox": "10.34.0",
"ffprobe": "1.1.2",
"ffprobe-static": "3.1.0",
"fluent-ffmpeg": "2.1.2",
"node-exiftool": "2.3.0",
"node-fetch": "3.3.2",
"rc": "1.2.8",
Expand Down

0 comments on commit 2d973cb

Please sign in to comment.