Created
February 10, 2026 21:24
-
-
Save autumn-mck/085d33c63864b4c88185806086225cc3 to your computer and use it in GitHub Desktop.
script to convert .flac files to .opus
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 | |
| 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