forked from uxtely/ops-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmedia-optimizer.sh
More file actions
executable file
·45 lines (38 loc) · 1.42 KB
/
media-optimizer.sh
File metadata and controls
executable file
·45 lines (38 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/sh
set -o noglob
IFS=$'\n'
# https://blog.uidrafter.com/convert-to-avif-programmatically
# https://blog.uidrafter.com/conditional-avif-for-video-posters
# Requires:
# brew install oxipng webp libavif ffmpeg
# We only use PNGs, no JPGs. This way we can ensure
# there's no color shifting and PNGs look sharper anyway.
nJPG=$(find $1 -type f -name *.jpg | awk 'END{print NR}')
if [ $nJPG != 0 ]; then
echo "ERROR: Found a JPG. Convert it to PNG" >&2
exit 1
fi
# If there's no foo.png.avif, foo.png outputs:
# - foo.png (better compressed lossless, and without EXIF metadata)
# - foo.png.avif
# - foo.png.webp
for img in $(find $1 -type f -name *.png); do
if [ ! -f "$img.avif" ]; then
chmod 644 $img
oxipng --opt max --strip safe $img
cwebp $img -o "$img.webp"
avifenc --speed 0 --min 25 --max 35 $img "$img.avif"
fi
done
# For faster video playback, the metadata section (moov) of an mp4
# should appear before the video and audio section (mdat).
# https://trac.ffmpeg.org/wiki/HowToCheckIfFaststartIsEnabledForPlayback
# https://www.ramugedia.com/mp4-container
for video in $(find $1 -type f -name *.mp4); do
mp4_section_appearing_first=`ffmpeg -v trace -i $video NUL 2>&1 | grep --max-count 1 --only-matching -e "type:'mdat'" -e "type:'moov'"`
if [ $mp4_section_appearing_first != "type:'moov'" ]; then
ffmpeg -i $video -movflags +faststart $video.tmp.mp4
rm $video
mv $video.tmp.mp4 $video
fi
done