0

I'm writing a script to batch convert videos into h.265 format which uses repeat with to go through all videos. It works great for 3 or 4 files at the same time, but my old mac restarts when the number of videos reaches to ~50.

repeat with videofile in video_list
    set filePath to POSIX path of videofile
    set filePath to esc_space(filePath)
    set [folderPath, filename] to split_path_name(filePath)

    tell application "Terminal"
        activate
        do script with command "ffmpeg -hide_banner -i " & filePath & " -vcodec libx265 -tag:v hvc1 " & folderPath & filename & "_hevc.mp4; mv " & filePath & " ~/.Trash"
    end tell
end repeat

Therefore, I want to use applescript to achieve the "Queue" functionality: converting limited number of videos in terminal windows (let's say 10) and monitoring if any windows finished executing, activate some remaining tasks if number of active windows is less than 10.

I did some searches and found system event can tell if apps are running, but I'm not sure how to monitoring multiple windows, especially new windows would be activated when some tasks finished.

Any suggestions is appreciated. (shell script is also welcome if it's convenient)

nochenon
  • 326
  • 2
  • 12

1 Answers1

0

After some attempts I successfully did it on my own, hope my answer would help someone who happens to have similar questions.

Since the list in AppleScript cannot be modified (correct me if I'm wrong), I have to use index to loop through my videos instead of getting the 1st item and then remove it from list:

set next_video_index to 1

The following infinite repeat loop is for monitoring the number of active terminal windows, which is counted by System Events. It is not the best solution, because System Events counts all windows including those manually opened by the user.

repeat while true
    tell application "System Events"
        tell application "Terminal"
            set window_count to (count of windows)
        end tell
    end tell

if statement helps to start new converting task when the number of terminal windows doesn't reach the maximum (set to 5 in my code) and not all videos are converted.

It should be noted that ; exit at the end of terminal script, it ensures the finished task window is not messing up the window counts, but you need to change terminal preference first, see this link: OSX - How to auto Close Terminal window after the "exit" command executed.

    set task_not_finished to (next_video_index ≤ length of video_list)

    if (window_count < 5) and task_not_finished then
        set filePath to POSIX path of item next_video_index in video_list
        set filePath to esc_space(filePath)
        set [folderPath, filename] to split_path_name(filePath)
        set next_video_index to next_video_index + 1

        tell application "Terminal"
            activate
            do script with command "ffmpeg -hide_banner -i " & filePath & " -vcodec libx265 -tag:v hvc1 " & folderPath & filename & "_hevc.mp4; mv " & filePath & " ~/.Trash; exit"
        end tell
    end if

When the last video is converting, it's time to end the repeat loop.

    if not task_not_finished then exit repeat
    delay 1
end repeat
nochenon
  • 326
  • 2
  • 12