Created
April 20, 2026 18:56
-
-
Save hq6/3d1f291de5b808da7d545ed44d6fc82b to your computer and use it in GitHub Desktop.
Wrapper script to play a file with vlc and resume from last position intended for use with https://github.com/hq6/sq
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # SUMMARY: Resume playback at given time or time last saved | |
| # HELP: | |
| # USAGE | |
| # sq resume <media_file> [timestamp] | |
| if [[ $# -lt 1 ]]; then | |
| exec sq help resume | |
| fi | |
| MEDIA_FILE="$1" | |
| ROOT_DIR="$HOME/.local/share/sq-resume" | |
| mkdir -p "$ROOT_DIR" | |
| pos_file_name="$ROOT_DIR/$(echo "$MEDIA_FILE" | md5sum | awk '{print $1}').timestamp" | |
| if [[ $# -gt 1 ]]; then | |
| LAST_TIME="$2" | |
| else | |
| # Look for LAST_TIME from a file. | |
| if [[ ! -f "$pos_file_name" ]]; then | |
| LAST_TIME=0 | |
| else | |
| >&2 echo "Resuming from $pos_file_name" | |
| LAST_TIME="$(tail -n 1 "$pos_file_name")" | |
| fi | |
| fi | |
| # Playback with store | |
| vlc_output_on_quit() { | |
| local MEDIA_FILE="$1" | |
| local start_time="$2" | |
| local last_time="$start_time" | |
| local current_time | |
| re='^[0-9]+$' | |
| while read current_line; do | |
| current_time=$(awk '{if ($1 == ">" && $NF > 1) printf "%d", $2}' <<<"$current_line") | |
| if [[ $current_time =~ $re ]]; then | |
| last_time=$current_time | |
| fi | |
| done < <( | |
| while true; do | |
| sleep 0.5 | |
| echo "get_time" | |
| done |\ | |
| vlc \ | |
| --fullscreen \ | |
| --start-time="$start_time" \ | |
| --extraintf rc \ | |
| "$MEDIA_FILE" | |
| ) | |
| echo $last_time | |
| } | |
| seconds="$(echo "$LAST_TIME" | awk -F: '{ print $NF + ( NF > 1 ? $(NF - 1) * 60 : 0) + (NF > 2 ? ($(NF - 2) * 3600) : 0) }')" | |
| vlc_output_on_quit "$MEDIA_FILE" "$seconds" >> "$pos_file_name" | |
| tail -n1 "$pos_file_name" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment