Skip to content

Instantly share code, notes, and snippets.

@AdrienHorgnies
Created November 20, 2019 14:17
Show Gist options
  • Save AdrienHorgnies/f575f9d797915873950fb5b068f44e81 to your computer and use it in GitHub Desktop.
Save AdrienHorgnies/f575f9d797915873950fb5b068f44e81 to your computer and use it in GitHub Desktop.
base to build an etl music tagger
#!/usr/bin/env python3
import argparse
import re
from pathlib import Path
from subprocess import run
DEFAULT_PATTERN = re.compile(r'^(?P<track>\d+)---(?P<title>.+) - (?P<album>.+)---(?P<video_id>.+)\.opus$')
def extract_meta_from_path(full_path, pattern):
tentative_match = pattern.match(full_path.name)
if hasattr(tentative_match, 'groupdict'):
match = tentative_match.groupdict()
else:
raise ValueError(f'"{path}" does not match "{pattern.pattern}"')
match['album'] = 'Devilman Crybaby (2018) OST'
return match
def write_meta(full_path, meta):
fresh_file_name = f'{meta["album"]} - {meta["track"]} - {meta["title"]}' + full_path.suffix
run([
'ffmpeg', '-i', full_path,
'-metadata', f'title=\"{meta["title"]}\"',
'-metadata', f'track={int(meta["track"])}',
'-metadata', f'album=\"{meta["album"]}\"',
Path('out', fresh_file_name)
])
def main():
p = argparse.ArgumentParser()
p.add_argument('path', type=Path)
args = p.parse_args()
meta = extract_meta_from_path(args.path, DEFAULT_PATTERN)
write_meta(args.path, meta)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment