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)
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
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
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
Thank you everything works perfect except the frames
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 script. You tried it? As I see, no. My script rounds as it is necessary, because doesn’t exist half frame or 1.3 frame, but exists 1 frame, or 2 frames. It is integer value. And script shows it now. Get info rounds to nearest second, but my script rounds to the nearest whole number of frames. As it should be.
And I repeat, I can’t download your Vimeo file to test. I already said that above.
So, open the last script from here and run. Not the script you have on your computer but that updated script from here. Say what you get in the text file.
I was aking is your script rounding off the frame to the nearest 0 , is that’s why all frames are displayed as 00 instead of the frame number of the file ?
the minute and seconds are displayed perfectly but the frames are not
when you had the frames of the entire duration displayed in previous script and I took that into frame calculator that converted back to (hh)(mm)(ss)(ff) it covered to exact timecode
Thank you for you time and effort into trying to help me figure this out
I changed little the script to get frames number simpler. This should work:
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 aMod to theSeconds - (round theSeconds rounding down)
set framesNumber to round (aMod * 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
Hi, byronheath.
Tell me - is one frame missing or is there an extra frame? If one frame is missing, then this inaccuracy is most likely to be corrected if you replace this:
set framesNumber to round (aMod * frameRate) rounding down
with this:
set framesNumber to round (aMod * frameRate) rounding up
set framesNumber to round (aMod * frameRate) rounding down
with
set framesNumber to round (aMod * frameRate) - 1
and it seems to work perfectly
I also realised it would be less accurate if I had mixed frame rates in the folder I was trying to get durations from
Thank You so much
I was also wandering if it was possible and how I would go about it if I wanted each video clip in the folder to have its own text document with the text file name set to that of the video with title and duration
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 aMod to theSeconds - (round theSeconds rounding down)
set framesNumber to round (aMod * frameRate) - 1
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
set aText to (current application's NSString's stringWithFormat_("%@ %@", theName's stringByDeletingPathExtension(), aDuration)) as text
tell application "Finder" to set folderName to name of theFolder
set filepath to (POSIX path of theFolder) & aText & ".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
end repeat
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