Improved video metadata parser function and documented params and return values.

pull/2513/head
morpheus65535 3 months ago
parent 1f843c7d15
commit 35b65dcd4e

@ -213,6 +213,25 @@ def subtitles_sync_references(subtitles_path, sonarr_episode_id=None, radarr_mov
def parse_video_metadata(file, file_size, episode_file_id=None, movie_file_id=None, use_cache=True): def parse_video_metadata(file, file_size, episode_file_id=None, movie_file_id=None, use_cache=True):
"""
This function return the video file properties as parsed by knowit using ffprobe or mediainfo using the cached
value by default.
@type file: string
@param file: Properly mapped path of a video file
@type file_size: int
@param file_size: File size in bytes of the video file
@type episode_file_id: int or None
@param episode_file_id: episode ID of the video file from Sonarr (or None if it's a movie)
@type movie_file_id: int or None
@param movie_file_id: movie ID of the video file from Radarr (or None if it's an episode)
@type use_cache: bool
@param use_cache:
@rtype: dict or None
@return: return a dictionary including the video file properties as parsed by ffprobe or mediainfo
"""
# Define default data keys value # Define default data keys value
data = { data = {
"ffprobe": {}, "ffprobe": {},
@ -228,12 +247,12 @@ def parse_video_metadata(file, file_size, episode_file_id=None, movie_file_id=No
if episode_file_id: if episode_file_id:
cache_key = database.execute( cache_key = database.execute(
select(TableEpisodes.ffprobe_cache) select(TableEpisodes.ffprobe_cache)
.where(TableEpisodes.path == path_mappings.path_replace_reverse(file))) \ .where(TableEpisodes.episode_file_id == episode_file_id)) \
.first() .first()
elif movie_file_id: elif movie_file_id:
cache_key = database.execute( cache_key = database.execute(
select(TableMovies.ffprobe_cache) select(TableMovies.ffprobe_cache)
.where(TableMovies.path == path_mappings.path_replace_reverse_movie(file))) \ .where(TableMovies.movie_file_id == movie_file_id)) \
.first() .first()
else: else:
cache_key = None cache_key = None
@ -243,6 +262,7 @@ def parse_video_metadata(file, file_size, episode_file_id=None, movie_file_id=No
# Unpickle ffprobe cache # Unpickle ffprobe cache
cached_value = pickle.loads(cache_key.ffprobe_cache) cached_value = pickle.loads(cache_key.ffprobe_cache)
except Exception: except Exception:
# No cached value available, we'll parse the file
pass pass
else: else:
# Check if file size and file id matches and if so, we return the cached value if available for the # Check if file size and file id matches and if so, we return the cached value if available for the
@ -291,19 +311,19 @@ def parse_video_metadata(file, file_size, episode_file_id=None, movie_file_id=No
else: else:
logging.error("BAZARR require ffmpeg/ffprobe or mediainfo, please install it and make sure to choose it in " logging.error("BAZARR require ffmpeg/ffprobe or mediainfo, please install it and make sure to choose it in "
"Settings-->Subtitles.") "Settings-->Subtitles.")
return return None
# we write to db the result and return the newly cached ffprobe dict # we write to db the result and return the newly cached ffprobe dict
if episode_file_id: if episode_file_id:
database.execute( database.execute(
update(TableEpisodes) update(TableEpisodes)
.values(ffprobe_cache=pickle.dumps(data, pickle.HIGHEST_PROTOCOL)) .values(ffprobe_cache=pickle.dumps(data, pickle.HIGHEST_PROTOCOL))
.where(TableEpisodes.path == path_mappings.path_replace_reverse(file))) .where(TableEpisodes.episode_file_id == episode_file_id))
elif movie_file_id: elif movie_file_id:
database.execute( database.execute(
update(TableMovies) update(TableMovies)
.values(ffprobe_cache=pickle.dumps(data, pickle.HIGHEST_PROTOCOL)) .values(ffprobe_cache=pickle.dumps(data, pickle.HIGHEST_PROTOCOL))
.where(TableMovies.path == path_mappings.path_replace_reverse_movie(file))) .where(TableMovies.movie_file_id == movie_file_id))
return data return data

Loading…
Cancel
Save