我有一个 .dat 文件,其中包含许多 webp 图像。 我想每次在字符串 WEBM 之前分割该文件,并将该部分保存为 .webm 文件。
我很想用自动化程序或苹果脚本来做到这一点,但我无法理解它。也许有人可以帮助我吗?
此脚本将读取文件并将其文本拆分为字符串“WEBM”,从而生成这些字符串(或文本项)的列表。然后,它将循环遍历该列表,并将每个字符串写入桌面上的一个新文件,名称如“image1.webm”。如果具有该名称的文件已经存在,它将覆盖它。我在这里使用了三个短语的示例文本(即fileText)。
set sourceDat to "longtext.dat"
set fPath to (path to desktop as text) & sourceDat -- path to DAT file
--> "MacHD:Users:username:Desktop:longtext.dat"
set fileText to read file fPath as «class utf8»
--> WEBMStack Overflow at WeAreDevelopers World Congress in Berlin;WEBMTemporary policy: Generative AI (e.g., ChatGPT) is banned;WEBMPreview of Search and Question-Asking Powered by GenAI;
set AppleScript's text item delimiters to "WEBM"
set fileBlock to (get rest of text items of fileText)
--> {"Stack Overflow at WeAreDevelopers World Congress in Berlin;", "Temporary policy: Generative AI (e.g., ChatGPT) is banned;", "Preview of Search and Question-Asking Powered by GenAI;"}
repeat with cc from 1 to count of fileBlock
set fileString to "WEBM" & text of item cc of fileBlock -- content of each new webm
--> "WEBMPreview of Search and Question-Asking Powered by GenAI;"
set newPath to ((path to desktop as text) & "image" & cc & ".webm")
set eof of file newPath to 0
try
-- close access file newPath -- only required to treat 'file is already open' error
set np to open for access file newPath with write permission
write fileString to np as «class utf8»
end try
close access np
end repeat
为了了解它的工作原理以及如何修改它,阅读 text item delimiters 和 write 命令 以及读/写套件中的其他命令可能会有所帮助。
我不清楚你是否真的有 WEBP 文件或 WEBM 文件,所以让我们这样做:
然后我们会:
因此,让我们使用 ImageMagick 创建一个包含 20 帧的 WEBP 文件,从石灰绿变为蓝色:
magick -size 180x100 xc:lime xc:blue -morph 18 a.webp
现在用
xxd
检查文件的开头,看到它以 RIFF
开头:
xxd a.webp | head -1
00000000: 5249 4646 8e0a 0000 5745 4250 5650 3858 RIFF....WEBPVP8X
现在将 5 个副本连接在一起以模拟您的 DAT 文件:
cat a.webp a.webp a.webp a.webp a.webp > BigBoy.dat
请注意,如果您想将 100 个文件连接在一起,您将整天打字,因此在这种情况下请使用:
for i in {1..100}; do cat a.webp ; done > BigBoy.dat
现在我们将拆分它,但使用所有
Perl
发行版中包含的 macOS
:
#!/usr/bin/env perl
# Derived from https://superuser.com/a/405495
my $magic = "RIFF";
my $ext = "webp";
my $i = 0;
my $buffer;
# Slurp entire file
{ local $/ = undef; $buffer = <stdin>; }
# Split buffer on every occurence of $magic
my @images = split /${magic}/, $buffer;
# Write list of images to disk
for my $image (@images) {
next if $image eq '';
my $filename = sprintf("image-%04d.%s", $i++, $ext);
open FILE, ">", $filename or die "open $filename: ";
print FILE $magic, $image or die "print $filename: ";
close FILE or die "close $filename: ";
}
如果我们检查得到的内容,我们将看到原始 WEBP 文件和所有提取的帧:
ls *webp
-rw-r--r-- 1 mark staff 2710 8 Aug 13:37 a.webp
-rw-r--r-- 1 mark staff 2710 8 Aug 16:23 image-0000.webp
-rw-r--r-- 1 mark staff 2710 8 Aug 16:23 image-0001.webp
-rw-r--r-- 1 mark staff 2710 8 Aug 16:23 image-0002.webp
-rw-r--r-- 1 mark staff 2710 8 Aug 16:23 image-0003.webp
-rw-r--r-- 1 mark staff 2710 8 Aug 16:23 image-0004.webp
现在我们将对 WEBM 做同样的事情:
# Create one file
magick -size 180x100 xc:lime xc:blue -morph 18 a.webm
# Check its magic number
xxd a.webm | head -1
00000000: 1a45 dfa3 9f42 8681 0142 f781 0142 f281 .E...B...B...B..
并调整代码以寻找新的魔法并使用 WEBM 扩展进行提取:
#!/usr/bin/env perl
# Derived from https://superuser.com/a/405495
my $magic = "\x1a\x45\xdf\xa3";
my $ext = "webm";
my $i = 0;
my $buffer;
# Slurp entire file
{ local $/ = undef; $buffer = <stdin>; }
# Split buffer on every occurence of $magic
my @images = split /${magic}/, $buffer;
# Write list of images to disk
for my $image (@images) {
next if $image eq '';
my $filename = sprintf("image-%04d.%s", $i++, $ext);
open FILE, ">", $filename or die "open $filename: ";
print FILE $magic, $image or die "print $filename: ";
close FILE or die "close $filename: ";
}
如果您不熟悉运行 shell 脚本,您可以将上述脚本保存为
$HOME/splitter
,然后在终端中使用以下命令使其可执行(只需一次):
chmod +x $HOME/splitter
然后运行它,将
BigBoy.dat
作为要处理的文件输入其中:
$HOME/splitter < $HOME/BigBoy.dat
我不确定您是否真的需要将其作为 Applescript 运行,或者您只是不熟悉终端,但如果您确实需要从 Applescript 运行它,您可以这样做:
do shell script "cd && ./splitter < BigBoy.dat"
它将目录更改为您的 HOME 目录,并运行您保存在
splitter
文件中的 BigBoy.dat
脚本,该文件也希望位于您的 HOME 目录中。
请注意,此技术应同样适用于串联 TIFF、JPEG、PNG、GIF 或任何其他图像。
请注意,如果您使用
TextEdit
创建 Perl 脚本,则需要确保保存为纯文本而不是 RTF - 请参阅此处。