Detect the TV Show, Season and Episode from a mp4 file in applescript

.mp4applescript

Assuming I have an MP4/M4V file of a TV show. The file has the necessary metadata so it can be imported into iTunes and you'll see the all the relevant information. Using AppleScript, one can extract that information using iTunes.

How would one obtain the show name, season and episode of that file using AppleScript without having to import it into iTunes first?

In essence, I have an incoming folder where I can "drop" my movies and tv shows and I'd like to use the script to organize the files without human intervention so that files will follow a naming convention so they are easier to find in the future.

Best Answer

Exiftool is a great tool to extract metadata. http://www.sno.phy.queensu.ca/~phil/exiftool/ This script should point you in the right direction.

set thePath to POSIX path of (path to documents folder) & "exifdata"
do shell script "mkdir -p " & quoted form of POSIX path of thePath

tell application "Finder" to set theSelection to selection
repeat with anItem in theSelection
    set theLocation to POSIX path of (anItem as text)
    set {name:fileName, name extension:nameExtension} to anItem
    set baseName to text 1 thru ((get offset of "." & nameExtension in fileName) - 1) of fileName
    set destLocation to quoted form of (thePath & "/" & baseName & ".txt")
    do shell script "exiftool -a " & quoted form of theLocation & " >" & destLocation
    do shell script "open " & destLocation
end repeat

Or another approach is :

set myFile to quoted form of (POSIX path of (path to desktop as text) & "movie.mp4")

set xxx to every paragraph of (do shell script "mdls " & myFile)