File metadata information

Thanks everybody!

I do have a lot to digest WOW!

Daniel

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.

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!


--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

#=====

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

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)

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:

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

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

Hey all I am a total novice at this . So sorry if I don’t speak the language you would understand

I only recently found how apple scripts can make my life so much easier

Shane thank you so much for this . It works great

Thank you for your time , It is greatly appreciated

I was wandering after the last line: return (theResults’s componentsJoinedByString:linefeed) as text

What would I need to add that the script opens Text edit app and copies the the result that appeared as text of the result box of script editor

Here is a link to the image to explain what information I would like to be able to save in a text document

https://imgur.com/jfX5Zez

If its possible can the text file be saved in the same location when the script prompted in the beginning where I selected the folder that contained the media

Hopefully I have explained what I am looking to do

Again thank you so much

Here is how to write to text file. But with avi and mkv movies the Shane’s script gives durations=0. I don’t know why. Well, to write durations to text file replace this:

return (theResults's componentsJoinedByString:linefeed) as text

with this:


set aText to (theResults's componentsJoinedByString:linefeed) as text

set filepath to (POSIX path of theFolder) & "myMoviesDurations.txt"

set openfile to open for access filepath with write permission
write aText as «class utf8» to openfile
close access openfile

Presumably for the same reason QuickTime can’t play them – they’re not formats supported by AVFoundation. A pity.

KniazidisR Thank you…You are a legend !!

It would of been great if AVFoundation supported more formats

Luckily I need it for .mov and .mp4 files

Again I can’t thank everyone enough that contributed to the forum

Maybe I am pushing my luck here a bit but would it be possible to get the duration with frames also
for eg. 01(hours)04(minutes)23(seconds)04(frames) > 01:04:23:04

also could I set the “myMoviesDurations.txt” to the name of the folder the media is sitting in (POSIX path of theFolder)

OK…:


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"
		set framesNumber to "unknown"
	else
		-- calculate duration in seconds and format as string
		set theSeconds to (value of durationInfo) / (timescale of durationInfo)
		set theDuration to (theFormatter's stringFromTimeInterval:theSeconds)
		-- Determine frames number here
		set theTracks to (get theAsset's tracks())
		repeat with i from 1 to count theTracks
			set aTrack to item i of theTracks
			if (aTrack's mediaType()) as string is "vide" then exit repeat
		end repeat
		set videoTrack to aTrack
		set frameRate to videoTrack's nominalFrameRate()
		set framesNumber to (theSeconds * frameRate) as integer
	end if
	
	(theResults's addObject:(current application's NSString's stringWithFormat_("%@	%@:%@", theName's stringByDeletingPathExtension(), theDuration, framesNumber)))
end repeat

set aText to (theResults's componentsJoinedByString:linefeed) as text

tell application "Finder" to set folderName to name of theFolder
set filepath to (POSIX path of theFolder) & folderName & ".txt"
set openfile to open for access filepath with write permission
set eof of openfile to 0
write aText as «class utf8» to openfile
close access openfile

KniazidisR that is awesome . Thank you

I see the script works out the entire frame count of the full duration of the file for eg 6530 which is the correct amount of frames for a 4min21sec05frames file at 25frames per a second

The duration of the file I have for eg. 00(h):04(m):21(sec):05(frames)

The previous script would just give the duration of 4m21sec and discard the 05 frames

How can I get the whole timecode represented with out the script rounding off the frames to the closest second ?

I was thinking if we started with the the full frame count and went from frames > timecode

I found the below script about getting frame count and converting to timecode but I haven’t figured how to apply it to KniazidisR frame count in the last script he wrote

tell application “QuickTime Player”
tell document 1 to set {currentTime, timeScale, theDuration, FrameCount} to {current time, time scale, duration, count (frames of track “Video track”)}
end tell

set FrameRate to (FrameCount * timeScale) / theDuration div 1
tell (currentTime / timeScale) to set {hr, mn, sc} to {it div 3600, it mod 3600 div 60, it mod 3600 mod 60 div 1}
set fr to (currentTime mod timeScale div (timeScale / FrameRate))
set SMPTE to addZero(TimeOffset + hr) & “:” & addZero(mn) & “:” & addZero(sc) & “:” & addZero(fr)
display dialog "TCR " & SMPTE

Could anyone help

What I am asking is there a way from the original script in this forum to have not just the hour:Minutes:seconds displayed but also the frames so the whole timecode of each video
hh:mm:ss:ff

Thank you all for your help

I will continue to try

Hi, byronheath.
If I understood correctly, then you just need to replace this in my script:

set framesNumber to (theSeconds * frameRate) as integer

with this:

set framesNumber to round ((theSeconds * frameRate) - ((theSeconds * frameRate) as integer))

Thank you so much for helping me you are a legend

Sorry I am not explaining it well

I have attached a video link so you can see it in visual form

I have burnt in timecode so you can see the duration rolling as clip plays

I would like to see the timecode as it is represtend on the video in below link

https://vimeo.com/374343428

so the final out come will be 00:00:03:17 (the 17 will be represented in the frames)

Again thank you so much

You want to be formatted zero hours and zero minutes as 00:00 ? To be presented zeroes?

Yes

So if the video was 1 hour 3 minutes 10sec and 17 frames it will be shown as 01:03:10:17

Or if the video was 1 minute 12 sec and 2 frames it would be shown as 00:01:12:02

If the video was 2 minute 5 seconds and 10 frames it would be shown as 00:02:05:10

As here?


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()

-- 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"
		set framesNumber to "unknown"
	else
		-- calculate duration in seconds and format as string
		set theSeconds to (value of durationInfo) / (timescale of durationInfo)
		
		-- Determine frames number here
		set theTracks to (get theAsset's tracks())
		repeat with i from 1 to count theTracks
			set aTrack to item i of theTracks
			if (aTrack's mediaType()) as string is "vide" then exit repeat
		end repeat
		set videoTrack to aTrack
		set frameRate to videoTrack's nominalFrameRate()
		set framesNumber to round ((theSeconds * frameRate) - (round (theSeconds * frameRate) rounding down))
	end if
	
	if framesNumber < 10 then
		set aDuration to (secondsToHMS from (round theSeconds rounding down)) & ":0" & framesNumber
	else
		set aDuration to (secondsToHMS from (round theSeconds rounding down)) & ":" & framesNumber
	end if
	(theResults's addObject:(current application's NSString's stringWithFormat_("%@    %@", theName's stringByDeletingPathExtension(), aDuration)))
end repeat

set aText to (theResults's componentsJoinedByString:linefeed) as text

tell application "Finder" to set folderName to name of theFolder
set filepath to (POSIX path of theFolder) & folderName & ".txt"
set openfile to open for access filepath with write permission
set eof of openfile to 0
write aText as «class utf8» to openfile
close access openfile

on secondsToHMS from theSeconds
	tell theSeconds
		tell {it div hours, it mod hours div minutes, it mod minutes}
			return "" & ((item 1) div 10) & ((item 1) mod 10) & ":" & ((item 2) div 10) & ((item 2) mod 10) & ":" & ((item 3) div 10) & ((item 3) mod 10)
		end tell
	end tell
end secondsToHMS

RESULT: —> file_example_MP4_1920_18MG 00:00:31:00

I tested with 1 mp4 file in the chosen folder

Thank you

Thank you everything works perfect except the frames :frowning:

it still doesn’t represent the frames I am looking for

I have noticed get info in Mac rounds off the frames to closest second instead of showing the frames

I don’t want it to round off the frame but show the actual frame number

For get info to round off the second from the frames it must be receiving that information

If you download the video I sent via Vimeo then the final timecode you see on the video would be
00:00:03:17 . The script now sees that clips duration as 00:00:04:00

I would like it show the actual duration of 00:00:03:17

I updated the last script as found 1 error. Need rounding down instead of rounding as integer. Try now.

NOTE: I can’t download your video from Vimeo