Resize Image
# Resize to max width/height 640
sips -Z 640 *.jpg
Convert Image Format
# Convert png to jpg
sips -s format jpeg *.png --out mydirectory
sips
is a CLI tool included in Mac, but the commands is not pretty. If you can install other tools, magick mogrify does resizing, converting formats, and more.
magick mogrify -resize 800x800 *.jpg
magick mogrify -format jpg *.png
List Process Running on a Port
# eg. Port 8080
lsof -i :8080
Kill Process running on a Port
lsof -P | grep ':8080' | awk '{print $2}' | xargs kill -9
Creating Markdown Journal
Uses journal:
journal new -d /path/to/journal "My entry for today"
Extract MP3 from Youtube
Uses yt-dlp:
yt-dlp -f mp3 https://www.youtube.com/watch?v=eu-5mvCNKbQ
# Other handy yt-dlp
# Download in mp4 video format
yt-dlp -f mp4 https://www.youtube.com/watch?v=eu-5mvCNKbQ
UPDATE: As youtube-dl is no longer updated since 2021, the replacement is yt-dlp, which works exactly the same, and even faster! All youtube-dl
were replaced with yt-dlp
in this post.
Download Youtube Live Stream
For live streams, youtube-dl
does not work well. We need another tool to the rescue - livestreamer streamlink
streamlink --hls-live-restart -o Live.ts https://www.youtube.com/watch?v=zkrq7Kpd1so best
Rip DVD
Uses HandBrake-CLI:
./HandBrakeCLI -i /Volumes/THE_DVD/VIDEO_TS/VIDEO_TS.VOB -o ~/Movies/rip.mp4 –preset="Normal" -c 1-3
The c
option is for specific chapter. Omit for all.
If you need to rip protected DVD, then brew install libdvdcss
first.
Download a file with curl
curl -o myfile.mp3 https://the.domain.com/file
# To resume, if download was interrupted
curl -o myfile.mp3 -C - https://the.domain.com/file
Rename multiple files in a directory
# Add a prefix "XXX_" to every file
for f in * ; do mv "$f" "XXX_$f" ; done
Search/grep
# Find recursively in the directory for the string 'needle'
grep -R 'needle' path/to/dir/
Find files
# Find mp4 files between 100-200 MB
find . -type f -name *.mp4 -size +100M -size -200M
You could execute other commands such as removing it
find ... -exec rm -f {} \;
Sleep and Timeout
# Start running a command (eg streamlink) after 60 seconds
sleep 60 && streamlink ...
# Run long running command (eg streamlink) and terminate after 60 seconds
gtimeout 60 streamlink ...
On macOS, you need to brew install coreutils
to use gtimeout
.
Rsync
I usually backup my external drive data to ANOTHER external drive, just keeping as a fallback if the drive fail. To sync them, I use rsync
. Mac ships with a rather outdated version, so install with brew install rsync
first.
rsync -axHAWXS --numeric-ids --info=progress2 /drive/primary/ /drive/backup/
The above will sync from everything from “primary” folder to “backup” folder.
For local copy, I would also use rsync
(rather than cp
) like this:
rsync -axvhWX --no-compress /src /des
Resize a video
I shoot too much video with my GoPro, yet I want to keep all of them without spending $ buying hard drives. To compromise, I downsize some of the raw footage. Install ffmpeg with brew install ffmpeg
first.
ffmpeg -i input.mp4 -s 960x540 -preset slow -crf 28 -c:a copy output-reduced.mp4
Extract mp3 from a video
ffmpeg -i input.mov -q:a 0 -map a output.mp3
Inject 360 meta into photo/video
exiftool -ProjectionType="equirectangular" my-360-photo.jpeg
For video, there is spatial-media, a python tool by Google.
python spatialmedia -i input.mov output.mov
Make sure image no alpha channel
mogrify -alpha off */*.png
Especially useful for app store screenshot uploading, which cannot accept alpha/transparency. You can also test if an image has alpha with sips -g all *.png
.
Change ‘Date Created’
SetFile -d '12/31/2020 23:59:59' *.jpg
Change ‘Date Modified’ only
touch -mt 202012312359 *.jpg
Download private Facebook video
Non-public videos require your Facebook account has access to it. First you have to retrieve your Facebook account’s cookie. To do so, open your browser web inspector > Network > Refresh the page > Copy Request’s Cookie. Eg. Cookie: dpr123...
(truncated)
Use yt-dlp
and pass in the cookie header.
yt-dlp https://www.facebook.com/12345/videos/67890 --add-header 'Cookie: dpr123...'