apply a terminal conversion command to each file in a folder
How do you apply this terminal command ffmpeg -i $f -ac 2 -codec:a libmp3lame -b:a 48k -ar 24000 -write_xing 0 $f_converted;
to every file in a directory? ($f is the filename and $f_converted is the converted name)
Solution 1:
Why AppleScript or Automator when this can be done in just Terminal? Are all the files in the same folder? What type of file is been converted, its extension, e.g .mp4?
If all the files are in the same folder and only the target files, then in Terminal, use to following commands:
cd /path/to/target_files
for f in *.*; do echo ffmpeg -i "$f" -ac 2 -codec:a libmp3lame -b:a 48k -ar 24000 -write_xing 0 "${f%.*}"_converted"${f##*.}"; done
Note: The for in do command has an intentional echo
command so as to run one time to see what the output of the formed command line will look like in order to see if it looks proper. Run it a second time without the echo
command to do the actual processing. If the target file type is e.g. .mp4, you can change *.*
to *.mp4
or *.[mM][pP]4
, where the latter handles both .mp4 and .MP4.
If you are using e.g. Automator with some Finder actions to get the target files to then run this in a Run Shell Script action, then set Pass input: to as arguments and use the following for the command:
for f in "$@"
do
ffmpeg -i "$f" -ac 2 -codec:a libmp3lame -b:a 48k -ar 24000 -write_xing 0 "${f%.*}"_converted"${f##*.}"
done
-
"${f%.*}"
expands to just the filename portion, without the file extension. -
"${f##*.}"
expands to just the extension portion, without the filename portion.