Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

armadillo22

macrumors newbie
Original poster
Nov 18, 2010
2
0
Hi

First of let me apologise for my lack of AppleScript knowledge.
Secondly let me thank you for any assistance you can provide.

I have a large number of Quicktime files that I want to trim in QT7 by a set amount (10 seconds from beginning, 10 seconds from end) and then drop into a compressor droplet to start transcoding.

Here's the workflow I'm trying to achieve:

1) Manually move content into "Input" folder.
2) Trim 10 seconds from beginning and end of every movie in the "Input" folder.
3) Save (here I want to simply save in place...not export/create new file).
4) Move trimmed movie into "Trimed" folder.
5) Have Compressor droplet activate and start processing content in "Trimed" folder.

Now I understand that it might not be possible to automate the activation of the compressor droplet (step 5)...but even just getting to step 4 would be a huge help.

Many thanks

Sean
 

HiRez

macrumors 603
Jan 6, 2004
6,250
2,576
Western US
I can't give you the whole solution, but I can give you a rough snippet of code that should do the batch trimming part:

Code:
set secondsFromStart to 10
set secondsFromEnd to 10
set srcFolder to choose folder

tell application "Finder"
	set fileList to files of folder srcFolder
	repeat with f in fileList
		tell application "QuickTime Player"
			open f
			tell document 1
				set timeScale to time scale
				set selection start to (timeScale * secondsFromStart)
				set selection end to (duration - (timeScale * secondsFromEnd))
				trim
				save
				close
			end tell
		end tell
	end repeat
end tell

If you're using Snow Leopard, you might need to change that one tell statement to "QuickTime Player 7", or whatever the exact name of the classic QuickTime app is, I can't remember right now.

EDIT: Also be careful about making sure your movies are all over 20 seconds long (the amount you're trimming). I'm not sure what would happen if they're not, but it could crash.
 
Last edited:

HiRez

macrumors 603
Jan 6, 2004
6,250
2,576
Western US
Here's an alternative version that checks the content length against the amount you're attempting to trim and skips over the ones that are too short, displaying a list of the skipped files at the end):

Code:
set secondsFromStart to 10
set secondsFromEnd to 10
set shortMoviesList to {}

set srcFolder to choose folder

tell application "Finder"
	set fileList to files of folder srcFolder
	repeat with f in fileList
		tell application "QuickTime Player"
			open f
			tell document 1
				-- Check to make sure we're not trimming more time than the movie contains
				set timeScale to time scale
				set trimmedTime to (timeScale * (secondsFromStart + secondsFromEnd))
				if trimmedTime is greater than duration then
					-- Not enough content to trim, add file name to a list
					set end of shortMoviesList to (the name of f as string) & return
				else
					-- We have enough content, proceed to trimming
					set selection start to (timeScale * secondsFromStart)
					set selection end to (duration - (timeScale * secondsFromEnd))
					trim
					save
				end if
				close
			end tell
		end tell
	end repeat
	if length of shortMoviesList is greater than 0 then
		tell application "Finder"
			-- bring Finder to front so dialog is not hidden
			activate
		end tell
		display dialog "The following movies could not be trimmed because they do not contain enough content: " & return & return & shortMoviesList with icon caution
	end if
end tell
 

armadillo22

macrumors newbie
Original poster
Nov 18, 2010
2
0
Thanks for the assist!

I've se up a folder and added this as a folder actions script along with a filesize check and delay that I culled from another script I've found.

I've also been able to set up a watch folder by assigning a folder acton script to open the contents of a folder with a Compressor droplet.

However I have a problem automating the moving of the trimmed content to this next watch folder.

So the script at the moment is:

Code:
on adding folder items to thisFolder after receiving theItems
	repeat with f in theItems
		set Was to 0
		set isNow to 1
		repeat while isNow ≠ Was
			set Was to size of (info for f)
			delay 30
			set isNow to size of (info for f)
		end repeat
		set secondsFromStart to 10
		set secondsFromEnd to 10
		set srcFolder to "Macintosh HD:Users:Sean:Desktop:SCRIPT_TEST_UNTRIMMED"
		tell application "Finder"
			set fileList to files of folder srcFolder
			repeat with f in fileList
				tell application "QuickTime Player 7"
					open f
					tell document 1
						set timeScale to time scale
						set selection start to (timeScale * secondsFromStart)
						set selection end to (duration - (timeScale * secondsFromEnd))
						trim
						save
						close
					end tell
				end tell
			end repeat
		end tell
	end repeat
end adding folder items to

Where would I put a MOVE command? Also the move command would wait until Quicktime has finished with EVERY file? Or would it (undesirably) start moving content before quicktime has processed the entire contents of the folder (if for example there was a large number of files)?

Once again thanks

Sean
 

poleary2000

macrumors newbie
May 5, 2010
18
0
Hi, I am desperate to use a script exactly like this on Mavericks. I have ~1,000 videos that I need to trim both front and back. I copied this code and pasted into applescript editor, but it does nothing. Any thoughts you might have would be great.
 

kryten2

macrumors 65816
Mar 17, 2012
1,114
99
Belgium
Hi, I am desperate to use a script exactly like this on Mavericks. I have ~1,000 videos that I need to trim both front and back. I copied this code and pasted into applescript editor, but it does nothing. Any thoughts you might have would be great.

What code did you use? The one from the last post or something else? If it's the last one then you should attach it to a folder because it is a folder action script. It also makes use of QuickTime Player 7. If you don't have QuickTime Player 7 then you should replace this line :

Code:
tell application "QuickTime Player 7"

with this :

Code:
tell application "QuickTime Player"

Info : Folder Actions Reference
 

poleary2000

macrumors newbie
May 5, 2010
18
0
What code did you use? The one from the last post or something else? If it's the last one then you should attach it to a folder because it is a folder action script. It also makes use of QuickTime Player 7. If you don't have QuickTime Player 7 then you should replace this line :

Code:
tell application "QuickTime Player 7"

with this :

Code:
tell application "QuickTime Player"

Info : Folder Actions Reference

I was using the code below. I don't need it to move the files or anything. I just want the files to be trimmed.

When I paste it into AppleScript, the text is all purple. If I hit compile to check it, it gives me a Syntax Error that says Expected End of Line but Found Identifier on the part that says: set timeScale to time scale.

I am using Mavericks and it says QuickTime Player.

Code:
set secondsFromStart to 10
set secondsFromEnd to 10
set srcFolder to choose folder

tell application "Finder"
	set fileList to files of folder srcFolder
	repeat with f in fileList
		tell application "QuickTime Player"
			open f
			tell document 1
				set timeScale to time scale
				set selection start to (timeScale * secondsFromStart)
				set selection end to (duration - (timeScale * secondsFromEnd))
				trim
				save
				close
			end tell
		end tell
	end repeat
end tell
 

poleary2000

macrumors newbie
May 5, 2010
18
0
OK, so I am making some progress. I am now using the code below. It is working...somewhat. The problems now are:

- It wants to save it as a QuickTime Composition. I want to save it as an m4v.
- It is prompting me to choose the save directory and name the file.

I would like it to run unprompted. Ideally, I'd like to save the trimmed files with the same name as the current file but in a subfolder of that directory. Thus, giving me a folder with the originals and a subfolder with the trimmed files. Any thoughts on how to do that?

Code:
set secondsFromStart to 3.5
set secondsFromEnd to 3.5
set srcFolder to choose folder

tell application "Finder"
	set fileList to files of folder srcFolder
	repeat with f in fileList
		tell application "QuickTime Player"
			open f
			delay (0.25)
			tell document 1
				trim from secondsFromStart to (duration - secondsFromEnd)
				save
				close
			end tell
		end tell
	end repeat
end tell
 
Last edited:

poleary2000

macrumors newbie
May 5, 2010
18
0
I'm messing around some more. But I can't get the Export correct. I have the following. I'd like to be able to select the new folder to send all of the files to (choose it once), then have it do the export using 480p and using the current filename. Thoughts?

Code:
set secondsFromStart to 3.5
set secondsFromEnd to 3.5
set srcFolder to choose folder

tell application "Finder"
	set fileList to files of folder srcFolder
	repeat with f in fileList
		tell application "QuickTime Player"
			open f
			delay (0.25)
			tell document 1
				trim from secondsFromStart to (duration - secondsFromEnd)
				export document in using settings preset "480p"
				close
			end tell
		end tell
	end repeat
end tell
 

kryten2

macrumors 65816
Mar 17, 2012
1,114
99
Belgium
I'm messing around some more. But I can't get the Export correct. I have the following. I'd like to be able to select the new folder to send all of the files to (choose it once), then have it do the export using 480p and using the current filename. Thoughts?

Try this :

Code:
set secondsFromStart to 3.5
set secondsFromEnd to 3.5
set srcFolder to choose folder
set dstFolder to choose folder with prompt "Choose the destination folder for the trimmed files." default location srcFolder

tell application "Finder"
	set fileList to files of folder srcFolder
end tell

repeat with f in fileList
	tell application "QuickTime Player"
		open f
		set documentName to name of f
		delay (0.25)
		tell document 1
			trim from secondsFromStart to (duration - secondsFromEnd)
			export in file ((dstFolder as text) & documentName) using settings preset "480p..."
			close saving no
		end tell
	end tell
end repeat
 

Attachments

  • Screen Shot 2013-12-07 at 22.07.13.png
    Screen Shot 2013-12-07 at 22.07.13.png
    196.9 KB · Views: 456
  • Screen Shot 2013-12-07 at 22.07.36.png
    Screen Shot 2013-12-07 at 22.07.36.png
    203.2 KB · Views: 390
Last edited:

poleary2000

macrumors newbie
May 5, 2010
18
0
OK, almost there. This is great.

Two issues:

1) There are a few short videos in there. In some cases, shorter than the Duration - End Time. Thus, it says the End Time must be Greater than the Start Time.

2) When it goes to export, it starts the export but says: You don't have permission to save the file "filename"

----------

On the short files, I'll just go through those manually. Most likely, they are just a few seconds long with no content I want. So...that solves that condition. The saving one though, I need some help with.

Thanks!
 

poleary2000

macrumors newbie
May 5, 2010
18
0
I repaired disk permissions via Disk Utility in the Restore Boot. No dice. It still says I do not have permission.
 

kryten2

macrumors 65816
Mar 17, 2012
1,114
99
Belgium
I repaired disk permissions via Disk Utility in the Restore Boot. No dice. It still says I do not have permission.

Please post the code you're using. Regarding the short files you can do this :

All files are opened in QuickTime Player. Only the ones that are longer than 7 seconds in duration (just as an example you can change if you want) will be trimmed and exported. The rest will be closed.

Code:
set secondsFromStart to 3.5
set secondsFromEnd to 3.5
set srcFolder to choose folder
set dstFolder to choose folder with prompt "Choose the destination folder for the trimmed files." default location srcFolder

tell application "Finder"
	set fileList to files of folder srcFolder
end tell


repeat with f in fileList
	tell application "QuickTime Player"
		open f
		set documentName to name of f
		delay (0.25)
		tell document 1
			if duration > 7 then
				trim from secondsFromStart to (duration - secondsFromEnd)
				export in file ((dstFolder as text) & documentName) using settings preset "480p..."
				close saving no
			else
				close saving no
			end if
		end tell
	end tell
end repeat


Note : Tested on Mavericks with 50 files.
 
Last edited:

poleary2000

macrumors newbie
May 5, 2010
18
0
This is what I have been using.

Code:
set secondsFromStart to 3.5
set secondsFromEnd to 3.5
set srcFolder to choose folder
set dstFolder to choose folder with prompt "Choose the destination folder for the trimmed files." default location srcFolder

tell application "Finder"
	set fileList to files of folder srcFolder
end tell

repeat with f in fileList
	tell application "QuickTime Player"
		open f
		set documentName to name of f
		delay (1.0)
		tell document 1
			trim from secondsFromStart to (duration - secondsFromEnd)
			export in file ((dstFolder as text) & documentName) using settings preset "480p..."
			close saving no
			delay (1.0)
		end tell
	end tell
end repeat
 

poleary2000

macrumors newbie
May 5, 2010
18
0
I tried the new code as well for the short files, that does work for the duration issue. I added a delay of 1.0 because otherwise, it would run too fast and error out.

Attached is a screen cap of what happens. You can see it says that "You don't have permission to save the file". I've tried choosing multiple folders, repairing permissions and making sure the folder I choose has read and write capability. Not sure what else to do.
 

Attachments

  • 2013-12-08 08.05.42 am.jpg
    2013-12-08 08.05.42 am.jpg
    45 KB · Views: 393

poleary2000

macrumors newbie
May 5, 2010
18
0
I tried it manually on one file, and no issue. It exports and saves to the folder.

It's just the applescript is giving permissions issues.
 

kryten2

macrumors 65816
Mar 17, 2012
1,114
99
Belgium
The only way I can reproduce this error is by using this :

Code:
export in (dstFolder as text) & documentName using settings preset "480p..."

instead of this :

Code:
export in file ((dstFolder as text) & documentName) using settings preset "480p..."

Don't know why it does that, sorry. I searched Google for the error but nothing useful came up.
 

poleary2000

macrumors newbie
May 5, 2010
18
0
Thanks, I've been looking too. I posted the issue on an Apple discussions board. Will respond back here if anything ever turns up. Thanks for your help.

I have a MBA that I use too, so I might try putting the files on there and seeing if I have the same issue. Maybe I'll get lucky.
 

poleary2000

macrumors newbie
May 5, 2010
18
0
FYI, I finally figured it out. The script does NOT work with M4V files. I converted the files to MP4 and the script worked perfectly. Weird.
 

randomentity

macrumors newbie
Feb 19, 2014
2
0
I just stumbled across this thread with ZERO scripting experience except for a semester of BASIC/VisualBASIC 12 years ago.
I have the need to edit batches of videos down to the last 7 minutes. I figure if I can somehow modify this script on a folder, that will save the trimmed video to a watch folder using Telestream Episode to take care of the rest I can save myself a ton of time. I'm currently doing the trimming by hand in Premiere but it's just not efficient.
The problem I have is that the in point for the trim will constantly change, so I can say set secondsFromEnd to 0
and set secondsFromStart to (secondsFromEnd-420)
??
 

randomentity

macrumors newbie
Feb 19, 2014
2
0
Completely new to this, I just found myself in serious need of automating some video batches.
I need to save just the last 7 minutes from multiple videos into a watch folder for Episode that will encode/watermark them.
Will this script work for that? what language should I use to set the in and out-point?
would

set secondsFromEnd to 0
set secondsFromStart to (secondsFromEnd-420)

work?
 

VideoBeagle

macrumors 6502a
Aug 17, 2010
822
18
App Q&A testing by request.
Hopefully someone will see this necro thread :)

I'm trying to set up a script that will trim 8.5 seconds off the start of a bunch of mp4's.

I can do that part.

but the export line isn't working.

It doesn't give an error, but it doesn't export anything. Nothing under events says anything is amiss.

The test file is 720p and it doesn't work with the 720p preset, 480, or Audio Only. (the ones I've tested).

I've mainly used the script from Kryten2's script on this page, with some bits I read elsewhere and then pounded into shape to fit Kryten2's code.

Thanks for any help.

(Boy is the lack of any good documentation for quicktime x scripting really annoying).

Code:
set srcFolder to choose folder
set dstFolder to choose folder with prompt "Choose the destination folder for the trimmed files." default location srcFolder

tell application "Finder"
	set fileList to files of folder srcFolder
end tell


repeat with f in fileList
	tell application "QuickTime Player"
		open f
		set documentName to name of f
		delay (0.25)
		tell document 1
						
			trim from 8.5 to duration
		
			export in file ((dstFolder as text) & documentName) using settings preset "720p..."
			(*
			close saving no
			*)
		end tell
	end tell
end repeat
 
Last edited:

kryten2

macrumors 65816
Mar 17, 2012
1,114
99
Belgium
Hopefully someone will see this necro thread :)

I'm trying to set up a script that will trim 8.5 seconds off the start of a bunch of mp4's.

I can do that part.

but the export line isn't working.

What version of OS X and QuickTime are you using? Can you upload a test file or provide a link to one?
 

VideoBeagle

macrumors 6502a
Aug 17, 2010
822
18
App Q&A testing by request.
What version of OS X and QuickTime are you using? Can you upload a test file or provide a link to one?

Quicktime 10.3 (727.4)
Mac OS X 10.9.3

I've tried a couple of files with no success.. This is the main test file (14 mb)
https://dl.dropboxusercontent.com/u/827776/test.mp4

I'm thinking it's some kind of permission issue more than the code, since it's an exact copy of your code.

Accessability Permissions has AppleScript Editor in it.

(Ideally, I'd use the export to audio only, but I figure get the 720 or 480 option working first)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.