OS X mdls command gives "null" in output

2

I have bash script that utilizes the Mac based mdls command in a shell script to generate a metadata report for media files. When the script works the output looks like this:

1) [./test1.mov]  
- Duration: 22.03  
- Dimensions: 480 X 640 pixels   
- Codec: ( "H.264" )  

Sometimes I get null in the results for all files in the report:

1) [./test1.mov]   
- Duration: (null)   
- Dimensions: (null) X (null) pixels  
- Codec: ( null )     

Here is my script that generates the report:

cd "path_to_folder"
while IFS= read -r -d $'\0' file; do
  duration=`mdls -name kMDItemDurationSeconds "$file" | cut -d "=" -f 2 `
  duration=`printf "%.2f" $duration;`
  pixel_height=`mdls -name kMDItemPixelHeight "$file" | cut -d "=" -f 2`
  pixel_width=`mdls -name kMDItemPixelWidth "$file" | cut -d "=" -f 2`
  codec=`mdls -name kMDItemCodecs "$file" | cut -d "=" -f 2`
  temp="$i) [$file]\n- Duration: $duration\n- Dimensions: $pixel_width X $pixel_height    pixels\n- Codec: $codec\n"
  metaDataOutput=$metaDataOutput"\n"$temp
  i=$((i + 1))
done < <(find .  \( -iname \*.m4v -o -iname \*.mov -o -iname \*.m4r -o -iname \*.m4a \)  -print0 )

 echo -e  "\n[Report]\n"$metaDataOutput 

Any idea what I might be doing wrong here? Why the nulls?

smokinguns

Posted 2012-04-06T19:15:59.337

Reputation: 1 188

Interesting edge case: If you make a hard link of a file, then trash one of the filenames, OSX (10.12 for me in this moment) will throw out the spotlight info for the remaining file and leave nulls. – joshfindit – 2019-10-01T19:40:07.957

So you say that sometimes, it just works for all files and then, it doesn't? Or is this only happening to specific files? – slhck – 2012-04-07T18:58:39.670

Answers

2

Perhaps the file is excluded from spotlight's index? Or perhaps it's on a hard drive or network share that doesn't support spotlight indexing?

If none of those are the case, it sounds like your spotlight database is broken. There are some extremely complicated optimisations in spotlight to keep it's performance impact to a minimum, and sometimes it breaks.

You can use mdutil to find out the status of a drive's spotlight database (use /Volumes/other_disk if you don't want to apply these to the boot disk):

sudo mdutil -s /

You can delete/flush the database on a particular disk by running:

sudo mdutil -E /

Spotlight will then rebuild the index when it feels like it (probably when your system is idle). You can bump it to re-index right away with:

sudo mdutil -i on /

Depending how many files you have on your system, it could take many hours to rebuild the index. You can follow progress in the GUI's spotlight search icon.

See man mdutil for more info.

Abhi Beckert

Posted 2012-04-06T19:15:59.337

Reputation: 550

mdimport may be a helpful command here as well if the index is not broken. In the UI one can accomplish the same thing by browsing to and then opening the file (or presumably open <filename> in terminal) – joshfindit – 2019-10-01T19:41:26.710