CIS: BitTorrent support added; master, worker and queue threading facilities added...
[living-lab-site.git] / cis / cisd.py
1 #!/usr/bin/env python
2
3 import sys
4 import os
5 import time
6 import threading
7 from Queue import Queue
8
9 import config
10 import bt
11
12
13 class CIWorker(threading.Thread):
14     """
15     Content Ingestion Worker. A class which executes content ingestion jobs
16     on a separate thread.
17
18     CIWorker shares a Queue with its master where jobs are submitted.
19     """
20
21     def __init__(self, queue):
22         threading.Thread.__init__(self)
23
24         self.queue = queue
25
26     def run(self):
27         while True:
28             job = self.queue.get()
29
30             # * TRANSFER RAW VIDEO IN
31             file_transfer = config.FILE_TRANSFERER_CLASS( \
32                     'tmp/raw', config.INPUT_PATH)
33             file_transfer.get([job.raw_video])
34             file_transfer.close()
35
36             # * TRANSCODE RAW VIDEO
37             transcoder = config.TRANSCODER_CLASS(input_file = job.raw_video, \
38                     name = job.name, prog_bin = config.TRANSCODER_BIN)
39             
40             # Transcode the raw video in each requested format.
41             for transcode_config in job.transcode_configs:
42                 transcode_config['output_file'] = transcoder.transcode( \
43                        container = transcode_config.container, \ 
44                        a_codec = transcode_config.a_codec, \
45                        a_bitrate = transcode_config.a_bitrate, \ 
46                        a_samplingrate = transcode_config.a_samplingrate, \ 
47                        a_channels = transcode_config.a_channels, \ 
48                        v_codec = transcode_config.v_codec, \ 
49                        v_bitrate = transcode_config.v_bitrate, \ 
50                        v_framerate = transcode_config.v_framerate, \ 
51                        v_resolution = transcode_config.v_resolution, \ 
52                        v_dar = transcode_config.dar)
53
54             # * EXTRACT THUMBNAIL IMAGES
55             thumb_extractor = config.THUMB_EXTRACTOR_CLASS( \
56                     input_file = job.raw_video, name = job.name, \
57                     prog_bin = config.THUMB_EXTRACTOR_BIN)
58             # TODO thumbnail extraction type must be got from input
59             thumb_extractor.extract_random_thumb()
60             print thumb_extractor.extract_summary_thumbs(5)
61
62
63             queue.task_done()
64
65
66 class TranscodeConfig:
67     """
68     Structure that contains parameters for a transcoding procedure.
69     """
70
71     def __init__(self, container, a_codec, v_codec,
72             a_bitrate, a_samplingrate, a_channels,
73             v_bitrate, v_framerate, v_resolution, v_dar):
74
75         self.container = container
76         self.a_codec = a_codec
77         self.v_codec = v_codec
78         self.a_bitrate = a_bitrate
79         self.a_samplingrate = a_samplingrate
80         self.a_channels = a_channels
81         self.v_bitrate = v_bitrate
82         self.v_framerate = v_framerate
83         self.v_resolution = v_resolution
84         self.v_dar = v_dar
85
86
87
88 class Job:
89     """
90     Structure that contains information about a job.
91
92     Members are documented in the constructor.
93     """
94
95     def __init__(self, raw_video, name, transcode_configs):
96         """
97         @param raw_video the input raw video file name transfered from WS
98         @param name video name (must be a valid file name)
99         @param transcode_configs a list of TranscodeConfig instances
100         """
101
102         self.raw_video = raw_video
103         self.name = name
104         self.transcode_configs
105
106
107 if __name__ == '__main__':
108     # Jobs queue.
109     queue = Queue()
110
111     # Worker thread.
112     ci_worker = CIWorker(queue)
113     ci_worker.daemon = True
114     ci_worker.start()
115
116     while True:
117         raw_video = sys.stdin.readline().strip()
118         if raw_video == 'x':
119             break
120
121         job = Job(raw_video)
122         queue.put(job)
123
124     queue.join()
125
126
127
128
129 #    transcoder = config.TRANSCODER_CLASS(sys.argv[1])
130 #    transcoder.transcode('webm', "vorbis", "vp8", a_bitrate="128k", a_samplingrate=22050, a_channels=2, v_bitrate="256k", v_framerate=15, v_resolution="320x240", v_dar="4:3")
131     
132 #    thumb_extractor = config.THUMB_EXTRACTOR_CLASS(sys.argv[1])
133 #    #print thumb_extractor.get_video_duration()
134 #    #thumb_extractor.extract_random_thumb()
135 #    print thumb_extractor.extract_summary_thumbs(5)
136
137 #    file_transfer = config.FILE_TRANSFERER_CLASS()
138 #    file_transfer.get(['vim_config.tar.gz'])
139 #    #file_transfer.put(['cisd.py'])
140 #    file_transfer.close()
141
142 #    create_torrent(sys.argv[1])
143
144 #    bt_inst = bt.BitTorrent()
145 #
146 #    bt_inst.download(sys.argv[1], '/tmp')
147 #    bt_inst.download(sys.argv[2], '/tmp')
148 #
149 #    print threading.active_count(), threading.enumerate()
150 #    time.sleep(30)