Trim and save a Quicktime file into multiple videos of max 30 seconds

Hey guys!

I’m trying to create a script droplet to sit on my desktop.
(I will drag a video file onto it.)

A new folder must be created on the desktop (with the same name as the video file)
Within this folder, the video clips (of max length 30 seconds) from the original video, labelled 1, 2, 3, … must be saved. It’s for posting to WhatsApp status, it only accepts videos of max 30 seconds.

I am a Computer Science student but have never used Scripts before, somebody wrote this up for me but doesn’t seem to want to help me finish. Any advice & fixes would be appreciated!

e.g. a 3-minute video (example.mp4) is dragged onto the application. This would result in 6x 30-second videos (labelled 1-6.mp4) in a folder called “example” on my desktop.

e.g. a 75-second video would result in 3 videos (2x 30 seconds & 1x 15 second video)

This is the script I’m working with so far, and I’m getting export errors (see below)

“End time must be less than or equal to duration
QuickTime Player got an error: End time must be less than or equal to duration (6)”

on open droppedItems
	
	tell application "Finder" to repeat with item_ in droppedItems
		
	end repeat
	set UnixPath to POSIX path of ((path to me as text) & "::")
	
	tell application "QuickTime Player"
		activate
		open (POSIX path of item_) as POSIX file
		set videoDuration to duration of document 1
		set fileName to name of document 1
	end tell
	
	set startTime to 0
	set i to 1
	
	repeat while (startTime + 30) is less than videoDuration
		tell application "QuickTime Player"
			trim document 1 from startTime to (startTime + 30)
			export document 1 in (UnixPath & "/Output/" & i & "-" & fileName as POSIX file) using settings preset "720p"
			close document 1 saving no
			open (POSIX path of item_) as POSIX file
			set i to i + 1
		end tell
		set startTime to (startTime + 30)
	end repeat
	
	tell application "QuickTime Player"
		trim document 1 from startTime to (videoDuration - 1)
		export document 1 in (UnixPath & "/Output/" & i & "-" & fileName as POSIX file) using settings preset "720p"
		close document 1 saving no
	end tell
	
	tell application "QuickTime Player" to close window "Export Progress"
end open

Your probably better off not trimming you master video.

Just set your in and out points.
Increasing by 30sec,
make sure your last out point is less than max length:
Then use quicktimes Copy function.
Create a new QT movie.
Then paste.

Save your new Movie.

You may try :

-- trim.app

-- Edited by Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 25 mai 2020 17:30:29
-- Edited on 2020/05/27 to take care of original files shorter than 30 seconds

on open droppedItems
	-- The name of the window is a localized string
	tell application "QuickTime Player"
		activate
		set x to "1.title"
		set _loc to localized string x from table "MGJobProcessingWindow"
		if _loc = x then set _loc to "Export Progress" -- for 10.13.6
		set exportProgress_loc to _loc
	end tell
	
	set localFolder to "Output"
	set mySelf to (path to desktop as string) -- EDITED
	set myFolder to (mySelf & localFolder & ":")
	tell application "System Events"
		if not (exists folder myFolder) then make new folder at end of folder mySelf with properties {name:localFolder}
	end tell
	set UnixPath to POSIX path of myFolder
	--> "/Users:**********:Desktop:trim.app:Contents:Resources:Output:"
	
	-- Now the storage folder is available, we may work
	
	-- No need to speak to the Finder
	
	repeat with item_ in droppedItems
		tell application "QuickTime Player"
			open item_ as «class furl»
			set videoDuration to duration of document 1
			set fileName to name of document 1
		end tell
		
		set startTime to 0
		set i to 1
		
		repeat while (startTime + 30) < videoDuration
			tell application "QuickTime Player"
				trim document 1 from startTime to (startTime + 30)
				export document 1 in POSIX file (UnixPath & i & "-" & fileName) using settings preset "720p"
				close document 1 saving no
				open item_ as «class furl»
				set i to i + 1
			end tell
			delay 0.2
			set startTime to startTime + 30
		end repeat
		
		tell application "QuickTime Player"
			trim document 1 from startTime to (videoDuration - 1)
			export document 1 in POSIX file (UnixPath & i & "-" & fileName) using settings preset "720p"
			close document 1 saving no
		end tell
		
		tell application "QuickTime Player" to close window exportProgress_loc
	end repeat -- CORRECT LOCATION
end open

As far as I know the script doesn’t change the original files.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 25 mai 2020 16:51:51

Oops, I misunderstood what was doing the exotic instruction :

set UnixPath to POSIX path of ((path to me as text) & "::")

Now the script achieve your real needs.

Edited on 2020/05/27 to take care of original files shorter than 30 seconds

Wow, thank you so much, Yvan for your updated code!

Your script outputs the first few parts but fails to finish the last few parts.

But I still receive this error when I used your code after I saved as an application droplet.

Any suggestions?

May ou check what is the duration of your original file ?
I guess that it’s less than 30 seconds.
Honestly I didn’t imagined that you would try to do that.

Let me know if it’s really what you did.
If it is, I will add a test to prevent you to do that.

In fact, I edited the loop to be able to trace is easily and I forgot the case when the initial file is less than 30 second long.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 27 mai 2020 14:46:26

The question was not about trimming a video to a single new duration,
It was to split the file into possibly several files whose duration don’t exceed a given duration, here, 30 seconds.
With my test file I got a 30 seconds file and a 13 seconds one.

The code supposed to execute the trim action was correct.
I just changed the construction of the path of the created files.
The original script failed to check for the availability of a subfolder named “Output” on the desktop.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 27 mai 2020 15:23:00

Of course we may choose to calculate explicitly the number of files to create, the length of the very lat one it’s shorter than the allowed max duration and apply trim to such values.
It’s what is done by the script below.
I’m not sure that it’s more efficient than the first one.

on run
	set droppeditems to choose file of type {"mov"} with multiple selections allowed -- to get a list
	open droppeditems
end run

on open droppeditems
	-- The name of the window is a localized string
	tell application "QuickTime Player"
		activate
		set x to "1.title"
		set _loc to localized string x from table "MGJobProcessingWindow"
		if _loc = x then set _loc to "Export Progress" -- for 10.13.6
		set exportProgress_loc to _loc
	end tell
	
	set localFolder to "Output"
	set mySelf to (path to desktop as string) -- EDITED
	set myFolder to (mySelf & localFolder & ":")
	tell application "System Events"
		if not (exists folder myFolder) then make new folder at end of folder mySelf with properties {name:localFolder}
	end tell
	set UnixPath to POSIX path of myFolder
	--> "/Users:**********:Desktop:trim.app:Contents:Resources:Output:"
	
	-- Now the storage folder is available, we may work
	
	-- No need to speak to the Finder
	
	set max to 30
	repeat with item_ in droppeditems
		tell application "QuickTime Player"
			open item_ as «class furl»
			set videoDuration to duration of document 1
			set fileName to name of document 1
		end tell
		tell application "QuickTime Player"
			set startTimes to 0
			if videoDuration < max then
				export document 1 in POSIX file (UnixPath & i & "-" & fileName) using settings preset "720p"
				close document 1 saving no
			else
				set cnt to videoDuration div max
				if cnt * max < videoDuration then
					-- here we have cnt segment of same duration : max
					repeat with i from 1 to cnt
						trim document 1 from startTimes to (startTimes + max)
						export document 1 in POSIX file (UnixPath & i & "-" & fileName) using settings preset "720p"
						close document 1 saving no
						open item_ as «class furl»
						set startTimes to startTimes + max
					end repeat
					-- plus one file whose duration is :
					set i to cnt + 1
					set lastDur to videoDuration mod max
					trim document 1 from startTimes to (startTimes + lastDur - 0.1)
					export document 1 in POSIX file (UnixPath & i & "-" & fileName) using settings preset "720p"
					close document 1 saving no
					open item_ as «class furl»
					set startTimes to startTimes + max
				else
					repeat with i from 1 to cnt
						trim document 1 from startTimes to (startTimes + max)
						export document 1 in POSIX file (UnixPath & i & "-" & fileName) using settings preset "720p"
						close document 1 saving no
						open item_ as «class furl»
						set startTimes to startTimes + max
					end repeat
				end if
			end if
			close window exportProgress_loc
		end tell
	end repeat -- CORRECT LOCATION
end open

After that, we may choose to use shell script with code delivered in the box : SIPs or with third party CLI.
It’s matter of taste. On my side I never liked shell. In year 2001, when OSX appeared, in a French magazine named Tremplin Micro, I wrote an article entitled “Au secours, les grands prêtres reviennent” (in English “to the rescue, the high priests return”) in which I explained why I disliked shell. 20 years later I didn’t changed my advice. I wanted a language resembling to human language, no cabalistic incantations. My quite 77 years old brain has difficulties to memorize the vocabulary used by ASObjC but it absolutely refuse shell, python and so on. I apologizes, I’m a human being with its own taste, not a machine.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 27 mai 2020 21:16:11

I added 4 instructions allowing to execute the script from the Script Editor and see the log history.

Hi Yvan!

Thank you so much for you help!

I have tried your latest update but the same error persists!

(I have never tried using a file shorter that 30 seconds as per your questions…)

I don’t know what the problem is…

@tristdrum

May you send the script which you ran and a video giving the error to my mailbox:
koenigyvan mac com ?

Maybe it would help me to understand what is failing.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 9 juin 2020 16:53:33

I added 4 instructions allowing to execute the script from the Script Editor and see the log history.

Here is what I got:

Don't click upon [Open this Scriplet in your Editor]
Here is a log history.

tell application "Script Editor"
	choose file of type {"mov"} with multiple selections allowed
		--> {alias "SSD 1000:Users:**********:Desktop:MagicTrackpad.mov"}
end tell
tell application "QuickTime Player"
	activate
	localized string "1.title" from table "MGJobProcessingWindow"
		--> "Progression de l’exportation"
end tell
tell current application
	path to desktop as string
		--> "SSD 1000:Users:**********:Desktop:"
end tell
tell application "System Events"
	exists folder "SSD 1000:Users:**********:Desktop:Output:"
		--> false
	make new folder at end of folder "SSD 1000:Users:**********:Desktop:" with properties {name:"Output"}
		--> folder "SSD 1000:Users:**********:Desktop:Output:"
end tell
tell application "QuickTime Player"
	open file "SSD 1000:Users:**********:Desktop:MagicTrackpad.mov"
		--> document "MagicTrackpad.mov"
	get duration of document 1
		--> 152.333333333333
	get name of document 1
		--> "MagicTrackpad.mov"
	trim document 1 from 0 to 30
	export document 1 in file "SSD 1000:Users:**********:Desktop:Output:1-MagicTrackpad.mov" using settings preset "720p"
	close document 1 saving no
	open file "SSD 1000:Users:**********:Desktop:MagicTrackpad.mov"
		--> document "MagicTrackpad.mov"
	trim document 1 from 30 to 60
	export document 1 in file "SSD 1000:Users:**********:Desktop:Output:2-MagicTrackpad.mov" using settings preset "720p"
	close document 1 saving no
	open file "SSD 1000:Users:**********:Desktop:MagicTrackpad.mov"
		--> document "MagicTrackpad.mov"
	trim document 1 from 60 to 90
	export document 1 in file "SSD 1000:Users:**********:Desktop:Output:3-MagicTrackpad.mov" using settings preset "720p"
	close document 1 saving no
	open file "SSD 1000:Users:**********:Desktop:MagicTrackpad.mov"
		--> document "MagicTrackpad.mov"
	trim document 1 from 90 to 120
	export document 1 in file "SSD 1000:Users:**********:Desktop:Output:4-MagicTrackpad.mov" using settings preset "720p"
	close document 1 saving no
	open file "SSD 1000:Users:**********:Desktop:MagicTrackpad.mov"
		--> document "MagicTrackpad.mov"
	trim document 1 from 120 to 150
	export document 1 in file "SSD 1000:Users:**********:Desktop:Output:5-MagicTrackpad.mov" using settings preset "720p"
	close document 1 saving no
	open file "SSD 1000:Users:**********:Desktop:MagicTrackpad.mov"
		--> document "MagicTrackpad.mov"
	trim document 1 from 150 to 152.233333333333
	export document 1 in file "SSD 1000:Users:**********:Desktop:Output:6-MagicTrackpad.mov" using settings preset "720p"
	close document 1 saving no
	open file "SSD 1000:Users:**********:Desktop:MagicTrackpad.mov"
		--> document "MagicTrackpad.mov"
	close window "Progression de l’exportation"
end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 9 juin 2020 17:32:12

Replying to post #11

Hey @Yvan Koenig,

Sure thing, thanks so much for your help. I suspect it may be something weird and system specific, or a silly error on my part.

Here with the Exported Application Droplet, the Script, a demo video (which should split into 12 parts), and a screen recording of what happens. (I couldn’t email it, illegal attachments)

Sometimes it completes multiple parts, sometimes (as it did this time) it fails after the first part. Always the same error.

Regards
Tristan

https://drive.google.com/drive/folders/1Zrnx0blLpfCeO8s711mjwEqQYEHhL2SM?usp=sharing

I don’t understand what to do with your Post #12.

Just for you, I added instructions allowing to use the script from the editor so that you may get its log history which is the only set of infos which may be helpful.
Sending a video showing what you get just waste your time and mine.

Alas, you didn’t used this version of the script.
If you don’t send the wanted infos I can’t help you.
Message #12 give an example of what is supposed to be returned by the enhanced script.
It’s exactly what I asked you to send.

You wrote that you can’t send the video which issue errors due to problems of illegal attachments.
Is it the only video which issue the errors ?
What is the script behavior if you submit it the video displaying its behavior with your “illegal” one ?

In fact, I learnt something from your video:
you are trying to split a mp4 but I never tested with such files because I didn’t own such ones. I only treated mov ones.

To be able to test it, I edited the script allowing it’s run handler to accept mp4 files.

What I got is not the error which you reported, it’s : "You aren’t allowed to save the file «1-MY NAME IS 1080p.mp4» which has nothing in common with what you reported.

I tried to do the job by hand and discovered that the only way to save sub-files was to export them as mov ones.
So I edited the script which now export mov sub-files and, what a surprise, your mp4 was correctly treated.
Here is the enhanced script:

-- trim.app

-- Edited by Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 25 mai 2020 17:30:29
-- Edited on 2020/05/27 to take care of original files shorter than 30 seconds
-- Edited on 2020/06/11 to take care of original files which aren't mov ones
-- If you dislike the fact that the script wait that a sub-file is really saved
-- before exporting the next one, set the property waitSave to false

property waitSave : true
-- true --> wait that the file is saved
-- false --> doesn't wait

on run
	set droppeditems to choose file of type {"mov", "mp4"} with multiple selections allowed -- to get a list
	open droppeditems
end run

on open droppeditems
	set localFolder to "Output"
	set base to path to desktop as string -- EDITED
	set myFolder to base & localFolder & ":"
	tell application "System Events"
		if not (exists folder myFolder) then make new folder at end of folder base with properties {name:localFolder}
	end tell
	-- Now the storage folder is available, we may work
	
	-- No need to speak to the Finder
	
	tell application "QuickTime Player"
		-- The name of the window is a localized string
		activate
		set x to "1.title"
		set _loc to localized string x from table "MGJobProcessingWindow"
		if _loc = x then set _loc to "Export Progress" -- for 10.13.6
		set exportProgress_loc to _loc
		
		repeat with item_ in droppeditems
			set item_ to item_ as «class furl» -- only once for each file
			open item_
			tell document 1
				set videoDuration to duration
				set fileName to name
			end tell
			set origExt to name extension of (get info for item_)
			if origExt is not "mov" then
				set newName to text 1 thru -(1 + (count origExt)) of fileName & "mov"
			else
				copy fileName to newName
			end if
			
			set max to 30
			set startTime to 0
			set i to 1
			repeat while (startTime + max) < videoDuration
				trim document 1 from startTime to (startTime + max) -- CAUTION, create an untitled document
				set hfs to myFolder & i & "-" & newName
				export document 1 in hfs as «class furl» using settings preset "720p"
				close document 1 saving no
				open item_
				set i to i + 1
				set startTime to startTime + max
				if waitSave then
					tell application "System Events"
						repeat until exists disk item hfs
							delay 0.5
						end repeat
					end tell
				end if
			end repeat
			
			trim document 1 from startTime to videoDuration -- CAUTION, create an untitled document
			set hfs to myFolder & i & "-" & newName
			export document 1 in hfs as «class furl» using settings preset "720p"
			close document 1 saving no
			close window exportProgress_loc
		end repeat -- item_
	end tell -- "QuickTime Player"
end open

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 11 juin 2020 12:03:45

Here is how to Trim and save a Quicktime file into multiple videos of max 30 seconds, stable script:


-- Choose master movie file
set item_ to (choose file of type {"m4v", "mp4"}) as text

-- Create Destination Folder on the Desktop (if need)
try
	set destinationFolder to alias (((path to desktop) as text) & "Output:")
	tell application "Finder" to move items of destinationFolder to trash
on error
	tell application "Finder"
		set destinationFolder to (make new folder with properties {name:"Output"}) as alias
	end tell
end try

tell application "QuickTime Player"
	activate
	
	-- Open first time to get Duration and Name of master movie
	set masterDoc to open item_
	set videoDuration to duration of document 1
	set fileName to name of document 1
	
	set startTime to -30.0
	set i to 0 -- newly generated files counter
	
	repeat
		set i to i + 1
		set startTime to startTime + 30.0
		if videoDuration - startTime < 30.0 then
			set endTime to videoDuration
		else
			set endTime to startTime + 30.0
		end if
		-- need opening master movie again every time
		set masterDoc to open item_
		-- need try block here
		try
			trim masterDoc from startTime to endTime
		end try
		-- get reference to newly trimmed piece
		set untitledDoc to first document whose name begins with "Untitled"
		-- export it, then close it
		export untitledDoc in ((POSIX path of destinationFolder) & i & "-" & fileName as POSIX file) using settings preset "720p"
		close untitledDoc saving no
		-- if this step was last part of trimming process, then exit repeat
		if videoDuration - startTime < 30.0 then exit repeat
	end repeat
	
	-- quit all
	close window "Export Progress"
end tell