Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[How to Convert m4a Files to mp3, Preserving Bitrate and ID3 Tags Using ffmpeg] #ffmpeg #bash

How to convert m4a files to mp3, preserving bitrate and id3 tags using ffmpeg

Prerequisites

  1. ffmpeg
  2. cygwin (on windows)

Commands

cd /path/to/m4a/files
mkdir out
for f in *.m4a; do 
    ffmpeg -i "$f" -ab `ffmpeg -i "$f" 2>&1 | grep Audio | awk -F', ' '{print $5}' | cut -d' ' -f1`k -map_metadata 0 -id3v2_version 3 -write_id3v1 1 "out/${f%%.*}.mp3"
done

References

http://blog.kdecherf.com/2012/01/14/ffmpeg-converting-m4a-files-to-mp3-with-the-same-bitrate http://jonhall.info/tips_and_tricks/id3_tags_on_windows_using_ffmpeg

@timusus
Copy link

timusus commented Aug 25, 2019

This appears to be missing a semicolon, on line 4.

for f in *.m4a; do 
    ffmpeg -i "$f" -ab `ffmpeg -i "$f" 2>&1 | grep Audio | awk -F', ' '{print $5}' | cut -d' ' -f1`k -map_metadata 0 -id3v2_version 3 -write_id3v1 1 "out/${f%%.*}.mp3";
done

@RayCist1608
Copy link

RayCist1608 commented Mar 26, 2021

I'm going to explain how this work:

  1. It changes the current working directory (folder) to where your m4a are located.
  2. It creates a directory named "out" in the current directory
  3. It iterates for every m4a file in the folder and it will a) check for the original bitrate, b) copies the id3 tags.
  4. After getting the data of one file, it starts to encode the audio to mp3

@bkr1969
Copy link

bkr1969 commented Nov 27, 2021

Can this be done on a mac?

@RayCist1608
Copy link

RayCist1608 commented Nov 27, 2021

I'm not sure if the commands used in Mac is 100% the same in linux, but perhaps with correct dependencies it is possible on Mac, and also possible in windows with some command changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment