this post was submitted on 15 Jul 2023
8 points (100.0% liked)

Bash

722 readers
1 users here now

Talk about the Bash Shell and Bash scripting

founded 4 years ago
MODERATORS
 

This will convert all webp in a directory to jpg or mp4 if animated. It will utilize all cores with gnu parallel. It will also handle an error with ffmpeg that happens if animated webp pixel width or height is an odd number by padding 1 pixel if necessary

cat convertwebp

#!/usr/bin/env bash
# Convert webp to jpg if static or gif/mp4 if animated
# Pads animated webp to avoid "libx264 not divisible by 2 error"
# Deps imagemagick libwebp parallel

# Usage assuming saved as convertwebp
# convertwebp             # Will convert all webp in current dir to jpeg or mp4 and gif if animated
# convertwebp /some/dir/  # Same but on specified dir
# convertwebp /some/file  # Same but on specified file

######### Set jpg quality. There will be 2 outputs one of each quality setting
#          0-100 percent
export     QUALITY_ONE=35
export     QUALITY_TWO=75

find_args=(-maxdepth 1 -type f -regextype posix-extended -iregex '.*\.(webp)$' -print0)

dir="$PWD"
if [[ -d "$@" ]]; then
    dir="$@"
    echo "Dir $@"
else
    if [[ -f "$@" ]]; then
        dir="$@"
        echo "File $@"
    fi
fi

mkdir -p "$MY_DWEBP_OUTDIR"

find "$dir" "${find_args[@]}" | parallel -0 -j+0 --eta --bar '
jpg_out_quality_one=$(echo {/.}_"$QUALITY_ONE"_percent.jpg)
jpg_out_quality_two=$(echo {/.}_"$QUALITY_TWO"_percent.jpg)
png_out=$(echo {/.}.ffmpeg.png)
gif_out=$(echo {/.}.gif)
mp4_out=$(echo {/.}.mp4)
isanimated="$(webpmux -info {} | grep animation)"
if [[ "$isanimated" == "Features present: animation transparency" ]]; then
    convert '{}' "$gif_out"
    # Begin mp4 conversion handler to pad geometry 1 pixel to x and y if either are odd to avoid "libx264 not divisible by 2 error"
    geometry_x=$(webpmux -info '{}' | head -n 1 | tr "[:space:]" "\n" | tail -3 | head -n 1)
    geometry_y=$(webpmux -info '{}' | head -n 1 | tr "[:space:]" "\n" | tail -3 | tail -1)
    if [ $(( $geometry_x  % 2)) -ne 0 ] | [ $(( $geometry_y  % 2)) -ne 0 ]; then
        if [ $(( $geometry_x  % 2)) -ne 0 ] && [ $(( $geometry_y  % 2)) -ne 0 ]; then
            splice_geometry="1x1"
            gravity_direction="northeast"
            convert -splice $splice_geometry -gravity $gravity_direction '{}' "$mp4_out"
        else 
            if [ $(( $geometry_x  % 2)) -ne 0 ]; then
                splice_geometry="1x0"
                gravity_direction="east"
                convert -splice $splice_geometry -gravity $gravity_direction '{}' "$mp4_out"
            else
                if [ $(( $geometry_y  % 2)) -ne 0 ]; then
                    splice_geometry="0x1"
                    gravity_direction="north"
                    convert -splice $splice_geometry -gravity $gravity_direction '{}' "$mp4_out"
                fi
            fi
        fi
    else
        convert '{}' "$mp4_out"
    fi
    # End mp4 conversion handler to pad geometry 1 pixel to x and y if either are odd to avoid "libx264 not divisible by 2 error"
    else
        dwebp '{}' -o - | convert - -quality $QUALITY_ONE% "$jpg_out_quality_one"    # pipe to convert for filesize reduction
        dwebp '{}' -o - | convert - -quality $QUALITY_TWO% "$jpg_out_quality_two"    # pipe to convert for filesize reduction
fi
'
unset QUALITY_ONE
unset QUALITY_TWO
no comments (yet)
sorted by: hot top controversial new old
there doesn't seem to be anything here