我正在写一个脚本批量转换视频到h.265格式,这使用repeat with来通过所有的视频。它可以同时处理3或4个文件,但我的旧mac会在视频数量达到~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因此,我想使用applescript来实现“队列”功能:在终端窗口中转换有限数量的视频(比如说10个),并监控是否有任何窗口完成执行,如果活动窗口的数量少于10个,则激活一些剩余的任务。
我做了一些搜索,发现系统事件可以判断应用程序是否正在运行,但我不确定如何监控多个窗口,特别是当一些任务完成时,新窗口将被激活。
如有任何建议,欢迎光临。(如果方便的话,也欢迎使用shell脚本)
发布于 2019-01-16 20:38:14
经过几次尝试,我自己成功地完成了这项工作,希望我的答案能对碰巧有类似问题的人有所帮助。
由于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当终端窗口的数量没有达到最大值(在我的代码中设置为5)并且不是所有视频都被转换时,if语句帮助启动新的转换任务。
应该注意的是,在终端脚本末尾的; 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 repeathttps://stackoverflow.com/questions/54191854
复制相似问题