Using mdls command to generate metadata report
That's it,
I have a shell script that goes through all the media (audio, video) files in the user supplied folder and generates a nice text based metadata report. I am on Mac OS X and I am using Spotlight command mdls
to get all the relevant metadata. The problem is that sometimes I see "zero" results in my report for all media. If I run the script again, it seems to work. I am confused why this is happening. This is the 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 looks 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 media:
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 run sometimes and output null sometimes?
source to share