Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
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 Apr 7 '12 at 18:58
add comment

1 Answer

up vote 0 down vote accepted

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.