Skip to content

Instantly share code, notes, and snippets.

@jneidel
Last active March 3, 2023 17:27
Show Gist options
  • Save jneidel/ac90d13812c863ec72851cdbb6f823ac to your computer and use it in GitHub Desktop.
Save jneidel/ac90d13812c863ec72851cdbb6f823ac to your computer and use it in GitHub Desktop.
Timestamping with mpv

Timestamping with mpv

Background

I did some usability testing and wanted to measure performance. We did a recording where the participants were solving the tasks.

And I wanted a quick and easy way to note down the beginning and end of a task. I needed these value pairs (like 300.52, 313.4, as the seconds since the start of the video) to do some computation with them.

Solution

An mpv script, that runs a shell script for validation/formatting and is triggered by a keybinding in mpv.

Data format

a = [
  123.45, 145.57,
  222, 245.9888,
  ...

This can of course be adjusted by modifying the shell script.

-- FILE: ~/.config/mpv/scripts/timestamp.lua
-- credit to: https://github.com/nimatrueway/mpv-bookmark-lua-script
local utils = require('mp.utils')
local msg = require('mp.msg')
function saveTable(t, path)
-- I know you can just pass the data directly, artifact from a previous implementation
local file = io.open(path .. ".tmp", "wb")
file:write(t)
io.close(file)
os.remove(path)
os.rename(path .. ".tmp", path)
msg.debug("[persistence]", "data successfully saved.")
return true
end
function start()
local data = mp.get_property_number("time-pos")
local result = saveTable(data, "/tmp/mpv")
if result ~= true then
mp.osd_message("Error saving: " .. result)
end
mp.osd_message("Wrote timestamp")
os.execute( "~/Downloads/ba/add-data start" )
end
function start_force()
local data = mp.get_property_number("time-pos")
local result = saveTable(data, "/tmp/mpv")
if result ~= true then
mp.osd_message("Error saving: " .. result)
end
mp.osd_message("Wrote timestamp")
os.execute( "~/Downloads/ba/add-data start -f" )
end
function stop()
local data = mp.get_property_number("time-pos")
local result = saveTable(data, "/tmp/mpv")
if result ~= true then
mp.osd_message("Error saving: " .. result)
end
mp.osd_message("Wrote timestamp")
os.execute( "~/Downloads/ba/add-data end")
end
mp.register_script_message("timestamp_start", start)
mp.register_script_message("timestamp_start_force", start_force)
mp.register_script_message("timestamp_end", stop)
# FILE: ~/.config/mpv/input.conf
...
Ctrl+u script_message timestamp_start
Ctrl+U script_message timestamp_start_force
Ctrl+x script_message timestamp_end
# FILE: ~/Downloads/ba/add-data
#! /bin/sh
DATA_FILE=~/Downloads/ba/participant.data
START_END=$1
TIME=$(cat /tmp/mpv)
LAST=$(tail -n1 $DATA_FILE | rev | cut -c2)
if [ "$START_END" = "start" ] && [ "$LAST" = "]" ]; then # valid start
printf " [ $TIME, " >>$DATA_FILE
notify-send $START_END $TIME -t 3500
elif [ "$START_END" = "end" ] && [ "$LAST" = "," ]; then # valid end
printf "$TIME ],\n" >>$DATA_FILE
notify-send $START_END $TIME -t 3500
elif [ "$START_END" = "start" ] && [ "$(tail -n1 $DATA_FILE | rev | cut -c1)" = "[" ]; then # inital position
printf " [ $TIME, " >>$DATA_FILE
notify-send $START_END $TIME -t 3500
elif [ "$START_END" = "start" ] && [ "$2" = "-f" ]; then # overwrite
sed -i '$d' $DATA_FILE
printf " [ $TIME, " >>$DATA_FILE
notify-send "$START_END -f" $TIME -t 3500
elif [ "$START_END" = "end" ] && [ "$2" = "-f" ]; then # overwrite
printf "$TIME ],\n" >>$DATA_FILE
notify-send "$START_END -f" $TIME -t 3500
else
notify-send "wrong cmd" -t 3500
fi
# FILE: ~/Downloads/ba/participant.data
#! /bin/octave
# this is the supported intital position
a = [
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment