Search code examples
macosbashmetadataspotlight

using mdls command to generate a metadata report


All,
I have shell script which goes through all the media (audio,video) files in a user-supplied folder and creates a nice text based metadata report. I'm on a Mac OS X and I'm using Spotlight's mdls command to get all the relevant metadata. The issue is that sometimes I see "null" results in my report for all the media files. If I run the script again it seems to work. I'm confused why this is happening. This is part of the script that outputs the metadata 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 \*.mp3 -o -iname \*.m4r -o -iname \*.m4a \)  -print0 )

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

The expected output is like this:

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

2) [./test2.mov]
- Duration: 25.03
- Dimensions: 480 X 640 pixels
- Codec: ( "H.264" )

But sometimes the output is null for all the media files:

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

2) [./test2.mov]
- Duration: null
- Dimensions: null X null pixels
- Codec: ( null )

Am I missing a trick here? Why does the script work sometimes and sometimes outputs null?


Solution

  • The issue was Spotlight not indexing the files. I used the mdimport command to force Spotlight to index the folder and that has resolved the issue