Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pipe the filename into an ffmpeg command?

I would like to run this command in the terminal: ffmpeg -i <input-file> -ac 2 -codec:a libmp3lame -b:a 48k -ar 16000 <output-file.mp3> on every mp3 file in a folder.

The input and output could be the same (an overwrite), but if this is not possible, if there was a way to take the filename and append _converted maybe?

I'm not a bash expert, but I know that I need to pipe the results of an ls command into the ffmpeg command using a variable perhaps?

like image 695
Lee Probert Avatar asked Dec 02 '25 23:12

Lee Probert


1 Answers

Could you please try following, since I don't have ffpmeg command so couldn't test it. This should save output into same Input_file itself, better to test it on a test folder and once happy with results could run on actual folder.

for file in *.mp3
do
   ffmpeg -i "$file" -ac 2 -codec:a libmp3lame -b:a 48k -ar 16000 "temp" && mv "temp" "$file"
done

OR as per OP you want to take output _converted string into output file name then try following.

for file in *.mp3
do
   output_file="${file}_converted"
   ffmpeg -i "$file" -ac 2 -codec:a libmp3lame -b:a 48k -ar 16000 "$output_file"
done

OR as per @Gordon Davisson sir's comment use following.

for file in *.mp3
do
   output_file="${file%.mp3}_converted.mp3"
   ffmpeg -i "$file" -ac 2 -codec:a libmp3lame -b:a 48k -ar 16000 "$output_file"
done
like image 139
RavinderSingh13 Avatar answered Dec 04 '25 15:12

RavinderSingh13