use glob to only find media files when we only want media files
This commit is contained in:
parent
3e38c98c3b
commit
f9e10ecb76
|
@ -0,0 +1,32 @@
|
|||
import random
|
||||
|
||||
class ListDict(object):
|
||||
def __init__(self):
|
||||
self.item_to_position = {}
|
||||
self.items = []
|
||||
|
||||
def add_item(self, item):
|
||||
if item in self.item_to_position:
|
||||
return
|
||||
self.items.append(item)
|
||||
self.item_to_position[item] = len(self.items)-1
|
||||
|
||||
def remove_item(self, item):
|
||||
position = self.item_to_position.pop(item)
|
||||
last_item = self.items.pop()
|
||||
if position != len(self.items):
|
||||
self.items[position] = last_item
|
||||
self.item_to_position[last_item] = position
|
||||
|
||||
def choose_random_item(self):
|
||||
return random.choice(self.items)
|
||||
|
||||
def __contains__(self, item):
|
||||
return item in self.item_to_position
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self.items)
|
||||
|
||||
def length(self):
|
||||
return len(self.items)
|
||||
|
|
@ -1,6 +1,8 @@
|
|||
import argparse
|
||||
import datetime
|
||||
from glob import glob
|
||||
import json
|
||||
from listdict import ListDict
|
||||
import os
|
||||
|
||||
# globals
|
||||
|
@ -11,9 +13,11 @@ verbose = False
|
|||
from_date = datetime.date.today()
|
||||
to_date = datetime.date.today()
|
||||
playlist_duration = 6 * 60 * 60 # six hours, in seconds
|
||||
mediadirs = ListDict()
|
||||
|
||||
def main():
|
||||
parse_config()
|
||||
get_media_dirs(mediadir)
|
||||
for single_date in daterange(from_date, to_date):
|
||||
if verbose:
|
||||
print("Processing date", single_date)
|
||||
|
@ -69,6 +73,19 @@ def parse_config():
|
|||
print("from_date:", from_date)
|
||||
print("to_date:", to_date)
|
||||
|
||||
def get_media_dirs(rootdir):
|
||||
for file in os.listdir(rootdir):
|
||||
if file == "Commercials":
|
||||
pass
|
||||
elif file == "Television":
|
||||
get_media_dirs(os.path.join(rootdir, file))
|
||||
else:
|
||||
d = os.path.join(rootdir, file)
|
||||
if os.path.isdir(d):
|
||||
if verbose:
|
||||
print("adding " + d + " to media dirs")
|
||||
mediadirs.add_item(d)
|
||||
|
||||
def build_day(this_date):
|
||||
d = {} # empty dict that will become our JSON output
|
||||
|
||||
|
@ -77,11 +94,14 @@ def build_day(this_date):
|
|||
d["program"] = [] # empty list to populate with programs
|
||||
|
||||
total_time = 0
|
||||
get_commercial = False
|
||||
|
||||
while total_time < playlist_duration:
|
||||
entry, length = get_playlist_entry()
|
||||
entry, length = get_playlist_entry(get_commercial)
|
||||
d["program"].append(entry)
|
||||
total_time += length
|
||||
if length > 0:
|
||||
get_commercial = not get_commercial
|
||||
if verbose:
|
||||
print(' added program:', json.dumps(entry), length)
|
||||
|
||||
|
@ -93,14 +113,34 @@ def build_day(this_date):
|
|||
|
||||
return playlist
|
||||
|
||||
def get_playlist_entry():
|
||||
def get_playlist_entry(get_commercial):
|
||||
exts = ['*.mp4', '*.webm', '*.mov']
|
||||
entry_dir = mediadir + '/Commercials' if get_commercial else mediadirs.choose_random_item()
|
||||
|
||||
media_files = ListDict()
|
||||
|
||||
found_media = [f for ext in exts
|
||||
for f in glob(os.path.join(entry_dir, '**', ext), recursive=True)]
|
||||
|
||||
for d in found_media:
|
||||
media_files.add_item(d)
|
||||
|
||||
this_file = media_files.choose_random_item() if media_files.length() > 0 else ""
|
||||
if this_file == "":
|
||||
return "", 0 # get out of this oopsie situation and try again
|
||||
|
||||
# TODO: ffprobe file for duration
|
||||
|
||||
entry = {
|
||||
"in": 0,
|
||||
"out": 300,
|
||||
"duration": 300,
|
||||
"source": "test.mp4"
|
||||
"source": this_file
|
||||
}
|
||||
|
||||
if get_commercial:
|
||||
entry["category"] = "advertisement"
|
||||
|
||||
length = entry["duration"]
|
||||
|
||||
return entry, length
|
||||
|
|
Loading…
Reference in New Issue