]> git.sesse.net Git - mlt/blob - src/swig/python/webvfx_generator.py
Merge branch 'frei0r-metadata'
[mlt] / src / swig / python / webvfx_generator.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # webvfx_generator.py
5 # Copyright (C) 2013 Dan Dennedy <dan@dennedy.org>
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software Foundation,
19 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
21 # Import required modules
22 import mlt
23 import time
24 import sys
25 import tornado.ioloop
26 import tornado.web
27 import shutil
28 import tempfile
29 import os
30 import os.path
31
32 # Start the mlt system
33 mlt.mlt_log_set_level(40) # verbose
34 mlt.Factory.init()
35
36 # Establish a pipeline
37 profile = mlt.Profile("atsc_1080i_5994")
38 #profile = mlt.Profile('square_ntsc_wide')
39 profile.set_explicit(1)
40 tractor = mlt.Tractor()
41 tractor.set('eof', 'loop')
42 playlist = mlt.Playlist()
43 playlist.append(mlt.Producer(profile, 'color:'))
44
45 # Setup the consumer
46 consumer = 'decklink:0'
47 if len(sys.argv) > 1:
48   consumer = sys.argv[1]
49 consumer = mlt.Consumer(profile, consumer)
50 consumer.connect(playlist)
51 #consumer.set("real_time", -2)
52 consumer.start()
53
54 def switch(resource):
55   global playlist
56   resource = resource
57   playlist.lock()
58   playlist.append(mlt.Producer(profile, str(resource)))
59   playlist.remove(0)
60   playlist.unlock()
61
62 state = {}
63 state['tempdir'] = None
64
65 class MainHandler(tornado.web.RequestHandler):
66   def get(self):
67     resource = self.get_argument('url', None)
68     if resource:
69       global state
70
71       self.write('Playing %s\n' % (resource))
72       switch(resource)
73
74       olddir = state['tempdir']
75       if olddir:
76         shutil.rmtree(olddir, True)
77       state['tempdir'] = None
78
79     else:
80       self.write('''
81 <p>POST a bunch of files to / to change the output.</p>
82 <p>Or GET / with query string parameter "url" to display something from the network.</p>
83 ''')
84
85   def post(self):
86     if len(self.request.files) == 0:
87       self.write('POST a bunch of files to / to change the output')
88     else:
89       global state
90       olddir = state['tempdir']
91       resource = None
92       state['tempdir'] = tempfile.mkdtemp()
93       for key, items in self.request.files.iteritems():
94         for item in items:
95           path = os.path.dirname(key)
96           fn = item.filename
97           if path: 
98             if not os.path.exists(os.path.join(state['tempdir'], path)):
99               os.makedirs(os.path.join(state['tempdir'], path))
100             fn = os.path.join(path, fn)
101           fn = os.path.join(state['tempdir'], fn)
102           if not path and fn.endswith('.html') or fn.endswith('.qml'):
103             resource = fn
104           with open(fn, 'w') as fo:
105             fo.write(item.body)
106           self.write("Uploaded %s\n" % (fn))
107       if resource:
108         self.write('Playing %s\n' % (resource))
109         switch(resource)
110       if olddir:
111         shutil.rmtree(olddir, True)
112
113 application = tornado.web.Application([
114     (r"/", MainHandler),
115 ])
116
117 application.listen(8888)
118 try:
119   tornado.ioloop.IOLoop.instance().start()
120 except:
121   pass
122
123 consumer.stop()
124 if state['tempdir']:
125   shutil.rmtree(state['tempdir'], True)