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
  • 28,623
  • 5
  • 46
  • 74
Migs123
  • 11
  • 1
  • Did you look at using `rename` ? (though scanning https://askubuntu.com/questions/1177838/how-to-remove-end-of-file-names may allow you to understand what you were missing with some of your tried examples) or a prior answer of mine using `rename` (https://askubuntu.com/questions/999181/delete-first-words-from-all-folder-names-in-directory/999183#999183) – guiverc Mar 23 '23 at 01:44

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
  • 110,292
  • 102
  • 269
  • 299
  • Thanks - I did that, but I got some errors and it just created a blank file right next to each mp3 file. Errors - Could not find tag for codec h264 in stream #0, codec not currently supported in container Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument Error initializing output stream 0:1 -- [aac @ 0x55e9b3e01e80] Qavg: nan Conversion failed! and Frame rate very high for a muxer not efficiently supporting it. Please consider specifying a lower framerate, a different muxer or -vsync 2 – Migs123 Mar 23 '23 at 05:04
  • You're missing some media codecs. Try `sudo apt install lame ubuntu-restricted-extras` to install the mp3 and m4a codecs. Run the commands again and show me the error message if there is one so that I can see what's missing. The framerate is not high. There's no problem with the framerate. I think you're missing codecs. – karel Mar 23 '23 at 05:24
  • Here is the error message now: "Could not find tag for codec h264 in stream #0, codec not currently supported in container Could not write header for output file #0 (incorrect codec parameters ?): Invalid argument Error initializing output stream 0:1 -- [aac @ 0x56453e929e80] Qavg: nan Conversion failed!" I did install lame. (I had ubuntu-restricted-extras installed already.) Maybe I am missing other codecs? – Migs123 Mar 23 '23 at 05:53
  • `sudo apt install libx264-163` – karel Mar 23 '23 at 06:06
  • already installed – Migs123 Mar 23 '23 at 07:17
  • But it converted perfectly when I used the 3rd method, "ls | grep "mp3" | awk '{printf "file..." it worked but it just converted it into one m4b. I just don´t get it. Could we use that code and just do it one by one instead of combining all the mp3s and converting the? Thanks – Migs123 Mar 24 '23 at 07:35
  • So how do I do that? So there isn´t a way to do a loop with that method so it will convert all of them one at a time? And just changing the extension will make it unplayable right? Thanks for your help – Migs123 Mar 24 '23 at 08:52
  • Your command without a loop is `ffmpeg -i input-filename.mp3 -b:a 320k output-filename.m4a` – karel Mar 24 '23 at 09:47
  • Oh - that is maybe why it wasn´t working because my mp3 is 96 kbps. So is that why it isn´t working? I just want to convert it to 96 kbps. Also I enter that code as it is should I have put in specific filenames? – Migs123 Mar 24 '23 at 17:11
  • The `-b:a 96k` option converts the bitrate of the output into 96kbit/s. I put the usage for `-b:a XXXk` into my answer in the first method. – karel Mar 24 '23 at 23:30
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
  • 110,292
  • 102
  • 269
  • 299
Migs123
  • 11
  • 1