Fooling around with bash scripts 2

This is a follow-up to my previous entries: Fooling around with bash scripts, which I used as a base for Migration from Google Photos to Nextcloud and Piwigo

Since some of my old picture albums have old .mov, .mpg and .avi videos in them I decided to also convert them to .mp4 (piwigo can show them in the albums), so once again I tried came up with something to automate the task


  • ffmpeg


mkdir -p converted_videos

for file in ./*.MOV ./*.mov ./*.MPG ./*.AVI;  do
    echo "converting file $file ..."
    ffmpeg -i "$file" -vcodec h264 -acodec mp2 "./converted_videos/$file".mp4

 there are mp4 files in the directory move them all to the same directory
mv *.mp4 converted_videos/

echo "Finished video conversion..."

I also further tweaked my resize and compress script to do its task for sub directories in the current directory and also call the video converting script:

for D in *; do
    if [ -d "$D" ]; then
        cd "$D"
        echo "Current directory: ${D}"

        mkdir -p compressed_images
        mkdir -p resized_images

        echo "Resizing and compressing pictures"
        for file in ./*.jpg ./*.jpeg ./*.JPG; do
                echo "resizing and optimizing $file ..."
                convert "$file" \
                        -resize '1920x1080>' \
                jpegoptim "./resized_images/$file" \
                        -d './compressed_images' \
                        --max 65 \
                        --all-progressive \
        echo "done!"

        echo "Renaming pictures according to creation date..."
        for i in ./compressed_images/*; do
                jhead -n%Y%m%d-%H%M%S $i
        echo "done!"

        echo "Deleting temporal resized_images directory"
        rm -rf resized_images
        echo "done!"

        echo "converting videos..."
        echo "done!"

        echo "Deleting temporal converted_videos directory"
        rm -rf converted_videos
        echo "done!"
        cd ../

I am using this script to before uploading for all my photo albums and I thought it would be nice to share it

Day 53 of my 2020’s

Join 100DaysToOffload!

10 responses to “Fooling around with bash scripts 2”

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Proud member of the 512kb club, blue team