Created
June 18, 2023 02:48
-
-
Save chapmanjacobd/4cbcc1e3cfdfc0ad7b3c2746578166c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def mpv_enrich(args, media) -> List[Dict]: | |
for m in media: | |
md5 = path_to_mpv_watchlater_md5(m["path"]) | |
metadata_path = Path(args.watch_later_directory, md5) | |
if metadata_path.exists(): | |
m["time_partial_first"] = int(metadata_path.stat().st_ctime) | |
m["time_partial_last"] = int(metadata_path.stat().st_mtime) | |
else: | |
m["time_partial_first"] = 0 | |
m["time_partial_last"] = 0 | |
return sorted(media, key=lambda m: m.get("time_partial_first") or 0, reverse=True) | |
def mpv_enrich2(args, media) -> List[Dict]: | |
md5s = {path_to_mpv_watchlater_md5(m["path"]): m for m in media} | |
paths = set(Path(args.watch_later_directory).glob("*")) | |
previously_watched = [ | |
{ | |
**(md5s.get(p.stem) or {}), | |
"time_partial_first": int(p.stat().st_ctime), | |
"time_partial_last": int(p.stat().st_mtime), | |
"playhead": safe_int(mpv_watchlater_value(p, "start")), | |
} | |
for p in paths | |
if md5s.get(p.stem) | |
] | |
if "s" in args.partial: # skip; only play unseen | |
previously_watched_paths = [m["path"] for m in previously_watched] | |
return [m for m in media if m["path"] not in previously_watched_paths] | |
def mpv_progress(m): | |
playhead = m.get("playhead") | |
duration = m.get("duration") | |
if not playhead: | |
return float("-inf") | |
if not duration: | |
return float("-inf") | |
if "p" in args.partial and "t" in args.partial: | |
return (duration / playhead) * -(duration - playhead) # weighted remaining | |
elif "t" in args.partial: | |
return -(duration - playhead) # time remaining | |
else: | |
return playhead / duration # percent remaining | |
def sorting_hat(): | |
if "f" in args.partial: # first-viewed | |
return lambda m: m.get("time_partial_first") or 0 | |
elif "p" in args.partial or "t" in args.partial: # sort by remaining duration | |
return mpv_progress | |
return lambda m: m.get("time_partial_last") or m.get("time_partial_first") or 0 | |
reverse_chronology = True | |
if "o" in args.partial: # oldest first | |
reverse_chronology = False | |
key = sorting_hat() | |
if args.print: | |
reverse_chronology = not reverse_chronology | |
media = sorted( | |
previously_watched, | |
key=key, | |
reverse=reverse_chronology, | |
) | |
if args.skip: | |
media = media[int(args.skip) :] | |
return media |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment