Skip to content

Instantly share code, notes, and snippets.

@avih
Last active December 12, 2017 14:49
Show Gist options
  • Select an option

  • Save avih/60ea4a20b98b9fb43eae018c1b34fab1 to your computer and use it in GitHub Desktop.

Select an option

Save avih/60ea4a20b98b9fb43eae018c1b34fab1 to your computer and use it in GitHub Desktop.

Revisions

  1. avih revised this gist Dec 12, 2017. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion autoloop.js
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,12 @@ 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]
    <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.
  2. avih revised this gist Dec 12, 2017. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions autoloop.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,9 @@
    /***********************************************************************
    autoloop.js - read and play a custom playlist file format where images are
    displayed for a specified duration and other clips are played normally.
    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]
  3. avih created this gist Dec 12, 2017.
    37 changes: 37 additions & 0 deletions autoloop.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    /***********************************************************************
    autoloop.js - read and play a custom playlist file format where images are
    displayed for a specified duration and other clips are played normally.
    The info file should consist of lines where each line is:
    [length] [clip-path]
    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();