Skip to content

Instantly share code, notes, and snippets.

@dmikurube
Last active February 3, 2024 11:31
Show Gist options
  • Save dmikurube/41b80b5b9a8128ecc4b36f3bf3c2b09d to your computer and use it in GitHub Desktop.
Save dmikurube/41b80b5b9a8128ecc4b36f3bf3c2b09d to your computer and use it in GitHub Desktop.
Convert FLAC to m4a (ALAC)
#!/usr/bin/env python3
from pathlib import Path
import os
import subprocess
import sys
def main(argv):
if len(argv) < 3:
print("Too few arguments.", file=sys.stderr)
return 1
src = Path(argv[1])
if not src.exists():
print("{} does not exist.".format(src))
return 1
if not src.is_dir():
print("{} is not a directory.".format(src))
return 1
dst = Path(argv[2])
if not dst.exists():
dst.mkdir()
print("mkdir: {}".format(dst))
if not dst.is_dir():
print("{} is not a directory.".format(dst))
return 1
for srcpath in src.glob("*"):
if srcpath.is_dir():
relative = srcpath.relative_to(src)
dstpath = dst.joinpath(relative)
dstpath.mkdir()
for flac in src.glob("**/*.flac"):
flac_relative = flac.relative_to(src)
alac_path = dst.joinpath(flac_relative.with_suffix('.m4a'))
alac_command = ['ffmpeg', '-i', flac, '-vn', '-acodec', 'alac', alac_path]
print(alac_command)
subprocess.check_call(alac_command)
jpeg_path = dst.joinpath(flac_relative.with_suffix('.jpeg'))
jpeg_command = ['ffmpeg', '-i', flac, jpeg_path]
print(jpeg_command)
subprocess.check_call(jpeg_command)
artwork_command = ['AtomicParsley', alac_path, '--artwork', jpeg_path, '--overWrite']
print(artwork_command)
subprocess.check_call(artwork_command)
stat = os.stat(srcpath)
os.utime(path=alac_path, ns=(stat.st_atime_ns, stat.st_mtime_ns))
os.utime(path=jpeg_path, ns=(stat.st_atime_ns, stat.st_mtime_ns))
for srcpath in src.glob("*"):
if srcpath.is_dir():
stat = os.stat(srcpath)
relative = srcpath.relative_to(src)
dstpath = dst.joinpath(relative)
os.utime(path=dstpath, ns=(stat.st_atime_ns, stat.st_mtime_ns))
if __name__ == '__main__':
sys.exit(main(sys.argv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment