aboutsummaryrefslogtreecommitdiffstats
path: root/lvc/ui/console.py
blob: 871bfd60310c7f5312d6b3857603802eaf8d8872 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import json
import operator
import optparse
import time
import sys

import lvc

from lvc.variables import (
    __version__
)

from lvc.widgets import app
from lvc.widgets import initialize

parser = optparse.OptionParser(
    usage='%prog [-l] [--list-converters] [-c <converter> <filenames..>]',
    version='%prog ' + __version__,
    prog='python -m lvc.ui.console')
parser.add_option('-j', '--json', action='store_true',
                  dest='json',
                  help='Output JSON documents, rather than text.')
parser.add_option('-l', '--list-converters', action='store_true',
                  dest='list_converters',
                  help="Print a list of supported converter types.")
parser.add_option('-c', '--converter', dest='converter',
                  help="Specify the type of conversion to make.")


class Application(lvc.Application):

    def run(self):
        (options, args) = parser.parse_args()

        if options.list_converters:
            for c in sorted(self.converter_manager.list_converters(),
                            key=operator.attrgetter('name')):
                if options.json:
                    print(json.dumps({'name': c.name,
                                      'identifier': c.identifier}))
                else:
                    print('%s (-c %s)' % (
                        c.name,
                        c.identifier))
            return

        try:
            self.converter_manager.get_by_id(options.converter)
        except KeyError:
            message = '%r is not a valid converter type.' % (
                options.converter,)
            if options.json:
                print(json.dumps({'error': message}))
            else:
                print('ERROR:', message)
                print('Use "%s -l" to get a list of valid converters.' % (
                    parser.prog,))
                print('')
                parser.print_help()
            sys.exit(1)

        any_failed = False

        def changed(c):
            if c.status == 'failed':
                any_failed = True
            if options.json:
                output = {
                    'filename': c.video.filename,
                    'output': c.output,
                    'status': c.status,
                    'duration': c.duration,
                    'progress': c.progress,
                    'percent': (c.progress_percent * 100 if c.progress_percent
                                else 0),
                    }
                if c.error is not None:
                    output['error'] = c.error
                print(json.dumps(output))
            else:
                if c.status == 'initialized':
                    line = 'starting (output: %s)' % (c.output,)
                elif c.status == 'converting':
                    if c.progress_percent is not None:
                        line = 'converting (%i%% complete, %is remaining)' % (
                            c.progress_percent * 100, c.eta)
                    else:
                        line = 'converting (0% complete, unknown remaining)'
                elif c.status == 'staging':
                    line = 'staging'
                elif c.status == 'failed':
                    line = 'failed (error: %r)' % (c.error,)
                elif c.status == 'finished':
                    line = 'finished (output: %s)' % (c.output,)
                else:
                    line = c.status
                print('%s: %s' % (c.video.filename, line))

        for filename in args:
            try:
                c = app.start_conversion(filename, options.converter)
            except ValueError:
                message = 'could not parse %r' % filename
                if options.json:
                    any_failed = True
                    print(json.dumps({'status': 'failed', 'error': message,
                                      'filename': filename}))
                else:
                    print('ERROR:', message)
                continue
            changed(c)
            c.listen(changed)

        # XXX real mainloop
        while self.conversion_manager.running:
            self.conversion_manager.check_notifications()
            time.sleep(1)
        self.conversion_manager.check_notifications()  # one last time

        sys.exit(0 if not any_failed else 1)


if __name__ == "__main__":
    initialize(None)
    app.widgetapp = Application()
    app.widgetapp.startup()
    app.widgetapp.run()