Skip to content

Instantly share code, notes, and snippets.

@simonw
Last active April 17, 2024 03:14
Show Gist options
  • Save simonw/42e5a0e4d80785d7595db75faa11534c to your computer and use it in GitHub Desktop.
Save simonw/42e5a0e4d80785d7595db75faa11534c to your computer and use it in GitHub Desktop.

2024-04-17T02:36:56 conversation: 01hvmymvjfj22ra5bxkw8nsehb

Model: claude-3-opus-20240229

Prompt:

/Users/simon/.local/bin/capture.sh
---
#!/bin/zsh

# Function to convert time in seconds to HH:MM:SS format
function seconds_to_hms {
    local total_seconds=$1
    local hours=$((total_seconds / 3600))
    local minutes=$(((total_seconds % 3600) / 60))
    local seconds=$((total_seconds % 60))
    printf "%02d:%02d:%02d\n" $hours $minutes $seconds
}

# Function to capture frame
function capture_frame {
    local current_time=$1
    local formatted_time=$(seconds_to_hms $current_time)
    local filename="/tmp/frame_${formatted_time//:/}.jpg"
    
    # Run ffmpeg to capture the frame
    ffmpeg -ss $formatted_time -i video.mp4 -frames:v 1 $filename
    
    echo "Saved frame to $filename"
}

# Main loop
while true; do
    echo "Press any key to capture the current video frame..."
    read -sk 1 key

    # Use osascript to get the current time from QuickTime Player
    current_time=$(osascript -e 'tell application "QuickTime Player" to tell document 1 to get current time')

    # Capture the frame at the current time
    capture_frame $current_time
done

System:

modify this script so that the video.mp4 filename is a requried positional argument

Response:

Here's the modified script that takes the video.mp4 filename as a required positional argument:

/Users/simon/.local/bin/capture.sh
---
#!/bin/zsh

# Check if the video filename is provided as an argument
if [ $# -eq 0 ]; then
    echo "Please provide the video filename as an argument."
    exit 1
fi

video_filename=$1

# Function to convert time in seconds to HH:MM:SS format
function seconds_to_hms {
    local total_seconds=$1
    local hours=$((total_seconds / 3600))
    local minutes=$(((total_seconds % 3600) / 60))
    local seconds=$((total_seconds % 60))
    printf "%02d:%02d:%02d\n" $hours $minutes $seconds
}

# Function to capture frame
function capture_frame {
    local current_time=$1
    local formatted_time=$(seconds_to_hms $current_time)
    local filename="/tmp/frame_${formatted_time//:/}.jpg"
    
    # Run ffmpeg to capture the frame
    ffmpeg -ss $formatted_time -i "$video_filename" -frames:v 1 $filename
    
    echo "Saved frame to $filename"
}

# Main loop
while true; do
    echo "Press any key to capture the current video frame..."
    read -sk 1 key

    # Use osascript to get the current time from QuickTime Player
    current_time=$(osascript -e 'tell application "QuickTime Player" to tell document 1 to get current time')

    # Capture the frame at the current time
    capture_frame $current_time
done

In this modified version:

  1. We check if the video filename is provided as a command-line argument using $#. If no argument is provided, an error message is displayed, and the script exits with a status code of 1.
  2. The video filename is assigned to the variable video_filename using $1, which represents the first command-line argument.
  3. In the capture_frame function, the ffmpeg command now uses "$video_filename" instead of the hardcoded video.mp4 filename. Double quotes are used to handle filenames with spaces.

Now, you can run the script by providing the video filename as an argument, like this:

./capture.sh path/to/your/video.mp4

Make sure to replace path/to/your/video.mp4 with the actual path and filename of your video file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment