nix

configuration files that power my machines
Log | Files | Refs | README | LICENSE

commit 552263030b6f801781c7526173f051f68ab43801
parent 1406ed6def3464fed2e5ebe58b571166ddf0b395
Author: mtmn <miro@haravara.org>
Date:   Thu, 16 Apr 2026 11:57:17 +0200

bin: add aif2flac

Diffstat:
Amodules/mixins/dotfiles/bin/aif2flac | 121+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 121 insertions(+), 0 deletions(-)

diff --git a/modules/mixins/dotfiles/bin/aif2flac b/modules/mixins/dotfiles/bin/aif2flac @@ -0,0 +1,121 @@ +#!/usr/bin/env python3 +import subprocess +import argparse +from pathlib import Path + + +def check_artwork(file_path): + result = subprocess.run( + [ + "ffprobe", + "-v", "error", + "-select_streams", "v", + "-show_entries", "stream=codec_type", + "-of", "default=noprint_wrappers=1:nokey=1", + str(file_path), + ], + capture_output=True, + text=True, + ) + return "video" in result.stdout + + +def convert_aif_to_flac(input_path, delete_original=True): + output_path = input_path.with_suffix(".flac") + + if output_path.exists(): + print(f"Skipping {input_path} - {output_path} already exists") + return True + + print(f"Converting {input_path} to {output_path}") + + try: + subprocess.run( + [ + "ffmpeg", + "-i", str(input_path), + "-c:a", "flac", + "-compression_level", "8", + "-map", "0:a", + "-map", "0:v?", + "-map_metadata", "0", + str(output_path), + ], + check=True, + capture_output=True, + ) + + had_artwork = check_artwork(input_path) + if had_artwork: + if check_artwork(output_path): + print(f"Successfully converted {input_path} (artwork preserved)") + else: + print(f"Warning: Artwork may not have transferred for {input_path}") + else: + print(f"Successfully converted {input_path}") + + if delete_original: + try: + input_path.unlink() + print(f"Deleted original file: {input_path}") + except Exception as e: + print(f"Warning: Could not delete original file {input_path}: {e}") + + return True + + except subprocess.CalledProcessError as e: + print(f"Error converting {input_path}:") + print(f"ffmpeg error: {e.stderr.decode()}") + return False + except Exception as e: + print(f"Unexpected error converting {input_path}: {e}") + return False + + +def main(): + parser = argparse.ArgumentParser( + description="Convert AIF/AIFF files to FLAC format recursively." + ) + parser.add_argument("folder_path", help="Path to the folder containing AIF files") + parser.add_argument( + "--keep-original", + action="store_true", + help="Keep original AIF files after conversion (default: delete)", + ) + args = parser.parse_args() + + start_dir = Path(args.folder_path).resolve() + if not start_dir.exists(): + print(f"Error: The directory '{start_dir}' does not exist.") + return + if not start_dir.is_dir(): + print(f"Error: '{start_dir}' is not a directory.") + return + + aif_files = list(start_dir.rglob("*.aif")) + list(start_dir.rglob("*.aiff")) + + converted = 0 + deleted = 0 + errors = 0 + + for aif_file in aif_files: + try: + success = convert_aif_to_flac(aif_file, not args.keep_original) + if success: + converted += 1 + if not args.keep_original and not aif_file.exists(): + deleted += 1 + except Exception as e: + print(f"Failed to process {aif_file}: {e}") + errors += 1 + + print("\nConversion Complete!") + print(f"Successfully converted: {converted} files") + if not args.keep_original: + print(f"Original files deleted: {deleted} files") + if errors > 0: + print(f"Errors encountered: {errors} files") + + +if __name__ == "__main__": + main()