我正在编写一个脚本,以将视频批量转换为h.265格式,该格式使用repeat with
浏览所有视频。它可以同时很好地处理3个或4个文件,但是当视频数量达到约50个时,我的旧Mac重新启动。
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
因此,我想使用applescript来实现“队列”功能:在终端窗口中转换有限数量的视频(比如说10个),并监视是否有任何窗口完成执行,如果活动窗口的数量较少,则激活一些剩余的任务大于10。
我进行了一些搜索,发现系统事件可以判断应用程序是否正在运行,但是我不确定如何监视多个窗口,尤其是在某些任务完成后会激活新窗口。
任何建议都值得赞赏。 (如果方便,也欢迎使用shell脚本)
答案 0 :(得分:0)
经过几次尝试,我自己成功完成了任务,希望我的回答对碰到类似问题的人有所帮助。
由于无法修改AppleScript中的列表(如果我输入错了,请纠正我),我必须使用索引来遍历我的视频,而不是获取第一个项目,然后将其从列表中删除:
set next_video_index to 1
以下无限repeat
循环用于监视活动终端窗口的数量,该数量由System Events
计算。这不是最佳解决方案,因为System Events
会计算所有窗口,包括用户手动打开的窗口。
repeat while true
tell application "System Events"
tell application "Terminal"
set window_count to (count of windows)
end tell
end tell
if语句有助于在终端窗口的数量未达到最大值(在我的代码中设置为5)并且并非所有视频都被转换的情况下启动新的转换任务。
应注意,终端脚本末尾的; exit
可确保完成的任务窗口不会弄乱窗口计数,但是您需要先更改终端首选项,请参见以下链接:
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
当最后一个视频正在转换时,是时候结束重复循环了。
if not task_not_finished then exit repeat
delay 1
end repeat