Ayyappadas V
Ayyappadas V

Reputation: 15

Renaming the output file name from ffmpeg when using find and xargs

I am trying to rename the output files that are coming out of ffmpeg processes that I have spawned using find and xargs. I have a directory of files that I would like to convert to a certain format and all those files should ultimately be named .mov. So I am filtering out .mp4 & .mov files and then using xargs to pass it on to ffmpeg.

Suppose there are files such as abc.mp4, abcd.mp4, abcde.mp3, bcd.mov etc in the current working dir and then I am doing something like this to filter out just the mp4 and mov files and pass it on to ffmpeg for encoding:

find . -type f -name "*.mp4" -o -name "*.mov" | xargs -n 1 -p 5 -I '{}' ffmpeg -i '{}' ....... $PWD/{}"_h264.mov"

This will do what I want ffmpeg to do but it will name the files like

abc.mp4_h264.mov,abcd.mp4_h264.mov,bcd.mov_h264.mov

What I would ideally like to achieve is to name the output files as

abc_h264.mov,abcd_h264.mov,bcd_h264.mov

I am not getting any sparks in my brain on how to do that at the place where I specify the output files of ffmpeg. I know that I can call basename command using xargs like this

find . -type f -name "*.mp4" -o -name "*.mov" | xargs -n 1 basename | rev | cut -d . -f 2 | rev

and extract the filename without the extension but if i use that then how can I pass it on to ffmpeg as the input file because it would have lost the extension when it comes out of xargs and ffmpeg will throw an error.

Could anyone help me achieve this in one go where I can specify what 'basename' is doing but where I am specifying the output file name of ffmpeg please?

Thanks very much for any help that you can provide. Really appreciate the community.

Upvotes: 1

Views: 4552

Answers (1)

tshiono
tshiono

Reputation: 22012

Would you please try the following:

find . -type f -name "*.mp4" -o -name "*.mov" | xargs -I {} -n 1 -P 5 bash -c 'org="{}"; new="$PWD/${org%.*}_h264.mov"; ffmpeg -i "$org" {options} "$new"'

But it is not efficient for spawning bash command as a subprocess multiple times. If you do not have a specific reason to use find and xargs, it will be simpler to say:

for org in *.mp4 *.mov; do
    new="$PWD/${org%.*}_h264.mov"
    ffmpeg -i "$org" {options} "$new"
done

Upvotes: 1

Related Questions