Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 18, 2010, 02:58 PM   #1
armadillo22
macrumors newbie
 
Join Date: Nov 2010
Batch trim QT files by set amount...

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
armadillo22 is offline   0 Reply With Quote
Old Nov 18, 2010, 05:20 PM   #2
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: 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.
__________________
Go outside, the graphics are amazing!

Last edited by HiRez; Nov 18, 2010 at 05:25 PM.
HiRez is offline   0 Reply With Quote
Old Nov 18, 2010, 06:03 PM   #3
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: 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
__________________
Go outside, the graphics are amazing!
HiRez is offline   0 Reply With Quote
Old Nov 18, 2010, 07:50 PM   #4
armadillo22
Thread Starter
macrumors newbie
 
Join Date: Nov 2010
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
armadillo22 is offline   0 Reply With Quote
Old Dec 6, 2013, 01:45 PM   #5
poleary2000
macrumors newbie
 
Join Date: May 2010
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.
poleary2000 is offline   0 Reply With Quote
Old Dec 6, 2013, 08:12 PM   #6
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
Quote:
Originally Posted by poleary2000 View Post
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
__________________
Space Corps Directive 34124
kryten2 is offline   0 Reply With Quote
Old Dec 7, 2013, 12:46 PM   #7
poleary2000
macrumors newbie
 
Join Date: May 2010
Quote:
Originally Posted by kryten2 View Post
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 is offline   0 Reply With Quote
Old Dec 7, 2013, 01:02 PM   #8
poleary2000
macrumors newbie
 
Join Date: May 2010
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 by poleary2000; Dec 7, 2013 at 01:11 PM.
poleary2000 is offline   0 Reply With Quote
Old Dec 7, 2013, 01:26 PM   #9
poleary2000
macrumors newbie
 
Join Date: May 2010
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
poleary2000 is offline   0 Reply With Quote
Old Dec 7, 2013, 03:11 PM   #10
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
Quote:
Originally Posted by poleary2000 View Post
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
Attached Thumbnails
Click image for larger version

Name:	Screen Shot 2013-12-07 at 22.07.13.png
Views:	36
Size:	196.9 KB
ID:	450677   Click image for larger version

Name:	Screen Shot 2013-12-07 at 22.07.36.png
Views:	24
Size:	203.2 KB
ID:	450678  
__________________
Space Corps Directive 34124

Last edited by kryten2; Dec 7, 2013 at 04:07 PM.
kryten2 is offline   0 Reply With Quote
Old Dec 7, 2013, 05:06 PM   #11
poleary2000
macrumors newbie
 
Join Date: May 2010
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 is offline   0 Reply With Quote
Old Dec 7, 2013, 09:49 PM   #12
poleary2000
macrumors newbie
 
Join Date: May 2010
I repaired disk permissions via Disk Utility in the Restore Boot. No dice. It still says I do not have permission.
poleary2000 is offline   0 Reply With Quote
Old Dec 7, 2013, 10:39 PM   #13
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
Quote:
Originally Posted by poleary2000 View Post
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.
__________________
Space Corps Directive 34124

Last edited by kryten2; Dec 7, 2013 at 10:58 PM.
kryten2 is offline   0 Reply With Quote
Old Dec 8, 2013, 07:03 AM   #14
poleary2000
macrumors newbie
 
Join Date: May 2010
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 is offline   0 Reply With Quote
Old Dec 8, 2013, 07:08 AM   #15
poleary2000
macrumors newbie
 
Join Date: May 2010
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.
Attached Thumbnails
Click image for larger version

Name:	2013-12-08 08.05.42 am.jpg
Views:	20
Size:	45.0 KB
ID:	450794  
poleary2000 is offline   0 Reply With Quote
Old Dec 8, 2013, 07:24 AM   #16
poleary2000
macrumors newbie
 
Join Date: May 2010
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.
poleary2000 is offline   0 Reply With Quote
Old Dec 8, 2013, 07:49 AM   #17
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: 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.
__________________
Space Corps Directive 34124
kryten2 is offline   0 Reply With Quote
Old Dec 8, 2013, 08:01 AM   #18
poleary2000
macrumors newbie
 
Join Date: May 2010
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 is offline   0 Reply With Quote
Old Dec 12, 2013, 06:36 PM   #19
poleary2000
macrumors newbie
 
Join Date: May 2010
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.
poleary2000 is offline   0 Reply With Quote
Old Feb 19, 2014, 07:02 PM   #20
randomentity
macrumors newbie
 
Join Date: Feb 2014
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 is offline   0 Reply With Quote
Old Feb 19, 2014, 07:07 PM   #21
randomentity
macrumors newbie
 
Join Date: Feb 2014
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?
randomentity is offline   0 Reply With Quote
Old Jun 23, 2014, 02:53 PM   #22
VideoBeagle
macrumors 6502
 
Join Date: Aug 2010
Location: 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
__________________
iPad - 32gb 3g ; 2012 iMac i7, MBP 2.2 GHZ; Power Mac G5 Dual 2GHZ; B&W G3

Last edited by VideoBeagle; Jun 23, 2014 at 04:02 PM.
VideoBeagle is offline   0 Reply With Quote
Old Jun 23, 2014, 06:19 PM   #23
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
Quote:
Originally Posted by VideoBeagle View Post
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?
__________________
Space Corps Directive 34124
kryten2 is offline   0 Reply With Quote
Old Jun 23, 2014, 07:21 PM   #24
VideoBeagle
macrumors 6502
 
Join Date: Aug 2010
Location: App Q&A testing by request.
Quote:
Originally Posted by kryten2 View Post
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)
__________________
iPad - 32gb 3g ; 2012 iMac i7, MBP 2.2 GHZ; Power Mac G5 Dual 2GHZ; B&W G3
VideoBeagle is offline   0 Reply With Quote
Old Jun 24, 2014, 10:07 PM   #25
VideoBeagle
macrumors 6502
 
Join Date: Aug 2010
Location: App Q&A testing by request.
I tried the code in http://forums.macrumors.com/showpost...5&postcount=13 and it runs, but the export doesn't happen, so it's definitely something with that part of it.
__________________
iPad - 32gb 3g ; 2012 iMac i7, MBP 2.2 GHZ; Power Mac G5 Dual 2GHZ; B&W G3
VideoBeagle is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Tags
applescript, quicktime

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Batch Rename files using CSV list diddykiddy Mac Programming 4 Jan 7, 2014 02:53 AM
Set TRIM before a fresh install possible? ls1dreams OS X Mavericks (10.9) 29 Nov 1, 2013 03:49 PM
Batch Renaming Files OSX 10.7.5 ndamaren Mac OS X 10.7 Lion 5 Jun 3, 2013 01:26 PM
Batch organizing files xamp OS X 10.8 Mountain Lion 1 May 28, 2013 10:34 AM
How to Batch Edit PDF files? ryan.1 Mac Applications and Mac App Store 3 Jun 21, 2012 01:02 PM

Forum Jump

All times are GMT -5. The time now is 09:52 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps