I now have a new requirement where those folders could be containing files with the following name pzv01001.mp4, pzv01002.mp4, mhd01001.mp4, mhd01002.mp4, pzv01001.mp4, pzv01002.mp4. Of those files only the one starting by “mhd” are to have 15 seconds clips to be created.
In order to do this I wrote something like this:
repeat with aURL in theURLs
set prefvalue to aURL as text
set iprefvalue to get text (46) thru (-12) of prefvalue
--display dialog ("***" & iprefvalue & "***")
if iprefvalue = "MHD" then
--display dialog ("will be converting " & aURL's |path|() as text)
my createclipvideo(aURL)
end if
return
PURPOSE OF THE SCRIPT The post containing this script has been deleted and for documentation purposes I am re-posting it. Special thanks to StefanK and Shane Stanley.
Over a week-end I could be creating more than 350 videos of of 2, 3, 4 up to 6 minutes. Those videos are contained in subfolders of a parent folder called “/DATA/ZVinformatique/CompétitionCourante/”. The purpose of this script is to go through each of the subfolders and create 15 seconds clips. The clips are to have the same name as their parent in the word “-clip” is to be added to it’s name. For example : a file called pzv09005.mp4 it’s clip would be name pzv09005-clip.mp4
Subfolder “/DATA/ZVinformatique/CompétitionCourante/01” could contain: pzv01001.mp4, pzv01002.mp4, pzv01003.mp4.
Subfolder “/DATA/ZVinformatique/CompétitionCourante/02” could contain: pzv02001.mp4, pzv02002.mp4, pzv02003.mp4.
Subfolder “/DATA/ZVinformatique/CompétitionCourante/03” could contain: pzv03001.mp4, pzv03002.mp4
Subfolder under “/Data/ZVinformatique/CompétitionCourante/“ are always from 01 to 99 (always 2 characters long).
The expected behaviour and result is :
goto “/DATA/ZVinformatique/CompétitionCourante/01” subfolder and create the following clips : pzv01001-clip.mp4, pzv01002-clip.mp4, pzv01003-clip.mp4
goto “/DATA/ZVinformatique/CompétitionCourante/02” subfolder and create the following clips : pzv02001-clip.mp4, pzv02002-clip.mp4, pzv02003-clip.mp4.
goto “/DATA/ZVinformatique/CompétitionCourante/03” subfolder and create the following clips: pzv03001-clip.mp4, pzv03002-clip.mp4
use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AVFoundation"
use scripting additions
set MP4DestinationFolder to "/Volumes/DATA/ZVinformatique/CompétitionCourante/"
set destURL to current application's |NSURL|'s fileURLWithPath:MP4DestinationFolder
set theOptions to (current application's NSDirectoryEnumerationSkipsHiddenFiles) + (get current application's NSDirectoryEnumerationSkipsPackageDescendants)
set theURLs to (current application's NSFileManager's defaultManager()'s enumeratorAtURL:destURL includingPropertiesForKeys:(missing value) options:theOptions errorHandler:(missing value))'s allObjects()
--set thePred to current application's NSPredicate's predicateWithFormat_("pathExtension == %@", "mp4")
set thePred to current application's NSPredicate's predicateWithFormat:"(pathExtension == %@) AND (NOT lastPathComponent.stringByDeletingPathExtension ENDSWITH %@)" argumentArray:{"mp4", "-clip"}
set theURLs to theURLs's filteredArrayUsingPredicate:thePred
repeat with aURL in theURLs
--display dialog ("will be converting " & aURL's |path|() as text)
my createclipvideo(aURL)
end repeat
on createclipvideo(theURL)
set theAsset to current application's AVURLAsset's assetWithURL:theURL
set destPath to (theURL's |path|()'s stringByDeletingPathExtension()'s stringByAppendingString:"-clip")'s stringByAppendingPathExtension:"mp4"
set destURL to current application's NSURL's fileURLWithPath:destPath
if (destURL's checkResourceIsReachableAndReturnError:(missing value)) as boolean then return
set durationInfo to theAsset's duration()
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)
end if
set theStart to (0.65 * theDuration) div 1
set theEnd to 15
if theStart + 15 > theDuration then
set theStart to (0.55 * theDuration) div 1
set theEnd to 15
end if
if theStart + 15 > theDuration then
set theStart to (0.4 * theDuration) div 1
set theEnd to 15
end if
if theStart + 15 > theDuration then
set theStart to (0.25 * theDuration) div 1
set theEnd to 15
end if
if theStart + 15 > theDuration then
set theStart to 1
set theEnd to theDuration
end if
set destURL to current application's NSURL's fileURLWithPath:destPath
set startTime to current application's CMTimeMakeWithSeconds(theStart, 1)
set durTime to current application's CMTimeMakeWithSeconds(15, 1)
set theRange to current application's CMTimeRangeMake(startTime, durTime)
set theAsset to current application's AVURLAsset's assetWithURL:theURL
set exportSession to current application's AVAssetExportSession's alloc()'s initWithAsset:theAsset presetName:(current application's AVAssetExportPresetHighestQuality)
exportSession's setOutputURL:destURL
exportSession's setOutputFileType:(current application's AVFileTypeQuickTimeMovie)
exportSession's setTimeRange:theRange
exportSession's exportAsynchronouslyWithCompletionHandler:(missing value)
repeat
delay 0.01
set theStatus to exportSession's status()
if theStatus > 2 then exit repeat -- 3 is OK
end repeat
if theStatus = 4 then error exportSession's |error|()'s localizedDescription() as text
end createclipvideo