Skip to content

Instantly share code, notes, and snippets.

@blackspike
Last active May 26, 2025 17:59
Show Gist options
  • Save blackspike/a33e4b2f61ce4bfe98ae76b14521efc7 to your computer and use it in GitHub Desktop.
Save blackspike/a33e4b2f61ce4bfe98ae76b14521efc7 to your computer and use it in GitHub Desktop.
on open droppedFiles
repeat with theFile in droppedFiles
set posixPath to POSIX path of theFile
set fileName to do shell script "basename " & quoted form of posixPath
set baseName to text 1 thru ((offset of "." & "mp4" in fileName) - 1) of fileName
set dirName to do shell script "dirname " & quoted form of posixPath
-- Append .optim to baseName for output files
set optimBaseName to baseName & ".optim"
-- Set relative filenames
set optimMP4File to optimBaseName & ".mp4"
set optimWebMFile to optimBaseName & ".webm"
set optimAVIFFile to optimBaseName & ".avif"
-- Set full output paths
set optimMP4Path to dirName & "/" & optimMP4File
set optimWebMPath to dirName & "/" & optimWebMFile
set optimAVIFPath to dirName & "/" & optimAVIFFile
-- Path to ffmpeg binary
set ffmpegPath to "/opt/homebrew/bin/ffmpeg"
-- Run ffmpeg for H.264 optimization
do shell script ffmpegPath & " -i " & quoted form of posixPath & " -vcodec h264 -acodec aac -crf 30 -strict -2 " & quoted form of optimMP4Path
-- Run ffmpeg for VP9 WebM conversion
do shell script ffmpegPath & " -i " & quoted form of posixPath & " -vcodec libvpx-vp9 -b:v 1M -acodec libvorbis " & quoted form of optimWebMPath
-- Export first frame as high-quality AVIF
do shell script ffmpegPath & " -i " & quoted form of posixPath & " -frames:v 1 -q:v 0 -still-picture 1 " & quoted form of optimAVIFPath
-- Generate HTML video tag with relative paths
set htmlCode to "<video autoplay loop muted playsinline controls poster=\"" & optimAVIFFile & "\">
<source src=\"" & optimMP4File & "\" type=\"video/mp4\" />
<source src=\"" & optimWebMFile & "\" type=\"video/webm\" />
</video>"
-- Copy HTML to clipboard
set the clipboard to htmlCode
end repeat
end open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment