Created
November 29, 2024 11:20
-
-
Save 599316527/ce61e53f9605fe9bbc9cca16a8c9c6b0 to your computer and use it in GitHub Desktop.
Watch folder and convert newly added videos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* | |
Video Minifier Script | |
Monitors a selected folder for new video files and automatically | |
creates minified versions using ffmpeg | |
*) | |
property ffmpegPath : "/opt/homebrew/bin/ffmpeg" | |
property ffmpegParams : "-movflags +faststart" | |
-- Ask user to select the folder to monitor | |
on run | |
set targetFolder to choose folder with prompt "Select the folder to monitor for new video files:" | |
set posixFolderPath to POSIX path of targetFolder | |
-- Start the file monitoring | |
monitorFolder(posixFolderPath) | |
end run | |
-- Main monitoring function | |
on monitorFolder(folderPath) | |
try | |
-- Get initial list of files | |
set initialFiles to list folder folderPath without invisibles | |
-- Continuous monitoring loop | |
repeat | |
delay 5 -- Check every 5 seconds | |
-- Get current list of files | |
set currentFiles to list folder folderPath without invisibles | |
-- Find new files | |
set newFiles to {} | |
repeat with aFile in currentFiles | |
if (aFile is not in initialFiles) and (aFile does not end with "_minified.mp4") then | |
set end of newFiles to aFile | |
end if | |
end repeat | |
log newFiles | |
-- Process new video files | |
repeat with newFile in newFiles | |
set fileExtension to name extension of (info for (folderPath & newFile)) | |
if fileExtension is in {"mp4", "mov", "avi", "mkv", "webm"} then | |
processVideoFile(folderPath, newFile) | |
end if | |
end repeat | |
-- Update initial files list | |
set initialFiles to currentFiles | |
end repeat | |
on error errMsg | |
display dialog "Error monitoring folder: " & errMsg buttons {"OK"} default button "OK" with icon stop | |
end try | |
end monitorFolder | |
-- Function to process video file with ffmpeg | |
on processVideoFile(folderPath, fileName) | |
try | |
-- Construct full file paths | |
set inputFile to (folderPath & fileName) | |
set outputFileName to fileName & "_minified.mp4" | |
set outputFile to (folderPath & outputFileName) | |
-- Construct ffmpeg command | |
set ffmpegCommand to ffmpegPath & " -i " & quoted form of inputFile & " " & ffmpegParams & " " & quoted form of outputFile | |
-- Display processing message | |
-- display notification "Processing " & fileName & " to minified version" with title "Video Minifier" | |
-- Execute ffmpeg command | |
do shell script ffmpegCommand | |
-- Display completion message | |
display notification outputFileName & " created successfully" with title "Video Minifier" | |
on error errMsg | |
display dialog "Error processing " & fileName & ": " & errMsg buttons {"OK"} default button "OK" with icon stop | |
end try | |
end processVideoFile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment