Skip to content

Instantly share code, notes, and snippets.

@autumn-mck
Created February 10, 2026 21:24
Show Gist options
  • Select an option

  • Save autumn-mck/085d33c63864b4c88185806086225cc3 to your computer and use it in GitHub Desktop.

Select an option

Save autumn-mck/085d33c63864b4c88185806086225cc3 to your computer and use it in GitHub Desktop.
script to convert .flac files to .opus
#!/usr/bin/env python3
import os
from multiprocessing import Pool, cpu_count
from pathlib import Path
import subprocess
REMOVE = True
BITRATE = "144"
EXTENSIONS = [ ".flac" ]
def main():
to_convert = dir_tree("./")
workers = max(1, cpu_count() - 1)
with Pool(workers) as pool:
pool.map(convert, to_convert)
def is_convertable(file_name: str) -> bool:
return Path(file_name).suffix in EXTENSIONS
def dir_tree(root_path: str):
to_convert: list[Path] = []
for root, _, files in os.walk(root_path):
to_convert += [
Path(os.path.join(root, file))
for file in files
if is_convertable(file)
]
return to_convert
def convert(file: Path):
print(f"Processing {file.stem}")
folder = file.parent.absolute().as_posix()
new_file = f"{folder}/{file.stem}.opus"
subprocess.run(["opusenc", "--bitrate", f"{BITRATE}", "--quiet", file, new_file])
if REMOVE:
file.unlink()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment