🔍 Search
RSS feed

AI-assisted shell script - Create slideshow from random videos on hard drive


Frame of video captured by the script

http://John1126.com/share/20250608-slideshow-using-shell-script-with-help-of-AI-on-Linux.mp4

As I was finishing this coding project, I was impressed with how fast an idea can come into reality, especially with the help fo AI. I am reminded that everything is made with words. This script is written with words, in the shell scripting language. AI helped me, which is also itself made with words, which men programmed into machines. And now with AI being connected to robots and other machines, words are being put into motion in the physical realm to do and make things. We can have a thought and if we could put it into words correctly, something will happen.... or something will be created (or destroyed).

It reminds me of the truth from all of creation around us. Everything is made with words.

In the beginning was the Word, and the Word was with God, and the Word was God. The same was in the beginning with God. All things were made by him; and without him was not any thing made that was made. In him was life; and the life was the light of men. And the light shineth in darkness; and the darkness comprehended it not. John 1:1-5 (KJV)


If you want to try using this script on your computer, here is the code:

#!/bin/bash
# Show slideshow from videos in current directory
# Save random frame from random video in current working directory as PNG image

# How many screenshots?
playframes=30

# How long to show each screenshot, in seconds?
sleeptime=3

# What is the maximum character count you want in filename?
length=40

###
clear

for (( i=1; i<=playframes; i++ ))
do

    mapfile -t video_files_array < <(find . -type f -iname "*.mp4" -o -iname "*.mov" -o -iname "*.avi" -o -iname "*.mkv" -o -iname "*.mpeg" -o -iname "*.mpg" -o -iname "*.wmv" -o -iname "*.webm" -o -iname "*.flv" -o -iname "*.ogv" -o -iname "*.m4v" -o -iname "*.m2t")

    # Check if the array is not empty
    if (( ${#video_files_array[@]} == 0 )); then
      echo "No video files found in the current directory."
    else
      # Get a random index
      random_index=$(( RANDOM % ${#video_files_array[@]} ))
      
      # Echo a random filename from the array
      video="${video_files_array[random_index]}"
    fi

    # Remove the './' prefix if it exists
    video="${video#./}"

    echo -e "Video = \e[33m$video\e[0m"
    # select first X characters of filename for constructing the image name
    truncated_video="${video:0:length}"
    # Remove the file extension
    truncated_video="${truncated_video%.*}"

    duration=$(ffprobe -v error -show_entries format=duration -of default=nokey=1:noprint_wrappers=1 "$video")
    # round off to a whole number
    duration=$(printf "%.0f" "$duration")

    echo "duration = "$duration

    # Generate a random whole number between 0 and duration (inclusive)
    random_time=$((RANDOM % (duration + 1)))
    echo "random time:  "$random_time

    # Construct filename for framegrab in the current directory
    framegrab="$(pwd)/${truncated_video}_${random_time}_seconds.png"
    echo -e "framegrab = \e[33m$framegrab\e[0m"

    # Check if the file specified by $framegrab was written
    if [ -e "$framegrab" ]; then
      echo -e "File already exists:  \033[32m$framegrab\033[0m"
      ffmpeg_flag="NO"
    else
      echo "Creating file"
      ffmpeg_flag="YES"
    fi

    # Only run ffmpeg if the image does not already exist
    if [ "$ffmpeg_flag" != "NO" ]; then
        # Create PNG image
        ffmpeg -hide_banner -ss "$random_time" -i "$video" -frames:v 1 -n "$framegrab"
    fi

    # Show the image in full-screen
    echo "Attempting to play: ${framegrab}"
    ffplay -loglevel quiet -fs -i "${framegrab}" &
    sleep $sleeptime
    # Kill the older ffplay process
    kill $FFPLAY_OLD
    # Extract the filename from the full path
    filename=$(basename "$framegrab")
    mv "$framegrab" "$filename"
    echo "image moved to $(pwd)"
    # Store the PID
    FFPLAY_OLD=$!
done
# Close the last image shown
kill $FFPLAY_OLD

Jun 8, 2025, 10:12:51 PM | by Admin


0 Comments

Comment on this Blog

formatting help