Mac: 批量重命名(并重新编号)每n个文件

huangapple go评论63阅读模式
英文:

Mac: Batch rename (and renumber) files every n number of files

问题

(macOS / 终端问题)

我正在渲染动画序列的帧并需要减慢整个过程以获得子帧模糊,并从四个合成帧中合成每个最终帧。这意味着将帧数增加400%。但是输出图像的软件只能按顺序输出文件名,并不能指示将它们分组。

最终将成为2帧的输出如下所示(8帧):

第1帧: 0001.png 0002.png 0003.png 0004.png

第2帧: 0005.png 0006.png 0007.png 0008.png

要在合成中“堆叠”这些帧,我需要将它们重新映射为“层”,我们称之为A、B、C、D:

第1帧: a0001.png b0001.png c0001.png d0001.png

第2帧: a0002.png b0002.png c0002.png d0002.png

然后它们可以很容易地移入文件夹,并从那里进行合成:

<img src="https://i.stack.imgur.com/vM1JB.png" width="135"/>

合成器将根据文件名中的数字“步进”。只要ABCD与正确的帧号对齐,问题就解决了。

我还安装了一个批量文件重命名应用程序,名为Transnomino,如果使用该应用程序更容易进行操作,我也愿意采用这种方式。或者通过使用Automator中的AppleScript,想想也可以。谢谢。

英文:

(macOS / Terminal question)

I am rendering frames of an animation sequence and need to slow down the whole thing to get sub-frame blur and composite each each final frame from four composited frames. This means extending the number of frames by 400%. However the software outputting the images can only output filenames sequentially and can't be instructed to group them.

The output for what will ultimately become 2 frames will look like this (8 frames):

Frame 1: 0001.png 0002.png 0003.png 0004.png

Frame 2: 0005.png 0006.png 0007.png 0008.png

To "stack" these in compositing, I need to remap them into "layers", let's call them A, B, C, D:

Frame 1: a0001.png b0001.png c0001.png d0001.png

Frame 2: a0002.png b0002.png c0002.png d0002.png

Then they could optionally be moved into folders quite easily, and composited from there:

<img src="https://i.stack.imgur.com/vM1JB.png" width="135"/>

The compositor will "step" according to the number in the file names. So as long as ABCD are aligned with the correct corresponding frame numbers, the problem is solved.

I have also installed a batch file renaming app called Transnomino, so if it's easier to do this using that app, I am open to doing it that way too. Or by using AppleScript in Automator, come to think of it. Thanks.

答案1

得分: 2

这个脚本可以完成你所需的两部分操作。脚本做了一些基本的假设,例如输入文件的命名方式与问题中相同,例如"0001.png",但这可以根据需要很容易进行更改。

首先,它在桌面上创建了用于排序的输出文件夹,位于一个名为'frames'的文件夹内。它在桌面上的一个名为'inputs'的文件夹中查找你的源文件。它会要求输入要处理的文件数量,但也可以设置为获取文件夹中的所有文件。

接下来,它创建了一些列表:帧列表(例如0001,0002)、帧文件名列表(0001.png,0002.png)和前缀列表(a,b,c,d)。

然后,它循环遍历每个帧并确定应该在其文件名前添加哪个字母,构建新的文件名并重命名文件(使用System Events)。最后,它循环遍历前缀列表并将所有匹配的文件移动到相应的排序文件夹中(根据第一个字母)。

这是脚本的代码部分,不包含翻译。

英文:

This should do both parts of what you need. The script makes some basic assumptions, e.g. that input files are names as in the question, e.g. "0001.png", but this is straightforward enough to change as required.

First, it creates output sorting folders on the desktop, inside a 'frames' folder. It looks in a folder named 'inputs' on the desktop for your source files. It asks for a count of how many to process but it could be set to just grab every file in the folder.

Next, it creates some lists: of frames (e.g. 0001, 0002), of frame file names (0001.png, 0002.png) and of prefixes (a, b, c, d).

It then cycles through each frame and determines which letter should be prepended to its file name, builds the new file name and renames the file (using System Events). Finally, it cycles through the prefixes and moves all matching files to the appropriate sorting folder (based on the first letter).

-- create frames destination folders
do shell script &quot;cd ~/Desktop ; /bin/mkdir -p frames/{a,b,c,d}&quot;
-- specify input and destination folders
set inputDir to (path to desktop) &amp; &quot;inputs:&quot; as text
set frameDir to (path to desktop) &amp; &quot;frames:&quot; as text

set padX to {&quot;000&quot;, &quot;00&quot;, &quot;0&quot;} -- pads out to 3 zeroes, e.g. 0001
set frameList to {}
set nameList to {}
set inputFrameCt to 4
set inpFr to display dialog &quot;Enter number of input frames&quot; default answer 4
set inputFrameCt to text returned of inpFr as integer

repeat with x from 1 to inputFrameCt
	set y to x as string -- 1 to frame count
	set zx to item (length of y) of padX &amp; y --  4 characters, e.g. 0005
	set end of frameList to zx
	set end of nameList to zx &amp; &quot;.png&quot;
end repeat
frameList -- list of frame numbers, does not include extension
nameList -- list of frame file names, includes extension

set preList to {&quot;d&quot;, &quot;a&quot;, &quot;b&quot;, &quot;c&quot;} -- letter to prepend to file name
set fNameList to {} -- list of resulting file names

tell application &quot;System Events&quot;
	repeat with f in frameList -- cycle through frame numbers
		
		set inFile to inputDir &amp; item f of nameList -- frame&#39;s path and name
		set pre to item (f mod 4 + 1) of preList -- get appropriate prefix
		set preName to pre &amp; item f of nameList -- frame&#39;s resulting file name
		set end of fNameList to (pre &amp; item f of nameList)
		
		set name of file inFile to preName -- rename file, e.g. 0001.png &gt; a0001.png
	end repeat
	
	set letterList to {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;}
	repeat with l in letterList
		set lFiles to (disk items of folder inputDir whose name begins with l)
		move lFiles to folder l of folder frameDir -- move files based on prefix
	end repeat
end tell

NB The script uses x mod 4 to determine which of the letters to use as the prefix. Since every fourth number will result in 0, it adds 1 and moves 'd' to the beginning of the list.

Should you want to change the script in the future to account for a different ratio of frames, you could simply alter the various letter lists and change the mod value to match. It's easy enough to automate this so that you could just enter a single number and everything else would follow.

答案2

得分: 1

使用Transnomino,可以在编号重命名操作中使用“每4个重置”来实现这一点。您可以将它设置为十六进制,并从10开始,让它为每4个文件创建A、B、C、D。

  • 位置:在开头
  • 格式:十六进制
  • 起始:10
  • 每4个重置

这将把以下文件重命名为:

00001.png
00002.png
00003.png
00004.png
00005.png
00006.png
00007.png
00008.png

到:

A00001.png
B00002.png
C00003.png
D00004.png
A00005.png
B00006.png
C00007.png
D00008.png

如果重要的是你得到像这样的文件名:
A00001.png、A00002.png、A00003.png、A00004.png
而不是
A00001.png、A00005.png、A00009.png、A00013.png
你可以修剪最后的数字并使用“重复4”添加额外的编号步骤:

  • 位置:在末尾
  • 格式:十进制
  • 数字:4
  • 重复:4

这将最终得到类似这样的文件名:

A00001.png
B00001.png
C00001.png
D00001.png
A00002.png
B00002.png
C00002.png
D00002.png
英文:

It is possible with Transnomino using "Reset every 4" in the numbering renaming action. You can set it to hexadecimal and start at 10 to let it create A, B, C, D for every 4 files.

  • Location: At Beginning
  • Format: Hexadecimal
  • Start: 10
  • Reset every: 4

This will rename:

00001.png
00002.png
00003.png
00004.png
00005.png
00006.png
00007.png
00008.png

to:

A00001.png
B00002.png
C00003.png
D00004.png
A00005.png
B00006.png
C00007.png
D00008.png

If it is important that you get for example:
A00001.png, A00002.png, A00003.png, A00004.png
instead of
A00001.png, A00005.png, A00009.png, A00013.png
you could trim the last digits of and add an additional numbering step using "Repeat 4":

  • Location: At End
  • Format: Decimal
  • Digits: 4
  • Repeat: 4

This would end up in something like this:

A00001.png
B00001.png
C00001.png
D00001.png
A00002.png
B00002.png
C00002.png
D00002.png

答案3

得分: 0

以下是翻译好的部分:

我找到了一个解决方案。可能还有更好的方法,所以我仍然愿意接受其他答案。但以下是对我有效的方法:

以下方法有效是因为在渲染时我有跳过帧的选项。所以我可以在渲染之前将帧步长设置为4,将输出设置为/a/Image

这将给我Image0001.pngImage0005.png等在文件夹a中。

将起始帧增加1,并将输出路径更改为/b/Image

这将在文件夹b中生成Image0002.pngImage0006.png等。

之后,我能够使用一个叫做NameChanger的应用程序来按顺序重命名文件(这个应用程序允许控制文件顺序,不像Automator)。

Mac: 批量重命名(并重新编号)每n个文件

文件扩展名在这一步被删除,但可以在随后的步骤中轻松添加:

Mac: 批量重命名(并重新编号)每n个文件

编辑: 我后来发现NameChanger的首选项中有一个设置,可以隐藏扩展名。启用此设置后,扩展名不受任何重命名操作的影响。

Mac: 批量重命名(并重新编号)每n个文件

英文:

I have found one solution. There are probably better ways to do this, so I'm still open to other answers. But here's what has worked for me:

The following works because I have the option to skip frames when rendering. So I can set the frame step to 4 before rendering, set the output to /a/Image

It will give me Image0001.png, Image0005.png, etc within folder a

Increment the start frame by 1, and change the output path to /b/Image

It will produce Image0002.png, Image0006.png, etc within folder b

After that, I was able to use an app called NameChanger to rename the files in sequence. (This app allows control over the file order, unlike Automator.)

Mac: 批量重命名(并重新编号)每n个文件

The file extensions get stripped in that step, but can easily be appended in a subsequent step:

Mac: 批量重命名(并重新编号)每n个文件

Edit: I have since discovered that there is a setting in NameChanger's Preferences to hide extensions. With this enabled, extensions are left untouched by any renaming operations.

<img src="https://i.stack.imgur.com/IfDd6.png" width="300"/>

huangapple
  • 本文由 发表于 2023年2月24日 14:49:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553372.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定