👷🏻‍♀️ make sources something to configure

master
neingeist 3 years ago
parent 488ae1aa71
commit 40e1f51faf

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

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

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

Loading…
Cancel
Save