~ubuntu-branches/ubuntu/trusty/pitivi/trusty

« back to all changes in this revision

Viewing changes to pitivi/settings.py

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2011-07-07 13:43:47 UTC
  • mto: (6.1.9 sid) (1.2.12)
  • mto: This revision was merged to the branch mainline in revision 32.
  • Revision ID: james.westby@ubuntu.com-20110707134347-cari9kxjiakzej9z
Tags: upstream-0.14.1
ImportĀ upstreamĀ versionĀ 0.14.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
1
# PiTiVi , Non-linear video editor
3
2
#
4
3
#       settings.py
17
16
#
18
17
# You should have received a copy of the GNU Lesser General Public
19
18
# License along with this program; if not, write to the
20
 
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21
 
# Boston, MA 02111-1307, USA.
 
19
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 
20
# Boston, MA 02110-1301, USA.
22
21
 
23
22
"""
24
23
Settings
44
43
else:
45
44
    HOME = 'HOME'
46
45
 
 
46
 
47
47
def get_bool_env(var):
48
48
    value = os.getenv(var)
49
49
    if not value:
56
56
    else:
57
57
        return bool(value)
58
58
 
 
59
 
59
60
def get_env_by_type(type_, var):
60
61
    """
61
62
    Returns the environment variable.
77
78
            return type_(os.getenv(var))
78
79
        return None
79
80
 
 
81
 
80
82
def get_env_default(var, default):
81
83
    value = os.getenv(var)
82
84
    if value:
83
85
        return value
84
86
    return default
85
87
 
 
88
 
86
89
def get_dir(path, autocreate=True):
87
90
    if autocreate and not os.path.exists(path):
88
91
        os.makedirs(path)
89
92
    return path
90
93
 
 
94
 
91
95
def get_dirs(glob):
92
96
    return [d for d in glob.split(os.path.pathsep) if os.path.exists(d)]
93
97
 
 
98
 
94
99
def get_env_dir(var, default, autocreate=True):
95
100
    return get_dir(get_env_default(var, default))
96
101
 
 
102
 
97
103
def get_env_dirs(var, default):
98
104
    return get_dirs(get_env_default(var, default))
99
105
 
 
106
 
100
107
def xdg_config_home(autocreate=True):
101
108
    return get_dir(xdg_dirs.xdg_config_home, autocreate)
102
109
 
 
110
 
103
111
def xdg_data_home(autocreate=True):
104
112
    return get_dir(xdg_dirs.xdg_data_home, autocreate)
105
113
 
 
114
 
106
115
def xdg_cache_home(autocreate=True):
107
116
    return get_dir(xdg_dirs.xdg_cache_home, autocreate)
108
117
 
 
118
 
109
119
def xdg_data_dirs():
110
120
    return get_env_dirs(xdg_dirs.xdg_data_dirs)
111
121
 
 
122
 
112
123
def xdg_config_dirs():
113
124
    return get_env_dirs(xdg_dirs.xdg_config_dirs)
114
125
 
 
126
 
115
127
class ConfigError(Exception):
116
128
    pass
117
129
 
 
130
 
118
131
class Notification(object):
119
132
 
120
133
    """A descriptor to help with the implementation of signals"""
130
143
        setattr(instance, self.attrname, value)
131
144
        instance.emit(self.signame)
132
145
 
 
146
 
133
147
class GlobalSettings(Signallable):
134
148
    """
135
149
    Global PiTiVi settings.
237
251
        return get_dir(os.path.join(xdg_config_home(autocreate), "pitivi"),
238
252
            autocreate)
239
253
 
240
 
    def get_local_plugin_path(self, autocreate=True):
241
 
        """
242
 
        Compute the absolute path to local plugin repository
243
 
 
244
 
        @param autocreate: create the path if missing
245
 
        @return: the plugin repository path
246
 
        """
247
 
 
248
 
        return get_dir(
249
 
            os.path.join(
250
 
                get_dir(
251
 
                    os.path.join(
252
 
                        xdg_data_home(autocreate),
253
 
                        "pitivi"),
254
 
                    autocreate),
255
 
                "plugins"),
256
 
            autocreate)
257
 
 
258
 
    def get_plugin_settings_path(self, autocreate=True):
259
 
        """
260
 
        Compute the absolute path to local plugin settings' repository
261
 
 
262
 
        @param autocreate: create the path if missing
263
 
        @return: the plugin settings path
264
 
        """
265
 
 
266
 
        return get_dir(os.path.join(self.get_local_settings_path(autocreate),
267
 
            "plugin-settings"), autocreate)
268
 
 
269
254
    def iterAllOptions(self):
270
255
        """
271
256
        Iterate over all registered options
364
349
            raise ConfigError("Duplicate Section \"%s\"." % section)
365
350
        cls.options[section] = {}
366
351
 
 
352
 
367
353
class StreamEncodeSettings(object):
368
354
    """
369
355
    Settings for encoding a L{MultimediaStream}.
418
404
    def __str__(self):
419
405
        return "<StreamEncodeSettings %s>" % self.encoder
420
406
 
 
407
 
421
408
class RenderSettings(object):
422
409
    """
423
410
    Settings for rendering and multiplexing one or multiple streams.
438
425
    def __str__(self):
439
426
        return "<RenderSettings %s [%d streams]>" % (self.muxer, len(self.settings))
440
427
 
 
428
 
441
429
class ExportSettings(Signallable, Loggable):
442
430
    """
443
431
    Multimedia export settings
444
432
 
445
433
    Signals:
446
 
 
447
434
    'settings-changed' : the settings have changed
448
435
    'encoders-changed' : The encoders or muxer have changed
 
436
 
 
437
    @ivar render_scale: The scale to be applied to the video width and height
 
438
    when rendering.
 
439
    @type render_scale: int
449
440
    """
450
441
    __signals__ = {
451
 
        "settings-changed" : None,
452
 
        "encoders-changed" : None,
 
442
        "settings-changed": None,
 
443
        "encoders-changed": None,
453
444
        }
454
445
 
455
446
    # Audio/Video settings for processing/export
457
448
    # TODO : Add PAR/DAR for video
458
449
    # TODO : switch to using GstFraction internally where appliable
459
450
 
460
 
 
461
 
    # The following dependant attributes caches are common to all instances!
462
 
    # A (muxer -> containersettings) map.
463
 
    _containersettings_cache = {}
464
 
    # A (vencoder -> vcodecsettings) map.
465
 
    _vcodecsettings_cache = {}
466
 
    # A (aencoder -> acodecsettings) map.
467
 
    _acodecsettings_cache = {}
468
 
 
469
451
    muxers, aencoders, vencoders = available_combinations()
470
452
 
471
453
    def __init__(self, **unused_kw):
481
463
        self.vencoder = "theoraenc"
482
464
        self.aencoder = "vorbisenc"
483
465
        self.muxer = "oggmux"
 
466
        # A (muxer -> containersettings) map.
 
467
        self._containersettings_cache = {}
 
468
        # A (vencoder -> vcodecsettings) map.
 
469
        self._vcodecsettings_cache = {}
 
470
        # A (aencoder -> acodecsettings) map.
 
471
        self._acodecsettings_cache = {}
484
472
 
485
473
    def copy(self):
486
474
        ret = ExportSettings()
487
475
        ret.videowidth = self.videowidth
488
476
        ret.videoheight = self.videoheight
 
477
        ret.render_scale = self.render_scale
489
478
        ret.videorate = gst.Fraction(self.videorate.num, self.videorate.denom)
490
479
        ret.videopar = gst.Fraction(self.videopar.num, self.videopar.denom)
491
480
        ret.audiochannels = self.audiochannels
505
494
    def __str__(self):
506
495
        msg = _("Export Settings\n")
507
496
        msg += _("Video: ") + str(self.videowidth) + " " + str(self.videoheight) +\
508
 
               " " + str(self.videorate) + " " + str (self.videopar)
509
 
        msg += "\n\t" + str(self.vencoder) + " " +str(self.vcodecsettings)
 
497
               " " + str(self.videorate) + " " + str(self.videopar)
 
498
        msg += "\n\t" + str(self.vencoder) + " " + str(self.vcodecsettings)
510
499
        msg += _("\nAudio: ") + str(self.audiochannels) + " " + str(self.audiorate) +\
511
500
               " " + str(self.audiodepth)
512
501
        msg += "\n\t" + str(self.aencoder) + " " + str(self.acodecsettings)
529
518
    def getVideoCaps(self, render=False):
530
519
        """ Returns the GstCaps corresponding to the video settings """
531
520
        videowidth, videoheight = self.getVideoWidthAndHeight(render=render)
532
 
        astr = "width=%d,height=%d,pixel-aspect-ratio=%d/%d,framerate=%d/%d" % (
 
521
        vstr = "width=%d,height=%d,pixel-aspect-ratio=%d/%d,framerate=%d/%d" % (
533
522
                videowidth, videoheight,
534
523
                self.videopar.num, self.videopar.denom,
535
524
                self.videorate.num, self.videorate.denom)
536
 
        vcaps = gst.caps_from_string("video/x-raw-yuv,%s;video/x-raw-rgb,%s" % (astr, astr))
 
525
        caps_str = "video/x-raw-yuv,%s;video/x-raw-rgb,%s" % (vstr, vstr)
 
526
        video_caps = gst.caps_from_string(caps_str)
537
527
        if self.vencoder:
538
 
            return get_compatible_sink_caps(self.vencoder, vcaps)
539
 
        return vcaps
 
528
            return get_compatible_sink_caps(self.vencoder, video_caps)
 
529
        return video_caps
540
530
 
541
531
    def getAudioCaps(self):
542
532
        """ Returns the GstCaps corresponding to the audio settings """
543
 
        astr = "rate=%d,channels=%d" % (self.audiorate, self.audiochannels)
544
 
        astrcaps = gst.caps_from_string("audio/x-raw-int,%s;audio/x-raw-float,%s" % (astr, astr))
 
533
        astr = "rate=%d,channels=%d,depth=%d" % (
 
534
                self.audiorate, self.audiochannels, self.audiodepth)
 
535
        caps_str = "audio/x-raw-int,%s;audio/x-raw-float,%s" % (astr, astr)
 
536
        audio_caps = gst.caps_from_string(caps_str)
545
537
        if self.aencoder:
546
 
            return get_compatible_sink_caps(self.aencoder, astrcaps)
547
 
        return astrcaps
548
 
 
549
 
        # interset with current audioencoder sink pad caps
 
538
            return get_compatible_sink_caps(self.aencoder, audio_caps)
 
539
        return audio_caps
550
540
 
551
541
    def setVideoProperties(self, width=-1, height=-1, framerate=-1, par=-1,
552
542
            render_scale=-1):
636
626
        muxer """
637
627
        return self.vencoders[self.muxer]
638
628
 
 
629
 
639
630
def export_settings_to_render_settings(export,
640
631
        have_video=True, have_audio=True):
641
632
    """Convert the specified ExportSettings object to a RenderSettings object.