Converting 216 Songs with Python & FFmpeg (m4a to mp3)
Last week, I was summoned to my uncle’s house to solve a very serious tech problem.
He had a pen drive full of old (the gold) songs and wanted to play them on his very old music player. The problem?
All of the songs, 216 to be exact, were in the m4a file format. And the music player immediately surrendered.
That’s when I realized:
“This is a perfect use case for Python + FFmpeg.”
Requirements
To solve this, we need just two things:
- Python — to loop through files and automate the process
- FFmpeg — to handle the actual audio conversion
Installing Python and FFmpeg
Install Python
Installing Python is straightforward. You can download it from the official site:
Make sure to check “Add Python to PATH” during installation (especially on Windows).
Install FFmpeg
FFmpeg installation can be a bit tricky on Windows.
- Windows:
Download FFmpeg from https://ffmpeg.org/download.html
Extract it and add thebinfolder to your systemPATH.
Quick Guide: https://www.youtube.com/watch?v=eRZRXpzZfM4 - macOS (via Homebrew):
brew install ffmpegConfirm the Installation
Once installed, confirm everything is working.
Get Armaan Chaand’s stories in your inbox
Join Medium for free to get updates from this writer.
Check Python (Windows & macOS)
python --versionCheck FFmpeg (Windows & macOS)
ffmpeg -versionIf both commands return version info, you’re good to go ✅
Writing the Script
The Goal
- Read all
.m4afiles from a folder - Convert them to
.mp3 - Save the converted files in a different folder
How It Works
- Python scans the input folder and loops over files
- FFmpeg converts each file
- Python saves the output to another directory
With this in mind, I asked an LLM (ChatGPT works too) to generate a Python script, and here’s what I got:
import subprocess
from pathlib import Path
# -------- CONFIG --------
INPUT_DIR = Path("input_m4a")
OUTPUT_DIR = Path("output_mp3")
BITRATE = "192k" # mp3 quality
# ------------------------
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
m4a_files = list(INPUT_DIR.glob("*.m4a"))
if not m4a_files:
print("No .m4a files found.")
exit(0)
for m4a_file in m4a_files:
mp3_file = OUTPUT_DIR / (m4a_file.stem + ".mp3")
if mp3_file.exists():
print(f"Skipping (already exists): {mp3_file.name}")
continue
cmd = [
"ffmpeg",
"-y", # overwrite without asking
"-i", str(m4a_file), # input file
"-vn", # no video
"-ab", BITRATE, # audio bitrate
str(mp3_file)
]
try:
subprocess.run(
cmd,
stdout=subprocess.DEVNULL,
stderr=subprocess.STDOUT,
check=True
)
print(f"Converted: {m4a_file.name} → {mp3_file.name}")
except subprocess.CalledProcessError:
print(f"❌ Failed to convert: {m4a_file.name}")
print("✅ Conversion complete.")Folder Structure
The script assumes the following structure:
project/
│
├── input_m4a/
│ ├── audio1.m4a
│ ├── audio2.m4a
│
├── output_mp3/
│ ├── audio1.mp3
│ ├── audio2.mp3
│
└── convert.py- Put all
.m4afiles insideinput_m4a - Converted
.mp3files will appear inoutput_mp3
Running the Script
Open a terminal (Command Prompt or PowerShell on Windows) at the project root ( /project/ level), then run:
python convert.pyThe script will start converting files one by one.
On average, each file (around 3–5 MB) took 4–5 seconds on my system.
Conclusion
This was a very fun programming task for me and I enjoy doing these things once in a while.
And yes… don’t forget to delete the original m4a files before handing the pen drive back to your uncle.





