👷🏻‍♀️ make sources something to configure

master
neingeist 3 years ago
parent 488ae1aa71
commit 40e1f51faf

@ -1,22 +1,56 @@
import os import os
import random import random
import string import string
from datetime import datetime, timedelta
def random_choices(population, k): def random_choices(population, k):
"""Emulate random.choices for Python < 3.6""" """Emulate random.choices for Python < 3.6"""
return [random.choice(population) for _ in range(k)] 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: class Config:
SECRET_KEY = os.environ.get('SECRET_KEY') or \ SECRET_KEY = os.environ.get('SECRET_KEY') or \
''.join(random_choices(string.ascii_letters, k=20)) ''.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' OUT_DIR = os.environ.get('OUT_DIR') or '/var/tmp/prolefeeder-test-out'
MAX_LENGTH = os.environ.get('MAX_LENGTH') or 180 MAX_LENGTH = os.environ.get('MAX_LENGTH') or 180
KBITS = os.environ.get('KBITS') or 128
DEBUG = False DEBUG = False
if not os.path.exists(DATA_DIR):
raise RuntimeError("DATA_DIR does not exist")
if not os.path.exists(OUT_DIR): if not os.path.exists(OUT_DIR):
raise RuntimeError("OUT_DIR does not exist") 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', 'ffmpeg',
'-f', 'lavfi', '-f', 'lavfi',
'-i', 'sine=frequency=1000:duration=%d' % delta.total_seconds(), '-i', 'sine=frequency=1000:duration=%d' % delta.total_seconds(),
'-b:a', '%dk' % Config.KBITS, '-b:a', '%dk' % Config.kbits,
fn fn
]) ])
@ -21,7 +21,7 @@ delta = timedelta(hours=1)
t = start t = start
while t < end: 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)) t.year, t.month, t.day, t.hour, t.minute))
print(fn) print(fn)
generate_test_file(fn, delta) generate_test_file(fn, delta)

@ -86,24 +86,9 @@ def download_file(filename):
def prepare_download(form): def prepare_download(form):
"""Prepare a download given the user's request 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 # Get a sorted list of all source files with start time and length
sources = [] sources = sorted(Config.SOURCES, key=lambda s: s['start_time'])
for fn in os.listdir(app.config['DATA_DIR']): print(sources)
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'])
# Only interested in the source files from the start file # Only interested in the source files from the start file
start_index = None start_index = None
@ -128,8 +113,7 @@ def prepare_download(form):
c = [ c = [
'ffmpeg', '-y', 'ffmpeg', '-y',
'-ss', str(ss), '-ss', str(ss),
'-i', 'concat:' + '|'.join( '-i', 'concat:' + '|'.join(s['fn'] for s in sources),
[os.path.join(app.config['DATA_DIR'], s['fn']) for s in sources]),
'-codec', 'copy', '-codec', 'copy',
'-t', str(form.length.data * 60), '-t', str(form.length.data * 60),
os.path.join(app.config['OUT_DIR'], output_filename) os.path.join(app.config['OUT_DIR'], output_filename)

Loading…
Cancel
Save