Skip to content

Instantly share code, notes, and snippets.

@avih
Last active December 12, 2017 14:49
Show Gist options
  • Save avih/60ea4a20b98b9fb43eae018c1b34fab1 to your computer and use it in GitHub Desktop.
Save avih/60ea4a20b98b9fb43eae018c1b34fab1 to your computer and use it in GitHub Desktop.
/***********************************************************************
autoloop.js - mpv script which reads and play a custom playlist file in
a loop. Images are displayed for a specified duration and other clips are
played normally.
mpv should be invoked as: `mpv --script=path/to/autoloop.js --idle`
The info file should consist of lines where each line is:
<length> <clip-path>
E.g.
0 path/to/clip.mp4
5 path/to/some/image.jpg
0 /another/clip.mkv
Sets image-display-duration to length and plays the clip. Images would be
affected by the length value while other clips will not.
After the last line was processed, start again with the first line.
Note: For simplicity, there should be no empty lines, exactly one space
between length and clip-path, and no extra spaces before or after
(but spaces as part of clip-path are premitted).
***********************************************************************/
var data = mp.utils.read_file("~/autoloop.info");
data = data.split("\r\n").join("\n").split("\n");
var i = 0;
function start_item(length, clip) {
mp.set_property("image-display-duration", length);
mp.commandv("loadfile", clip, "replace");
}
function play_next_item() {
var space = data[i].indexOf(" "),
length = Number(data[i].substring(0, space)),
clip = data[i].substring(space + 1);
print("next_item: '" + length + "', '" + clip + "'");
start_item(length, clip);
i = (i + 1) % data.length;
}
mp.register_event("end-file", play_next_item);
play_next_item();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment