From 40e1f51faf33ea02d0d32ae3ad2a44bced76deca Mon Sep 17 00:00:00 2001 From: neingeist Date: Mon, 6 Dec 2021 22:45:38 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=B7=F0=9F=8F=BB=E2=80=8D=E2=99=80?= =?UTF-8?q?=EF=B8=8F=20make=20sources=20something=20to=20configure?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.py.example | 42 ++++++++++++++++++++++++++++++++++++++---- generate-test-data.py | 4 ++-- prolefeeder.py | 22 +++------------------- 3 files changed, 43 insertions(+), 25 deletions(-) diff --git a/config.py.example b/config.py.example index 75c76e4..094d1f0 100644 --- a/config.py.example +++ b/config.py.example @@ -1,22 +1,56 @@ import os import random import string +from datetime import datetime, timedelta + def random_choices(population, k): """Emulate random.choices for Python < 3.6""" return [random.choice(population) for _ in range(k)] + +def mp3_sources(data_dir, pattern, kbits): + def start_time_for_source_fn(fn): + return datetime.strptime(fn, pattern) + + def length_for_source_fn(fn): + size = os.stat(os.path.join(data_dir, fn)).st_size + length = timedelta(minutes=size / (1000*kbits/8) / 60) + return length + + # Get a sorted list of all source files with start time and length + sources = [] + for fn in os.listdir(data_dir): + try: + start_time = start_time_for_source_fn(fn) + length = length_for_source_fn(fn) + sources.append({'fn': os.path.join(data_dir, fn), 'start_time': start_time, 'length': length}) + except ValueError: + pass + return sources + + class Config: SECRET_KEY = os.environ.get('SECRET_KEY') or \ ''.join(random_choices(string.ascii_letters, k=20)) - DATA_DIR = os.environ.get('DATA_DIR') or '/var/tmp/prolefeeder-test-data' OUT_DIR = os.environ.get('OUT_DIR') or '/var/tmp/prolefeeder-test-out' MAX_LENGTH = os.environ.get('MAX_LENGTH') or 180 - KBITS = os.environ.get('KBITS') or 128 DEBUG = False - if not os.path.exists(DATA_DIR): - raise RuntimeError("DATA_DIR does not exist") if not os.path.exists(OUT_DIR): raise RuntimeError("OUT_DIR does not exist") + + # Here's the interesting part: configuring the source. + # + # We need to return a list of dicts of source files with metadata, e.g. + # [ {"fn": "/path/to/foo-20:01.mp3", "start_time": datetime(...), "length": timedelta(hours=1)} ] + + data_dir = os.environ.get('DATA_DIR') or '/var/tmp/prolefeeder-test-data' + kbits = os.environ.get('KBITS') or 128 + pattern = 'qfhi-%Y%m%d-%H%M.mp3' + + if not os.path.exists(data_dir): + raise RuntimeError("DATA_DIR does not exist") + + SOURCES = mp3_sources(data_dir, pattern, kbits) diff --git a/generate-test-data.py b/generate-test-data.py index c22f1f2..b6b0cdd 100644 --- a/generate-test-data.py +++ b/generate-test-data.py @@ -10,7 +10,7 @@ def generate_test_file(fn, delta): 'ffmpeg', '-f', 'lavfi', '-i', 'sine=frequency=1000:duration=%d' % delta.total_seconds(), - '-b:a', '%dk' % Config.KBITS, + '-b:a', '%dk' % Config.kbits, fn ]) @@ -21,7 +21,7 @@ delta = timedelta(hours=1) t = start while t < end: - fn = os.path.join(Config.DATA_DIR, 'qfhi-{:04d}{:02d}{:02d}-{:02d}{:02d}.mp3'.format( + fn = os.path.join(Config.data_dir, 'qfhi-{:04d}{:02d}{:02d}-{:02d}{:02d}.mp3'.format( t.year, t.month, t.day, t.hour, t.minute)) print(fn) generate_test_file(fn, delta) diff --git a/prolefeeder.py b/prolefeeder.py index 5da0a0e..2167c49 100644 --- a/prolefeeder.py +++ b/prolefeeder.py @@ -86,24 +86,9 @@ def download_file(filename): def prepare_download(form): """Prepare a download given the user's request form""" - def start_time_for_source_fn(fn): - return datetime.strptime(fn, 'qfhi-%Y%m%d-%H%M.mp3') - - def length_for_source_fn(fn): - size = os.stat(os.path.join(app.config['DATA_DIR'], fn)).st_size - length = timedelta(minutes=size / (1000*app.config['KBITS']/8) / 60) - return length - # Get a sorted list of all source files with start time and length - sources = [] - for fn in os.listdir(app.config['DATA_DIR']): - try: - start_time = start_time_for_source_fn(fn) - length = length_for_source_fn(fn) - sources.append({'fn': fn, 'start_time': start_time, 'length': length}) - except ValueError: - pass - sources = sorted(sources, key=lambda s: s['start_time']) + sources = sorted(Config.SOURCES, key=lambda s: s['start_time']) + print(sources) # Only interested in the source files from the start file start_index = None @@ -128,8 +113,7 @@ def prepare_download(form): c = [ 'ffmpeg', '-y', '-ss', str(ss), - '-i', 'concat:' + '|'.join( - [os.path.join(app.config['DATA_DIR'], s['fn']) for s in sources]), + '-i', 'concat:' + '|'.join(s['fn'] for s in sources), '-codec', 'copy', '-t', str(form.length.data * 60), os.path.join(app.config['OUT_DIR'], output_filename)