Simplify subtitles code

This commit is contained in:
DC
2016-05-17 06:27:58 -07:00
parent d466ed085a
commit 0095687bf5
2 changed files with 46 additions and 34 deletions

View File

@@ -16,6 +16,7 @@
"dependencies": { "dependencies": {
"airplay-js": "guerrerocarlos/node-airplay-js", "airplay-js": "guerrerocarlos/node-airplay-js",
"application-config": "^0.2.1", "application-config": "^0.2.1",
"async": "^2.0.0-rc.5",
"bitfield": "^1.0.2", "bitfield": "^1.0.2",
"chromecasts": "^1.8.0", "chromecasts": "^1.8.0",
"concat-stream": "^1.5.1", "concat-stream": "^1.5.1",

View File

@@ -14,6 +14,7 @@ var ipcRenderer = electron.ipcRenderer
setupIpc() setupIpc()
var appConfig = require('application-config')('WebTorrent') var appConfig = require('application-config')('WebTorrent')
var Async = require('async')
var concat = require('concat-stream') var concat = require('concat-stream')
var dragDrop = require('drag-drop') var dragDrop = require('drag-drop')
var fs = require('fs-extra') var fs = require('fs-extra')
@@ -423,9 +424,7 @@ function openSubtitles () {
properties: [ 'openFile' ] properties: [ 'openFile' ]
}, function (filenames) { }, function (filenames) {
if (!Array.isArray(filenames)) return if (!Array.isArray(filenames)) return
// autoselect the newly added subtitle tracks addSubtitles(filenames, true)
state.playing.subtitles.selectedIndex = -1
addSubtitle({path: filenames[0]}, true)
}) })
} }
@@ -545,9 +544,7 @@ function onOpen (files) {
// In the player, the only drag-drop function is adding subtitles // In the player, the only drag-drop function is adding subtitles
var isInPlayer = state.location.current().url === 'player' var isInPlayer = state.location.current().url === 'player'
if (isInPlayer) { if (isInPlayer) {
// always autoselect one of the newly added subtitle tracks return addSubtitles(files.filter(isSubtitle), true)
state.playing.subtitles.selectedIndex = -1
return files.filter(isSubtitle).forEach((file) => addSubtitle(file, true))
} }
// Otherwise, you can only drag-drop onto the home screen // Otherwise, you can only drag-drop onto the home screen
@@ -596,27 +593,51 @@ function addTorrent (torrentId) {
ipcRenderer.send('wt-start-torrenting', torrentKey, torrentId, path) ipcRenderer.send('wt-start-torrenting', torrentKey, torrentId, path)
} }
function addSubtitle (file, autoSelect) { function addSubtitles (files, autoSelect) {
var srtToVtt = require('srt-to-vtt')
var LanguageDetect = require('languagedetect')
// Subtitles are only supported while playing video // Subtitles are only supported while playing video
if (state.playing.type !== 'video') return if (state.playing.type !== 'video') return
// No dupes allowed // Read the files concurrently, then add all resulting subtitle tracks
console.log(files)
var subs = state.playing.subtitles var subs = state.playing.subtitles
var filePath = file.path || file Async.map(files, loadSubtitle, function (err, tracks) {
if (subs.tracks.some((track) => track.filePath === filePath)) return if (err) return onError(err)
for (var i = 0; i < tracks.length; i++) {
// No dupes allowed
var track = tracks[i]
if (subs.tracks.some((t) => track.filePath === t.filePath)) continue
// Add the track
subs.tracks.push(track)
// If we're auto-selecting a track, try to find one in the user's language
if (autoSelect && (i === 0 || isSystemLanguage(track.language))) {
state.playing.subtitles.selectedIndex = subs.tracks.length - 1
}
}
// Finally, make sure no two tracks have the same label
relabelSubtitles()
})
}
function loadSubtitle (file, cb) {
var srtToVtt = require('srt-to-vtt')
var LanguageDetect = require('languagedetect')
// Read the .SRT or .VTT file, parse it, add subtitle track // Read the .SRT or .VTT file, parse it, add subtitle track
var filePath = file.path || file
fs.createReadStream(filePath).pipe(srtToVtt()).pipe(concat(function (buf) { fs.createReadStream(filePath).pipe(srtToVtt()).pipe(concat(function (buf) {
// Set the cue text position so it appears above the player controls. // Detect what language the subtitles are in
// The only way to change cue text position is by modifying the VTT. It is not
// possible via CSS.
var vttContents = buf.toString().replace(/(.*-->.*)/g, '') var vttContents = buf.toString().replace(/(.*-->.*)/g, '')
var langDetected = (new LanguageDetect()).detect(vttContents, 2) var langDetected = (new LanguageDetect()).detect(vttContents, 2)
langDetected = langDetected.length ? langDetected[0][0] : 'subtitle' langDetected = langDetected.length ? langDetected[0][0] : 'subtitle'
langDetected = langDetected.slice(0, 1).toUpperCase() + langDetected.slice(1) langDetected = langDetected.slice(0, 1).toUpperCase() + langDetected.slice(1)
// Set the cue text position so it appears above the player controls.
// The only way to change cue text position is by modifying the VTT. It is not
// possible via CSS.
var subtitles = Buffer(buf.toString().replace(/(-->.*)/g, '$1 line:88%')) var subtitles = Buffer(buf.toString().replace(/(-->.*)/g, '$1 line:88%'))
var track = { var track = {
buffer: 'data:text/vtt;base64,' + subtitles.toString('base64'), buffer: 'data:text/vtt;base64,' + subtitles.toString('base64'),
@@ -624,9 +645,8 @@ function addSubtitle (file, autoSelect) {
label: langDetected, label: langDetected,
filePath: filePath filePath: filePath
} }
subs.tracks.push(track)
if (autoSelect) selectNewlyAddedSubtitle() cb(null, track)
relabelSubtitles()
})) }))
} }
@@ -634,21 +654,12 @@ function selectSubtitle (ix) {
state.playing.subtitles.selectedIndex = ix state.playing.subtitles.selectedIndex = ix
} }
// Automatically choose the subtitle in the user's language, if possible // Checks whether a language name like "English" or "German" matches the system
function selectNewlyAddedSubtitle () { // language, aka the current locale
var oldIx = state.playing.subtitles.selectedIndex function isSystemLanguage (language) {
if (oldIx === -1) oldIx = undefined
var newIx = state.playing.subtitles.tracks.length - 1
// Find which subtitle track fits the current locale
var osLangISO = window.navigator.language.split('-')[0] // eg "en" var osLangISO = window.navigator.language.split('-')[0] // eg "en"
var trackLang = state.playing.subtitles.tracks[newIx].language // eg "German" var langIso = iso639.getCode(language) // eg "de" if language is "German"
var trackLangISO = iso639.getCode(trackLang) // eg "de" return langIso === osLangISO
if (trackLangISO === osLangISO) {
selectSubtitle(newIx) // newly added track is in the user's language
} else {
selectSubtitle(oldIx || newIx) // otherwise, if we've already selected a track, keep it
}
} }
// Make sure we don't have two subtitle tracks with the same label // Make sure we don't have two subtitle tracks with the same label
@@ -672,7 +683,7 @@ function checkForSubtitles () {
var file = torrentSummary.files[ix] var file = torrentSummary.files[ix]
if (!isSubtitle(file.name)) return if (!isSubtitle(file.name)) return
var filePath = path.join(torrentSummary.path, file.path) var filePath = path.join(torrentSummary.path, file.path)
addSubtitle(filePath) addSubtitles([filePath], false)
}) })
} }