Skip to content

Instantly share code, notes, and snippets.

@modille
Last active July 11, 2017 20:54
Show Gist options
  • Save modille/7b463256e424ca7a5982e833a1250096 to your computer and use it in GitHub Desktop.
Save modille/7b463256e424ca7a5982e833a1250096 to your computer and use it in GitHub Desktop.
HOW TO: Record macOS screencast as animated GIF

Capture screencast

You can capture the screencast video using QuickTime Player.

Capture screen or selection

  1. Open Quicktime Player
  2. Go to File > New Screen Recording
  3. Click the record button and follow the instructions
  4. Go to File > Export, choose the highest quality and save as screencast.mov

Capture single window

Download the clickdrag.swift file below.

# Make the clickdrag.swift file executable
chmod +x ~/Downloads/clickdrag.swift

Download the StartWindowRecording.applescript file below and open it with the Script Editor utility.

Change the values of theProcessName and theWindowNumber at the top to the target application window (and window number) that you want to capture.

Choose Script > Run to run the script. This will launch QuickTime Player, start a new screen recording, and select the exact dimensions of the target window.

Finally:

  1. Click Start Recording
  2. To end the recording, click the stop button in the system menu bar
  3. Go to File > Export, choose the highest quality and save as screencast.mov

Convert to GIF

# Install dependencies
brew install ffmpeg 
brew cask install xquartz
open /usr/local/Caskroom/xquartz/2.7.9/XQuartz.pkg
brew install gifsicle

# Convert screencast.mov to screencast.gif
ffmpeg -i screencast.mov -vf scale=800:-1 -r 10 -f image2pipe -vcodec ppm - | convert -delay 5 -layers Optimize -loop 0 - screencast.gif

# View the GIF
open -a Safari screencap.gif

Sources

#!/usr/bin/env xcrun swift
import Foundation
let kDelayUSec : useconds_t = 500_000
func DragMouse(from p0: CGPoint, to p1: CGPoint) {
let mouseDown = CGEvent.init(mouseEventSource:nil, mouseType:.leftMouseDown, mouseCursorPosition:p0, mouseButton:.left)!
let mouseDrag = CGEvent.init(mouseEventSource:nil, mouseType:.leftMouseDragged, mouseCursorPosition:p1, mouseButton:.left)!
let mouseUp = CGEvent.init(mouseEventSource:nil, mouseType:.leftMouseUp, mouseCursorPosition:p1, mouseButton:.left)!
mouseDown.post(tap:.cghidEventTap)
usleep(kDelayUSec)
mouseDrag.post(tap:.cghidEventTap)
usleep(kDelayUSec)
mouseUp.post(tap:.cghidEventTap)
}
func main() {
let args = UserDefaults.standard
let x = CGFloat(args.integer(forKey:"x"))
let y = CGFloat(args.integer(forKey:"y"))
let dx = CGFloat(args.integer(forKey:"dx"))
let dy = CGFloat(args.integer(forKey:"dy"))
let p0 = CGPoint(x:x, y:y)
let p1 = CGPoint(x:x + dx, y:y + dy)
DragMouse(from: p0, to: p1)
}
main()
set theProcessName to "Safari"
set theWindowNumber to 1
tell application "System Events"
tell process theProcessName
activate
tell window theWindowNumber
set thePosition to position
set theSize to size
end tell
end tell
end tell
tell application "QuickTime Player"
activate
new screen recording
delay 1
tell application "System Events" to key code 49
delay 1
do shell script "~/Downloads/clickdrag.swift -x " & (item 1 of thePosition) & " -y " & (item 2 of thePosition) & " -dx " & (item 1 of theSize) & " -dy " & (item 2 of theSize)
end tell
@modille
Copy link
Author

modille commented Jul 11, 2017

To apply a filter to set saturation to 0 (black and white):

ffmpeg -i screencast.mov -vf "scale=800:-1,hue=s=0" -r 10 -f image2pipe -vcodec ppm - | convert -delay 5 -layers Optimize -loop 0 - screencast.gif

See https://ffmpeg.org/ffmpeg-filters.html#hue

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