Thursday, July 27, 2017

#1 2017-07-17 11:51:42 am

DPaquin
Member
Registered: 2012-01-23
Posts: 69

File metadata information

I would like to know if the following is possible.

Is there a way in AppleScript to get file metadata information.

I am going to merge many video (i.e MP4, m44...) files together. Each of the files are located under a specific folder and were recorded individually. I now need to merge them together in order to have one big video file for a given event.

The duration of the video is important as I need to create some kind of index for someone to be able to go directly to that video sequence without having to look at the whole thing.

When I do a File "Get info" command a window appears with all the information that I need.

Is there a way to gather using AppleScript :
1 - the Name without the extension "filename" and
2 - the Duration "00:32"

Thanks!

Daniel

Offline

 

#2 2017-07-17 02:08:31 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3127

Re: File metadata information

Here is a quick and dirty answer :

Applescript:

(*
[url]http://www.macscripter.net/viewtopic.php?id=45840[/url]
*)




--set hfsPath to "Seagate 3TB:Jazz youtube:Jimmy Giuffre:" as alias
set hfsPath to choose folder

set ourFilesAndNames to my listFolder:hfsPath

set localDecimalSeparator to item 2 of (0.1 as text) # Useful to convert the string duration to a number
set ourDescriptors to {}
repeat with aCouple in ourFilesAndNames
   set {hfsPath, itsBareName} to contents of aCouple
   set theDuration to (do shell script "mdls " & quoted form of POSIX path of hfsPath & " -name kMDItemDurationSeconds")
   --> "kMDItemDurationSeconds = 500.7366666666667"
   set theDuration to my remplace(theDuration, "kMDItemDurationSeconds = ", "")
   --> "500.7366666666667"
   # or
   --> "(null)"
   try
       set theDuration to my remplace(theDuration, ".", localDecimalSeparator) as number
       # Convert it in minutes:seconds
       set theSeconds to theDuration mod 60
       set theMinutes to theDuration div 60
       if theMinutes > 59 then
           set theHours to theMinutes div 60
           set theMinutes to theMinutes mod 60
           set theDuration to text 2 thru 3 of ((100 + theHours) as text) & ":" & text 2 thru 3 of ((100 + theMinutes) as text) & ":" & text 2 thru 3 of ((100 + theSeconds) as text)
       else
           set theDuration to text 2 thru 3 of ((100 + theMinutes) as text) & ":" & text 2 thru 3 of ((100 + theSeconds) as text)
       end if
       
   on error
       set theDuration to "(null)"
   end try
   set thePath to hfsPath as text
   -- set thePath to POSIX path of hfsPath # Enable it if you want to get POSIX paths
   
   set end of ourDescriptors to {thePath, itsBareName, theDuration}
end repeat

ourDescriptors

#=====

on listFolder:anAlias
   tell application "Finder"
       set allFiles to every file in anAlias as alias list
       
       set ourFiles to {}
       repeat with aFile in allFiles
           set itsExtension to name extension of aFile
           
           if itsExtension is in {"webm", "mp4", "mov"} then # Edit to fit your needs
               set itsName to get name of aFile
               set end of ourFiles to {aFile as text, text 1 thru -(2 + (count itsExtension)) of itsName}
           end if
       end repeat
   end tell
   return ourFiles
end listFolder:

#=====
(*
replaces every occurences of d1 by d2 in the text t
*)

on remplace(t, d1, d2)
   local oTIDs, l
   set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d1}
   set l to text items of t
   set AppleScript's text item delimiters to d2
   set t to l as text
   set AppleScript's text item delimiters to oTIDs
   return t
end remplace

#=====

Edited to take care of a possible null value returned as duration.
Edited to return duration as min:sec ( thank you Shane wink )

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) lundi 17 juillet 2017 21:07:55

Last edited by Yvan Koenig (2017-07-19 08:17:39 am)

Offline

 

#3 2017-07-17 07:39:47 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5055

Re: File metadata information

Here's an alternative that lets Spotlight do more of the work. Requires 10.10 or later:

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

-- choose folder and make URL
set theFolder to choose folder
set theURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of theFolder)

-- start Spotlight search
set thePred to current application's NSPredicate's predicateWithFormat_("%K CONTAINS %@", current application's NSMetadataItemContentTypeTreeKey, "public.movie")
set theQuery to current application's NSMetadataQuery's new()
theQuery's setPredicate:thePred
theQuery's setSearchScopes:{theURL}
theQuery's startQuery()
repeat while theQuery's isGathering() as boolean
   delay 0.01
end repeat
theQuery's stopQuery()

-- set up results array, formatter for duration
set theResults to current application's NSMutableArray's array()
set theFormatter to current application's NSDateComponentsFormatter's new()

-- loop through results
set theCount to theQuery's resultCount()
repeat with i from 1 to theCount
   set aResult to (theQuery's resultAtIndex:(i - 1))
   set theName to (aResult's valueForAttribute:(current application's NSMetadataItemDisplayNameKey))
   set theDuration to (aResult's valueForAttribute:(current application's NSMetadataItemDurationSecondsKey))
   try
       set theDuration to (theFormatter's stringFromTimeInterval:theDuration)
   on error
       set theDuration to "unknown"
   end try
   -- format values tab-delimited
   (theResults's addObject:(current application's NSString's stringWithFormat_("%@    %@", theName's stringByDeletingPathExtension(), theDuration)))
end repeat
return (theResults's componentsJoinedByString:linefeed) as text

Edited to deal with files lacking the duration metadata.

Last edited by Shane Stanley (2017-07-18 06:22:32 am)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#4 2017-07-18 06:29:19 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2675
Website

Re: File metadata information

If you have AppleScript Toolbox installed:

Applescript:

set thePaths to AST list folder ((choose folder) as string) matching regex "\\.(mp4|mov|webm)$" with returning HFS paths

set report to "filename        duration"
repeat with aPath in thePaths
   set metadata to ((AST metadata for file aPath) & {kMDItemDurationSeconds:0})
   set duration to kMDItemDurationSeconds of metadata as integer
   set basename to kMDItemFSName of metadata
   -- remove extension
   set filename to (AST find regex "(.*)\\.(.*)$" in string basename regex group 2) as string
   -- convert time
   set sec to text -2 thru -1 of ("0" & duration mod 60)
   set theTime to text -2 thru -1 of ("0" & duration div 60) & ":" & sec
   
   set report to report & return & filename & tab & theTime
end repeat

Last edited by DJ Bazzie Wazzie (2017-07-19 06:57:05 am)

Offline

 

#5 2017-07-18 07:25:42 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3127

Re: File metadata information

In case you need to get the path attached to the returned datas (as is done by my original script) you may edit the second loop of Shane's script this way :

Applescript:

repeat with i from 1 to theCount
   set aResult to (theQuery's resultAtIndex:(i - 1))
   set thePath to (aResult's valueForAttribute:(current application's NSMetadataItemPathKey)) # ADDED
   set theName to (aResult's valueForAttribute:(current application's NSMetadataItemDisplayNameKey))
   set theDuration to (aResult's valueForAttribute:(current application's NSMetadataItemDurationSecondsKey))
   try
       set theDuration to (theFormatter's stringFromTimeInterval:theDuration)
   on error
       set theDuration to "unknown"
   end try
   -- format values tab-delimited
   -- set thePath to (POSIX file (thePath as text)) as text # If you want to convert it into Hfs path
   (theResults's addObject:(current application's NSString's stringWithFormat_("%@    %@    %@", thePath, theName's stringByDeletingPathExtension(), theDuration))) # EDITED
end repeat

CAUTION. I added an instruction which may be enabled if you want to get Hfs path of the files. It may be useful if a component of the Hfs path contained a slash which is automatically replaced by a colon in POSIX format.


Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) mardi 18 juillet 2017 14:23:50

Last edited by Yvan Koenig (2017-07-18 11:37:50 am)

Offline

 

#6 2017-07-18 07:54:48 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5055

Re: File metadata information

Yvan Koenig wrote:

My script return Hfs paths, the modified Shane's one return POSIX paths



One other thing I should point out: my version is recursive, searching all enclosed folders as well as the chosen folder.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#7 2017-07-18 08:06:04 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2675
Website

Re: File metadata information

Yvan Koenig wrote:

CAUTION. My script return Hfs paths, the modified Shane's one return POSIX paths



Does it matter? The TS asked only for filenames with no extension smile

Offline

 

#8 2017-07-18 08:21:36 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3127

Re: File metadata information

As the list of names and durations is not linked to a list of paths, it would be difficult to retrieve which file has a given couple of datas.
If a folder contain a file named trucmuche.mov and a file named trucmuche.mp4 how will you identify which one own the couple {trucmuche, 2:34} and which one own the couple  {trucmuche, 1:23} ?
It's why I took care to return the three values for every file.

More, with Shane's code - which is recursive - we may have different files with the same name in several subfolders.

But maybe it doesn't make sense for the asker.
I just choose to play safe.


Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) mardi 18 juillet 2017 15:21:31

Last edited by Yvan Koenig (2017-07-18 08:24:27 am)

Offline

 

#9 2017-07-18 07:06:33 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5055

Re: File metadata information

DJ Bazzie Wazzie wrote:

The TS asked only for filenames with no extension smile



They also asked for duration in the form of "00:32" wink


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#10 2017-07-19 06:58:24 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2675
Website

Re: File metadata information

Shane Stanley wrote:
DJ Bazzie Wazzie wrote:

The TS asked only for filenames with no extension smile



They also asked for duration in the form of "00:32" wink



And it returns that from now on

Offline

 

#11 2017-07-19 02:11:23 pm

DPaquin
Member
Registered: 2012-01-23
Posts: 69

Re: File metadata information

Thanks everybody!

I do have a lot to digest WOW!

Daniel

Offline

 

#12 2017-07-22 01:02:51 pm

DPaquin
Member
Registered: 2012-01-23
Posts: 69

Re: File metadata information

OK, I've taken the first code provided by Yvan and ran it on a local folder and the result was OK. However, running the same code with the data located on a NAS did not work. The duration is set to null.

LOCAL DRIVE :
{{"/Users/DanielPaquin/Desktop/27/M2U02701.mp4", "M2U02701", "31:13"}, {"/Users/DanielPaquin/Desktop/27/M2U02702.mp4", "M2U02702", "15:24"}, {"/Users/DanielPaquin/Desktop/27/M2U02703.mp4", "M2U02703", "01:08:14"} }


EXTERNAL DRIVE :
{{"/Volumes/Data/ZVinformatique/CompétitionCourante/27/M2U02701.mp4", "M2U02701", "(null)"}, {"/Volumes/Data/ZVinformatique/CompétitionCourante/27/M2U02702.mp4", "M2U02702", "(null)"}, {"/Volumes/Data/ZVinformatique/CompétitionCourante/27/M2U02703.mp4", "M2U02703", "(null)"}}



More than likely, the video files are going to be on an external drive. Bizarre, when looking at the directory path everything is correct. Why would that be ?

THANKS AGAIN!



[applescript]
--set hfsPath to "Seagate 3TB:Jazz youtube:Jimmy Giuffre:" as alias
set hfsPath to choose folder

set ourFilesAndNames to my listFolder:hfsPath

set localDecimalSeparator to item 2 of (0.1 as text) # Useful to convert the string duration to a number
set ourDescriptors to {}
repeat with aCouple in ourFilesAndNames
    set {hfsPath, itsBareName} to contents of aCouple
    set theDuration to (do shell script "mdls " & quoted form of POSIX path of hfsPath & " -name kMDItemDurationSeconds")
    display dialog "hfsPath: " & hfsPath
    --display dialog "theDuration: " & theDuration
    --> "kMDItemDurationSeconds = 500.7366666666667"
    set theDuration to my remplace(theDuration, "kMDItemDurationSeconds = ", "")
    --> "500.7366666666667"
    # or
    --> "(null)"
    try
        set theDuration to my remplace(theDuration, ".", localDecimalSeparator) as number
        # Convert it in minutes:seconds
        set theSeconds to theDuration mod 60
        set theMinutes to theDuration div 60
        if theMinutes > 59 then
            set theHours to theMinutes div 60
            set theMinutes to theMinutes mod 60
            set theDuration to text 2 thru 3 of ((100 + theHours) as text) & ":" & text 2 thru 3 of ((100 + theMinutes) as text) & ":" & text 2 thru 3 of ((100 + theSeconds) as text)
        else
            set theDuration to text 2 thru 3 of ((100 + theMinutes) as text) & ":" & text 2 thru 3 of ((100 + theSeconds) as text)
        end if
       
    on error
        set theDuration to "(null)"
    end try
    set thePath to hfsPath as text
    set thePath to POSIX path of hfsPath # Enable it if you want to get POSIX paths
   
    set end of ourDescriptors to {thePath, itsBareName, theDuration}
end repeat

ourDescriptors

#=====

on listFolder:anAlias
    tell application "Finder"
        set allFiles to every file in anAlias as alias list
       
        set ourFiles to {}
        repeat with aFile in allFiles
            set itsExtension to name extension of aFile
           
            if itsExtension is in {"webm", "mp4", "mov"} then # Edit to fit your needs
                set itsName to get name of aFile
                set end of ourFiles to {aFile as text, text 1 thru -(2 + (count itsExtension)) of itsName}
            end if
        end repeat
    end tell
    return ourFiles
end listFolder:

#=====
(*
replaces every occurences of d1 by d2 in the text t
*)
on remplace(t, d1, d2)
    local oTIDs, l
    set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d1}
    set l to text items of t
    set AppleScript's text item delimiters to d2
    set t to l as text
    set AppleScript's text item delimiters to oTIDs
    return t
end remplace

#=====[applescript]

Offline

 

#13 2017-07-22 03:08:25 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3127

Re: File metadata information

I tested with files stored on an external HD but I'm not using a NAS.
I don't know if it may explain the difference.

Here is what I got :
{{"/Volumes/Seagate 3TB/Jazz youtube/Ahmad Jamal/Ahmad Jamal + Ben Webster, 1960, studio 61.mp4", "Ahmad Jamal + Ben Webster, 1960, studio 61", "26:02"}, {"/Volumes/Seagate 3TB/Jazz youtube/Ahmad Jamal/Ahmad Jamal + Yusef Lateef - Marciac (2011-08-08).mp4", "Ahmad Jamal + Yusef Lateef - Marciac (2011-08-08)", "11:06"}, {"/Volumes/Seagate 3TB/Jazz youtube/Ahmad Jamal/Ahmad Jamal - One - Vienne 2011.mp4", "Ahmad Jamal - One - Vienne 2011", "08:18"}, {"/Volumes/Seagate 3TB/Jazz youtube/Ahmad Jamal/Ahmad Jamal - Poinciana - Olympia 2012.mp4", "Ahmad Jamal - Poinciana - Olympia 2012", "10:53"}, {"/Volumes/Seagate 3TB/Jazz youtube/Ahmad Jamal/Ahmad Jamal Trio & George Coleman - My foolish heart.mp4", "Ahmad Jamal Trio & George Coleman - My foolish heart", "10:16"}, {"/Volumes/Seagate 3TB/Jazz youtube/Ahmad Jamal/Ahmad Jamal Trio - Germany 1999.mp4", "Ahmad Jamal Trio - Germany 1999", "43:52"}, {"/Volumes/Seagate 3TB/Jazz youtube/Ahmad Jamal/Ahmad Jamal Trio-1959-Darn That Dream.webm", "Ahmad Jamal Trio-1959-Darn That Dream", "(null)"}}

I activated the use of POSIX paths because it clearly show that the files aren't on the boot volume.

Just a question : the script rely upon Spotlight to get the metadatas. Are you sure that this feature search in the NAS ?

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) samedi 22 juillet 2017 22:05:23

Last edited by Yvan Koenig (2017-07-22 03:11:53 pm)

Offline

 

#14 2017-07-22 04:28:49 pm

DPaquin
Member
Registered: 2012-01-23
Posts: 69

Re: File metadata information

The script rely upon Spotlight to get the metadatas. Are you sure that this feature search in the NAS ?


It did not.

I've re-index the whole thing and it does now appear when doing a search with spotlight.

However, when I do a get info on the file, the duration does not show.

Then the script works as expected. I am not sure what to do to get the duration for file on the NAS.

Moving files from to NAS onto local the local drive is not option.

My NAS is a Synology DS214+.

Thanks!

Daniel

Browser: Safari 602.1
Operating System: Mac OS X (10.13 Developer Beta 3)

Last edited by DPaquin (2017-07-22 09:24:38 pm)

Offline

 

#15 2017-07-22 10:01:53 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5055

Re: File metadata information

DPaquin wrote:

I am not sure what to do to get the duration for file on the NAS.



All the alternatives here rely on Spotlight, which won't index your NAS disks. In fact, I think you will find that Get Info also fails to get the duration of files stored on your NAS disks, for the same reason.

You can, however, get it using AVFoundation. This script requires 10.11 or later:

Applescript:

use AppleScript version "2.5" -- 10.11 or later
use framework "Foundation"
use framework "AVFoundation"
use scripting additions

-- get files
set theFolder to choose folder
set theURL to current application's |NSURL|'s fileURLWithPath:(POSIX path of theFolder)
set fileManager to current application's NSFileManager's defaultManager()
set {theFiles, theError} to fileManager's contentsOfDirectoryAtURL:theURL includingPropertiesForKeys:{} options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) |error|:(reference)
-- eliminate non-video files
set thePred to current application's NSPredicate's predicateWithFormat:"pathExtension IN %@" argumentArray:{{"mp4", "mov", "webm"}}
set vidFiles to theFiles's filteredArrayUsingPredicate:thePred

-- set up results array, formatter for duration
set theResults to current application's NSMutableArray's array()
set theFormatter to current application's NSDateComponentsFormatter's new()

-- lop through
repeat with aFile in vidFiles
   -- get localized name
   set {theResult, theName, theError} to (aFile's getResourceValue:(reference) forKey:(current application's NSURLLocalizedNameKey) |error|:(reference))
   -- create an AVAsset so you can get duration
   set theAsset to (current application's AVURLAsset's assetWithURL:aFile)
   set durationInfo to theAsset's duration() -- returns record like: {value:2911360, timescale:1000, flags:1, epoch:0}
   if durationInfo is missing value then
       set theTime to "unknown"
   else
       -- calculate duration in seconds and format as string
       set theDuration to (value of durationInfo) / (timescale of durationInfo)
       set theDuration to (theFormatter's stringFromTimeInterval:theDuration)
   end if
   (theResults's addObject:(current application's NSString's stringWithFormat_("%@    %@", theName's stringByDeletingPathExtension(), theDuration)))
end repeat
return (theResults's componentsJoinedByString:linefeed) as text


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#16 2017-07-22 10:34:25 pm

DPaquin
Member
Registered: 2012-01-23
Posts: 69

Re: File metadata information

Exactly, "Get Info" fails to get the duration of files stored on my NAS disks.

I ran the script created by Shane and it now work on my NAS.

I'll keep working on the script because this is only the beginning.

Many thanks everybody, this was impossible for me to write something like this. I do have a real lack of knowledge when comes to write such AppleScript programs.

You guys are great!

MANY THANKS!

Daniel

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)