implement countdown timer overlay, add countdown and shutter sounds (transparency of label is missing)

This commit is contained in:
Andreas Frisch 2016-04-19 12:49:07 +02:00
parent 74d2df108d
commit f90d22abec
4 changed files with 90 additions and 25 deletions

BIN
beep.m4a Normal file

Binary file not shown.

8
photobooth.css Normal file
View file

@ -0,0 +1,8 @@
.snapshot_label {
/* background-color: rgba (100, 100, 0, 0); */
background: rgba (50, 100, 10, 50);
color: white;
font-weight: bold;
font-size: 60px;
padding: 0 20px 0 20px;
}

BIN
shutter.mp3 Normal file

Binary file not shown.

View file

@ -8,6 +8,11 @@ gi.require_version('GstVideo', '1.0')
gi.require_version('GdkX11', '3.0')
from gi.repository import Gst, GObject, Gtk, GdkX11, Gdk, GstVideo
COUNTDOWN_SECONDS = 4
AUDIOFILE_COUNTDOWN = "beep.m4a"
AUDIOFILE_SHUTTER = "shutter.mp3"
class GTK_Main:
def __init__(self):
window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
@ -15,27 +20,46 @@ class GTK_Main:
window.set_default_size(1024, 830)
window.connect("destroy", Gtk.main_quit, "WM destroy")
Gtk.Window.fullscreen(window)
vbox = Gtk.VBox()
window.add(vbox)
style_provider = Gtk.CssProvider()
css = open('photobooth.css')
css_data = css.read()
css.close()
style_provider.load_from_data(css_data)
Gtk.StyleContext.add_provider_for_screen(
Gdk.Screen.get_default(),
style_provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
)
self.overlay = Gtk.Overlay()
window.add(self.overlay)
self.movie_window = Gtk.DrawingArea()
self.movie_window.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
self.movie_window.connect("button-press-event", self.snapshot)
vbox.add(self.movie_window)
self.overlay.add(self.movie_window)
vbox = Gtk.VBox()
hbox = Gtk.HBox()
vbox.pack_start(hbox, False, False, 0)
hbox.set_border_width(10)
hbox.pack_start(Gtk.Label(), False, False, 0)
self.button = Gtk.Button("Start")
self.button.connect("clicked", self.start_stop)
hbox.pack_start(self.button, False, False, 0)
self.button2 = Gtk.Button("Quit")
self.button2.connect("clicked", self.exit)
hbox.pack_start(self.button2, False, False, 0)
hbox.add(Gtk.Label())
self.button_play = Gtk.Button("Start")
self.button_play.connect("clicked", self.start_stop)
hbox.pack_start(self.button_play, False, False, 0)
self.button_quit = Gtk.Button("Quit")
self.button_quit.connect("clicked", self.exit)
hbox.pack_start(self.button_quit, False, False, 0)
vbox.set_valign(Gtk.Align.END)
vbox.set_halign(Gtk.Align.CENTER)
self.overlay.add_overlay(vbox)
self.overlay.show_all()
window.show_all()
self.player = Gst.Pipeline.new("player")
self.videoplayer = Gst.Pipeline.new("player")
source = Gst.ElementFactory.make("videotestsrc", "file-source")
source.set_property("pattern", 1)
capsfilter = Gst.ElementFactory.make("capsfilter", "capsfilter")
@ -55,7 +79,7 @@ class GTK_Main:
videosink = Gst.ElementFactory.make("xvimagesink", "video-output")
for ele in (source, capsfilter, video_convert, png_source, png_decoder, alphacolor, png_convert, imagefreeze, mixer, mix_convert, videosink):
self.player.add(ele)
self.videoplayer.add(ele)
source.link(capsfilter)
capsfilter.link(video_convert)
@ -70,38 +94,71 @@ class GTK_Main:
mixer.link(mix_convert)
mix_convert.link(videosink)
bus = self.player.get_bus()
bus = self.videoplayer.get_bus()
bus.add_signal_watch()
bus.enable_sync_message_emission()
bus.connect("message", self.on_message)
bus.connect("sync-message::element", self.on_sync_message)
self.countdown_timer = None
self.soundplayer = Gst.Pipeline.new("soundplayer")
def start_stop(self, w):
if self.button.get_label() == "Start":
if self.button_play.get_label() == "Start":
self.xid = self.movie_window.get_property('window').get_xid()
self.button.set_label("Stop")
self.player.set_state(Gst.State.PLAYING)
self.button_play.set_label("Stop")
self.videoplayer.set_state(Gst.State.PLAYING)
else:
self.player.set_state(Gst.State.NULL)
self.button.set_label("Start")
self.videoplayer.set_state(Gst.State.NULL)
self.button_play.set_label("Start")
def snapshot(self, widget, event):
if event.button == 1:
print "TAKE SNAPSHOT!"
if event.button == 1 and self.videoplayer.get_state(0).state == Gst.State.PLAYING and not self.countdown_timer:
self.countdown_timer = GObject.timeout_add(1000, self.countdown_cb)
self.snapshot_label = Gtk.Label(label="SNAPSHOT!", angle=25, halign=Gtk.Align.END)
self.snapshot_label.set_valign(Gtk.Align.CENTER)
self.snapshot_label.set_halign(Gtk.Align.CENTER)
self.snapshot_label.set_size_request(500, 300)
self.snapshot_label.get_style_context().add_class("snapshot_label")
self.audiplaybin = Gst.ElementFactory.make("playbin", "soundfile")
uri = 'file://'+os.path.abspath(AUDIOFILE_COUNTDOWN)
self.audiplaybin.set_property('uri',uri)
self.soundplayer.add(self.audiplaybin)
self.soundplayer.set_state(Gst.State.PLAYING)
self.overlay.add_overlay(self.snapshot_label)
self.overlay.show_all()
self.counter = COUNTDOWN_SECONDS
def countdown_cb(self):
self.counter -= 1
if self.counter == 0:
self.snap()
return False
self.snapshot_label.set_text("SNAP IN %i" % self.counter)
return True
def snap(self):
print "TAKE SNAPSHOT!"
self.countdown_timer = None
self.soundplayer.set_state(Gst.State.READY)
uri = 'file://'+os.path.abspath(AUDIOFILE_SHUTTER)
self.audiplaybin.set_property('uri',uri)
self.soundplayer.set_state(Gst.State.PLAYING)
self.overlay.remove(self.snapshot_label)
def exit(self, widget, data=None):
Gtk.main_quit()
def on_message(self, bus, message):
t = message.type
if t == Gst.MessageType.EOS:
self.player.set_state(Gst.State.NULL)
self.button.set_label("Start")
self.videoplayer.set_state(Gst.State.NULL)
self.button_play.set_label("Start")
elif t == Gst.MessageType.ERROR:
err, debug = message.parse_error()
print "Error: %s" % err, debug
self.player.set_state(Gst.State.NULL)
self.button.set_label("Start")
self.videoplayer.set_state(Gst.State.NULL)
self.button_play.set_label("Start")
def on_sync_message(self, bus, message):
struct = message.get_structure()