script to folderize bulk media files

 I download lots of bulk media files from my phone as I am snap happy.

movs, m4as, pngs

These take up too much space.

Enter this script

~/scripts/iphone_media_clean_convert_folderize.sh

#!/bin/bash
find . -type f ! -name "*.sh" | while read -r f; do
    dir=$(dirname "$f")
    base=$(basename "$f")
    clean=$(echo "$base" | tr ' ' '_' | tr -d "'\"()")
    if [ "$base" != "$clean" ]; then
        mv "$f" "$dir/$clean"
        f="$dir/$clean"
        base="$clean"
    fi
    filedate=$(date -r "$f" "+%Y-%m-%d")
    mkdir -p "./$filedate"
    ext="${f##*.}"
    out="./$filedate/${base%.*}"
    case "${ext,,}" in
        mov|avi|mkv|wmv|flv|webm|3gp|3g2|mts|m2ts|mpg|mpeg|m4v|vob|ogv|ts)
            echo "$f"
            ffmpeg -y -i "$f" -vcodec libx264 -acodec aac "$out.mp4" && rm "$f"
        ;;
        m4a|aac|wav|aiff|aif|caf|opus|ogg|oga|flac|wma|amr|au|ra|mka|ac3)
            echo "$f"
            ffmpeg -y -i "$f" -codec:a libmp3lame -q:a 2 "$out.mp3" && rm "$f"
        ;;
        png|heic|heif|tiff|tif|webp|bmp|gif|jp2|j2k|ppm|pgm|pbm|pnm|svg)
            echo "$f"
            convert "$f" "$out.jpg" && rm "$f"
        ;;
    esac
done

Popular posts from this blog

Automatically Fix Song Metadata and Filenames on Linux with Beets

DropboxMount turns your Dropbox into a live, on-demand drive instead of a sync folder.

script to create an EFI partition on a target bootdrive if none exists