1

I have a bunch of mp3 files that I would like to change the extension of into m4bs, in the terminal for audiobooks. I want to convert/change the name of each indivicual mp3. I am pretty sure I can just change the file extensino and it would be work. I looked online and there are a few ways to do this, but I don't understand the steps too well. So I tried in the terminal in Ubuntu:

for i in *.mp3; do mv -- "$i" "ren-$i.m4b"; done

but that just appended .m4b to the end of each .mp3 file.

I also tried:

for file in /path/to; do mv $file $(basename -s mp3 $file)m4b ; done,

but I can“t get it to work because I am not sure what path this is. If I am in the folder itself where the mp3s are located, what is the "/path/to" supposed to refer to? I keep getting the error "mv: cannot stat '/mybook': No such file or directory"

Also I tried to convert them using ffmpeg with:

ls | grep "mp3" | awk '{printf "file |%s|\n", $0}' | sed -e "s/|/\'/g" > list.txt \
  && ffmpeg -f concat -safe 0 -i list.txt -c copy output.mp3 \
  && ffmpeg -i output.mp3 output.m4a \
  && mv output.m4a output.m4b

but that just combined all mp3s into one file and converted it to m4b, where I would like to convert all files individual within the folder to m4b.

Is there a simple command or script I can use?

If I am in the current folder in the terminal? Thanks

guiverc
  • 33,561
Migs123
  • 11

2 Answers2

1

mp3 and m4b use different types of encoding which is independent of the file's extension. m4b is an audiobook file based on the MPEG-4 container format. It is usually compressed with AAC encoding, which is almost the same as a .m4a file. mp3, in full MPEG-1 Audio Layer 3, is a data compression format for encoding digital audio, most commonly music.

To convert multiple files from .mp3 to m4b you need to install ffmpeg with sudo apt install ffmpeg. Run the following commands:

for file in *.mp3; do name=`echo "$file" | cut -d'.' -f1`; echo "$name"; ffmpeg -i "$file" -b:a 320k "${name}.m4a"; done
for file in *.m4a; do 
    mv -- "$file" "${file%.m4a}.m4b"
done

-b:a 320k option in the first command is for output that has bitrate=320kbit/s. You can change the bitrate from 320k (the maximum bitrate) to any other lesser bitrate. m4b files play exactly the same as m4a files even though the extension is different.

To rename the extensions of multiple files from mp3 to m4b open the terminal and change directories using cd to the directory which contains the mp3 files. Then run the following command to change the file extension of all of the mp3 files from .mp3 to .m4b without changing the files' encoding.

for file in *.mp3; do 
    mv -- "$file" "${file%.mp3}.m4b"
done
karel
  • 122,292
  • 133
  • 301
  • 332
0

I found a way that works.

for f in ./*.mp3; do ffmpeg -i "$f" -vn -b:a 96k "${f%.mp3}.m4b"; done

Someone told me that I needed to pass the -vn because for some reason it tries to write mp4 to m4b by default, but obviously that fails and -vn nulls the video.

karel
  • 122,292
  • 133
  • 301
  • 332
Migs123
  • 11