Make duration optional in playlists

This commit is contained in:
Martin Sekera 2020-11-13 00:10:39 +01:00
parent 04c8f3901b
commit 8d346e8942
3 changed files with 43 additions and 9 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
__pycache__

View file

@ -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
##

View file

@ -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