Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
add more known info about the media file/streams; resolves #270
Browse files Browse the repository at this point in the history
  • Loading branch information
pannal committed May 22, 2017
1 parent 89b4305 commit 6a9112f
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 42 deletions.
138 changes: 98 additions & 40 deletions Contents/Code/support/plex_media.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,49 @@ def get_metadata_dict(item, part, add):
tvdb_guid_identifier = "com.plexapp.agents.thetvdb://"


def get_plexapi_stream_info(plex_item, part_id=None):
d = {"stream": {}}
data = d["stream"]

# find current part
current_part = None
current_media = None
for media in plex_item.media:
for part in media.parts:
if not part_id or str(part.id) == part_id:
current_part = part
current_media = media
break
if current_part:
break

if not current_part:
return d

data["video_codec"] = current_media.video_codec
data["audio_codec"] = current_media.audio_codec.upper()

if data["audio_codec"] == "DCA":
data["audio_codec"] = "DTS"

if current_media.audio_channels == 8:
data["audio_channels"] = "7.1"

elif current_media.audio_channels == 6:
data["audio_channels"] = "5.1"
else:
data["audio_channels"] = "%s.0" % str(current_media.audio_channels)

# iter streams
for stream in current_part.streams:
if stream.stream_type == 1:
# video stream
data["resolution"] = "%s%s" % (current_media.video_resolution,
"i" if stream.scan_type != "progressive" else "p")

return d


def media_to_videos(media, kind="series"):
"""
iterates through media and returns the associated parts (videos)
Expand All @@ -34,6 +77,7 @@ def media_to_videos(media, kind="series"):
"""
videos = []

# this is a Show or a Movie object
plex_item = get_item(media.id)
year = plex_item.year
original_title = plex_item.title_original
Expand All @@ -52,34 +96,42 @@ def media_to_videos(media, kind="series"):

# get plex item via API for additional metadata
plex_episode = get_item(ep.id)
stream_info = get_plexapi_stream_info(plex_episode)

for item in media.seasons[season].episodes[episode].items:
for part in item.parts:
videos.append(
get_metadata_dict(plex_episode, part,
{"plex_part": part, "type": "episode", "title": ep.title,
"series": media.title, "id": ep.id, "year": year,
"series_id": media.id, "season_id": season_object.id,
"imdb_id": None, "series_tvdb_id": series_tvdb_id, "tvdb_id": tvdb_id,
"original_title": original_title,
"episode": plex_episode.index, "season": plex_episode.season.index,
"section": plex_episode.section.title
})
dict(stream_info, **{"plex_part": part, "type": "episode",
"title": ep.title,
"series": media.title, "id": ep.id, "year": year,
"series_id": media.id,
"season_id": season_object.id,
"imdb_id": None, "series_tvdb_id": series_tvdb_id,
"tvdb_id": tvdb_id,
"original_title": original_title,
"episode": plex_episode.index,
"season": plex_episode.season.index,
"section": plex_episode.section.title
})
)
)
else:
stream_info = get_plexapi_stream_info(plex_item)
imdb_id = None
if imdb_guid_identifier in media.guid:
imdb_id = media.guid[len(imdb_guid_identifier):].split("?")[0]
for item in media.items:
for part in item.parts:
videos.append(
get_metadata_dict(plex_item, part, {"plex_part": part, "type": "movie",
"title": media.title, "id": media.id,
"series_id": None, "year": year,
"season_id": None, "imdb_id": imdb_id,
"original_title": original_title,
"series_tvdb_id": None, "tvdb_id": None,
"section": plex_item.section.title})
get_metadata_dict(plex_item, part, dict(stream_info, **{"plex_part": part, "type": "movie",
"title": media.title, "id": media.id,
"series_id": None, "year": year,
"season_id": None, "imdb_id": imdb_id,
"original_title": original_title,
"series_tvdb_id": None, "tvdb_id": None,
"section": plex_item.section.title})
)
)
return videos

Expand Down Expand Up @@ -207,7 +259,7 @@ def get_plex_metadata(rating_key, part_id, item_type):
"""
uses the Plex 3rd party API accessor to get metadata information
:param rating_key:
:param rating_key: movie or episode
:param part_id:
:param item_type:
:return:
Expand All @@ -228,6 +280,8 @@ def get_plex_metadata(rating_key, part_id, item_type):
if not current_part:
raise helpers.PartUnknownException("Part unknown")

stream_info = get_plexapi_stream_info(plex_item, part_id)

# get normalized metadata
# fixme: duplicated logic of media_to_videos
if item_type == "episode":
Expand All @@ -240,35 +294,39 @@ def get_plex_metadata(rating_key, part_id, item_type):
tvdb_id = plex_item.guid[len(tvdb_guid_identifier):].split("?")[0]
series_tvdb_id = tvdb_id.split("/")[0]
metadata = get_metadata_dict(plex_item, current_part,
{"plex_part": current_part, "type": "episode", "title": plex_item.title,
"series": plex_item.show.title, "id": plex_item.rating_key,
"series_id": plex_item.show.rating_key,
"season_id": plex_item.season.rating_key,
"imdb_id": None,
"year": year,
"tvdb_id": tvdb_id,
"series_tvdb_id": series_tvdb_id,
"original_title": original_title,
"season": plex_item.season.index,
"episode": plex_item.index
})
dict(stream_info,
**{"plex_part": current_part, "type": "episode", "title": plex_item.title,
"series": plex_item.show.title, "id": plex_item.rating_key,
"series_id": plex_item.show.rating_key,
"season_id": plex_item.season.rating_key,
"imdb_id": None,
"year": year,
"tvdb_id": tvdb_id,
"series_tvdb_id": series_tvdb_id,
"original_title": original_title,
"season": plex_item.season.index,
"episode": plex_item.index
})
)
else:
imdb_id = None
original_title = plex_item.title_original
if imdb_guid_identifier in plex_item.guid:
imdb_id = plex_item.guid[len(imdb_guid_identifier):].split("?")[0]
metadata = get_metadata_dict(plex_item, current_part, {"plex_part": current_part, "type": "movie",
"title": plex_item.title, "id": plex_item.rating_key,
"series_id": None,
"season_id": None,
"imdb_id": imdb_id,
"year": plex_item.year,
"tvdb_id": None,
"series_tvdb_id": None,
"original_title": original_title,
"season": None,
"episode": None,
"section": plex_item.section.title})
metadata = get_metadata_dict(plex_item, current_part,
dict(stream_info, **{"plex_part": current_part, "type": "movie",
"title": plex_item.title, "id": plex_item.rating_key,
"series_id": None,
"season_id": None,
"imdb_id": imdb_id,
"year": plex_item.year,
"tvdb_id": None,
"series_tvdb_id": None,
"original_title": original_title,
"season": None,
"episode": None,
"section": plex_item.section.title})
)
return metadata


Expand Down
10 changes: 8 additions & 2 deletions Contents/Libraries/Shared/subzero/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@ def parse_video(fn, video_info, hints, external_subtitles=False, embedded_subtit
# refiners

refine_kwargs = {
"episode_refiners": ('sz_metadata', 'tvdb', 'sz_omdb'),
"movie_refiners": ('sz_metadata', 'sz_omdb',),
"episode_refiners": ('tvdb', 'sz_omdb'),
"movie_refiners": ('sz_omdb',),
"embedded_subtitles": False,
}

# our own metadata refiner :)
for key, value in video_info["stream"].iteritems():
if hasattr(video, key) and not getattr(video, key):
logger.info(u"Adding stream %s info: %s", key, value)
setattr(video, key, value)

plex_title = video_info["original_title"] or video_info["title"]
if hints["type"] == "episode":
plex_title = video_info["original_title"] or video_info["series"]
Expand Down

0 comments on commit 6a9112f

Please sign in to comment.