AppleScript + QuickTime — batch trim + create edit + export video

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



AppleScript + QuickTime — batch trim + create edit + export video



I'm looking for some help.



What I'm trying to do is to have AppleScript open a folder of videos, then get QuickTime to randomly trim them (as in the start frame from where it trims) and then have a variable length of the trim itself (say random between 1sec to 2sec as boundaries for the new 'clip'). After trimming and creating the new 'edit' (it would add this new random trimming to all the videos in the folder then add to timeline). QT then needs to export the edit to a new folder.



In summary, trying to make a quick auto-editing app that can just pick random selects from a folder full of videos then save an edit to a new video.



ANY help with this would be hugely appreciated!



I've been trying but to nail avail (I'm fairly new to coding to going around in circles).



Thanks!
Dylan





My advice would be to avoid trying to AppleScript QuickTime as it's pretty buggy and a big headache. Look at the command line tool ffpmeg, which can do that and so much more. Being a command line tool also means you can use it from AppleScript, or from a bash script.
– CJK
Aug 11 at 18:32


ffpmeg





Not very clear at all. You start with a folder of videos, correct? What will the result be? A new video with random clips of random lengths selected in a random order from all the videos? What's the point of that? I suspect @CJK has the correct approach but the problem is unclear.
– Mark Setchell
Aug 11 at 19:56





Hi, sorry — on second read, it was a bit convoluted. What I'm trying to do is essentially create a simple 'auto-editing' system. So you just load up a folder of videos, and then run a script that takes one small clip from each video in the folder and compiles them into a new clip / timeline (then exports to a final compiled video. To get interesting (and different each time) results, having some of the parameters random, for example, the size of each clip could randomise between 1 and 3 seconds. Does that make more sense?
– Dylan Galletly
Aug 12 at 8:35





1 Answer
1



Like @CJK, I would recommend you use bash and ffmpeg as they are both available on macOS and both better known and more widely applicable than Applescript and QuickTime.


bash


ffmpeg



IMHO, as Apple does not ship a package manager, you would be well advised to use homebrew to install, update and remove packages. It is available on the homebrew website.



Once you have that, you can find any package you want with:


brew search packageXYZ



So, you can now install ffmpeg with:


ffmpeg


brew install ffmpeg



Now you would want a bash script that:


bash



That will look something like this, which I would suggest you save in a bash script called $HOME/RandomClips


bash


$HOME/RandomClips


#!/bin/bash

# Set up globbing
shopt -s nullglob nocaseglob

# Clear list of files we are going to concatenate
> list.txt

N=1
# Loop through all ".MOV" files
for f in *.MOV ; do
# Tell user which one we are processing
echo Processing file $f

# Get length of this video in seconds
duration=$(mdls -raw -name kMDItemDurationSeconds "$f")
echo ... Duration: $duration

# Generate a random clip length less than 5 seconds
((seconds=RANDOM%5))
echo ... Clip length: $seconds

# Generate start time
((start=RANDOM%(duration-seconds)))
echo ... Start time: $start

# Extract clip into file called "Clip-1.mov", "Clip-2.mov" etc
clipname="Clip-$N.mov"
echo ... Extracting $clipname
ffmpeg -hide_banner -ss $start -i "$f" -t $seconds -pix_fmt yuv420p "$clipname"

# Add name of this clip to the list of files to concatenate at the end
echo "file $clipname" >> list.txt

# Increment clip counter
((N=N+1))
done

# Now join together all the extracted clips into a single file
ffmpeg -hide_banner -f concat -i list.txt -c copy -pix_fmt yuv420p mergedVideo.mov



Now you need to make that executable, just necessary once, with:


chmod +x $HOME/RandomClips



Then use cd to navigate to a directory of movies:


cd


cd some/place/with/movies



and run the script with:


$HOME/RandomClips



The script is not the most robust or well-tested in the entire world but it should be 90+% good. You may have to resize videos to a fixed size so they all match formats. Likewise with codecs. Probably ask another question if that becomes an issue.



No-one said answers have to be 100% perfect, and as no-one else has suggested anything, this will hopefully get you well on your way.



If you want to debug the script, you can:






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS