Diff of /trunk/tubetutor/ffmpeg.py [r2] .. [r3]  Maximize  Restore

Switch to side-by-side view

--- a/trunk/tubetutor/ffmpeg.py
+++ b/trunk/tubetutor/ffmpeg.py
@@ -7,7 +7,9 @@
 # Otherwise, the windows search path is used
 #
 import subprocess
+import platform
 import ctypes
+import signal
 import sys
 import os
 import re
@@ -15,6 +17,11 @@
 #font="Arial"
 font=os.path.join("media", "Roboto-Regular.ttf")
 proc_ffmpeg=None
+
+if platform.system() == "Linux":
+	grabber="-video_size 1920x1080 -f x11grab -framerate 20 -i :0.0"
+else:
+	grabber="-f gdigrab -framerate 20 -i desktop"
 
 #
 # The following helper functions will retrieve the pathnames for the ffmpeg tools
@@ -23,19 +30,19 @@
 	subdir=os.path.join(os.path.dirname(sys.argv[0]), "ffmpeg")
 	if os.path.exists(subdir):
 		return os.path.join(subdir, "ffmpeg.exe")
-	return "ffmpeg.exe"
+	return "ffmpeg"
 
 def get_cmd_ffprobe():
 	subdir=os.path.join(os.path.dirname(sys.argv[0]), "ffmpeg")
 	if os.path.exists(subdir):
 		return os.path.join(subdir, "ffprobe.exe")
-	return "ffprobe.exe"
+	return "ffprobe"
 
 def get_cmd_convert():
 	subdir=os.path.join(os.path.dirname(sys.argv[0]), "imagemagick")
 	if os.path.exists(subdir):
 		return os.path.join(subdir, "convert.exe")
-	return "convert.exe"
+	return "convert"
 
 def is_recording():
 	return proc_ffmpeg != None
@@ -48,7 +55,7 @@
 	cmd += " " + output
 	print ("====================================================")
 	print ("cmd: %s" % cmd)
-	p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=False)
+	p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
 	(out, error) = p.communicate()
 	print("output:\n%s" % out)
 	print("error:\n%s" % error)
@@ -64,19 +71,25 @@
 	cmd += " " + filename
 	print ("====================================================")
 	print ("cmd: %s" % cmd)
-	proc_ffmpeg = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=False)
+	if platform.system() == "Linux":
+		proc_ffmpeg = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, preexec_fn=os.setsid)
+	else:
+		proc_ffmpeg = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
 
 def record(filename, resolution=""):
 	if resolution != "":
-		start(filename, " -f gdigrab -framerate 20 -i desktop -y -vf scale=%s -v:q 1" % (resolution))
+		start(filename, " %s -y -vf scale=%s -v:q 1" % (grabber, resolution))
 	else:
-		start(filename, " -f gdigrab -framerate 20 -i desktop -y -v:q 1")
+		start(filename, " %s -y -v:q 1" % (grabber))
 
 def stop():
 	global proc_ffmpeg
 	# Kill remaining ffmpeg instances
 	if proc_ffmpeg:
-		ctypes.windll.kernel32.TerminateProcess(int(proc_ffmpeg._handle), -1)
+		try:
+			ctypes.windll.kernel32.TerminateProcess(int(proc_ffmpeg._handle), -1)
+		except:
+			os.killpg(os.getpgid(proc_ffmpeg.pid), signal.SIGTERM)
 		proc_ffmpeg.kill()
 		proc_ffmpeg=None
 		
@@ -86,7 +99,7 @@
 	cmd += " -show_entries format=duration"
 	print ("====================================================")
 	print ("cmd: %s" % cmd)
-	p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=False)
+	p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
 	(output, error) = p.communicate()
 	print("duration output: %s - %s" % (output, error))
 	durations = re.findall(r'duration=[0-9.]*', str(output))
@@ -152,7 +165,7 @@
 	
 	def add_video(self, filename):
 		# fade video temporarily
-		tmpfilename = os.path.join(self.tmppath, os.path.basename(filename))
+		tmpfilename = os.path.join(self.tmppath, "add_" + os.path.basename(filename))
 		(w,h) = self.vidresolution.split(":")
 		params = " -y -i %s -filter_complex \"scale=-1:%s[v];[v]crop=%s:0:0[v];[v]fade=in:0:10\"" % (filename, h, self.vidresolution)
 		params += self.qfilter
@@ -214,6 +227,7 @@
 		self.tmppath = path
 
 	def process(self, filename, param):
+		print("process %s -> %s" % (self.lastfilename, filename))
 		params = " -y -i %s %s " % (self.lastfilename, param)
 		self.lastfilename = filename
 		params += self.qfilter