Skip to content
o770 edited this page Aug 18, 2024 · 8 revisions

MPVLOOP & MPVCLIP

MPVLOOP and MPVCLIP are bash scripts that execute MPV with playback options for loops, start and end times set according to a pattern in file names, a playlist or the command line.

Usage

mpvloop {[<mpv profile,...>] [<loop pattern>... | -l] [-s] [path/]filename | --lpat <timestamp range>... | --help | --version}
mpvclip {[<mpv profile,...>] [<clip pattern>... | -l] [-s] [path/]filename | --lpat <timestamp range>... | --help | --version}

Description

MPVLOOP runs MPV with any combination of the options: --start, --ab-loop-a, --ab-loop-b, --loop-file.

MPVCLIP runs MPV with any combination or none of the options: --start, --end.

MPV profile names are parsed and MPV is run according to: --profile.

MPV is only invoked to play a file with a loop pattern and is executed once for each loop pattern; otherwise the files are skipped. Input directories are expanded recursively. The line is read from left to right. If a key is pressed within one second before MPV runs again, the script may exit, continue, or skip the file after confirmation.

Status messages report clip patterns and MPV options used, clip duration, invalid inputs such as a file name without a pattern, and the number of successful iterations.

Except for time options and informational messages, both scripts share the same code and run independently of each other.

Clip/Loop Pattern

Recognized patterns are formed with an initial letter A followed by numbers next to an intermediate letter B followed by numbers.

Letters are matched regardless of the case. Numbers are positive integers or zero and represent time in deciseconds, that is, tenths of a second. Zero disables each time point.

Patterns are converted to seconds with one decimal place and translated into time settings for loop playback from points A to B in MPVLOOP or playback start and end points in MPVCLIP.

Pattern Timestamp Seconds
a1b2 from 00:00:00.1 to 00:00:00.2 from 0.1 seconds to 0.2 seconds
a10b20 from 00:00:01 to 00:00:02 from 1 second to 2 seconds
A1234B5678 from 00:02:03.4 to 00:09:27.8 from 123.4 seconds to 567.8 seconds
a0b123 from 00:00:00 to 00:00:12.3 from start of file to 12.3 seconds
a123b0 from 00:00:12.3 from 12.3 seconds to end of file
a0b0 from 00:00:00 from the beginning to the end of the file

Examples

For MPVLOOP command line:

$ mpvloop foo-a1b2-a10b20.mp4

How MPV is run:

$ mpv --start=0.1 --ab-loop-a=0.1 --ab-loop-b=0.2 foo-a1b2-a10b20.mp4 ; \
mpv --start=1 --ab-loop-a=1 --ab-loop-b=2 foo-a1b2-a10b20.mp4

For MPVCLIP command line:

$ mpvclip foo-a1b2-a10b20.mp4

How MPV is run:

$ mpv --start=0.1 --end=0.2 foo-a1b2-a10b20.mp4 ; \
mpv --start=1 --end=2 foo-a1b2-a10b20.mp4

Options

MPV profile names and any number of clip patterns without flags can be entered on the command line. Usage is printed if no arguments are given.

<mpv profile,...>

Name of the MPV profile to use.

Examples

For MPVLOOP command line:

$ mpvloop gpu-hq foo-a1b2.mp4

How MPV is run:

$ mpv --start=0.1 --ab-loop-a=0.1 --ab-loop-b=0.2 --profile=gpu-hq foo-a1b2.mp4

For MPVCLIP command line:

$ mpvclip gpu-hq foo-a1b2.mp4

How MPV is run:

$ mpv --start=0.1 --end=0.2 --profile=gpu-hq foo-a1b2.mp4

<loop pattern>...

Play files according to this optional pattern. Other patterns in filenames, if any, are ignored.

Examples

For MPVLOOP command line:

$ mpvloop a10b20 foo-a1b2.mp4

How MPV is run:

$ mpv --start=1 --ab-loop-a=1 --ab-loop-b=2 foo-a1b2.mp4

For MPVCLIP command line:

$ mpvclip a10b20 foo-a1b2.mp4

How MPV is run:

$ mpv --start=1 --end=2 foo-a1b2.mp4

-l file

Plaintext playlist with commas to separate a single file followed by any number of associated patterns on each line. Patterns in filenames, if any, are ignored.

A line which does not satisfy all of the conditions below is also ignored:

  • There is comma.
  • There is loop/clip pattern.
  • The first field is a regular file.

Example

For playlist file foo.txt which contains the following two lines:

path/to/foo1.mp4,a1b2,a123b0
foo2.mp4,a0b123

How MPVLOOP runs MPV:

$ mpv --start=0.1 --ab-loop-a=0.1 --ab-loop-b=0.2 path/to/foo1.mp4 ; \
mpv --start=12.3 --ab-loop-a=12.3 --ab-loop-b=no path/to/foo1.mp4 ; \
mpv --ab-loop-a=no --ab-loop-b=12.3 foo2.mp4

How MPVCLIP runs MPV:

$ mpv --start=0.1 --end=0.2 path/to/foo1.mp4 ; \
mpv --start=12.3 path/to/foo1.mp4 ; mpv --end=12.3 foo2.mp4

-s

Shuffle the file list. With -l, options must be combined in any order, for example: -ls.

--lpat <timestamp range>...

Print loop patterns equivalent to the timestamp ranges entered in any of the following three formats. Decisecond place is optional. A single missing timestamp translates to zero in the loop pattern:

  1. HH:MM:SS[.n]-HH:MM:SS[.n]
  2. HH:MM:SS[.n]-
  3. -HH:MM:SS[.n]

Example

$ mpvloop --lpat '00:02:03.4-00:09:27.8' '-00:00:12.3' '00:00:12.3-'
a1234b5678      (00:02:03.4-00:09:27.8)
a0b123          (-00:00:12.3)
a123b0          (00:00:12.3-)

--version

Display version information.

--help

Print help summary.

Dependencies

  • Awk
  • file (command)
  • GNU Bourne-Again SHell (Bash)
  • GNU Coreutils
  • GNU Grep
  • mpv

MPVCLIPF & MPVLOOPF

MPVCLIPF and MPVLOOPF are bash scripts that run MPVCLIP and MPVLOOP to play tagged clips and loops in a playlist or directory tree.

Usage

mpvclipf {[<mpv profile,...>] <tag> [-p <pattern> | -l] [-s] [path/]filename | --help | --version}
mpvloopf {[<mpv profile,...>] <tag> [-p <pattern> | -l] [-s] [path/]filename | --help | --version}

Description

Once for each tagged clip/loop pattern associated with a file and optionally a path pattern and MPV profile:

MPVCLIPF executes MPVCLIP; MPVLOOPF executes MPVLOOP; the file plays according to the tagged pattern.

Entered directories are searched recursively. The line is read from left to right. If a key is pressed within one second before MPVCLIP or MPVLOOP is executed again, the script may exit, continue, or skip the file after confirmation.

Status messages report the specified tag, invalid inputs, and the number of successful iterations. Both scripts run independently of each other.

Tagged Clip/Loop Pattern

A tag is defined if a match to a regular expression specified on the command line is found at the end of a clip/loop pattern in file names or a playlist.

Options

MPV profile names and a single tag without flags can be entered on the command line. Usage is printed if no arguments are given. The command line examples below only show MPVLOOPF running MPVLOOP with the same options as those with which MPVCLIPF runs MPVCLIP.

<tag>

Search input directories, file names, or a playlist for a match to tag at the end of a clip/loop pattern. Tag, as specified on the command line, is a regular expression. Found clips or loops are played sequentially.

Example

For MPVLOOPF command line:

$ mpvloopf Earth path/to

How MPVLOOP is run:

$ mpvloop a12b34 path/to/foo-a12b34Earth-a23b45Mars.mp4

-p <pattern>

Compares a shell pattern with the concatenation of a directory name and the base name of the file. The test applies to the full path.

Example

For MPVLOOPF command line:

$ mpvloopf Earth -p Saturn path/to

How MPVLOOP is run:

$ mpvloop a12b34 path/to/Saturn/foo-a12b34Earth-a23b45Mars.mp4 ; \
mpvloop a12b34 path/to/foo-a12b34Earth-a23b45Mars-a34b56Saturn.mp4

<mpv profile,...>

Name of the MPV profile to use.

Example

For MPVLOOPF command line:

$ mpvloopf gpu-hq Pluto path/to

How MPVLOOP is run:

$ mpvloop a1b2 --profile=gpu-hq path/to/foo-a1b2Pluto.mp4

-l file

Plaintext playlist with commas to separate a single file followed by any number of associated patterns on each line. A tagged clip or loop plays if it matches the specified tag. Patterns in file names, if any, are ignored.

A line which does not satisfy all of the conditions below is also ignored:

  • There is comma.
  • There is clip/loop pattern.
  • The first field is a regular file.

Example

For playlist file foo.txt which contains the following two lines:

path/to/foo1.mp4,a1b2Neptune,a123b0
foo2.mp4,a0b123Neptune

How MPVLOOPF runs MPVLOOP:

$ mpvloop a1b2 path/to/foo1.mp4 ; mpvloop a0b123 foo2.mp4

-s file

Shuffle the file list. With -l, options must be combined in any order, for example: -ls.

--version

Display version information.

--help

Print help summary.

Dependencies

  • file (command)
  • GNU Bourne-Again SHell (Bash)
  • GNU Coreutils
  • GNU Findutils (find)
  • GNU Grep
  • mpv

One or the other in a $PATH location:

  • mpvclip (MPVCLIPF)
  • mpvloop (MPVLOOPF)

MPVIVAL

MPVIVAL is a bash script that runs MPV with the start and length of playback set based on intervals of the total duration of a file.

Usage

mpvival {[<mpv profile,...>] <clip duration> <interval scale> [-l|-s|-t] [path/]filename | --help | --version}

Description

MPV is run with the options: --start, --length.

MPV profile names are parsed and MPV is run according to: --profile.

MPV is only invoked once for each interval of the total duration of a file. The playback start time is random and moves forward in the stream without repeating. Intervals are calculated using a scale. The clip duration is reset to the interval length if it is longer than the interval.

Input directories are expanded recursively. If a key is pressed within 1 second before MPV runs again, the script may exit, continue, or skip the file after confirmation.

Status messages report clip duration, number of intervals, successful iterations, and MPV options used.

Interval Scale

Intervals are scaled based on a range of 1 to 5, where 5 divides 1 hour into 60 one-minute intervals and 1 does not divide 1 minute.

12 seconds is the longest undivided duration. More than 1 hour, the duration is divided into intervals of 5, 4, 3, 2 or 1 minutes for the scale of 1, 2, 3, 4 or 5 respectively.

Number of intervals per scale and longest duration between 60 and 3600 seconds:

Duration/Scale 1 2 3 4 5
60 1 2 3 4 5
327 2 4 6 8 10
654 3 6 9 12 15
981 4 8 12 16 20
1309 5 10 15 20 25
1636 6 12 18 24 30
1963 7 14 21 28 35
2290 8 16 24 32 40
2618 9 18 27 36 45
2945 10 20 30 40 50
3272 11 22 33 44 55
3600 12 24 36 48 60

Example

For a 1 minute video, clip duration of 2 seconds, and interval scale 3:

$ mpvival 2 3 foo.mp4

How MPV is run a first and second time, showing a random start time:

$ mpv --start=7 --length=2 foo.mp4 ; \
mpv --start=20 --length=2 foo.mp4 ; \
mpv --start=48 --length=2 foo.mp4
$ mpv --start=18 --length=2 foo.mp4 ; \
mpv --start=37 --length=2 foo.mp4 ; \
mpv --start=41 --length=2 foo.mp4

Options

The interval scale number is followed by files unless a playlist flag is entered on the command line, which must follow the scale. Usage is printed if no arguments are given.

<mpv profile,...>

Name of the MPV profile to use.

Example

For a 1 minute video, clip duration of 1 second, and interval scale 2:

$ mpvival gpu-hq 1 2 foo.mp4

How MPV is run with a random start time:

$ mpv --start=25 --length=1 --profile=gpu-hq foo.mp4 ; \
mpv --start=54 --length=1 --profile=gpu-hq foo.mp4

<clip duration>

How long a file should play during each interval of the total duration in seconds.

<interval scale>

An integer between 1 and 5 where the higher the number, the more intervals and therefore the more times the file is played. How many times the file list is repeated with the -t option.

-l file

Plaintext playlist where each line is a file location.

Example

For the playlist file foo.txt that contains the following two lines, where foo1.mp4 is 1 minute and foo2.mp4 is 2 minutes:

/path/to/foo1.mp4
foo2.mp4

And command line:

$ mpvival 2 1 -l foo.txt

How MPV is run with a random start time:

$ mpv --start=21 --length=2 /path/to/foo1.mp4 ; \
mpv --start=17 --length=2 foo2.mp4 ; \
mpv --start=84 --length=2 foo2.mp4

-s file

Shuffle the file list. Options must be combined in any order with -t or -l, for example: -tls.

-t file

Each file is played once each time the file list is repeated. With -l, options must be combined in any order, for example: -tl.

--version

Display version information.

--help

Print help summary.

Dependencies

  • FFmpeg (ffprobe)
  • file (command)
  • GNU Bourne-Again SHell (Bash)
  • GNU Coreutils
  • GNU Grep
  • mpv

See Also

An option to parse and convert timestamps to clip patterns is available in the scripts, but MPV can help and print seconds instead.

When lowercase L is pressed to set up a loop (this tip is included in the scripts). Add this line to your input.conf:

l ab-loop ; print-text "My text: ${=time-pos}"

With another key assigned just to print the playback time in seconds, like for the lowercase A here:

a print-text "My text: ${=time-pos}"

On the status line if this option is entered on the command line:

--term-status-msg='${=playback-time}'

Once the time in seconds is known, simply move the radix point one place to the right in the clip and loop patterns to read with the bash scripts.