def task(item, sessionkey): """Main func for processing a episode. Args: item(str): a episode's ratingkey sessionkey(str): streams sessionkey Returns: None """ global HT media = PMS.fetchItem(int(item)) # LOG.debug('Found %s', media._prettyfilename()) if media.TYPE not in ('episode', 'show'): return #if not HT.get_theme(media): LOG.debug('Download the first 10 minutes of %s as .wav', media._prettyfilename()) vid = convert_and_trim(check_file_access(media), fs=11025, trim=600) # Check if this shows theme exist in the hash table. # We should prop just check if x in HT.names #try: # HT.name_to_id(theme) #except ValueError: # LOG.debug('No fingerprint for theme %s does exists in the %s', # os.path.basename(theme), FP_HASHES) # analyzer().ingest(HT, theme) #HT = HT.save_then_reload(FP_HASHES) #start, end = get_offset_end(vid, HT) #ffmpeg_end = find_offset_ffmpeg(check_file_access(media)) #if end != -1 or ffmpeg_end != 1: # # End is -1 if not found. Or a positiv int. # process_to_db(media, theme=theme, vid=vid, start=start, end=end, ffmpeg_end=ffmpeg_end) process_to_db(media, vid=vid) try: os.remove(vid) LOG.debug('Deleted %s', vid) except IOError: LOG.excetion('Failed to delete %s', vid) try: IN_PROG.remove(item) except ValueError: LOG.debug('Failed to remove %s from IN_PROG', item) nxt = find_next(media) if nxt: process_to_db(nxt)
def task(item, sessionkey): """Main func for processing a episode. Args: item(str): a episode's ratingkey sessionkey(str): streams sessionkey Returns: None """ global HT media = PMS.fetchItem(int(item)) LOG.debug('Found %s', media._prettyfilename()) if media.TYPE not in ('episode', 'show', 'movie'): # pragma: no cover return if media.TYPE == 'episode': LOG.debug('Download the first 10 minutes of %s as .wav', media._prettyfilename()) vid = convert_and_trim(check_file_access(media), fs=11025, trim=CONFIG['tv'].get('check_for_theme_sec', 600)) process_to_db(media, vid=vid) try: os.remove(vid) LOG.debug('Deleted %s', vid) except IOError: # pragma: no cover LOG.exception('Failed to delete %s', vid) elif media.TYPE == 'movie': process_to_db(media) try: IN_PROG.remove(item) except ValueError: # pragma: no cover LOG.debug('Failed to remove %s from IN_PROG', item) nxt = find_next(media) if nxt: process_to_db(nxt)
def get_theme(media): """Get the current location of the theme or download the damn thing and convert it so it's ready for matching.""" #LOG.debug('theme media type %s', media.TYPE) if media.TYPE == 'show': name = media.title rk = media.ratingKey else: name = media.grandparentTitle rk = media.grandparentRatingKey theme = SHOWS.get(rk) if theme is None: theme = search_for_theme_youtube(name, rk=rk, save_path=THEMES) LOG.debug('Downloaded theme for %s', name) theme = convert_and_trim(theme, fs=11025, theme=True) SHOWS[rk] = theme return theme
def process_to_db(media, theme=None, vid=None, start=None, end=None, ffmpeg_end=None, recap=None): """Process a plex media item to the db Args: media (Episode obj): theme: path to the theme. vid: path to the stripped wav of the media item. start (None, int): of theme. end (None, int): of theme. ffmpeg_end (None, int): What does ffmpeg think is the start of the ep. Returns: None """ global HT ff = -1 name = media._prettyfilename() LOG.debug('Started to process %s', name) if theme is None: theme = get_theme(media) theme = convert_and_trim(theme, fs=11025, theme=True) if vid is None: vid = convert_and_trim(check_file_access(media), fs=11025, trim=600) # too cover manual process_to_db. if theme not in HT.names: analyzer().ingest(HT, theme) # Lets skip the start time for now. This need to be added later to support shows # that have show, theme song show. if end is None: start, end = get_offset_end(vid, HT) if ffmpeg_end is None: ffmpeg_end = find_offset_ffmpeg(check_file_access(media)) if recap is None: recap = has_recap(media, CONFIG.get('words'), audio=vid) if end is not None: with session_scope() as se: try: se.query(Preprocessed).filter_by(ratingKey=media.ratingKey).one() except NoResultFound: p = Preprocessed(show_name=media.grandparentTitle, ep_title=media.title, theme_end=end, theme_start=start, theme_start_str=to_time(start), theme_end_str=to_time(end), ffmpeg_end=ffmpeg_end, ffmpeg_end_str=to_time(ff), duration=media.duration, ratingKey=media.ratingKey, grandparentRatingKey=media.grandparentRatingKey, prettyname=media._prettyfilename(), updatedAt=media.updatedAt, has_recap=recap) se.add(p) LOG.debug('Added %s to media.db', name)
def process_to_db(media, theme=None, vid=None, start=None, end=None, ffmpeg_end=None, recap=None, credits_start=None, credits_end=None): """Process a plex media item to the db Args: media (Episode obj): theme: path to the theme. vid: path to the stripped wav of the media item. start (None, int): of theme. end (None, int): of theme. ffmpeg_end (None, int): What does ffmpeg think is the start of the ep. recap(None, bool): If this how has a recap or not credits_start(None, int): The offset (in sec) the credits text starts credits_end(None, int): The offset (in sec) the credits text ends Returns: None """ global HT # Disable for now. # if media.TYPE == 'movie': # return # This will download the theme and add it to # the hashtable if its missing if media.TYPE == 'episode' and theme is None: if HT.has_theme(media, add_if_missing=False) is False: LOG.debug('downloading theme from process_to_db') theme = download_theme(media, HT) name = media._prettyfilename() LOG.debug('Started to process %s', name) if vid is None and media.TYPE == 'episode': vid = convert_and_trim(check_file_access(media), fs=11025, trim=CONFIG['tv'].get('check_for_theme_sec', 600)) # Find the start and the end of the theme in the episode file. if end is None and media.TYPE == 'episode': start, end = get_offset_end(vid, HT) # Guess when the intro ended using blackframes and audio silence. if ffmpeg_end is None: if media.TYPE == 'episode': trim = CONFIG['tv'].get('check_intro_ffmpeg_sec') else: trim = CONFIG['movie'].get('check_intro_ffmpeg_sec') ffmpeg_end = find_offset_ffmpeg(check_file_access(media), trim=trim) # Check for recap. if recap is None: recap = has_recap(media, CONFIG['tv'].get('words', []), audio=vid) if (media.TYPE == 'episode' and CONFIG['tv'].get('check_credits') is True and credits_start is None and credits_end is None): dur = media.duration / 1000 - CONFIG['tv'].get('check_credits_sec', 120) credits_start, credits_end = find_credits(check_file_access(media), offset=dur, check=-1) elif (media.TYPE == 'movie' and CONFIG['movie'].get('check_credits') is True and credits_start is None and credits_end is None): dur = media.duration / 1000 - CONFIG['movie'].get( 'check_credits_sec', 600) credits_start, credits_end = find_credits(check_file_access(media), offset=dur, check=-1) else: # We dont want to find the credits. credits_start = -1 credits_end = -1 # We assume this is kinda right, # double check this # TODO location = list(i.file for i in media.iterParts() if i)[0] with session_scope() as se: try: se.query(Processed).filter_by(ratingKey=media.ratingKey).one() except NoResultFound: if media.TYPE == 'episode': p = Processed(show_name=media.grandparentTitle, title=media.title, type=media.TYPE, theme_end=end, theme_start=start, theme_start_str=to_time(start), theme_end_str=to_time(end), ffmpeg_end=ffmpeg_end, ffmpeg_end_str=to_time(ffmpeg_end), credits_start=credits_start, credits_start_str=to_time(credits_start), credits_end=credits_end, credits_end_str=to_time(credits_end), duration=media.duration, ratingKey=media.ratingKey, grandparentRatingKey=media.grandparentRatingKey, prettyname=media._prettyfilename(), updatedAt=media.updatedAt, has_recap=recap, location=location) elif media.TYPE == 'movie': p = Processed(title=media.title, type=media.TYPE, ffmpeg_end=ffmpeg_end, ffmpeg_end_str=to_time(ffmpeg_end), credits_start=credits_start, credits_start_str=to_time(credits_start), credits_end=credits_end, credits_end_str=to_time(credits_end), duration=media.duration, ratingKey=media.ratingKey, prettyname=media._prettyfilename(), updatedAt=media.updatedAt, location=location) se.add(p) LOG.debug('Added %s to media.db', name) if media.TYPE == 'movie' and CONFIG['movie']['create_edl']: edl.write_edl( location, edl.db_to_edl(p, type=CONFIG['movie']['edl_action_type'])) elif media.TYPE == 'episode' and CONFIG['tv']['create_edl']: edl.write_edl( location, edl.db_to_edl(p, type=CONFIG['tv']['edl_action_type']))
def process_to_db(media, theme=None, vid=None, start=None, end=None, ffmpeg_end=None, recap=None): """Process a plex media item to the db Args: media (Episode obj): theme: path to the theme. vid: path to the stripped wav of the media item. start (None, int): of theme. end (None, int): of theme. ffmpeg_end (None, int): What does ffmpeg think is the start of the ep. Returns: None """ global HT # This will download the theme and add it to # the hashtable if its missing if theme is None: if HT.has_theme(media, add_if_missing=False) is False: LOG.debug('downloading theme from process_to_db') theme = download_theme(media, HT) ff = -1 name = media._prettyfilename() LOG.debug('Started to process %s', name) if vid is None: vid = convert_and_trim(check_file_access(media), fs=11025, trim=600) # Find the start and the end of the theme in the video file. if end is None: start, end = get_offset_end(vid, HT) # Guess when the intro ended using blackframes and audio silence. if ffmpeg_end is None: ffmpeg_end = find_offset_ffmpeg(check_file_access(media)) # Check for recap. if recap is None: recap = has_recap(media, CONFIG.get('words', []), audio=vid) with session_scope() as se: try: se.query(Preprocessed).filter_by(ratingKey=media.ratingKey).one() except NoResultFound: p = Preprocessed(show_name=media.grandparentTitle, ep_title=media.title, theme_end=end, theme_start=start, theme_start_str=to_time(start), theme_end_str=to_time(end), ffmpeg_end=ffmpeg_end, ffmpeg_end_str=to_time(ffmpeg_end), duration=media.duration, ratingKey=media.ratingKey, grandparentRatingKey=media.grandparentRatingKey, prettyname=media._prettyfilename(), updatedAt=media.updatedAt, has_recap=recap) se.add(p) LOG.debug('Added %s to media.db', name)