The purpose of this script is to play short video clips.Those video clips are dependant on the duration of the video time. Clips could be 1 minutes, 2 minutes, 5 minutes… Once knowing the duration, the clip is to start at position representing 65% of the clip.
Let’s say the clip is 2 min 25 sec which totals up at 145 seconds. The clip is then to start playing at 94 seconds and will terminated 15 seconds later.
At the moment, I’ve been able to do it manually doing the following under Terminal
I’ve written the following script and having difficulties with :
getting the duration time;
getting VLC to use start-time and end-time in the “do shell script command”
set VideoTime to 145
set theFile to POSIX path of "Users:username:Desktop:M2U01507.mp4"
tell application "System Events"
try
display dialog (theFile)
set q to QuickTime file (theFile as text)
display dialog (oneitem)
tell (contents of q) to set {timeScale, theDuration} to {time scale, duration}
tell (theDuration / timeScale) to set {hr, mn, sc} to {it div hours, it mod hours div minutes, it mod hours mod minutes div 1}
set VideoTime to (mn*60)+ sc
display dialog "VideoTime"
end try
end tell
set vStartTime to round of ((65 * VideoTime) / 100) rounding down
set vEndTime to vStartTime + 15
do shell script "open -na /Applications/VLC.app " & quote & (POSIX path of theFile) & quote & " --start-time = " & vStartTime & "--stop-time = " & vEndTime
The following shell script works fine, I just do not know how to get it to work with start-time and end-time.
use AppleScript version "2.5" -- macOS 10.11 or later
use framework "Foundation"
use framework "AVFoundation"
use scripting additions
set aFile to POSIX path of (choose file)
set theURL to current application's |NSURL|'s fileURLWithPath:aFile
set theAsset to current application's AVURLAsset's assetWithURL:theURL
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 vlcPath to POSIX path of (path to application "VLC") & "Contents/MacOS/VLC"
do shell script quoted form of vlcPath & " --start-time=" & theStart & " --stop-time=" & theEnd & space & quoted form of aFile
The last line doesn’t return until the viewer is quit, and I’m not really sure how you’d work around that.
Control never returns to Applescript because it’s waiting for a shell response, but the VLC CLI is an interactive shell program like lftp, Vi, etc. It doesn’t return to the shell until the program is quit, and it never gets quit because the shell command never quits it. Manually quitting VLC quits it.
There are various workarounds, but the easiest is to pass the “–play-and-exit” argument with the VLC call.
set moviePath to "[POSIX path to your movie here]"
set shellLine to "'/Applications/VLC.app/Contents/MacOS/VLC' --play-and-exit --start-time=200 --stop-time=205 " & quoted form of moviePath
do shell script shellLine
I’ve looked at getting VLC to run using tell, end tell
set aFile to POSIX path of (choose file)
tell application "VLC"
OpenURL "file://" & aFile & ""
play
end tell
I’ve looked at the applescript dictionary for VLC. I do not see part of the dictionary start-time and end-time. Does that mean in using a tell, end tell starting and ending at a specific time is not possible when playing the video.
set aFile to POSIX path of (choose file)
tell application "VLC"
OpenURL "file://" & aFile & ""
set current time to 200
play
set endtime to 220
repeat
delay 1
if current time > endtime then
stop
exit repeat
end if
end repeat
end tell