adds duration parsing, changes default --to and --from datetimes to tomorrow from today

This commit is contained in:
Sundog 2023-10-06 13:19:02 -04:00
parent f9e10ecb76
commit 7f26c1aa98
1 changed files with 40 additions and 6 deletions

View File

@ -4,14 +4,15 @@ from glob import glob
import json import json
from listdict import ListDict from listdict import ListDict
import os import os
import subprocess
# globals # globals
mediadir = "/var/lib/ffplayout/tv-media" mediadir = "/var/lib/ffplayout/tv-media"
playlistdir = "/var/lib/ffplayout/playlists" playlistdir = "/var/lib/ffplayout/playlists"
channel = "newellijaytelevision" channel = "newellijaytelevision"
verbose = False verbose = False
from_date = datetime.date.today() from_date = datetime.date.today() + datetime.timedelta(days=1) # tomorrow by default
to_date = datetime.date.today() to_date = datetime.date.today() + datetime.timedelta(days=1) # tomorrow by default
playlist_duration = 6 * 60 * 60 # six hours, in seconds playlist_duration = 6 * 60 * 60 # six hours, in seconds
mediadirs = ListDict() mediadirs = ListDict()
@ -127,14 +128,30 @@ def get_playlist_entry(get_commercial):
this_file = media_files.choose_random_item() if media_files.length() > 0 else "" this_file = media_files.choose_random_item() if media_files.length() > 0 else ""
if this_file == "": if this_file == "":
return "", 0 # get out of this oopsie situation and try again if verbose:
print('cannot select file from empty folder')
return {}, 0 # get out of this oopsie situation and try again
# TODO: ffprobe file for duration if verbose:
print('selected file: ' + this_file)
# probe file for details
metadata = json.loads(json.dumps(probe(this_file), sort_keys=True, indent=4))
if verbose:
print('file metadata: ', metadata)
print('format: ', metadata['format'])
duration = int(float(metadata['format']['duration_secs'])) if "duration_secs" in metadata['format'].keys() else int(float(metadata["format"]["duration"])) if "duration" in metadata['format'].keys() else 0
if duration == 0:
if verbose:
print('cannot parse duration of ' + this_file)
return {}, 0 # cannot parse duration, so skip this file
entry = { entry = {
"in": 0, "in": 0,
"out": 300, "out": duration,
"duration": 300, "duration": duration,
"source": this_file "source": this_file
} }
@ -145,6 +162,23 @@ def get_playlist_entry(get_commercial):
return entry, length return entry, length
def probe(file):
cmd = ['ffprobe', '-v', 'quiet', '-print_format', 'json', '-show_format', '-show_streams', f"{file}"]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
if len(out) > 0:
result = json.loads(out)
else:
result = {}
if err:
print("===err===")
print(err)
if verbose:
print('ffprobe result: ', result)
return result
# generator function to yield single dates from a date range # generator function to yield single dates from a date range
def daterange(start_date, end_date): def daterange(start_date, end_date):
for n in range(int((end_date - start_date).days) + 1): # adding 1 to make the range inclusive for n in range(int((end_date - start_date).days) + 1): # adding 1 to make the range inclusive