2

I have a bunch of audio files in a directory some are 128Kbps , some are higher.

I want to convert the ones with a bitrate higher than 128Kbps to 128Kbps to save space. I tried writing a shell script to do this:

#!/bin/bash FILES="*.mp3" for F in $FILES do newname=`basename "$F" -smaller.mp3` ffmpeg -i "$F" -acodec libmp3lame -ac 2 -ab 128k -ar 44100 "$newname.mp3" done 

But it also converts files that are already 128Kbps and therefore would take a lot more time to complete.

How could I check if a file`s bitrate is higher than 128Kbps and convert it down to 128Kbps only if so.

1
  • Do you have the file command? Commented Jun 3, 2018 at 14:37

1 Answer 1

4

Using ffprobe

#!/bin/bash for file in *.mp3 do brate=$(ffprobe "$file" |& grep -Eo 'bitrate: [0-9]+' | cut -d' ' -f2) if [[ "$brate" -gt 128 ]] then newname=$(basename "$file" -smaller.mp3) ffmpeg -i "$file" -acodec libmp3lame -ac 2 -ab 128k -ar 44100 "${newname}.mp3" fi done 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.