Creating clips from parents video files

What would you recommend in writing a clips of parent video.

Let say I have a bunch of video files (mp4 format) in a folder. Those videos have a duration of 3, 4 , 5 minutes (not more than 8). I would only like to create small clips for each for those video files. The starting point will be depending on the length of each video file (for example 65% of duration) and finishes 15 to 20 seconds later.

Is there a way to browse through those videos contained in a given folder with applescript and re-create, generate video clips without having to re-encode them.

Those small clips are to be in the same format as there parents.

Thanks again for your advice!

I’ve found and modified the following applescript module to meet my needs.

I need to be able to individually open video files and create a short clip (15 seconds) into a mp4.

For example. taking the file “00319.mp4” and save the video starting to play at 10 seconds and end at 35 seconds into a new file called “cl00319.mp4”.


set startime to 10
set endingtime to 35

--set aFile to "/Users/danielpaquin/Desktop/00319.mp4"
set filepath to "/Users/danielpaquin/Desktop/"
set vfile to "00319.mp4"
set aFile to filepath & vfile

set aFile to POSIX path of (choose file)
display dialog (aFile)

tell application "Finder"
	tell application "QuickTime Player"
		open aFile
		delay (0.25)
		tell document 1
			trim from startime to endingtime
			save in filepath & "cl" & vfile
			close
		end tell
	end tell
end tell

When opening a video file located on an external drive, it does open but not capable to save the file under “cl…”

When the script attempt to open a video file located on my desktop, I am getting the following error message.

I’ve looked at the permission and all have read and write.

I have encoded and edited enough films and I would like to say that in order to properly cut a movie into clips, you should cut them only at special points - where keyframes begin. Their exact location must be accurate to the millisecond. So, it isn’t so simple, and to not alter the source, you should first find that keyframes. And cut at that keyframes. The best way to achieve this, I see GUI scripting of Avidemux.app, which has all you need for your clipping. Exists other apps but this one has efficient functionality, is free, and small, so doesn’t consume computer resources. Avidemux automatically finds keyframes for you.

Thanks, this is informative.

I’ve looked at Avidemux and unfortunately does not seem to be executable from an applescript program. There is no dictionary (Avidemux.sdef).

I may have to create clips of 200 videos per week-ends and those do not have to be perfect. I have to be filming videos of 2 to 6 minutes. From those videos I have to create 15 seconds clips which has to be done with no other human intervention other then submitting a batch job (applescript) to perform the 15 seconds trimming process.

Quicktime Player seems to be able to do this for me. Unfortunately, I have some issues with the script coding.Is this due to the fact I am not trimming where keyframes begin and end? If this is the case they’re not seem to be any find begin and end keyframe with Quicktime Player.

set startime to 65
set endingtime to 80

--set aFile to "/Users/danielpaquin/Desktop/pzv04002.mp4"
set filepath to "/Users/danielpaquin/Desktop/"
set vfile to "pzv04002.mp4"
set aFile to filepath & vfile

set aFile to POSIX path of (choose file)
display dialog (aFile)

tell application "Finder"
	tell application "QuickTime Player"
		open aFile
		delay (0.25)
		tell document 1
			trim from startime to endingtime
			close saving in filepath & "cl" & vfile
		end tell
	end tell
end tell

There seems to be a behaviour I do not understand. I have copies of the same file located at different places. One on an external drive and the other one on the Desktop.

When looking at the file permission it’s read and write.

Why the same file opens at one place and not at the other?

With regards!

Maybe, not. Some applications can autocorrect damaged by user keyframes. I don’t know for QuickTime Player. Now, for opening movies without error. Try this:


set aQuickTimePlayer to path to application "QuickTime Player"
set aMovie to choose file of type {"public.movie"}

tell application "Finder" to open aMovie using aQuickTimePlayer

And, mp4 codec of iTunes is different of usual mp4. So, if you have movies from iTunes, you should first encode movie to appropriate for QuickTime Player mp4 codec.

You could try using AVFoundation. There are some limitations from AppleScript, but you might be able to use all you need:

use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AVFoundation"
use scripting additions

set thePath to POSIX path of (choose file of type {"mp4"})
set theURL to current application's NSURL's fileURLWithPath:thePath
set destPath to (theURL's |path|()'s stringByDeletingPathExtension()'s stringByAppendingString:"-clip")'s stringByAppendingPathExtension:"mp4"
set destURL to current application's NSURL's fileURLWithPath:destPath
set startTime to current application's CMTimeMakeWithSeconds(10, 1)
set durTime to current application's CMTimeMakeWithSeconds(25, 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

Thanks!

As you can see down below, I’ve attempted different way of coding the scrip to work. The open close issue was fixe with the type(“public.movie”).

Using the type(“public.movie”) makes any files to open within quicktime. However, the trim command cause an error.

set startime to 65
set endingtime to 80

set aQuickTimePlayer to path to application "QuickTime Player"
set aMovie to choose file of type {"public.movie"}

set filepath to "/Users/danielpaquin/Desktop/"
set vfile to "pzv04002.mp4"
set aFile to filepath & vfile
--set aMovie to POSIX path of aFile of type("public.movie")

tell application "Finder" to open aMovie using aQuickTimePlayer
delay (0.25)
trim from startime to endingtime
close saving in filepath & "cl" & vfile

Next, I’ve made the trim command to be part of the tell, end tell application Quicktime Player.

set startime to 65
set endingtime to 80

set aQuickTimePlayer to path to application "QuickTime Player"
set aMovie to choose file of type {"public.movie"}

set filepath to "/Users/danielpaquin/Desktop/"
set vfile to "pzv04002.mp4"
set aFile to filepath & vfile
--set aMovie to POSIX path of aFile of type("public.movie")

tell application "Finder" to open aMovie using aQuickTimePlayer
tell application "QuickTime Player"
	delay (0.25)
	tell document 1
		trim aMovie from startime to endingtime
		export in filepath & "cl" & vfile
		close
	end tell
end tell

Next, I’ve tried to replace the choose file command by setting the filename of the video file to be opened by Quicktime.

set startime to 65
set endingtime to 80

set aQuickTimePlayer to path to application "QuickTime Player"
--set aMovie to choose file of type {"public.movie"}

set filepath to "/Users/danielpaquin/Desktop/"
set vfile to "pzv04002.mp4"
set aFile to filepath & vfile
set aMovie to POSIX path of aFile of type("public.movie")

tell application "Finder" to open aMovie using aQuickTimePlayer
delay (0.25)
trim from startime to endingtime
close saving in filepath & "cl" & vfile

OK, I am getting a bit further, the script received from Shane Stanley works. However, I cannot save this a script. What should it be done to be able to saved it.

Also the script as is, cannot be executed within FileMaker.

Instead of getting this script working in filemaker I will change my process. I will attempt to modify the script to get it to open a folder and to process all mp4 files contained in that folder . If there are 20 mp4 file there will be 24 clip of 15 seconds. Trimming start and end will depend on the size of the input. They will be set at 65%.

use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AVFoundation"
use scripting additions

set thePath to POSIX path of (choose file of type {"mp4"})
set theURL to current application's NSURL's fileURLWithPath:thePath
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 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 theStart + 15
if theEnd > theDuration then error "Clip is less than 15 seconds"


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

Plain AppleScript version. Edit as you need:


set {startime, endingtime} to {0.15, 5.25}
set aMovie to choose file of type {"mp4"}

tell application "QuickTime Player"
	activate
	open aMovie
	tell document 1
		trim from startime to endingtime
		save
		close
	end tell
end tell

NOTE: QuickTime Player saves documents as QT compositions, so, to save in, you should specify as QT composition

Here is my script allowing to browse through mp4 files on a given folder.

At the moment I am getting the following error

use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AVFoundation"
use scripting additions

set MP4DestinationFolder to (choose folder)
tell application "Finder"
	set mp4Files to files in folder MP4DestinationFolder whose name extension is "mp4"
	
	if (count of mp4Files) is equal to 0 then
		display dialog ("Aucun fichier mp4 dans le dossier " & MP4DestinationFolder)
		return
	end if
	repeat with fvideo in mp4Files
		my createclipvideo(fvideo)
		
	end repeat
end tell

set countofmp4 to (count of mp4Files) as string
display dialog (countofmp4 & " fichier vidéo de format mp4 trouver dans " & MP4DestinationFolder)


on createclipvideo(fvideo)
	set thePath to POSIX path of (fvideo)
	set theURL to current application's NSURL's fileURLWithPath:thePath
	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 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 theStart + 15
	if theEnd > theDuration then error "Clip is less than 15 seconds"
	
	
	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

Just as it says.

You’re using Finder file references, rather than files or aliases. Try this:

   set mp4Files to (files in folder MP4DestinationFolder whose name extension is "mp4") as alias list