Code: Select all
#!/usr/bin/python
""" A very, very simple front-end for ffmpeg to capture screencasts
Capture area begins in upper-left corner of screen
Size of capture area is user-definable
Frame rate is fixed at 25 fps
Output resolution = input resolution
No audio
Output file is user-specified (except for container type
Container type = Matroska
Cludged together by RockDoctor
Version 1 23-Sep-2012
"""
from gi.repository import Gtk
import os, signal, sys, subprocess
process = 'ffmpeg'
kill_sig = signal.SIGINT
mycmd = ''
# ======================================================================
def process_killer():
l_process = len(process)
for line in os.popen('ps ax'):
fields = line.split()
if fields[4][0:l_process]==process:
pid_of_process = int(fields[0])
print (pid_of_process)
os.kill(pid_of_process, kill_sig)
# ======================================================================
class SitApp:
"""
an adaptation and butchering of technosaurus's sit.c
adapted by RockDoctor
"""
def __init__(self):
si = Gtk.StatusIcon.new_from_stock("gtk-stop")
si.set_tooltip_text("Click to stop recording")
si.connect("activate",self.leftClick,process_killer)
si.connect("popup-menu",self.rightClick,process_killer)
Gtk.main()
def leftClick(self, status_icon, action):
#print ("Interrupting ",process)
action()
#print ("Left-click")
sys.exit()
def rightClick(self, status_icon, button, mytime, action):
#print ("Interrupting "+str(process)+" button="+str(button)+" time="+str(mytime)+"\n")
action()
#print ("Right-click")
sys.exit()
# ======================================================================
class ScreenRecorder:
""" A very simple screencast recorder
Original command:
ffmpeg -f x11grab -s 840x525 -r 25 -i :0.0 -threads 0 -sameq -an /home/a/Desktop/video.mkv 2>/dev/null
"""
def __init__(self):
self.i_params = {}
self.o_params = {}
self.i_params['f'] = 'x11grab' # input from X11 screen
self.i_params['s'] = '802x514' # input frame size
self.i_params['r'] = '25' # input file frame rate
self.i_params['i'] = ':0.0' # input file (or stream)
self.o_params['threads'] = '0'
self.o_params['sameq'] = '' # same quality in encoder as in decoder
self.o_params['an'] = '' # disable audio recording
self.outfile_name = '/home/a/Desktop/video'
self.outfile_extension = '.mkv'
# I'm only going to permit modification of a limited number of the
# above parameters (which are a very limited number of the
# parameters available to ffmpeg for screen recording)
w = Gtk.Window(title = "Screen Capture via FFMPEG")
w.connect('destroy', Gtk.main_quit)
w.set_default_size(400,300)
vbox = Gtk.VBox(spacing=15)
grid = Gtk.Grid()
grid.set_row_spacing(3)
grid.set_column_spacing(3)
btn = Gtk.Button()
btn.set_label("Frame Size: ")
btn.set_alignment(0,0.5)
btn.set_relief(Gtk.ReliefStyle.NONE)
grid.attach(btn,0,0,1,1)
s_entry = Gtk.Entry()
s_entry.set_text(self.i_params['s'])
grid.attach(s_entry,1,0,1,1)
btn = Gtk.Button()
btn.set_label("Output filename (w/o extension):")
btn.set_alignment(0,0.5)
btn.set_relief(Gtk.ReliefStyle.NONE)
grid.attach(btn,0,1,1,1)
f_entry = Gtk.Entry()
f_entry.set_text(self.outfile_name)
grid.attach(f_entry,1,1,1,1)
btn = Gtk.Button(stock='gtk-execute')
btn.connect('clicked', self.start_ffmpeg)
grid.attach(btn,0,2,2,1)
vbox.pack_start(grid, False, False, 0)
w.add(vbox)
self.w = w
self.w.show_all()
Gtk.main()
def start_ffmpeg(self, widget):
my_cmd = 'ffmpeg -f x11grab -s '+self.i_params['s']+' -r 25 -i :0.0 -threads 0 -sameq -an '+self.outfile_name+'.mkv'
self.w.hide()
command = my_cmd.split()
try:
subprocess.Popen(['rm',self.outfile_name+'.mkv'])
except:
pass
subprocess.Popen(command)
Gtk.main_quit()
# ======================================================================
def main(data=None):
app2 = ScreenRecorder()
app1 = SitApp()
if __name__ == '__main__':
main()