Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jomtones

macrumors newbie
Original poster
Jan 16, 2010
14
1
I've got a lot of files to rename for batch operations (turning png's into video files). I've been using finder to do 'batch rename' and it works great but it's laborious when there are lots of folders.

1657405532030.png

I use finder like this to turn folders full of:
1657405729044.png

Into: pic0001.png, pic0002.png etc. - ready for ffmpeg operations.

After a bunch of image generation, I now have loads of new folders like this full of png files:

1657405401246.png


Is there a way with terminal to rename all the files in one go? ... then I could rename the files and run ffmpeg inside each folder all in one go!

Would love your help - if there are any terminal wizards out there!
 
I personally use a great third party app (Name Mangler) for batch renaming operations, so I don't have any experience using Terminal to do that. However, I'm not sure I understand what's going on in your situation. Doesn't the built-in batch-renaming utility you're using rename the files in place? You seem to indicate that Finder is generating a bunch of new folders and placing the renamed files in them - that doesn't make sense to me. Could you clarify?
 
Out of curiosity: why don’t you run ffmpeg directly on the PNGs? What exactly do you want to acchieve with the ffmpeg run?

Anyway, here are 2 ”quick&dirty” lines for the terminal:

  • to avoid problems with the spaces in the name you can first run rename -n 's//_/g' * on all files. This will change all names in your example. E.g. Pulp SciFi Diffusion(1) 0000.png will be renamed to Pulp_SciFi_Diffusion(1)_0000.png, etc.. Use the command sequence to check on your files, when the result is okay, remove “-n” from it to make the changes.
  • The strip in the next step the Pulp_SciFi_Diffusion(1)_ -part from all files you can use: for file in Pulp_SciFi_Diffusion(1)_*.png ; do mv "$file" "${file#Pulp_SciFi_Diffusion(1)_}" ; done
If this is something you often do I suggest putting this in a shell script and place it in the binary path. And maybe make it prettier by combining both steps, going recursive trough a directory tree and rename all files in all directories, etc.. But I think that’s an exercise for the OP 🤓

nota bene: here is an example on how to integrate and execute shell commands from AppleSript.
 
Thanks all! Just to clarify, the bit that's slowing me down is going into each folder individually, manually, selecting all, 'renaming'.

So I was hoping I could script a way to select all the folders in one go, and rename their contents.

The files (unfortunately) have different names depending on the script that produced them, so I can't just run ffmpeg on them without changing the command each time which is laborious. Here's a couple of example contents:

1657448794548.png


1657448815454.png


So my ideal script would got through all folders (mak1, mak2, etc) and rename *.png as pic1.png, pic2.png, then run ffmpeg in that folder... oh and delete all the pngs! :)
 
still do not unterstand what prevents you running ffmpeg with the original files….

to execute in terminal from the root folder:

for file in `find -name "*.png"`; do newname=$(echo $file | awk -F"-" '{print $3}' | sed 's/ _//g'); mv "$file" $newname; done

I recommend to check this on a test folder structure because I’m prototyping this on my iPad and I probably do not have access to a Mac for a few days.
 
So long as all the folders are open in list view you can just select the entire bunch and rename the contents as per OP.

ETA: It’s essentially just a search & replace so as long as the search string isn’t included in any selected folder tiles it will leave all folders and subfolders untouched while only modifying the contents.
 
Last edited:
Why not just open the image sequence in QT and save as mp4 (or other format)......?
Why do they need to be renamed....🤔
 
So long as all the folders are open in list view you can just select the entire bunch and rename the contents as per OP.

ETA: It’s essentially just a search & replace so as long as the search string isn’t included in any selected folder tiles it will leave all folders and subfolders untouched while only modifying the contents.
Tried this but the numbers don't start at 0000 in each folder (need this for ffmpeg)
 
QuickAction made with Automator
View attachment 2028256
Agh this is so close again, but it doesn't restart numbering with each folder :/ ...

My ffmpeg command needs files that start with a low number. Maybe I could just tweak this somehow to work with any number?

Code:
ffmpeg -r 4 -f image2 -s 512x512 -i pic%05d.png -vcodec libx264 -crf 25  -pix_fmt yuv420p vid.mp4

1657459703149.png
 
cat $(find . -maxdepth 1 -name "*.png" | sort -V) | ffmpeg -r 4 -f image2 -s 512x512 -i -vcodec libx264 -crf 25 -pix_fmt yuv420p vid.mp4

That should probably do directly what you want from the terminal. No renaming of the PNGs needed 🤓. I didn’t check with your complete parameters but using cat $(find . -maxdepth 1 -name "*.png" | sort -V) | ffmpeg -framerate 25 -i - look_what_I_do_on_my_ipad.avi. I am using ffmpeg version n5.0.1-4-ga5ebb3d25e on a 11” iPP.
 
  • Like
Reactions: kryten2
So.... I mean like:

cd mak1
run command
cd mak2
run command ...

till it runs out of folders
 
Hmmm... found this and it looks real promising, but the stars are confusing me:

Code:
for f in mydir/**/*
do
  # operations here
done
 
OK! This does it 😃

Bash:
for D in ./*; do
    if [ -d "$D" ]; then
        cd "$D"
        ffmpeg -r 1 -f image2 -s 512x512 -pattern_type glob -i '*.png' -vcodec libx264 -crf 25  -pix_fmt yuv420p vid.mp4
        cd ..
    fi
done
 
OK ... so now how could I copy all those vid.mp4 files somewhere else, renaming them in the process? ... (I'm thinking vid0001.mp4, vid0002.mp4 etc. - so I don't end up with just one vid.mp4 file)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.