Skip to content

Instantly share code, notes, and snippets.

@davidmz
Last active September 12, 2019 13:06
Show Gist options
  • Save davidmz/f2dbeb4e29ae30b695c341ef98113bfd to your computer and use it in GitHub Desktop.
Save davidmz/f2dbeb4e29ae30b695c341ef98113bfd to your computer and use it in GitHub Desktop.
Loop control script for MPV video player #video #mpv
var hotKey = "k";
var numRe = /\d+(?:\.\d+)?/g;
var interval = null;
mp.register_event("file-loaded", function() {
// Trying to load loops file
mp.remove_key_binding("loopContinue");
clearInterval(interval);
var videoPath = mp.get_property("path");
var loopsPath = videoPath.replace(/[^.]+$/, "loops.txt");
if (!mp.utils.file_info(loopsPath)) {
// file is not exists?
return;
}
var allLoops = [];
var loopPtr = 0;
var activeLoop = function() { return allLoops[loopPtr]; };
var loopsText = mp.utils.read_file(loopsPath);
var lines = loopsText.split(/[\n\r]+/);
for (var i = 0; i < lines.length; i++) {
var m,
pair = [];
numRe.lastIndex = 0;
while ((m = numRe.exec(lines[i])) !== null && pair.length < 2) {
pair.push(parseFloat(m[0]));
}
if (pair.length === 2) {
allLoops.push(pair);
}
}
if (allLoops.length === 0) {
return;
}
interval = setInterval(function() {
if (!activeLoop()) {
return;
}
var pos = mp.get_property("time-pos");
if (pos < activeLoop()[0] || pos > activeLoop()[1]) {
return;
}
mp.osd_message("\u23f8");
}, 250);
mp.set_property_number("ab-loop-a", activeLoop()[0]);
mp.set_property_number("ab-loop-b", activeLoop()[1]);
mp.add_key_binding(hotKey, "loopContinue", function() {
if (!activeLoop()) {
return;
}
var pos = mp.get_property("time-pos");
if (pos < activeLoop()[0]) {
return;
}
loopPtr++;
mp.osd_message("");
if (!activeLoop()) {
mp.set_property("ab-loop-b", "no");
return;
}
mp.set_property_number("ab-loop-a", activeLoop()[0]);
mp.set_property_number("ab-loop-b", activeLoop()[1]);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment