From 8d346e8942528f3210c248b9676d4cc8f413e672 Mon Sep 17 00:00:00 2001 From: Martin Sekera Date: Fri, 13 Nov 2020 00:10:39 +0100 Subject: [PATCH] Make duration optional in playlists --- .gitignore | 1 + unmix.py | 45 +++++++++++++++++++++++++++++++++++++++------ version.py | 6 +++--- 3 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bee8a64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ diff --git a/unmix.py b/unmix.py index 1b7f278..03dd607 100755 --- a/unmix.py +++ b/unmix.py @@ -2,6 +2,7 @@ # encoding: utf-8 from dataclasses import dataclass +import json import os import over import re @@ -45,6 +46,19 @@ def detect_silence(path, min_length_s=2, level_dB=-32): return starts +def get_duration(path): + """ + Returns the duration of the media file pointed to by path. + """ + + cmd = over.cmd.Command("ffprobe", "-v", "quiet", "-print_format", "json", "-show_format", "INFILE") + cmd.INFILE = "file:" + path + cmd.run() + raw = cmd.get_all_output().decode("utf-8") + dic = json.loads(raw) + + return float(dic["format"]["duration"]) + def floats(*args): for arg in args: yield float(arg) @@ -132,7 +146,7 @@ if __name__ == "__main__": main.add_option("track-name", "Track name format.", str, ["%n %a - %t.opus"], abbr="t") main.add_option("verbose", "Print playlist lines as they are being parsed to stderr.", bool, [False], abbr="v") main.add_doc("Description", ["Takes a playlist and turns it into a shell script that splits a large mix into individual audio files."]) - main.add_doc("Playlist", ["Empty lines and those starting with # are ignored. Each line represents a track. The format is: START DURATION ARTIST TITLE. Fields are tab-separated."]) + main.add_doc("Playlist", ["Empty lines and those starting with # are ignored. Each line represents a track. The format is: START [DURATION] ARTIST TITLE. Fields are tab-separated."]) main.setup() ## Sanity checks @@ -166,6 +180,8 @@ if __name__ == "__main__": n += 1 elif main.cfg.playlist: + previous_track = None + with open(main.cfg.playlist) as f: n = 1 @@ -175,19 +191,36 @@ if __name__ == "__main__": if not line or line[0] == "#": continue - start_s, duration_s, artist, title = line.split("\t") - start = sanitize_timestamp(start_s) - duration = sanitize_timestamp(duration_s) + cols = line.split("\t") - tracks.append(Track( + if len(cols) == 3: + start_s, artist, title = cols + duration = None + else: + start_s, duration_s, artist, title = cols + duration = sanitize_timestamp(duration_s) + + start = sanitize_timestamp(start_s) + + track = Track( start = start, duration = duration, number = n, artist = artist.strip(), title = title.strip() - )) + ) + tracks.append(track) + + if previous_track and previous_track.duration is None: + previous_track.duration = track.start - previous_track.start + + previous_track = track n += 1 + + # if unset, set the last track's duration to the end of file + if tracks[-1].duration is None: + tracks[-1].duration = get_duration(main.cfg.path) - tracks[-1].start ## apply global overrides ## diff --git a/version.py b/version.py index b4d0d50..e20a80a 100644 --- a/version.py +++ b/version.py @@ -2,7 +2,7 @@ # encoding: utf-8 major = 0 # VERSION_MAJOR_IDENTIFIER -minor = 2 # VERSION_MINOR_IDENTIFIER -# VERSION_LAST_MM 0.2 +minor = 3 # VERSION_MINOR_IDENTIFIER +# VERSION_LAST_MM 0.3 patch = 0 # VERSION_PATCH_IDENTIFIER -str = "0.2.0" # VERSION_STRING_IDENTIFIER +str = "0.3.0" # VERSION_STRING_IDENTIFIER