Use largest image in torrent as the poster

...as a fallback, if no the torrent doesn't contain any video files
This commit is contained in:
DC
2016-03-21 17:49:19 -07:00
parent 78f1d4c8eb
commit 13902daedd
2 changed files with 29 additions and 10 deletions

View File

@@ -510,6 +510,7 @@ function updateTorrentProgress () {
var changed = false
state.client.torrents.forEach(function (torrent) {
var torrentSummary = getTorrentSummary(torrent.infoHash)
if (!torrentSummary) return
torrent.files.forEach(function (file, index) {
var numPieces = file._endPiece - file._startPiece + 1
var numPiecesPresent = 0
@@ -530,12 +531,12 @@ function updateTorrentProgress () {
}
function generateTorrentPoster (torrent, torrentSummary) {
torrentPoster(torrent, function (err, buf) {
torrentPoster(torrent, function (err, buf, extension) {
if (err) return onWarning(err)
// save it for next time
mkdirp(config.CONFIG_POSTER_PATH, function (err) {
if (err) return onWarning(err)
var posterFilePath = path.join(config.CONFIG_POSTER_PATH, torrent.infoHash + '.jpg')
var posterFilePath = path.join(config.CONFIG_POSTER_PATH, torrent.infoHash + extension)
fs.writeFile(posterFilePath, buf, function (err) {
if (err) return onWarning(err)
// show the poster

View File

@@ -4,19 +4,32 @@ var captureVideoFrame = require('./capture-video-frame')
var path = require('path')
function torrentPoster (torrent, cb) {
// filter out file formats that the <video> tag definitely can't play
// First, try to use the largest video file
// Filter out file formats that the <video> tag definitely can't play
var videoFile = getLargestFileByExtension(torrent, ['.mp4', '.m4v', '.webm', '.mov', '.mkv'])
if (videoFile) return torrentPosterFromVideo(videoFile, torrent, cb)
// Second, try to use the largest image file
var imgFile = getLargestFileByExtension(torrent, ['.gif', '.jpg', '.png'])
if (imgFile) return torrentPosterFromImage(imgFile, torrent, cb)
// TODO: generate a waveform from the largest sound file
// Finally, admit defeat
return cb(new Error('Cannot generate a poster from any files in the torrent'))
}
function getLargestFileByExtension (torrent, extensions) {
var files = torrent.files.filter(function (file) {
var extname = path.extname(file.name)
return ['.mp4', '.m4v', '.webm', '.mov', '.mkv'].indexOf(extname) !== -1
return extensions.indexOf(extname) !== -1
})
if (files.length === 0) return cb(new Error('cannot make screenshot for any files in torrent'))
// use largest file
var file = files.reduce(function (a, b) {
if (files.length === 0) return undefined
return files.reduce(function (a, b) {
return a.length > b.length ? a : b
})
}
function torrentPosterFromVideo (file, torrent, cb) {
var index = torrent.files.indexOf(file)
var server = torrent.createServer(0)
@@ -51,7 +64,12 @@ function torrentPoster (torrent, cb) {
server.destroy()
cb(null, buf)
cb(null, buf, '.jpg')
}
}
}
function torrentPosterFromImage (file, torrent, cb) {
var extname = path.extname(file.name)
file.getBuffer((err, buf) => cb(err, buf, extname))
}