Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/bump mediainfo #10564

Merged
merged 4 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#### New Features

#### Improvements
- Bump Knowit + pymediainfo to version 0.4.0 and 5.1.0 ([10564](https://github.com/pymedusa/Medusa/pull/10564))

#### Fixes

Expand Down
14 changes: 3 additions & 11 deletions ext/knowit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
# -*- coding: utf-8 -*-
"""Know your media files better."""
from __future__ import unicode_literals

__title__ = 'knowit'
__version__ = '0.3.0-dev'
__version__ = '0.4.0'
__short_version__ = '.'.join(__version__.split('.')[:2])
__author__ = 'Rato AQ2'
__license__ = 'MIT'
__copyright__ = 'Copyright 2016-2017, Rato AQ2'
__copyright__ = 'Copyright 2016-2021, Rato AQ2'
__url__ = 'https://github.com/ratoaq2/knowit'

#: Video extensions
Expand All @@ -19,9 +16,4 @@
'.omf', '.ps', '.qt', '.ram', '.rm', '.rmvb', '.swf', '.ts', '.vfw', '.vid', '.video', '.viv',
'.vivo', '.vob', '.vro', '.webm', '.wm', '.wmv', '.wmx', '.wrap', '.wvx', '.wx', '.x264', '.xvid')

try:
from collections import OrderedDict
except ImportError: # pragma: no cover
from ordereddict import OrderedDict

from .api import KnowitException, know
from knowit.api import KnowitException, know
238 changes: 156 additions & 82 deletions ext/knowit/__main__.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import argparse
import json
import logging
import os
import sys
import typing
from argparse import ArgumentParser

from six import PY2
import yaml

from . import (
from knowit import (
__url__,
__version__,
api,
)
from .provider import ProviderError
from .serializer import (
from knowit.provider import ProviderError
from knowit.serializer import (
get_json_encoder,
get_yaml_dumper,
)
from .utils import recurse_paths
from knowit.utils import recurse_paths

logging.basicConfig(stream=sys.stdout, format='%(message)s')
logging.getLogger('CONSOLE').setLevel(logging.INFO)
Expand All @@ -29,45 +28,96 @@
logger = logging.getLogger('knowit')


def build_argument_parser():
"""Build the argument parser.

:return: the argument parser
:rtype: ArgumentParser
"""
def build_argument_parser() -> ArgumentParser:
"""Build the argument parser."""
opts = ArgumentParser()
opts.add_argument(dest='videopath', help='Path to the video to introspect', nargs='*')
opts.add_argument(
dest='videopath',
help='Path to the video to introspect',
nargs='*',
type=str,
)

provider_opts = opts.add_argument_group('Providers')
provider_opts.add_argument('-p', '--provider', dest='provider',
help='The provider to be used: mediainfo, ffmpeg or enzyme.')
provider_opts.add_argument(
'-p',
'--provider',
dest='provider',
help='The provider to be used: mediainfo, ffmpeg, mkvmerge or enzyme.',
type=str,
)

output_opts = opts.add_argument_group('Output')
output_opts.add_argument('--debug', action='store_true', dest='debug',
help='Print useful information for debugging knowit and for reporting bugs.')
output_opts.add_argument('--report', action='store_true', dest='report',
help='Parse media and report all non-detected values')
output_opts.add_argument('-y', '--yaml', action='store_true', dest='yaml',
help='Display output in yaml format')
output_opts.add_argument('-N', '--no-units', action='store_true', dest='no_units',
help='Display output without units')
output_opts.add_argument('-P', '--profile', dest='profile',
help='Display values according to specified profile: code, default, human, technical')
output_opts.add_argument(
'--debug',
action='store_true',
dest='debug',
help='Print information for debugging knowit and for reporting bugs.'
)
output_opts.add_argument(
'--report',
action='store_true',
dest='report',
help='Parse media and report all non-detected values'
)
output_opts.add_argument(
'-y',
'--yaml',
action='store_true',
dest='yaml',
help='Display output in yaml format'
)
output_opts.add_argument(
'-N',
'--no-units',
action='store_true',
dest='no_units',
help='Display output without units'
)
output_opts.add_argument(
'-P',
'--profile',
dest='profile',
help='Display values according to specified profile: code, default, human, technical',
type=str,
)

conf_opts = opts.add_argument_group('Configuration')
conf_opts.add_argument('--mediainfo', dest='mediainfo',
help='The location to search for MediaInfo binaries')
conf_opts.add_argument('--ffmpeg', dest='ffmpeg',
help='The location to search for FFmpeg (ffprobe) binaries')
conf_opts.add_argument(
'--mediainfo',
dest='mediainfo',
help='The location to search for MediaInfo binaries',
type=str,
)
conf_opts.add_argument(
'--ffmpeg',
dest='ffmpeg',
help='The location to search for ffprobe (FFmpeg) binaries',
type=str,
)
conf_opts.add_argument(
'--mkvmerge',
dest='mkvmerge',
help='The location to search for mkvmerge (MKVToolNix) binaries',
type=str,
)

information_opts = opts.add_argument_group('Information')
information_opts.add_argument('--version', dest='version', action='store_true',
help='Display knowit version.')
information_opts.add_argument(
'--version',
dest='version',
action='store_true',
help='Display knowit version.'
)

return opts


def knowit(video_path, options, context):
def knowit(
video_path: typing.Union[str, os.PathLike],
options: argparse.Namespace,
context: typing.MutableMapping,
) -> typing.Mapping:
"""Extract video metadata."""
context['path'] = video_path
if not options.report:
Expand All @@ -77,27 +127,49 @@ def knowit(video_path, options, context):
info = api.know(video_path, context)
if not options.report:
console.info('Knowit %s found: ', __version__)
console.info(dump(info, options, context))

console.info(dumps(info, options, context))
return info


def dump(info, options, context):
def _as_yaml(
info: typing.Mapping[str, typing.Any],
context: typing.Mapping,
) -> str:
"""Convert info to string using YAML format."""
data = {info['path']: info} if 'path' in info else info
return yaml.dump(
data,
Dumper=get_yaml_dumper(context),
default_flow_style=False,
allow_unicode=True,
sort_keys=False,
)


def _as_json(
info: typing.Mapping[str, typing.Any],
context: typing.Mapping,
) -> str:
"""Convert info to string using JSON format."""
return json.dumps(
info,
cls=get_json_encoder(context),
indent=4,
ensure_ascii=False,
)


def dumps(
info: typing.Mapping[str, typing.Any],
options: argparse.Namespace,
context: typing.Mapping,
) -> str:
"""Convert info to string using json or yaml format."""
if options.yaml:
data = {info['path']: info} if 'path' in info else info
result = yaml.dump(data, Dumper=get_yaml_dumper(context),
default_flow_style=False, allow_unicode=True)
if PY2:
result = result.decode('utf-8')

else:
result = json.dumps(info, cls=get_json_encoder(context), indent=4, ensure_ascii=False)

return result
convert = _as_yaml if options.yaml else _as_json
return convert(info, context)


def main(args=None):
def main(args: typing.List[str] = None) -> None:
"""Execute main function for entry point."""
argument_parser = build_argument_parser()
args = args or sys.argv[1:]
Expand All @@ -111,40 +183,42 @@ def main(args=None):

paths = recurse_paths(options.videopath)

if paths:
report = {}
for i, videopath in enumerate(paths):
try:
context = dict(vars(options))
if options.report:
context['report'] = report
else:
del context['report']
knowit(videopath, options, context)
except ProviderError:
logger.exception('Error when processing video')
except OSError:
logger.exception('OS error when processing video')
except UnicodeError:
logger.exception('Character encoding error when processing video')
except api.KnowitException as e:
logger.error(e)
if options.report and i % 20 == 19 and report:
console.info('Unknown values so far:')
console.info(dump(report, options, vars(options)))

if options.report:
if report:
console.info('Knowit %s found unknown values:', __version__)
console.info(dump(report, options, vars(options)))
console.info('Please report them at %s', __url__)
if not paths:
if options.version:
console.info(api.debug_info())
else:
argument_parser.print_help()
return

report: typing.MutableMapping[str, str] = {}
for i, video_path in enumerate(paths):
try:
context = {k: v for k, v in vars(options).items() if v is not None}
if options.report:
context['report'] = report
else:
console.info('Knowit %s knows everything. :-)', __version__)

elif options.version:
console.info(api.debug_info())
else:
argument_parser.print_help()
del context['report']
knowit(video_path, options, context)
except ProviderError:
logger.exception('Error when processing video')
except OSError:
logger.exception('OS error when processing video')
except UnicodeError:
logger.exception('Character encoding error when processing video')
except api.KnowitException as e:
logger.error(e)

if options.report and i % 20 == 19 and report:
console.info('Unknown values so far:')
console.info(dumps(report, options, vars(options)))

if options.report:
if report:
console.info('Knowit %s found unknown values:', __version__)
console.info(dumps(report, options, vars(options)))
console.info('Please report them at %s', __url__)
else:
console.info('Knowit %s knows everything. :-)', __version__)


if __name__ == '__main__':
Expand Down
Loading