Skip to content

Instantly share code, notes, and snippets.

@kenakofer
Created July 28, 2024 00:05
Show Gist options
  • Save kenakofer/3b46ffdba8757f9f107fda4c109bb75b to your computer and use it in GitHub Desktop.
Save kenakofer/3b46ffdba8757f9f107fda4c109bb75b to your computer and use it in GitHub Desktop.
Checks VLC's current playlist against the images and videos in a folder (recursively), and without restarting VLC, adds missing items to the playlist.
#!/bin/bash
### Checks VLC's current playlist against the images and videos in a folder
### (recursively), and without restarting VLC, updates the playlist to match.
### Useful for kiosk slideshows, or smart picture frame setups.
### First run VLC with telnet and a password that matches this script. (Telnet
### is only needed within localhost here, it would be insecure to try this over
### the network or on a shared computer):
### > vlc --extraintf telnet --telnet-password your_pass_here
### Recommended VLC settings for kiosk (May require restarting VLC to take effect):
### Preferences -> Subtitles/OSD -> Uncheck "Show media title on video start"
### Restart VLC to take effect.
### Works well with e.g. gphotos-sync if your photos/videos are in google photos: https://gilesknap.github.io/gphotos-sync/main/index.html
VLC_HOST="localhost"
VLC_PORT="4212"
VLC_PASSWORD="your_pass_here"
MEDIA_DIR="$1"
# Check if directory is provided
if [ -z "$MEDIA_DIR" ]; then
echo "Please provide a directory path as an argument."
exit 1
fi
# Function to send commands to VLC and capture output
vlc_command() {
local command="$1"
(
echo "$VLC_PASSWORD"
echo "$command"
sleep .1 # For some reason the sleep is necessary if you want to see the output of the command.
echo "quit"
echo ""
) | nc $VLC_HOST $VLC_PORT 2>&1 | tee vlc.out
}
# Function to escape special characters in filenames
escape_filename() {
echo "$1" | sed 's/[\&\;\(\)]/\\&/g; s/\s/\\ /g'
}
# Get the current playlist
playlist=$(vlc_command "playlist")
playlist=$(cat vlc.out | grep -e '^|')
# echo "Playlist:"
# echo "$playlist"
# exit 1
# Find all media files in the directory
media_files=$(find "$MEDIA_DIR" -type f \( \
-iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" -o -iname "*.gif" -o \
-iname "*.bmp" -o -iname "*.webp" -o -iname "*.tiff" -o \
-iname "*.mp4" -o -iname "*.avi" -o -iname "*.mkv" -o -iname "*.mov" -o \
-iname "*.wmv" -o -iname "*.flv" -o -iname "*.webm" \
\))
# Prepare commands for adding new items
added_count=0
skipped_count=0
# Process each media file
while IFS= read -r item
do
basename_item=$(basename "$item")
#escaped_item=$(escape_filename "$item")
# Check if the item is already in the playlist
echo "Checking for $basename_item"
if ! echo "$playlist" | grep -q "$basename_item"; then
echo "Adding: $item"
vlc_command "add $item" > /dev/null
((added_count++))
else
((skipped_count++))
echo "Skipping (already in playlist): $item"
fi
done <<< "$media_files"
echo "Summary:"
echo "- Added $added_count new items to the playlist."
echo "- Skipped $skipped_count items that were already in the playlist."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment