Question

I've been wrestling with a 14TB+ video library for the last months trying to dedupe and sort all of it into production ready folder structure.

For the life of me I have not been able to find (spent 3 solid days with no sleep) a script that will sort a folder of video files, using their dimensions, into Portrait and Landscape folders.

The closest I've found is this -

#!/bin/bash
shopt -s nullglob

for f in *.{mp4,MP4,mov,MOV,m4v,M4V}
    do 
        height=`mdls -raw -name  kMDItemPixelHeight "$f"`
        width=`mdls -raw -name  kMDItemPixelWidth "$f"`
        mkdir -p "${height}x${width}"
        mv "$f" "${height}x${width}"/
        
        printf "File: $f\n"     
        printf "> Dimensions: $height x $width \n\n"
    done

printf "All done! \n"

Which gets me closer by sorting them into folders based on dimensions, but it's not what I'm trying to achieve.

Alternatively I found this script for images using Imagemagick, (which I installed via Homebrew) it does read the files, though it moves them ALL into the portraits directory. I'm assuming the variables may be different to extract height and width from a video file?

#/bin/zsh
mkdir -p portraits
mkdir -p landscapes
for f in ./*.mp4
do
  r=$(identify -format '%[fx:(h/w)]' "$f")
  if [[ r < 1.0 ]] 
  then
      echo "Portrait detected."
  mv "$f" ./portraits/
  elif  [[ r > 1.0 ]]
  then
  echo "Landscape detected."
      mv "$f" ./landscapes/
  fi
done

If ANYONE OUT THERE can help me with this, it's worth a PayPal to me. I know it's quick work for the right mind.

The same, if someone has another solution using folder actions or automator or ANYTHING else. I'm running Mac OS with terminal.

Much appreciated!

jason@speedheathens.com

Was it helpful?

Solution

You can use the test function provided by fish to compare the two dimensions:

#!/usr/local/bin/fish
for i in *.{mp4,MP4,mov,MOV,m4v,M4V}
  if test (mdls -raw -name kMDItemPixelHeight $i) -gt (mdls -raw -name kMDItemPixelWidth $i)
     mv $i portrait/
  else
     mv $i landscape/
  end
end

bash probably provides the same functionality, but I'm more familiar with fish.

Licensed under: CC-BY-SA with attribution
Not affiliated with apple.stackexchange
scroll top