Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#16 Preview video generation #22

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ bin/
media/
dist/
pouch__all_dbs__/
_previews/
_media
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ db.changes({
});
```

### Preview Videos
This tool is able to generate low resolution webm preview videos of all media, intended to be used in web based clients.
They are generated in the background after the media is found or detected to have changed, so may not be available immediately.
They can be accessed via the following url format `/media/preview/<name>`

Development
-----------
Expand Down
7 changes: 7 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const express = require('express')
const pinoHttp = require('pino-http')
const PouchDB = require('pouchdb-node')
const util = require('util')
const path = require('path')
const recursiveReadDir = require('recursive-readdir')
const { getId } = require('./util')

Expand All @@ -18,6 +19,12 @@ module.exports = function ({ db, config, logger }) {
mode: 'minimumForPouchDB'
}))

app.get('/media/preview/:id', wrap(async (req, res) => {
const { previewPath } = await db.get(req.params.id.toUpperCase())

res.sendFile(path.join(process.cwd(), previewPath))
}))

app.get('/cls', wrap(async (req, res) => {
const { rows } = await db.allDocs({ include_docs: true })

Expand Down
6 changes: 6 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ const defaults = {
width: 256,
height: -1
},
previews: {
enable: false,
width: 160,
height: -1,
bitrate: '40k'
},
isProduction: process.env.NODE_ENV === 'production',
logger: {
level: process.env.NODE_ENV === 'production' ? 'info' : 'trace',
Expand Down
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const pino = require('pino')
const config = require('./config')
const PouchDB = require('pouchdb-node')
const scanner = require('./scanner')
const previews = require('./previews')
const app = require('./app')

const logger = pino(Object.assign({}, config.logger, {
Expand All @@ -16,3 +17,7 @@ logger.info(config)

scanner({ logger, db, config })
app({ logger, db, PouchDB, config }).listen(config.http.port)

if (config.previews.enable) {
previews({ logger, db, config })
}
91 changes: 91 additions & 0 deletions src/previews.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const cp = require('child_process')
const { Observable } = require('@reactivex/rxjs')
const util = require('util')
const mkdirp = require('mkdirp-promise')
const os = require('os')
const fs = require('fs')
const path = require('path')
const { fileExists } = require('./util')

const statAsync = util.promisify(fs.stat)
const unlinkAsync = util.promisify(fs.unlink)
const renameAsync = util.promisify(fs.rename)

module.exports = function ({ config, db, logger }) {
Observable
.create(async o => {
db.changes({
since: 'now',
live: true
}).on('change', function (change) {
o.next([change.id, change.deleted])
}).on('error', function (err) {
logger.error({ err })
})

// Queue all for attempting to regenerate previews, if they are needed
const { rows } = await db.allDocs()
rows.forEach(d => o.next([d.id, false]))
logger.info('Queued all for preview validity check')
})
.concatMap(async ([id, deleted]) => {
await generatePreview(id, deleted)
})
.subscribe()

async function generatePreview (mediaId, deleted) {
try {
const destPath = path.join('_previews', mediaId) + '.webm'
if (deleted) {
await unlinkAsync(destPath)
return
}

const doc = await db.get(mediaId)
if (doc.previewTime === doc.mediaTime && await fileExists(destPath)) {
return
}

const mediaLogger = logger.child({
id: mediaId,
path: doc.mediaPath
})

const tmpPath = destPath + '.new'

const args = [
// TODO (perf) Low priority process?
config.paths.ffmpeg,
'-hide_banner',
'-i', `"${doc.mediaPath}"`,
'-f', 'webm',
'-an',
'-c:v', 'libvpx',
'-b:v', config.previews.bitrate,
'-auto-alt-ref', '0',
`-vf scale=${config.previews.width}:${config.previews.height}`,
'-threads 1',
`"${tmpPath}"`
]

await mkdirp(path.dirname(tmpPath))
mediaLogger.info('Starting preview generation')
await new Promise((resolve, reject) => {
cp.exec(args.join(' '), (err, stdout, stderr) => err ? reject(err) : resolve())
})

const previewStat = await statAsync(tmpPath)
doc.previewSize = previewStat.size
doc.previewTime = doc.mediaTime
doc.previewPath = destPath

await renameAsync(tmpPath, destPath)

await db.put(doc)

mediaLogger.info('Finished preview generation')
} catch (err) {
logger.error({ err })
}
}
}
13 changes: 3 additions & 10 deletions src/scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const mkdirp = require('mkdirp-promise')
const os = require('os')
const fs = require('fs')
const path = require('path')
const { getId } = require('./util')
const { getId, fileExists } = require('./util')
const moment = require('moment')

const statAsync = util.promisify(fs.stat)
Expand Down Expand Up @@ -60,15 +60,8 @@ module.exports = function ({ config, db, logger }) {
})
await Promise.all(rows.map(async ({ doc }) => {
try {
if (doc.mediaPath.indexOf(config.scanner.paths) === 0) {
try {
const stat = await statAsync(doc.mediaPath)
if (stat.isFile()) {
return
}
} catch (e) {
// File not found
}
if (doc.mediaPath.indexOf(config.scanner.paths) === 0 && await fileExists(doc.mediaPath)) {
return
}

deleted.push({
Expand Down
16 changes: 16 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const path = require('path')
const fs = require('fs')
const util = require('util')

const statAsync = util.promisify(fs.stat)

module.exports = {
getId (fileDir, filePath) {
Expand All @@ -7,5 +11,17 @@ module.exports = {
.replace(/\.[^/.]+$/, '')
.replace(/\\+/g, '/')
.toUpperCase()
},

async fileExists (destPath) {
try {
const stat = await statAsync(destPath)
if (stat.isFile()) {
return true
}
} catch (e) {
// File not found
}
return false
}
}