forked from danielgtaylor/arista
-
Notifications
You must be signed in to change notification settings - Fork 1
/
arista-gtk
executable file
·1684 lines (1396 loc) · 63.1 KB
/
arista-gtk
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
"""
Arista Desktop Transcoder (GTK+ client)
=======================================
An audio/video transcoder based on simple device profiles provided by
presets. This is the GTK+ version.
License
-------
Copyright 2008 - 2010 Daniel G. Taylor <[email protected]>
This file is part of Arista.
Arista is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 2.1 of
the License, or (at your option) any later version.
Arista is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Arista. If not, see
<http://www.gnu.org/licenses/>.
"""
import gettext
import locale
import logging
import os
import re
import sys
import threading
import time
import webbrowser
from optparse import OptionParser
import gobject
import gio
import gconf
import cairo
import gtk
# FIXME: Stupid hack, see the other fixme comment below!
if __name__ != "__main__":
import gst
try:
import pynotify
pynotify.init("icon-summary-body")
except ImportError:
pynotify = None
import arista
_ = gettext.gettext
_log = logging.getLogger("arista-gtk")
locale.setlocale(locale.LC_ALL, '')
CONFIG_PATH = "/apps/arista"
DEFAULT_CHECK_INPUTS = True
DEFAULT_SHOW_TOOLBAR = True
DEFAULT_SHOW_PREVIEW = True
DEFAULT_PREVIEW_FPS = 10
DEFAULT_CHECK_UPDATES = True
DEFAULT_OPEN_PATH = os.path.expanduser("~")
RE_ENDS_NUM = re.compile(r'^.*(?P<number>[0-9]+)$')
def _new_combo_with_image(extra = []):
"""
Create a new combo box with a list store of a pixbuf, a string, and any
extra passed types.
@type extra: list
@param extra: Extra types to add to the gtk.ListStore
@rtype: gtk.ComboBox
@return: The newly created combo box
"""
store = gtk.ListStore(gtk.gdk.Pixbuf, gobject.TYPE_STRING, *extra)
combo = gtk.ComboBox(store)
pixbuf_cell = gtk.CellRendererPixbuf()
text_cell = gtk.CellRendererText()
combo.pack_start(pixbuf_cell, False)
combo.pack_start(text_cell, True)
combo.add_attribute(pixbuf_cell, 'pixbuf', 0)
combo.add_attribute(text_cell, 'text', 1)
return combo
def _get_icon_pixbuf(uri, width, height):
"""
Get a pixbuf from an item with an icon URI set.
@type item: object
@param item: An object with an icon attribute
@type width: int
@param width: The requested width of the pixbuf
@type height: int
@param height: The requested height of the pixbuf
@rtype: gtk.Pixbuf or None
@return: The pixbuf of the icon if it can be found
"""
image = None
theme = gtk.icon_theme_get_default()
if not uri:
return image
if uri.startswith("file://"):
try:
path = arista.utils.get_path("presets", uri[7:])
except IOError:
path = ""
if os.path.exists(path):
image = gtk.gdk.pixbuf_new_from_file_at_size(path, width, height)
elif uri.startswith("stock://"):
image = theme.load_icon(uri[8:], gtk.ICON_SIZE_MENU, 0)
else:
raise ValueError(_("Unknown icon URI %(uri)s") % {
"uri": uri
})
return image
def _get_filename_icon(filename):
"""
Get the icon from a filename using GIO.
>>> icon = _get_filename_icon("test.mp4")
>>> if icon:
>>> # Do something here using icon.load_icon()
>>> ...
@type filename: str
@param filename: The name of the file whose icon to fetch
@rtype: gtk.ThemedIcon or None
@return: The requested unloaded icon or nothing if it cannot be found
"""
theme = gtk.icon_theme_get_default()
names = gio.content_type_get_icon(gio.content_type_guess(filename)).get_property("names")
icon = theme.choose_icon(names, gtk.ICON_SIZE_MENU, 0)
return icon
class UpdateChecker(threading.Thread):
"""
A thread to check for updates on startup.
"""
def __init__(self, main):
"""
@type main: MainWindow
@param main: The main Arista window
"""
self.main = main
super(UpdateChecker, self).__init__()
def run(self):
"""
Check for updates and present the user with the option of
installing any updated presets, then install them if she chooses
to. After the installation another dialog is shown asking to
restart the program.
"""
client = gconf.client_get_default()
try:
last_check = client.get_value(CONFIG_PATH + "/last_update_check")
except ValueError:
last_check = 0
# Let's not hammer the server - wait minimum of an hour between checks
if time.time() - last_check < 60 * 60:
return
# Set last update check time
client.set_value(CONFIG_PATH + "/last_update_check", time.time())
# Do the update checks
updates = arista.presets.check_for_updates()
if updates:
gtk.gdk.threads_enter()
dialog = gtk.MessageDialog(parent = self.main.window,
type = gtk.MESSAGE_QUESTION,
buttons = gtk.BUTTONS_YES_NO,
message_format = _("There are %(count)d new or updated " \
"device presets available. Install " \
"them now?") % {
"count": len(updates),
})
result = dialog.run()
dialog.hide()
gtk.gdk.threads_leave()
if result == gtk.RESPONSE_YES:
devices = []
for loc, name in updates:
devices += arista.presets.fetch(loc, name)
for device in devices:
icon = arista.utils.get_path("presets/" + arista.presets.get()[device].icon[7:])
notice = pynotify.Notification(_("Update Successful"), _("Device preset %(name)s successfully updated.") % {
"name": arista.presets.get()[device],
}, icon)
notice.show()
gtk.gdk.threads_enter()
dialog.destroy()
arista.presets.reset()
self.main.setup_devices()
gtk.gdk.threads_leave()
else:
gtk.gdk.threads_enter()
dialog.destroy()
gtk.gdk.threads_leave()
class LogoWidget(gtk.Widget):
"""
A widget to show the Arista logo.
See http://svn.gnome.org/viewvc/pygtk/trunk/examples/gtk/widget.py?view=markup
"""
def __init__(self):
gtk.Widget.__init__(self)
# Load the logo overlay
logo_path = arista.utils.get_path("ui", "logo.svg")
self.pixbuf = gtk.gdk.pixbuf_new_from_file(logo_path)
def do_realize(self):
"""
Realize the widget. Setup the window.
"""
self.set_flags(self.flags() | gtk.REALIZED)
self.window = gtk.gdk.Window(
self.get_parent_window(),
width = self.allocation.width,
height = self.allocation.height,
window_type = gtk.gdk.WINDOW_CHILD,
wclass = gtk.gdk.INPUT_OUTPUT,
event_mask = self.get_events() | gtk.gdk.EXPOSURE_MASK)
self.window.set_user_data(self)
self.style.attach(self.window)
self.style.set_background(self.window, gtk.STATE_NORMAL)
self.window.move_resize(*self.allocation)
self.gc = self.style.fg_gc[gtk.STATE_NORMAL]
def do_unrealize(self):
"""
Destroy the window.
"""
self.window.destroy()
def do_size_request(self, requisition):
"""
Request a minimum size.
"""
requisition.width = self.pixbuf.get_width()
requisition.height = self.pixbuf.get_height()
def do_size_allocate(self, allocation):
"""
Our size was allocated, save it!
"""
self.allocation = allocation
if self.flags() & gtk.REALIZED:
self.window.move_resize(*allocation)
def do_expose_event(self, event):
"""
Draw the logo.
"""
x, y, w, h = self.allocation
cr = self.window.cairo_create()
# Base the background color on a 50% luminosity version of the theme's
# selected color (the color you usually see in progress bars, for
# example) and make the gradient go from slightly lighter to slightly
# darker.
color = self.style.bg[gtk.STATE_SELECTED]
r, g, b = color.red / 65535.0, color.green / 65535.0, \
color.blue / 65535.0
avg = (r + g + b) / 3.0
r, g, b = [i + 0.5 - avg for i in [r, g, b]]
# Draw a gradient background
gradient = cairo.LinearGradient(0, 0, 0, h)
gradient.add_color_stop_rgb(0.0, r * 1.1, g * 1.1, b * 1.1)
gradient.add_color_stop_rgb(1.0, r, g, b)
cr.rectangle(0, 0, w, h)
cr.set_source(gradient)
cr.fill()
# Draw block shadow area
gradient = cairo.LinearGradient(1, (h / 2) + 5, 1, (h / 2) + 115)
gradient.add_color_stop_rgba(0.0, r * 0.95, g * 0.95, b * 0.95, 0.0)
gradient.add_color_stop_rgba(0.5, r * 0.95, g * 0.95, b * 0.95, 1.0)
gradient.add_color_stop_rgba(0.6, r * 0.9, g * 0.9, b * 0.9, 1.0)
gradient.add_color_stop_rgba(1.0, r * 0.9, g * 0.9, b * 0.9, 0.0)
cr.rectangle(1, (h / 2) + 5, w - 2, 30)
cr.rectangle(1, (h / 2) + 35 + 45, w - 2, 35)
cr.set_source(gradient)
cr.fill()
# Draw a highlighted block
cr.set_source_rgba(1.0, 1.0, 1.0, 0.13)
cr.rectangle(1, (h / 2) + 35, w - 2, 45)
cr.fill()
# Draw a border around the highlighted block
cr.rectangle(1, (h / 2) + 35, w - 2, 1)
cr.rectangle(1, (h / 2) + 35 + 45 - 1, w - 2, 1)
cr.fill()
# Draw the outer border
cr.set_source_rgba(0.0, 0.0, 0.0, 0.5)
cr.set_line_width(1.0)
cr.rectangle(0, 0, w, h)
cr.stroke()
# Draw the logo svg centered in the widget
self.window.draw_pixbuf(self.gc, self.pixbuf, 0, 0,
(w / 2) - (self.pixbuf.get_width() / 2),
(h / 2) - (self.pixbuf.get_height() / 2))
gobject.type_register(LogoWidget)
class MainWindow(object):
"""
Arista Main Window
==================
The main transcoder window. Provides a method of selecting a source,
output device, and preset for transcoding as well as managing the
transcoding queue.
"""
def __init__(self, runoptions):
self.runoptions = runoptions
ui_path = arista.utils.get_path("ui", "main.ui")
# Load the GUI
self.builder = gtk.Builder()
self.builder.add_from_file(ui_path)
self.builder.connect_signals(self)
self.window = self.builder.get_object("main_window")
self.menuitem_remove = self.builder.get_object("menuitem_remove")
self.menuitem_pause = self.builder.get_object("menuitem_pause")
self.menuitem_toolbar = self.builder.get_object("menuitem_toolbar")
self.toolbar = self.builder.get_object("toolbar")
self.toolbutton_remove = self.builder.get_object("toolbutton_remove")
self.toolbutton_pause = self.builder.get_object("toolbutton_pause")
self.devices = _new_combo_with_image([gobject.TYPE_PYOBJECT])
self.presets = _new_combo_with_image([gobject.TYPE_PYOBJECT])
self.hbox_progress = self.builder.get_object("hbox_progress")
self.progress = self.builder.get_object("progressbar")
self.button_cancel = self.builder.get_object("button_cancel")
self.preview = self.builder.get_object("video_preview")
self.settings_frame = self.builder.get_object("settings_frame")
self.preview_frame = self.builder.get_object("preview_frame")
self.queue_view = self.builder.get_object("queue")
self.source = None
self.source_hbox = None
self.finder = None
self.finder_video_found = None
self.finder_video_lost = None
self.image_preview = gtk.Alignment(xscale = 1.0, yscale = 1.0)
self.image_preview.set_padding(0, 5, 0, 0)
self.image_preview.add(LogoWidget())
self.builder.get_object("vbox_preview").pack_start(self.image_preview)
self.table = self.builder.get_object("settings_table")
self.table.attach(self.devices, 1, 2, 1, 2, yoptions = gtk.FILL)
self.table.attach(self.presets, 1, 2, 2, 3, yoptions = gtk.FILL)
self.setup_source()
self.setup_devices()
self.fileiter = None
self.transcoder = None
self.options = arista.transcoder.TranscoderOptions()
# Setup the transcoding queue and watch for events
self.queue = arista.queue.TranscodeQueue()
self.queue.connect("entry-discovered", self.on_queue_entry_discovered)
self.queue.connect("entry-error", self.on_queue_entry_error)
self.queue.connect("entry-complete", self.on_queue_entry_complete)
self.queue_model = gtk.ListStore(gtk.gdk.Pixbuf, # Stock image
gobject.TYPE_STRING, # Description
gobject.TYPE_PYOBJECT)
self.queue_view.set_model(self.queue_model)
self.queue_model.connect("row-changed", self.on_queue_row_changed)
self.queue_model.connect("row-deleted", self.on_queue_row_deleted)
self.queue_view.get_selection().connect("changed",
self.on_queue_selection_changed)
pixbuf_renderer = gtk.CellRendererPixbuf()
text_renderer = gtk.CellRendererText()
column = gtk.TreeViewColumn(_("Description"))
column.pack_start(pixbuf_renderer, False)
column.set_attributes(pixbuf_renderer, pixbuf = 0)
column.pack_start(text_renderer)
column.set_attributes(text_renderer, text = 1)
self.queue_view.append_column(column)
# Setup configuration system
client = gconf.client_get_default()
client.add_dir(CONFIG_PATH, gconf.CLIENT_PRELOAD_NONE)
# Update UI to reflect currently stored settings
try:
value = client.get_value(CONFIG_PATH + "/show_toolbar")
if value:
self.toolbar.show()
else:
self.toolbar.hide()
self.menuitem_toolbar.set_active(value)
except ValueError:
if DEFAULT_SHOW_TOOLBAR:
self.toolbar.show()
else:
self.toolbar.hide()
self.menuitem_toolbar.set_active(DEFAULT_SHOW_TOOLBAR)
try:
value = client.get_value(CONFIG_PATH + "/last_open_path")
if value and os.path.exists(value):
self.last_open_path = value
else:
self.last_open_path = DEFAULT_OPEN_PATH
except ValueError:
self.last_open_path = DEFAULT_OPEN_PATH
client.notify_add(CONFIG_PATH + "/show_toolbar",
self.on_gconf_show_toolbar)
client.notify_add(CONFIG_PATH + "/check_inputs", self.setup_source)
client.notify_add(CONFIG_PATH + "/last_open_path",
self.on_gconf_last_open_path)
# Show the interface!
self.source.show()
self.devices.show()
self.presets.show()
self.preview.hide()
self.hbox_progress.hide()
self.image_preview.show_all()
self.window.show()
# Are we using the simplified interface? Hide stuff!
if self.runoptions.simple:
self.builder.get_object("menubar").hide()
self.toolbar.hide()
self.builder.get_object("vbox3").hide()
self.window.resize(320, 240)
device = arista.presets.get()[self.runoptions.device]
if not self.runoptions.preset:
preset = device.presets[device.default]
else:
for (id, preset) in device.presets.items():
if preset.name == options.preset:
break
outputs = []
for fname in self.runoptions.files:
output = arista.utils.generate_output_path(fname, preset,
to_be_created=outputs,
device_name=self.runoptions.device)
outputs.append(output)
opts = arista.transcoder.TranscoderOptions(fname, preset, output)
self.queue.append(opts)
def setup_source(self, *args):
"""
Setup the source widget. Creates a combo box or a file input button
depending on the settings and available devices.
"""
theme = gtk.icon_theme_get_default()
size = gtk.ICON_SIZE_MENU
# Already exists? Remove it!
if self.source:
self.source_hbox.remove(self.source)
self.source.destroy()
if self.finder:
if self.finder_disc_found is not None:
self.finder.disconnect(self.finder_disc_found)
self.finder_disc_found = None
if self.finder_disc_lost is not None:
self.finder.disconnect(self.finder_disc_lost)
self.finder_disc_lost = None
# Should we check for DVD drives?
client = gconf.client_get_default()
try:
check_inputs = client.get_value(CONFIG_PATH + "/check_inputs")
except ValueError:
check_inputs = DEFAULT_CHECK_INPUTS
if check_inputs:
# Setup input source discovery
# Adds DVD and V4L devices to the source combo box
if not self.finder:
self.finder = arista.inputs.InputFinder()
if len(self.finder.drives) or len(self.finder.capture_devices):
icon = gtk.stock_lookup(gtk.STOCK_CDROM)[0]
self.source = _new_combo_with_image([gobject.TYPE_PYOBJECT])
model = self.source.get_model()
for block, drive in self.finder.drives.items():
iter = model.append()
model.set_value(iter, 0, theme.load_icon(icon, size, 0))
model.set_value(iter, 1, drive.nice_label)
model.set_value(iter, 2, "dvd://" + block)
for device, capture in self.finder.capture_devices.items():
iter = model.append()
model.set_value(iter, 0, theme.load_icon("camera-video",
size, 0))
model.set_value(iter, 1, capture.nice_label)
if capture.version == '1':
model.set_value(iter, 2, "v4l://" + device)
elif capture.version == '2':
model.set_value(iter, 2, "v4l2://" + device)
else:
_log.warning(_("Unknown V4L version %(version)s!") % {
"version": capture.version,
})
model.remove(iter)
iter = model.append()
icon = gtk.stock_lookup(gtk.STOCK_OPEN)[0]
model.set_value(iter, 0, theme.load_icon(icon, size, 0))
model.set_value(iter, 1, _("Choose File..."))
iter = model.append()
icon = gtk.stock_lookup(gtk.STOCK_OPEN)[0]
model.set_value(iter, 0, theme.load_icon(icon, size, 0))
model.set_value(iter, 1, _("Choose Directory..."))
self.source.set_active(0)
self.source.connect("changed", self.on_source_changed)
# Watch for DVD discovery events
self.finder_disc_found = self.finder.connect("disc-found",
self.on_disc_found)
self.finder_disc_lost = self.finder.connect("disc-lost",
self.on_disc_lost)
else:
self.source = gtk.FileChooserButton(_("Choose File..."))
else:
self.source = gtk.FileChooserButton(_("Choose File..."))
# Add properties button to set source properties like subtitles
source_prop_image = gtk.Image()
source_prop_image.set_from_stock(gtk.STOCK_PROPERTIES,
gtk.ICON_SIZE_MENU)
source_properties = gtk.Button()
source_properties.add(source_prop_image)
source_properties.connect("clicked", self.on_source_properties)
if not self.source_hbox:
self.source_hbox = gtk.HBox()
self.source_hbox.pack_end(source_properties, expand = False)
self.table.attach(self.source_hbox, 1, 2, 0, 1, yoptions = gtk.FILL)
self.source_hbox.pack_start(self.source)
# Attach and show the source
self.source_hbox.show_all()
def setup_devices(self):
# Find plugins and sort them nicely
# Adds output device profiles to the output device combo box
width, height = gtk.icon_size_lookup(gtk.ICON_SIZE_MENU)
model = self.devices.get_model()
# Disconnect from existing signals
if hasattr(self.devices, "handler_id"):
self.devices.disconnect(self.devices.handler_id)
# Remove existing items
model.clear()
self.default_device = 0
for x, (id, device) in enumerate(sorted(arista.presets.get().items(),
lambda x, y: cmp(x[1].name, y[1].name))):
iter = model.append()
image = _get_icon_pixbuf(device.icon, width, height)
if image:
model.set_value(iter, 0, image)
model.set_value(iter, 1, device.name)
model.set_value(iter, 2, device)
if id == "computer":
self.default_device = x
iter = model.append()
icon = _get_icon_pixbuf("stock://gtk-add", width, height)
model.set_value(iter, 0, icon)
model.set_value(iter, 1, "Create new")
model.set_value(iter, 2, "http://www.transcoder.org/presets/create/")
iter = model.append()
icon = _get_icon_pixbuf("stock://gtk-go-down", width, height)
model.set_value(iter, 0, icon)
model.set_value(iter, 1, "Download more")
model.set_value(iter, 2, "http://www.transcoder.org/presets/")
self.devices.handler_id = self.devices.connect("changed",
self.on_device_changed)
self.devices.set_active(self.default_device)
def on_source_properties(self, widget):
"""
Show source properties dialog so user can set things like
subtitles, forcing deinterlacing, etc.
"""
dialog = PropertiesDialog(self.options)
dialog.window.run()
dialog.window.destroy()
def on_quit(self, widget, *args):
"""
Stop the transcoder and hopefully let it cleanup, then exit.
"""
try:
if self.transcoder:
if self.transcoder.state in [gst.STATE_READY, gst.STATE_PAUSED]:
self.transcoder.start()
self.transcoder.pipe.send_event(gst.event_new_eos())
except:
pass
self.window.hide()
_log.debug(_("Cleaning up and flushing buffers..."))
def waiting_to_quit():
if not self.transcoder or self.transcoder.state == gst.STATE_NULL:
gobject.idle_add(gtk.main_quit)
return False
else:
return True
gobject.idle_add(waiting_to_quit)
return True
def on_disc_found(self, finder, device, label):
"""
A video DVD has been found, update the source combo box!
"""
model = self.source.get_model()
for pos, item in enumerate(model):
if item[2] and item[2].endswith(device.path):
model[pos] = (item[0], device.nice_label, item[2])
break
def on_disc_lost(self, finder, device, label):
"""
A video DVD has been removed, update the source combo box!
"""
model = self.source.get_model()
for pos, item in enumerate(model):
if item[2].endswith(device.path):
model[pos] = (item[0], device.nice_label, item[2])
break
def on_source_changed(self, widget):
"""
The source combo box or file chooser button has changed, update!
"""
theme = gtk.icon_theme_get_default()
size = gtk.ICON_SIZE_MENU
width, height = gtk.icon_size_lookup(size)
iter = widget.get_active_iter()
model = widget.get_model()
item = model.get_value(iter, 1)
if item == _("Choose File..."):
dialog = gtk.FileChooserDialog(title=_("Choose Source File..."),
buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
gtk.STOCK_OPEN, gtk.RESPONSE_ACCEPT))
dialog.set_property("local-only", False)
dialog.set_current_folder(self.last_open_path)
response = dialog.run()
dialog.hide()
if response == gtk.RESPONSE_ACCEPT:
if self.fileiter:
model.remove(self.fileiter)
filename = dialog.get_filename()
client = gconf.client_get_default()
client.set_string(CONFIG_PATH + "/last_open_path",
os.path.dirname(filename))
pos = widget.get_active()
newiter = model.insert(pos)
icon = _get_filename_icon(filename)
if icon:
model.set_value(newiter, 0, icon.load_icon())
basename = os.path.basename(filename)
if len(basename) > 25:
basename = basename[:22] + "..."
model.set_value(newiter, 1, basename)
model.set_value(newiter, 2, filename)
self.fileiter = newiter
widget.set_active(pos)
else:
if self.fileiter:
pos = widget.get_active()
widget.set_active(pos - 1)
else:
widget.set_active(0)
elif item == _("Choose Directory..."):
dialog = gtk.FileChooserDialog(title=_("Choose Source Directory..."),
action=gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,
buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
gtk.STOCK_OPEN, gtk.RESPONSE_ACCEPT))
dialog.set_property("local-only", False)
dialog.set_current_folder(self.last_open_path)
response = dialog.run()
dialog.hide()
if response == gtk.RESPONSE_ACCEPT:
if self.fileiter:
model.remove(self.fileiter)
directory = dialog.get_current_folder()
client = gconf.client_get_default()
client.set_string(CONFIG_PATH + "/last_open_path", directory)
pos = widget.get_active() - 1
newiter = model.insert(pos)
icon = icon = _get_icon_pixbuf("stock://gtk-directory", width, height)
model.set_value(newiter, 0, icon)
model.set_value(newiter, 1, os.path.basename(directory.rstrip("/")))
model.set_value(newiter, 2, directory)
self.fileiter = newiter
widget.set_active(pos)
else:
if self.fileiter:
pos = widget.get_active()
widget.set_active(pos - 2)
else:
widget.set_active(0)
# Reset the custom input options
self.options.reset()
def on_device_changed(self, widget):
"""
The device combo was changed - update the presets for the newly
selected device.
"""
width, height = gtk.icon_size_lookup(gtk.ICON_SIZE_MENU)
iter = self.devices.get_active_iter()
device = self.devices.get_model().get_value(iter, 2)
if isinstance(device, str):
webbrowser.open(device)
self.devices.set_active(self.default_device)
return
model = self.presets.get_model()
model.clear()
selected = 0
for (pos, (name, preset)) in enumerate(device.presets.items()):
iter = model.append()
if preset.icon:
image = _get_icon_pixbuf(preset.icon, width, height)
if image:
model.set_value(iter, 0, image)
model.set_value(iter, 1, name)
model.set_value(iter, 2, preset)
if device.default and device.default == preset.name:
selected = pos
self.presets.set_active(selected)
def on_open(self, widget):
"""
Show a file chooser and let the user select a media file.
"""
self.show_file_chooser()
def on_pause_toggled(self, widget):
"""
Pause toolbar button clicked.
"""
if widget.get_active():
self.transcoder.pause()
self.builder.get_object("toolbutton_pause").set_active(True)
self.builder.get_object("menuitem_pause").set_active(True)
else:
self.transcoder.start()
self.builder.get_object("toolbutton_pause").set_active(False)
self.builder.get_object("menuitem_pause").set_active(False)
def on_add(self, widget):
"""
Add an item to the queue. This shows a file chooser dialog to
pick the output filename and then adds the item to the queue for
transcoding.
"""
iter = self.presets.get_active_iter()
preset = self.presets.get_model().get_value(iter, 2)
self.source.set_sensitive(False)
self.devices.set_sensitive(False)
self.presets.set_sensitive(False)
can_encode = preset.check_elements(self.preset_ready)
def get_default_output_name(self, inname, preset):
"""
Get the default recommended output filename given an input path
and a preset. The original extension is removed, then the new
preset extension is added. If such a path already exists then
numbers are added before the extension until a non-existing path
is found to exist.
"""
if "." in inname:
default_out = ".".join(inname.split(".")[:-1]) + "." + preset.extension
else:
default_out = inname + "." + preset.extension
while os.path.exists(default_out):
parts = default_out.split(".")
name, ext = ".".join(parts[:-1]), parts[-1]
result = RE_ENDS_NUM.search(name)
if result:
value = result.group("number")
name = name[:-len(value)]
number = int(value) + 1
else:
number = 1
default_out = "%s%d.%s" % (name, number, ext)
return default_out
def preset_ready(self, preset, can_encode):
"""
Called when a preset is ready to be encoded after checking for
(and optionally installing) required GStreamer elements.
"""
gtk.gdk.threads_enter()
self.source.set_sensitive(True)
self.devices.set_sensitive(True)
self.presets.set_sensitive(True)
gtk.gdk.threads_leave()
if not can_encode:
gtk.gdk.threads_enter()
dialog = gtk.MessageDialog(self.window, type = gtk.MESSAGE_ERROR, buttons = gtk.BUTTONS_OK, message_format = _("Cannot add item to queue because of missing elements!"))
dialog.run()
dialog.destroy()
gtk.gdk.threads_leave()
return
gtk.gdk.threads_enter()
if isinstance(self.source, gtk.ComboBox):
iter = self.source.get_active_iter()
model = self.source.get_model()
inpath = model.get_value(iter, 2)
inname = os.path.basename(inpath)
else:
inpath = self.source.get_filename()
inname = os.path.basename(inpath)
iter = self.devices.get_active_iter()
device = self.devices.get_model().get_value(iter, 2)
filenames = []
if not os.path.isdir(inpath):
default_out = self.get_default_output_name(inpath, preset)
dialog = gtk.FileChooserDialog(title = _("Choose Output File..."),
action = gtk.FILE_CHOOSER_ACTION_SAVE,
buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
gtk.STOCK_SAVE, gtk.RESPONSE_ACCEPT))
dialog.set_property("local-only", False)
dialog.set_property("do-overwrite-confirmation", True)
dialog.set_current_folder(os.path.dirname(inpath))
dialog.set_current_name(os.path.basename(default_out))
response = dialog.run()
dialog.hide()
if response == gtk.RESPONSE_ACCEPT:
filenames.append((inpath, dialog.get_filename()))
else:
dialog = gtk.FileChooserDialog(title = _("Choose Output Directory..."),
action = gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,
buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
gtk.STOCK_SAVE, gtk.RESPONSE_ACCEPT))
dialog.set_property("local-only", False)
dialog.set_property("do-overwrite-confirmation", True)
dialog.set_current_folder(inpath)
response = dialog.run()
dialog.hide()
if response == gtk.RESPONSE_ACCEPT:
outdir = dialog.get_current_folder()
for root, dirs, files in os.walk(inpath):
for fname in files:
full_path = os.path.join(root, fname)
filenames.append((full_path, os.path.join(outdir, os.path.basename(self.get_default_output_name(full_path, preset)))))
for inpath, outpath in filenames:
# Setup the transcode job options
self.options.uri = inpath
self.options.preset = preset
self.options.output_uri = outpath
self.queue.append(self.options)
# Reset options for next item, but copy relevant data
options = arista.transcoder.TranscoderOptions()
options.subfile = self.options.subfile
options.font = self.options.font
options.deinterlace = self.options.deinterlace
self.options = options
iter = self.queue_model.append()
width, height = gtk.icon_size_lookup(gtk.ICON_SIZE_MENU)
image = _get_icon_pixbuf(device.icon, width, height)
if image:
self.queue_model.set_value(iter, 0, image)
self.queue_model.set_value(iter, 1, _("%(model)s (%(preset)s): %(filename)s") % {
"model": device.model,
"preset": preset.name,
"filename": os.path.basename(outpath),
})
self.queue_model.set_value(iter, 2, self.queue[-1])
# Reset the options for the next item so we don't inadvertently
# change the queued option data!
self.options = arista.transcoder.TranscoderOptions()
gtk.gdk.threads_leave()
def stop_processing_entry(self, entry):
"""
Stop processing an entry that is currently being processed. This
sends an end-of-stream signal down the pipe, hides the preview,
and makes sure the menu and toolbar is in the proper state.
The item will remain in the queue for up to a few seconds as
GStreamer finishes flushing its buffers, then will be removed.
If another item is in the queue it will start processing then.
"""
entry.stop()
if self.runoptions.simple and len(self.queue) == 1:
# This is the last item in the simplified GUI, so we are done and
# should exit as soon as possible!
gobject.idle_add(gtk.main_quit)
return
# Hide live preview while we wait for the item to finish
self.image_preview.show()
self.preview.hide()