如何用Automator或AppleScript按WEBM分割.dat文件为.webm文件
用AppleScript+Shell命令拆分.dat文件为.webm文件
方案一:独立AppleScript脚本
直接用AppleScript调用shell工具处理二进制文件分割,操作步骤如下:
- 打开「脚本编辑器」(Launchpad → 其他文件夹)
- 复制以下代码到编辑器中:
set inputFile to choose file with prompt "选择要分割的.dat文件" set inputPath to POSIX path of inputFile set outputFolder to choose folder with prompt "选择保存.webm文件的文件夹" set outputPath to POSIX path of outputFolder -- 获取所有"WEBM"字符串的字节偏移位置 set offsetList to do shell script "grep -aob 'WEBM' " & quoted form of inputPath & " | cut -d: -f1" set offsets to paragraphs of offsetList if offsets is {} then display alert "错误" message "文件中未找到WEBM标记" return end if -- 分割每个片段:从上次分割点到当前WEBM之前的内容 set prevOffset to 0 repeat with i from 1 to count of offsets set currentOffset to item i of offsets as integer set outputFileName to outputPath & "segment_" & (text -2 thru -1 of ("0" & i)) & ".webm" do shell script "dd if=" & quoted form of inputPath & " of=" & quoted form of outputFileName & " bs=1 skip=" & prevOffset & " count=" & (currentOffset - prevOffset) set prevOffset to currentOffset end repeat -- 处理最后一段:从最后一个WEBM到文件末尾 set fileSize to do shell script "stat -f %z " & quoted form of inputPath as integer set lastOutputFileName to outputPath & "segment_" & (text -2 thru -1 of ("0" & (count of offsets + 1))) & ".webm" do shell script "dd if=" & quoted form of inputPath & " of=" & quoted form of lastOutputFileName & " bs=1 skip=" & prevOffset & " count=" & (fileSize - prevOffset) display alert "完成" message "已分割出" & (count of offsets + 1) & "个.webm文件"
- 点击编辑器顶部的「运行」按钮,按提示选择目标.dat文件和输出文件夹即可。
方案二:Automator快速操作(右键直接处理)
如果需要右键快速处理文件,可创建Automator快速操作:
- 打开「Automator」,新建「快速操作」
- 顶部设置:「工作流收到当前」选择「文件或文件夹」,「在」选择「访达」
- 添加「运行AppleScript」动作,替换为以下代码:
on run {input, parameters} set inputFile to item 1 of input set inputPath to POSIX path of inputFile set outputFolder to choose folder with prompt "选择保存.webm文件的文件夹" set outputPath to POSIX path of outputFolder set offsetList to do shell script "grep -aob 'WEBM' " & quoted form of inputPath & " | cut -d: -f1" set offsets to paragraphs of offsetList if offsets is {} then display alert "错误" message "文件中未找到WEBM标记" return input end if set prevOffset to 0 repeat with i from 1 to count of offsets set currentOffset to item i of offsets as integer set outputFileName to outputPath & "segment_" & (text -2 thru -1 of ("0" & i)) & ".webm" do shell script "dd if=" & quoted form of inputPath & " of=" & quoted form of outputFileName & " bs=1 skip=" & prevOffset & " count=" & (currentOffset - prevOffset) set prevOffset to currentOffset end repeat set fileSize to do shell script "stat -f %z " & quoted form of inputPath as integer set lastOutputFileName to outputPath & "segment_" & (text -2 thru -1 of ("0" & (count of offsets + 1))) & ".webm" do shell script "dd if=" & quoted form of inputPath & " of=" & quoted form of lastOutputFileName & " bs=1 skip=" & prevOffset & " count=" & (fileSize - prevOffset) display alert "完成" message "已分割出" & (count of offsets + 1) & "个.webm文件" return input end run
- 保存快速操作(比如命名为「拆分dat为webm」),之后在访达中右键点击.dat文件,即可在「快速操作」菜单中找到该功能。
注意事项
- 如果.dat文件体积很大,
dd命令可能会较慢(因为bs=1是按字节读取),可以尝试将bs改为更大的值(比如bs=4096),但需确保count计算准确; - 代码中
grep -aob的-a参数用于强制将二进制文件视为文本文件处理,确保能找到"WEBM"标记。
内容的提问来源于stack exchange,提问作者Maximilian Becker
相关产品推荐
相关产品推荐

