Import tracks from command line

songdb.py -i FILENAME
This commit is contained in:
Keith Edmunds 2021-08-21 16:46:37 +01:00
parent 762a41bec6
commit ccbe8fdb1b
2 changed files with 28 additions and 7 deletions

View File

@ -21,12 +21,16 @@ def main():
# Parse command line
p = argparse.ArgumentParser()
p.add_argument('-u', '--update',
action="store_true", dest="update",
default=False, help="Update database")
p.add_argument('-f', '--full-update',
action="store_true", dest="full_update",
default=False, help="Update database")
# Only allow one option to be specified
group = p.add_mutually_exclusive_group()
group.add_argument('-u', '--update',
action="store_true", dest="update",
default=False, help="Update database")
group.add_argument('-f', '--full-update',
action="store_true", dest="full_update",
default=False, help="Update database")
group.add_argument('-i', '--import', dest="fname", help="Input file")
args = p.parse_args()
# Run as required
@ -38,13 +42,18 @@ def main():
INFO("Full update of database")
with Session() as session:
full_update_db(session)
elif args.fname:
fname = os.path.realpath(args.fname)
with Session() as session:
create_track_from_file(session, fname, verbose=True)
else:
INFO("No action specified")
INFO("Finished")
def create_track_from_file(session, path):
def create_track_from_file(session, path, verbose=False):
"""
Create track in database from passed path, or update database entry
if path already in database.
@ -52,13 +61,19 @@ def create_track_from_file(session, path):
Return track.
"""
if verbose:
INFO(f"Importing {path}...")
track = Tracks.get_or_create(session, path)
if verbose:
INFO("Get track info...")
t = get_music_info(path)
track.title = t['title']
track.artist = t['artist']
track.duration = int(round(
t['duration'], Config.MILLISECOND_SIGFIGS) * 1000)
if verbose:
INFO("Parse for start, fade and silence...")
audio = get_audio_segment(path)
track.start_gap = leading_silence(audio)
track.fade_at = round(fade_point(audio) / 1000,
@ -69,6 +84,8 @@ def create_track_from_file(session, path):
session.commit()
if Config.NORMALISE_ON_IMPORT:
if verbose:
INFO("Normalise...")
# Check type
ftype = os.path.splitext(path)[1][1:]
if ftype not in ['mp3', 'flac']:

4
mmimport Executable file
View File

@ -0,0 +1,4 @@
#!/bin/bash
# cd /home/kae/mm
# MYSQL_CONNECT="mysql+mysqldb://musicmuster:musicmuster@localhost/musicmuster_prod" ROOT="/home/kae/music" direnv exec .
app/songdb.py -i "$1"