Skip to content

Instantly share code, notes, and snippets.

@fardjad
Last active February 28, 2024 16:37
Show Gist options
  • Save fardjad/4f31a3eafc7fdd0fd34a to your computer and use it in GitHub Desktop.
Save fardjad/4f31a3eafc7fdd0fd34a to your computer and use it in GitHub Desktop.
[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

@RatCheese1608
Copy link

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?

@RatCheese1608
Copy link

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.

@teridon
Copy link

teridon commented Dec 17, 2023

This command assumes there are no periods (.) in the filenames other than for the extension. Bad assumpition!
Replace:
"out/${f%%.*}.mp3"
with
"out/${f%.*m4a}.mp3"

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