|
| 1 | +/* |
| 2 | + * GALLERIZER.JS |
| 3 | + * |
| 4 | + * Description: Image gallery autoloader for mpv. |
| 5 | + * Version: 1.1.0 |
| 6 | + * Author: SteveJobzniak |
| 7 | + * URL: https://github.com/SteveJobzniak/mpv-tools |
| 8 | + * License: Apache License, Version 2.0 |
| 9 | + */ |
| 10 | + |
| 11 | +// Read the bottom of this file for configuration and script setup instructions. |
| 12 | + |
| 13 | +/* jshint -W097 */ |
| 14 | +/* global mp, require, setTimeout */ |
| 15 | + |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +var Options = require('Options'), |
| 19 | + PathIndex = require('PathIndex'), |
| 20 | + PathTools = require('PathTools'), |
| 21 | + Utils = require('MicroUtils'); |
| 22 | + |
| 23 | +(function() { |
| 24 | + var userConfig = new Options.advanced_options({ |
| 25 | + find_extensions: 'jpg,jpeg,png,bmp,gif' |
| 26 | + }); |
| 27 | + |
| 28 | + var debug = false; |
| 29 | + |
| 30 | + var wantedExts = userConfig.getValue('find_extensions').split(','); |
| 31 | + |
| 32 | + var autoQueue = function(filename) |
| 33 | + { |
| 34 | + var currentFile = PathTools.getBasename(filename), |
| 35 | + path = PathTools.getPathname(PathTools.makePathAbsolute(filename)); |
| 36 | + if (!path || !currentFile) |
| 37 | + return; |
| 38 | + |
| 39 | + var i, len, file, ext, |
| 40 | + foundAt = -1, |
| 41 | + files = [], |
| 42 | + index = new PathIndex(path, { |
| 43 | + skipDotfiles: true, |
| 44 | + includeDirs: false |
| 45 | + }); |
| 46 | + for (i = 0, len = index.files.length; i < len; ++i) { |
| 47 | + file = index.files[i]; |
| 48 | + ext = PathTools.getExtension(file); |
| 49 | + if (wantedExts.indexOf(ext) < 0) |
| 50 | + continue; |
| 51 | + if (file === currentFile) |
| 52 | + foundAt = files.length; |
| 53 | + files.push(PathTools.getSubPath(path, file)); |
| 54 | + } |
| 55 | + if (foundAt === -1 || files.length <= 1) |
| 56 | + return; // We didn't find target file, or ONLY found that file. |
| 57 | + |
| 58 | + // Immediately ensure playback is paused, since mpv's playback is async. |
| 59 | + // NOTE: This works even before the player has fully initialized. |
| 60 | + mp.set_property_bool('pause', true); |
| 61 | + |
| 62 | + // Append all files, including the one we've already started with. This |
| 63 | + // means that it will exist as a duplicate at both offset 0 and X. We |
| 64 | + // cannot trigger any playlist-pos or playlist-move commands to take |
| 65 | + // care of that now, since mpv may not have fully initialized yet, and |
| 66 | + // would just insist on always starting at pos 0. |
| 67 | + for (i = 0, len = files.length; i < len; ++i) |
| 68 | + mp.commandv('loadfile', files[i], 'append'); |
| 69 | + |
| 70 | + // Swap position to the "duplicate" at the real offset, which is the |
| 71 | + // same file and therefore a flicker-free switch. Then delete original. |
| 72 | + mp.set_property('playlist-pos', foundAt + 1); |
| 73 | + mp.commandv('playlist-remove', 0); |
| 74 | + }; |
| 75 | + |
| 76 | + var resolvingDir = false; |
| 77 | + mp.observe_property('playlist/0/playing', 'bool', function(name, isPlaying) { |
| 78 | + var filename, info, ext; |
| 79 | + if (isPlaying && mp.get_property_number('playlist-count') === 1) { |
| 80 | + // There's only a single entry and it started playing. Analyze it. |
| 81 | + filename = mp.get_property('playlist/0/filename'); |
| 82 | + if (!filename || PathTools.isWebURL(filename)) { |
| 83 | + resolvingDir = false; |
| 84 | + return; |
| 85 | + } |
| 86 | + info = PathTools.getPathInfo(filename); |
| 87 | + switch (info) { |
| 88 | + case 'dir': |
| 89 | + // We know the next playlist-modification will be loaded |
| 90 | + // folder contents. And since there was only a single |
| 91 | + // playlist entry, we know whole list will be replaced. |
| 92 | + resolvingDir = true; |
| 93 | + if (debug) |
| 94 | + Utils.dump('resolving dir:'+filename); |
| 95 | + break; |
| 96 | + case 'file': |
| 97 | + resolvingDir = false; |
| 98 | + // The playlist contains a single, local file. Determine |
| 99 | + // if it's from a filetype that we should be autoloading. |
| 100 | + ext = PathTools.getExtension(filename); |
| 101 | + if (wantedExts.indexOf(ext) >= 0) { |
| 102 | + if (debug) |
| 103 | + Utils.dump('autoload:'+filename); |
| 104 | + autoQueue(filename); |
| 105 | + } |
| 106 | + break; |
| 107 | + default: // "missing". |
| 108 | + resolvingDir = false; |
| 109 | + } |
| 110 | + } |
| 111 | + }); |
| 112 | + mp.observe_property('playlist/0/filename', 'string', function(name, filename) { |
| 113 | + if (!resolvingDir) |
| 114 | + return; |
| 115 | + |
| 116 | + // When we load a dir, the whole playlist changes. If the 1st queued |
| 117 | + // file from the dir is a wanted type, we should now pause the playback. |
| 118 | + var ext = PathTools.getExtension(filename); |
| 119 | + if (wantedExts.indexOf(ext) >= 0) { |
| 120 | + if (debug) |
| 121 | + Utils.dump('dir contained autoload filetype, pausing:'+filename); |
| 122 | + mp.set_property_bool('pause', true); |
| 123 | + } |
| 124 | + |
| 125 | + resolvingDir = false; |
| 126 | + }); |
| 127 | +})(); |
0 commit comments