Skip to content

Instantly share code, notes, and snippets.

@pklaus pklaus/traycam.cfg
Created Jan 9, 2012

Embed
What would you like to do?
[Default]
#
# start running?
#
RunStatus = Yes
#
# initial startup for Size, Framerate and Quality
# Numbers are Zero-based according the popup menu
#
VideoSize = 1
Framerate = 4
VideoQuality = 3
#
# show a local video window?
#
ShowLocal = Yes
#
# start the network streamer?
#
ShowNetwork = No
#
# Folder for the html-files
#
WWWFolder = /path/to/folder
#
# IP-Port for listening on client connection
#
Port = 8090
#
# Record the current stream into pictures?
# you later need to convert it to Video using ffmpeg or such
#
Record = No
#
# Folder for saving recorded pictures
#
VideoFolder = /path/to/folder
#!/usr/bin/env python
import subprocess
import time
import gtk
import pygtk
import ConfigParser
str_size = ['160x120', '320x240', '352x288', '640x480', '800x600', '1024x768']
str_framerate = ['1', '2', '5', '10', '15', '20', '25']
str_quality = ['50', '55', '60', '65', '70', '75', '80', '85', '90', '95', '100']
str_cmd_line = 'export LD_LIBRARY_PATH=/usr/local/lib && ' \
+ 'mjpg_streamer -i "input_uvc.so -d /dev/video0 -f '
str_local_output = ' -o "output_viewer.so"'
class Traycam_Config:
def __init__(self):
self.config = ConfigParser.ConfigParser()
self.config.read('traycam.cfg')
def get_value(self, data, default_data):
try:
if isinstance(default_data, bool):
self.value = self.config.getboolean('Default',data)
else:
if isinstance(default_data,int):
self.value = self.config.getint('Default',data)
else:
self.value = self.config.get('Default',data)
except:
self.value = default_data
return self.value
class GTK_Main:
def __init__(self):
self.config = Traycam_Config()
self.run_status = self.config.get_value('RunStatus', True)
self.show_local = self.config.get_value('ShowLocal', True)
self.show_network = self.config.get_value('ShowNetwork', False)
self.record = self.config.get_value('Record', False)
self.size = str_size[self.config.get_value('VideoSize', 3)]
self.framerate = str_framerate[self.config.get_value('Framerate', 4)]
self.quality = str_quality[self.config.get_value('VideoQuality', 3)]
self.str_network_output = ' -o "output_http.so -w ' \
+ self.config.get_value('WWWFolder', '/usr/local/www') \
+ ' -p ' \
+ self.config.get_value('Port', '8080') \
+ '"'
self.str_record_output = ' -o "output_file.so -f ' \
+ self.config.get_value('VideoFolder', '/var/tmp') \
+ '"'
self.streamer_pid = None
statusIcon = gtk.StatusIcon()
activate_menu = gtk.Menu()
menuItem = gtk.MenuItem('Start')
menuItem.connect('activate', self.start_cb, statusIcon)
activate_menu.append(menuItem)
menuItem = gtk.MenuItem('Stop')
menuItem.connect('activate', self.stop_cb, statusIcon)
activate_menu.append(menuItem)
menuItem = gtk.CheckMenuItem('Schow local')
menuItem.set_active(self.show_local)
menuItem.connect('activate', self.local_cb, statusIcon)
activate_menu.append(menuItem)
menuItem = gtk.CheckMenuItem('Schow in Network')
menuItem.connect('activate', self.network_cb, statusIcon)
menuItem.set_active(self.show_network)
activate_menu.append(menuItem)
menuItem = gtk.CheckMenuItem('Record')
menuItem.connect('activate', self.record_cb, statusIcon)
menuItem.set_active(self.record)
activate_menu.append(menuItem)
menuItem = gtk.MenuItem('Exit')
menuItem.connect('activate', self.quit_cb, statusIcon)
activate_menu.append(menuItem)
popup_menu = gtk.Menu()
submenu = gtk.Menu()
menuItem = gtk.MenuItem('Video Size')
for obj in str_size:
menuItem1 = gtk.MenuItem(obj)
menuItem1.connect('activate', self.size_cb, obj)
submenu.append(menuItem1)
menuItem.set_submenu(submenu)
popup_menu.append(menuItem)
submenu = gtk.Menu()
menuItem = gtk.MenuItem('Framerate')
for item in str_framerate:
menuItem1 = gtk.MenuItem(item)
menuItem1.connect('activate', self.framerate_cb, item)
submenu.append(menuItem1)
menuItem.set_submenu(submenu)
popup_menu.append(menuItem)
submenu = gtk.Menu()
menuItem = gtk.MenuItem('Video Quality')
for obj in str_quality:
menuItem1 = gtk.MenuItem(obj)
menuItem1.connect('activate', self.quality_cb, obj)
submenu.append(menuItem1)
menuItem.set_submenu(submenu)
popup_menu.append(menuItem)
statusIcon.set_from_file("traycam.png")
statusIcon.set_tooltip("Webcam Viewer")
statusIcon.connect('activate', self.activate_menu_cb, activate_menu)
statusIcon.connect('popup-menu', self.popup_menu_cb, popup_menu)
statusIcon.set_visible(True)
self.cam_start()
def cam_start(self, widget = None, data = None):
if self.run_status and self.streamer_pid == None:
str_output = ''
if self.show_local:
str_output = str_local_output
if self.show_network:
str_output += self.str_network_output
if self.record:
str_output += self.str_record_output
self.streamer_pid = subprocess.Popen(str_cmd_line \
+ self.framerate \
+ ' -r '+self.size+' -q '+self.quality+'"' \
+ str_output, shell=True)
def cam_stop(self, widget = None, data = None):
if self.streamer_pid != None:
subprocess.call('killall mjpg_streamer', shell=True)
time.sleep( 1 )
self.streamer_pid = None
def cam_reset(self):
self.cam_stop()
self.cam_start()
def start_cb(self, widget, data=None):
self.run_status = True
self.cam_start()
def stop_cb(self, widget, data=None):
self.run_status = False
self.cam_stop()
def local_cb(self, widget, data=None):
self.show_local = not self.show_local
self.cam_reset()
def network_cb(self, widget, data=None):
self.show_network = not self.show_network
self.cam_reset()
def record_cb(self, widget, data=None):
self.record = not self.record
self.cam_reset()
def size_cb(self, widget, data=None):
self.size = data
self.cam_reset()
def framerate_cb(self, widget, data=None):
self.framerate = data
self.cam_reset()
def quality_cb(self, widget, data=None):
self.quality = data
self.cam_reset()
def quit_cb(self, widget, data = None):
self.cam_stop()
gtk.main_quit()
def activate_menu_cb(self, widget, data = None):
if data:
data.show_all()
data.popup(None, None, None, 1, gtk.get_current_event_time())
def popup_menu_cb(self, widget, button, time = None, data = None):
if data:
data.show_all()
data.popup(None, None, None, 3, time)
GTK_Main()
gtk.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.