~ubuntu-branches/ubuntu/utopic/lazygal/utopic

« back to all changes in this revision

Viewing changes to lazygal/mediautils.py

  • Committer: Package Import Robot
  • Author(s): Michal Čihař, Michal Čihař, Jakub Wilk
  • Date: 2013-06-06 12:05:08 UTC
  • mfrom: (1.2.13)
  • Revision ID: package-import@ubuntu.com-20130606120508-e3g94vl8w9pw7za7
Tags: 0.8-1
[ Michal Čihař ]
* New upstream release.
  - Uses new Genshi templates (Closes: #696682).
  - Correctly handles wronly encoded artist in EXIF (Closes: #696648).
  - Uses GExiv2 (LP: #1074028).
* Depend on GExiv2 instead of pyexiv2.
* Bump standards to 3.9.4.
* Use debhelper 9.

[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
 
40
40
 
41
41
interrupted = False
 
42
 
 
43
 
42
44
def signal_handler(signum, frame):
43
45
    global interrupted
44
46
    interrupted = True
51
53
    signal.signal(signal.SIGINT, signal_handler)
52
54
 
53
55
 
54
 
import Image as PILImage
 
56
from PIL import Image as PILImage
55
57
 
56
58
 
57
59
class TranscodeError(Exception): pass
95
97
        if not gobjects_threads_init:
96
98
            gobject_init()
97
99
 
98
 
        self.filesrc.set_property("location",
99
 
                            self.input_file.encode(sys.getfilesystemencoding()))
 
100
        self.filesrc.set_property(
 
101
            "location", self.input_file.encode(sys.getfilesystemencoding()))
100
102
 
101
103
    def check_interrupt(self):
102
104
        if interrupted:
104
106
                                              gst.Structure('interrupted'))
105
107
            self.pipeline.get_bus().post(msg)
106
108
        if interrupted or not self.running:
107
 
            return False # Remove timeout handler
 
109
            return False  # Remove timeout handler
108
110
        return True
109
111
 
110
112
    def __stop_pipeline(self):
146
148
        self.loop = gobject.MainLoop()
147
149
 
148
150
    def __stream_discovered(self, obj, success):
 
151
        if success:
 
152
            self.videowidth = obj.videowidth
 
153
            self.videoheight = obj.videoheight
 
154
            self.videorate = obj.videorate
 
155
            self.audiorate = obj.audiorate
 
156
            self.audiodepth = obj.audiodepth
 
157
            self.audiowidth = obj.audiowidth
 
158
            self.audiochannels = obj.audiochannels
 
159
 
 
160
            self.audiolength = obj.audiolength
 
161
            self.videolength = obj.videolength
 
162
 
 
163
            self.is_video = obj.is_video
 
164
            self.is_audio = obj.is_audio
 
165
        else: # TODO : handle error
 
166
            pass
 
167
 
149
168
        self.loop.quit()
150
169
 
151
170
    def inspect(self):
158
177
        discoverer.discover()
159
178
        self.loop.run()
160
179
 
161
 
        self.videowidth = discoverer.videowidth
162
 
        self.videoheight = discoverer.videoheight
163
 
        self.videorate = discoverer.videorate
164
 
        self.audiorate = discoverer.audiorate
165
 
        self.audiodepth = discoverer.audiodepth
166
 
        self.audiowidth = discoverer.audiowidth
167
 
        self.audiochannels = discoverer.audiochannels
168
 
 
169
 
        self.audiolength = discoverer.audiolength
170
 
        self.videolength = discoverer.videolength
171
 
 
172
 
        self.is_video = discoverer.is_video
173
 
        self.is_audio = discoverer.is_audio
174
 
 
175
 
 
176
180
class GstVideoReader(GstVideoOpener):
177
181
 
178
182
    def __init__(self, mediapath):
344
348
 
345
349
        # RGB is what is assumed in order to load the frame into a PIL Image.
346
350
        self.capsfilter = gst.element_factory_make('capsfilter')
347
 
        self.capsfilter.set_property('caps',
348
 
                              gst.Caps('video/x-raw-rgb,framerate=%s/1' % fps))
 
351
        self.capsfilter.set_property(
 
352
            'caps', gst.Caps('video/x-raw-rgb,framerate=%s/1' % fps))
349
353
        self.pipeline.add(self.capsfilter)
350
354
        videorate.link(self.capsfilter)
351
355
 
402
406
    def __init__(self, path, fps, intro_seconds):
403
407
        super(VideoBestFrameFinder, self).__init__(path, fps)
404
408
 
405
 
        self.max_frames = self.fps * intro_seconds # Frames to go through
 
409
        self.max_frames = self.fps * intro_seconds  # Frames to go through
406
410
        self.histograms = []
407
411
 
408
412
    def cb_grabbed_frame_buf(self, buf):
427
431
        for value_index in range(n_values):
428
432
            average = 0.0
429
433
            for histogram in self.histograms:
430
 
                average = average + (float(histogram[value_index])/n_samples)
 
434
                average = average + (float(histogram[value_index]) / n_samples)
431
435
            average_hist.append(average)
432
436
 
433
437
        # Find histogram closest to average histogram
438
442
            mse = 0.0
439
443
            for value_index in range(n_values):
440
444
                gap = average_hist[value_index] - hist[value_index]
441
 
                mse = mse + gap*gap
 
445
                mse = mse + gap * gap
442
446
 
443
447
            if min_mse is None or mse < min_mse:
444
448
                min_mse = mse
476
480
 
477
481
 
478
482
if __name__ == '__main__':
479
 
    import sys, os
 
483
    import sys
 
484
    import os
480
485
 
481
486
    converter_types = sys.argv[1].split(',')
482
487
    converters = {}
497
502
        file_path = file_path.decode(sys.getfilesystemencoding())
498
503
        fn, ext = os.path.splitext(os.path.basename(file_path))
499
504
        for converter_type, converter in converters.items():
500
 
            target_path = fn + '.' + converter_type
 
505
 
 
506
            counter_str = ''
 
507
            counter = 0
 
508
            filename_free = False
 
509
            while not filename_free:
 
510
                target_path = fn + counter_str + '.' + converter_type
 
511
                if os.path.isfile(target_path):
 
512
                    counter = counter + 1
 
513
                    counter_str = '_%d' % counter
 
514
                else:
 
515
                    filename_free = True
 
516
 
501
517
            converter(file_path).convert(target_path)
502
518
 
503
519