Last active
January 28, 2025 13:43
-
-
Save mrtnvgr/d5ee30a7573cd9156d13a43e03767dbe to your computer and use it in GitHub Desktop.
Convert audio files to 44.1, 16bit wav files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Useful for trackers (Dirtywave M8, Littlegptracker, ...) | |
# Rewrite of https://github.com/mackemint/sample_converter_m8tracker | |
from pydub import AudioSegment, effects | |
from pydub.exceptions import CouldntDecodeError | |
from argparse import ArgumentParser | |
import os, sys | |
parser = ArgumentParser() | |
parser.add_argument("dir") | |
parser.add_argument("--force", action="store_true") | |
args = parser.parse_args() | |
if not os.path.exists(args.dir): | |
print("Specified path does not exist") | |
sys.exit(1) | |
if not args.force: | |
print("WARNING: ~All~ files will be converted to 16bit 44100Hz") | |
print("Input files will be deleted.") | |
if input("Continue (y/n): ") != "y": | |
sys.exit(2) | |
for root, _, files in os.walk(args.dir): | |
for file in files: | |
path = os.path.join(root, file) | |
try: | |
audio = AudioSegment.from_file(path) | |
except: | |
print(f"Skipping: {path}") | |
continue | |
print(f"Converting: {path}") | |
# Don't convert strange sample rates | |
if audio.frame_rate in (44100, 48000): | |
audio = audio.set_frame_rate(44100) | |
# 24 bit -> 16 bit | |
if audio.sample_width == 3: | |
audio = audio.set_sample_width(2) | |
if not path.endswith("wav"): | |
os.remove(path) | |
path = ".".join(path.split(".")[:-1]) + ".wav" | |
audio.export(path, format="wav") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment