Skip to content

Commit

Permalink
articles/cmd/mov2gif: add
Browse files Browse the repository at this point in the history
  • Loading branch information
croaky authored Jan 24, 2025
1 parent 8731f06 commit d3bb8d9
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 3 deletions.
34 changes: 31 additions & 3 deletions articles/cmd/adblock.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# cmd / adblock

To improve speed, privacy, and safety on my laptop,
[the `adblock` script](https://github.com/croaky/laptop/blob/main/bin/adblock)
blocks ads, trackers, and malicious websites at the DNS host level:
I block ads, trackers, and malicious websites at the DNS host level:

```bash
adblock
Expand All @@ -12,7 +11,7 @@ Unlike browser extension ad blockers,
it works on all apps on my device (not only web browsers).

Unlike DNS sinkholes,
it only works on my laptop (not phones, tablets on the network)
it only works on my laptop (not phones or tablets on the network)
but it does not require an additional always-on device such as a Raspberry Pi
and it works reliably when using the laptop away from home.

Expand All @@ -22,3 +21,32 @@ To disable and re-enable it:
adblock undo
adblock
```

Script:

```bash
#!/bin/bash
#
# https://github.com/croaky/laptop/blob/main/bin/adblock

set -euo pipefail

if [[ "$1" == "undo" ]]; then
echo -e '127.0.0.1\tlocalhost\n# MacOS default\n255.255.255.255\tbroadcasthost' | sudo tee /etc/hosts > /dev/null
else
# Creative Commons Attribution-NonCommercial-ShareAlike
curl -s https://winhelp2002.mvps.org/hosts.txt > /tmp/adblock

# Re-write Windows to Unix line endings
tr -d '\r' < /tmp/adblock > /tmp/etchosts

# Restore macOS system defaults
echo -e '# MacOS default\n255.255.255.255\tbroadcasthost' >> /tmp/etchosts

# Apply to /etc/hosts
sudo mv /tmp/etchosts /etc/hosts
fi

# Flush DNS cache
sudo killall -HUP mDNSResponder
```
2 changes: 2 additions & 0 deletions articles/cmd/kill-pid-on-port.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ Script:
#
# https://github.com/croaky/laptop/blob/main/bin/kill-pid-on-port

set -euo pipefail

lsof -n -i :"$1" | grep LISTEN | awk '{ print $2 }' | xargs kill
```
98 changes: 98 additions & 0 deletions articles/cmd/mov2gif.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# mov2gif

I record screencasts with QuickTime.app, which produces a `.mov` file.
When I want a smaller file to share in product documentation or this blog:

```bash
mov2gif input.mov
```

`mov2gif` takes a `.mov` file as input and outputs a `.output.gif`.

Script:

```bash
#!/bin/bash
#
# Convert a .mov file to a .gif file
#
# Usage: mov2gif [-f fps] [-s scale] input.mov
#
# Options:
# -f fps Frames per second (default: 15)
# -s scale Scale width in pixels (default: 1400)

set -euo pipefail

# Default parameters
fps=15 # Frames per second
scale=1400 # Scale width in pixels

# Function to display usage information
usage() {
echo "Usage: $0 [-f fps] [-s scale] input.mov"
echo " -f fps Frames per second (default: 15)"
echo " -s scale Scale width in pixels (default: 1400)"
exit 1
}

# Parse options
while getopts ":f:s:" opt; do
case $opt in
f)
fps="$OPTARG"
;;
s)
scale="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage
;;
:)
echo "Option -$OPTARG requires an argument." >&2
usage
;;
esac
done

# Shift parsed options away
shift $((OPTIND - 1))

# Check for input file
if [ $# -ne 1 ]; then
echo "Error: Missing input file."
usage
fi

input_file="$1"

# Verify input file extension
if [[ "${input_file##*.}" != "mov" ]]; then
echo "Error: Input file must have a .mov extension."
exit 1
fi

# Check if input file exists
if [ ! -f "$input_file" ]; then
echo "Error: File '$input_file' not found."
exit 1
fi

# Check and install ffmpeg if not present
if ! command -v ffmpeg &>/dev/null; then
echo "ffmpeg not found. Installing via Homebrew..."
brew install ffmpeg
fi

# Generate a palette for better colors in the GIF
echo "Regenerating palette..."
rm palette.png
ffmpeg -y -i "$input_file" -vf "fps=${fps},scale=${scale}:-1:force_original_aspect_ratio=decrease,palettegen" -frames:v 1 palette.png

# Create the final GIF using the palette
echo "Creating GIF..."
ffmpeg -i "$input_file" -i palette.png -filter_complex "fps=${fps},scale=${scale}:-1:force_original_aspect_ratio=decrease[x];[x][1:v]paletteuse=dither=none" output.gif

echo "GIF created: output.gif"
```
3 changes: 3 additions & 0 deletions theme/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ <h3>cmd/</h3>
<li>
<a href="/cmd/mdembed">mdembed</a>
</li>
<li>
<a href="/cmd/mov2gif">mov2gif</a>
</li>
<li>
<a href="/cmd/replace">replace</a>
</li>
Expand Down

0 comments on commit d3bb8d9

Please sign in to comment.