Skip to content

Instantly share code, notes, and snippets.

@Rendiere
Created February 4, 2024 21:16
Show Gist options
  • Save Rendiere/d39c2435d89b5cfec4bd2d754694980f to your computer and use it in GitHub Desktop.
Save Rendiere/d39c2435d89b5cfec4bd2d754694980f to your computer and use it in GitHub Desktop.
Flac to MP3 Converter
'''
This script will convert all flac files in the current directory and its subdirectories to mp3 files.
What's nice about this script is that it will also remove the flac files after converting them to mp3.
This script uses ffmpeg to convert the files. You need to have ffmpeg installed on your system.
To install ffmpeg on Ubuntu, run the following command:
```
sudo apt-get install ffmpeg
```
To install ffmpeg on Mac, run the following command:
```
brew install ffmpeg
```
To install ffmpeg on Windows, download the binaries from the official website: https://ffmpeg.org/download.html
After installing ffmpeg, you can run this script to convert flac files to mp3 files.
No other dependencies are required.
Usage: python flac_to_mp3.py
'''
from genericpath import isfile
import os
import pathlib
from glob import glob
def remove_flac_file(fpath):
print('Deleting "{}"'.format(fpath))
os.system('rm "{}"'.format(fpath))
def flac_to_mp3(source_fpath, dest_fpath):
os.system(f'ffmpeg -i "{source_fpath}" -ab 320k -map_metadata 0 -id3v2_version 3 "{dest_fpath}"')
root = pathlib.Path(os.path.dirname (os.path.abspath (__file__)))
if __name__ == '__main__':
for filepath in root.rglob('*.flac'):
dest_fpath = filepath.__str__().replace('.flac', '.mp3')
if os.path.isfile(dest_fpath):
print('{} exists.')
else:
flac_to_mp3(source_fpath=filepath.__str__(), dest_fpath=dest_fpath)
remove_flac_file(filepath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment