Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.6: Use AppleScript for precise trimming in QuickTime X Apps
Snow Leopard only hintMost of us know that Quicktime X, included with Snow Leopard, can trim movie and audio files. However, this trim is less than precise, as you cannot specify an exact trim start and stop point. Worse yet, if you want to make 10 minute segments for YouTube, you have to write down the last trim end time, undo the previous trim after uploading, and hope the trim slider will let you select the same trim point to start your next clip. What a pain.

Enter AppleScript -- trim is a scriptable function. Open your movie in Quicktime X, and open the AppleScript Editor in the Application » Utilities folder. In the AppleScript Editor enter this script:
tell application "QuickTime Player"
  activate
  trim document 1 from start_trim to end_trim
end tell
Replace start_trim and end_trim with the time, in seconds, at which you wish to start and end the trim. For example, if you want to create a 10 minute (600 seconds) clip, starting from the beginning of the movie, you would type this for the trim line:
trim document 1 from 0 to 600
After you have typed in your start and stop point for your trim, hit Compile in the ApplesSript Editor window, then hit Run. You will then have a perfectly trimmed movie. You can then change the values to create the second clip, etc.

I am sure an AppleScript guru will figure out a way to automatically separate a movie into 10-minute clips for YouTube. Feel free to post suggestions in the comments!
    •    
  • Currently 3.22 / 5
  You rated: 2 / 5 (18 votes cast)
 
[14,909 views]  

10.6: Use AppleScript for precise trimming in QuickTime X | 8 comments | Create New Account
Click here to return to the '10.6: Use AppleScript for precise trimming in QuickTime X' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.6: Use AppleScript for precise trimming in QuickTime X
Authored by: gplawhorn on Mar 08, '10 07:55:15AM

I'm a happy man! This has my vote for script of the year!



[ Reply to This | # ]
10.6: Use AppleScript for precise trimming in QuickTime X
Authored by: kikjou on Mar 08, '10 10:32:23AM

Nice hint. But if you really want to edit your movie, use the old Quicktime 7. It offers precise frame-by-frame trimming, cutting, copying, pasting and more.



[ Reply to This | # ]
10.6: Use AppleScript for precise trimming in QuickTime X
Authored by: gopes on Mar 08, '10 01:32:43PM

I'm having a little trouble getting the 'trim' command to work. I extended the script to ask the user for start and end points (and convert those to seconds) and then perform the trim. In QuickTime Player itself, the result is fine. After exporting it to iTunes, however, only the video is properly trimmed and the audio simply plays from the beginning. So, for example, exporting any section of a Law & Order episode results in the correct section of video, but the audio starts from the voice introduction at the beginning.

Very useful hint: I know someone who's going to be very happy when I get this done :)



[ Reply to This | # ]
10.6: Use AppleScript for precise trimming in QuickTime X
Authored by: mr. applescript on Mar 08, '10 01:42:52PM
This will segment the front movie to the Movies folder. Be aware that the export queue is threaded, so exports will happen simultaneously (not necessarily a bad thing) unless you add a long delay between repeat loops. Enjoy!
property segment_length : (60 * 1)

tell application "QuickTime Player"
	activate
	if not (exists document 1) then error number -128
	repeat
		display dialog "Enter the base name for the export movie file:" default answer ""
		set the base_name to the text returned of the result
		if the base_name is not "" then exit repeat
	end repeat
	set the movie_length to the duration of document 1
	set the segment_count to the (movie_length div segment_length)
	if (movie_length mod segment_length) is not 0 then set the segment_count to the segment_count + 1
	set start_time to 0
	repeat with i from 1 to the segment_count
		if i is the segment_count then
			set end_time to movie_length
		else
			set end_time to start_time + segment_length
		end if
		set current time of document 1 to start_time
		trim document 1 from start_time to end_time
		set the target_file to ((path to movies folder as string) & base_name & "-" & (i as string) & ".mov")
		export document 1 in file target_file using settings preset "Computer" -- or "iPhone"
		delay 1 -- set to large number of seconds if you don't want the exports to queue
		my reset_movie()
		set start_time to end_time
	end repeat
	set current time of document 1 to 0
end tell

on reset_movie()
	tell application "System Events"
		tell process "QuickTime Player"
			keystroke "z" using command down
		end tell
	end tell
	delay 2
end reset_movie
Edited on Mar 08, '10 01:53:50PM by mr. applescript


[ Reply to This | # ]
10.6: Use AppleScript for precise trimming in QuickTime X
Authored by: philostein on Mar 08, '10 09:13:40PM
Hi, nice tips guys. I had trouble running mr. applescript's script - the time markers work well, but I got 5 identical movies. Perhaps keystroking 'z' using System Events is causing a problem? I've taken the liberty of adjusting the script so it closes and opens Document 1 instead:
mr applescript's QuickTime Player script PhilHints branch.scpt
QuickTime Player occasionally fails to complete the export process though.

I export movies linearly by using this code:

repeat
try -- Make sure file exists first
tell application "System Events" to physical size of file target_file
exit repeat
on error a number b
beep 1
end try
end repeat

repeat -- File only has physical size when completed
tell application "System Events" to set _size to physical size of file target_file
if _size ≠ 0 then exit repeat
delay 2
end repeat

Exports only have physical size after the export is complete.

I've incorporated the above code into the new script.
Cheers, PhilHints

[ Reply to This | # ]
10.6: Use AppleScript for precise trimming in QuickTime X
Authored by: mrmobius on Mar 11, '10 05:51:16AM

Thanks to all who replied to my hint. I love what one can do with Apple Script. I am just not good with scripting or any programming languages.

Mr. Apple Script: I tried your script under 10.6.2 with the same movie I've been using to test this. I ended up with 1 minute segments and a lot of system alert beeps during the process. Also, the script did an export not a save as which took longer.

What I did was a manual process. I would open the movie, enter my start and stop time in the script and run the script. Then I would do a save as and name the movie clip. The save as was very quick, way quicker than an export in my testing. I think the save as is just making a trimmed version with zero decompression of the audio or video. I would then hit Undo, and enter in the next start and stop times to create the next ten minute clip.

It would be amazing to have a script that asked for the movie file, asked for the base name, desired clip length, and then segmented the entire movie with using Save As until the end of the movie. You could call it Bread Slicer or something and sell it for a buck or two. I would be the first in line to purchase.



[ Reply to This | # ]
10.6: Use AppleScript for precise trimming in QuickTime X
Authored by: philostein on Mar 12, '10 04:52:50AM
I think the beeps were mine. 'Save as…' - genius! Ah, the power of constructive criticism. I've re-written the PhilHints version of mr. applescripts' script and saved it in the same place:
mr applescript's QuickTime Player script PhilHints branch.scpt
It contains most of the ideas you were thinking of.

It will overwrite old files if the new ones have the same name. It seems to work on audio files too. Remember, the segmenting code is still mr. applescript's.
Cheers, PhilHints.

[ Reply to This | # ]
10.6: Use AppleScript for precise trimming in QuickTime X
Authored by: lwein on Mar 07, '13 06:04:55AM

Hello there,
I'm new to Applescript and I randomly found your script doing almost exactly what I need.

Actually I'm asked to write a script that runs in command line with four arguments. The name of the file to trim, the start time of the trim, the end time of the trim and the name of the file under which to be saved.

Reusing your "mr applescript's QuickTime Player script PhilHints branch", I feel I'm not too far far from the result.
The problem is that I get a strange error: "QuickTime Player got an error: Can't make file "myMac:Users:myName:Movies:theFile.mov into type integer"

Here is the code at this time:
<code>

on run argv
set fileName to POSIX file (get item 1 of argv)
set start_trim to item 2 of argv
set end_trim to item 3 of argv
set resultName to item 4 of argv

log start_trim
log end_trim

tell application "Finder"
activate
open file fileName using application file "QuickTime Player.app" of folder "Applications" of startup disk
end tell

try
---This runs the app

tell application "QuickTime Player"
try
activate
trim document fileName from start_trim to end_trim
save document fileName in file resultName
close document fileName without saving
display dialog "Movie segmented" buttons {"OK"} default button "OK" giving up after 2

on error a number b
display dialog a
end try
end tell

end try

end run
</code>

Could someone help me please?



[ Reply to This | # ]