forked from openlp/openlp
Revert the mediainfo upstreaming
Keep the pymediainfo dependency for later
This commit is contained in:
parent
0484d0bf23
commit
e458622fcc
104
openlp/core/ui/media/vendor/mediainfoWrapper.py
vendored
104
openlp/core/ui/media/vendor/mediainfoWrapper.py
vendored
@ -20,21 +20,109 @@
|
|||||||
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
|
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
|
||||||
###############################################################################
|
###############################################################################
|
||||||
"""
|
"""
|
||||||
The :mod:`~openlp.core.ui.media.mediainfo` module contains code to run mediainfo
|
The :mod:`~openlp.core.ui.media.mediainfo` module contains code to run mediainfo on a media file and obtain
|
||||||
on a media file and obtain information related to the requested media.
|
information related to the rwquested media.
|
||||||
"""
|
"""
|
||||||
from pymediainfo import MediaInfo
|
import json
|
||||||
|
import os
|
||||||
from subprocess import check_output
|
from subprocess import check_output
|
||||||
|
|
||||||
|
from bs4 import BeautifulSoup, NavigableString
|
||||||
|
|
||||||
|
ENV_DICT = os.environ
|
||||||
|
|
||||||
|
|
||||||
|
class Track(object):
|
||||||
|
|
||||||
|
def __getattribute__(self, name):
|
||||||
|
try:
|
||||||
|
return object.__getattribute__(self, name)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
return None
|
||||||
|
|
||||||
|
def __init__(self, xml_dom_fragment):
|
||||||
|
self.xml_dom_fragment = xml_dom_fragment
|
||||||
|
self.track_type = xml_dom_fragment.attrs['type']
|
||||||
|
for el in self.xml_dom_fragment.children:
|
||||||
|
if not isinstance(el, NavigableString):
|
||||||
|
node_name = el.name.lower().strip().strip('_')
|
||||||
|
if node_name == 'id':
|
||||||
|
node_name = 'track_id'
|
||||||
|
node_value = el.string
|
||||||
|
other_node_name = "other_%s" % node_name
|
||||||
|
if getattr(self, node_name) is None:
|
||||||
|
setattr(self, node_name, node_value)
|
||||||
|
else:
|
||||||
|
if getattr(self, other_node_name) is None:
|
||||||
|
setattr(self, other_node_name, [node_value, ])
|
||||||
|
else:
|
||||||
|
getattr(self, other_node_name).append(node_value)
|
||||||
|
|
||||||
|
for o in [d for d in self.__dict__.keys() if d.startswith('other_')]:
|
||||||
|
try:
|
||||||
|
primary = o.replace('other_', '')
|
||||||
|
setattr(self, primary, int(getattr(self, primary)))
|
||||||
|
except:
|
||||||
|
for v in getattr(self, o):
|
||||||
|
try:
|
||||||
|
current = getattr(self, primary)
|
||||||
|
setattr(self, primary, int(v))
|
||||||
|
getattr(self, o).append(current)
|
||||||
|
break
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "<Track track_id='{0}', track_type='{1}'>".format(self.track_id, self.track_type)
|
||||||
|
|
||||||
|
def to_data(self):
|
||||||
|
data = {}
|
||||||
|
for k, v in self.__dict__.items():
|
||||||
|
if k != 'xml_dom_fragment':
|
||||||
|
data[k] = v
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
class MediaInfoWrapper(object):
|
class MediaInfoWrapper(object):
|
||||||
|
|
||||||
|
def __init__(self, xml):
|
||||||
|
self.xml_dom = xml
|
||||||
|
xml_types = (str,) # no unicode type in python3
|
||||||
|
if isinstance(xml, xml_types):
|
||||||
|
self.xml_dom = MediaInfoWrapper.parse_xml_data_into_dom(xml)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def parse(filename):
|
def parse_xml_data_into_dom(xml_data):
|
||||||
if MediaInfo.can_parse():
|
return BeautifulSoup(xml_data, "xml")
|
||||||
return MediaInfo.parse(filename)
|
|
||||||
else:
|
@staticmethod
|
||||||
|
def parse(filename, environment=ENV_DICT):
|
||||||
xml = check_output(['mediainfo', '-f', '--Output=XML', '--Inform=OLDXML', filename])
|
xml = check_output(['mediainfo', '-f', '--Output=XML', '--Inform=OLDXML', filename])
|
||||||
if not xml.startswith(b'<?xml'):
|
if not xml.startswith(b'<?xml'):
|
||||||
xml = check_output(['mediainfo', '-f', '--Output=XML', filename])
|
xml = check_output(['mediainfo', '-f', '--Output=XML', filename])
|
||||||
return MediaInfo(xml.decode("utf-8"))
|
xml_dom = MediaInfoWrapper.parse_xml_data_into_dom(xml)
|
||||||
|
return MediaInfoWrapper(xml_dom)
|
||||||
|
|
||||||
|
def _populate_tracks(self):
|
||||||
|
if self.xml_dom is None:
|
||||||
|
return
|
||||||
|
for xml_track in self.xml_dom.Mediainfo.File.find_all("track"):
|
||||||
|
self._tracks.append(Track(xml_track))
|
||||||
|
|
||||||
|
@property
|
||||||
|
def tracks(self):
|
||||||
|
if not hasattr(self, "_tracks"):
|
||||||
|
self._tracks = []
|
||||||
|
if len(self._tracks) == 0:
|
||||||
|
self._populate_tracks()
|
||||||
|
return self._tracks
|
||||||
|
|
||||||
|
def to_data(self):
|
||||||
|
data = {'tracks': []}
|
||||||
|
for track in self.tracks:
|
||||||
|
data['tracks'].append(track.to_data())
|
||||||
|
return data
|
||||||
|
|
||||||
|
def to_json(self):
|
||||||
|
return json.dumps(self.to_data())
|
||||||
|
@ -25,7 +25,6 @@ The Media plugin
|
|||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from pymediainfo import MediaInfo
|
|
||||||
from PyQt5 import QtCore
|
from PyQt5 import QtCore
|
||||||
|
|
||||||
from openlp.core.api.http import register_endpoint
|
from openlp.core.api.http import register_endpoint
|
||||||
@ -78,8 +77,8 @@ class MediaPlugin(Plugin):
|
|||||||
:return: true or false
|
:return: true or false
|
||||||
"""
|
"""
|
||||||
log.debug('check_installed Mediainfo')
|
log.debug('check_installed Mediainfo')
|
||||||
# Try to find libmediainfo or mediainfo in the path
|
# Try to find mediainfo in the path
|
||||||
exists = MediaInfo.can_parse() or process_check_binary(Path('mediainfo'))
|
exists = process_check_binary(Path('mediainfo'))
|
||||||
# If mediainfo is not in the path, try to find it in the application folder
|
# If mediainfo is not in the path, try to find it in the application folder
|
||||||
if not exists:
|
if not exists:
|
||||||
exists = process_check_binary(AppLocation.get_directory(AppLocation.AppDir) / 'mediainfo')
|
exists = process_check_binary(AppLocation.get_directory(AppLocation.AppDir) / 'mediainfo')
|
||||||
|
Loading…
Reference in New Issue
Block a user