aboutsummaryrefslogtreecommitdiff
path: root/command.py
blob: 97eddd2e8a5510f14cdcafd88f68afdc853213a1 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from PyQt4 import QtCore
from PyQt4.QtCore import QSettings
import argparse
import os

import core
import video_thread
from main import LoadDefaultSettings


class Command(QtCore.QObject):

    videoTask = QtCore.pyqtSignal(str, str, list)

    def __init__(self):
        QtCore.QObject.__init__(self)
        self.core = core.Core()
        self.dataDir = self.core.dataDir

        self.parser = argparse.ArgumentParser(
            description='Create a visualization for an audio file',
            epilog='EXAMPLE COMMAND:   main.py myvideotemplate.avp '
                '-i ~/Music/song.mp3 -o ~/video.mp4 '
                '-c 0 image ~/Pictures/thisWeeksPicture.jpg '
                '-c 1 vis classic')
        self.parser.add_argument(
            '-i', '--input', metavar='SOUND',
            help='input audio file', required=True)
        self.parser.add_argument(
            '-o', '--output', metavar='OUTPUT',
            help='output video file', required=True)

        # optional arguments
        self.parser.add_argument(
            'projpath', metavar='path-to-project',
            help='open a project file (.avp)', nargs='?')
        self.parser.add_argument(
            '-c', '--comp', metavar=('LAYER', 'NAME', 'ARG'),
            help='create component NAME at LAYER.'
            '"help" for information about possible args', nargs=3,
            action='append')

        '''
        self.parser.add_argument(
            '-b', '--background', dest='bgimage',
            help='background image file', required=True)
        self.parser.add_argument(
            '-t', '--text', dest='text', help='title text', required=True)
        self.parser.add_argument(
            '-f', '--font', dest='font', help='title font', required=False)
        self.parser.add_argument(
            '-s', '--fontsize', dest='fontsize',
            help='title font size', required=False)
        self.parser.add_argument(
            '-c', '--textcolor', dest='textcolor',
            help='title text color in r,g,b format', required=False)
        self.parser.add_argument(
            '-C', '--viscolor', dest='viscolor',
            help='visualization color in r,g,b format', required=False)
        self.parser.add_argument(
            '-x', '--xposition', dest='xposition',
            help='x position', required=False)
        self.parser.add_argument(
            '-y', '--yposition', dest='yposition',
            help='y position', required=False)
        self.parser.add_argument(
            '-a', '--alignment', dest='alignment',
            help='title alignment', required=False,
            type=int, choices=[0, 1, 2])
        '''

        self.args = self.parser.parse_args()
        self.settings = QSettings(
            os.path.join(self.dataDir, 'settings.ini'), QSettings.IniFormat)
        LoadDefaultSettings(self)

        if self.args.projpath:
            self.core.openProject(self, self.args.projpath)

        if self.args.comp:
            for comp in self.args.comp:
                pos, name, arg = comp
                try:
                    pos = int(pos)
                except ValueError:
                    print(pos, 'is not a layer number.')
                    quit(1)
                realName = self.parseCompName(name)
                if not realName:
                    print(name, 'is not a valid component name.')
                    quit(1)
                modI = self.core.moduleIndexFor(realName)
                i = self.core.insertComponent(pos, modI, self)
                self.core.selectedComponents[i].command(arg)

        self.createAudioVisualisation()

    def createAudioVisualisation(self):
        self.videoThread = QtCore.QThread(self)
        self.videoWorker = video_thread.Worker(self)
        self.videoWorker.moveToThread(self.videoThread)
        self.videoWorker.videoCreated.connect(self.videoCreated)

        self.videoThread.start()
        self.videoTask.emit(
          self.args.input,
          self.args.output,
          list(reversed(self.core.selectedComponents))
        )

    def videoCreated(self):
        self.videoThread.quit()
        self.videoThread.wait()

    def showMessage(self, **kwargs):
        print(kwargs['msg'])
        if 'detail' in kwargs:
            print(kwargs['detail'])

    def drawPreview(self, *args):
        pass

    def parseCompName(self, name):
        '''Deduces a proper component name out of a commandline arg'''

        if name.title() in self.core.compNames:
            return name.title()
        for compName in self.core.compNames:
            if name.capitalize() in compName:
                return compName

        compFileNames = [ \
            os.path.splitext(os.path.basename(
                mod.__file__))[0] \
            for mod in self.core.modules \
        ]
        for i, compFileName in enumerate(compFileNames):
            if name.lower() in compFileName:
                return self.core.compNames[i]
            return

        return None