[r8]: / trunk / tubetutor / ffmpeg.py  Maximize  Restore  History

Download this file

243 lines (207 with data), 6.9 kB

  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
#
# Wrapper for the ffmpeg utilities
#
# Default search path is relatively to the main script that is called:
# <script path>/ffmpeg
#
# Otherwise, the windows search path is used
#
import subprocess
import platform
import ctypes
import signal
import sys
import os
import re

#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
#
def get_cmd_ffmpeg():
	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"

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"

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"

def is_recording():
	return proc_ffmpeg != None

def convert_image(input, output, params):
	global font
	cmd = get_cmd_convert()
	cmd += " " + input
	cmd += " " + params
	cmd += " " + output
	print ("====================================================")
	print ("cmd: %s" % cmd)
	p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
	(out, error) = p.communicate()
	print("output:\n%s" % out)
	print("error:\n%s" % error)

def start(filename, params):
	global proc_ffmpeg

	# Kill remaining ffmpeg instances
	stop()
	# execute ffmpeg cmd
	cmd = get_cmd_ffmpeg()
	cmd += " " + params
	cmd += " " + filename
	print ("====================================================")
	print ("cmd: %s" % cmd)
	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, " %s -y -vf scale=%s -v:q 1" % (grabber, resolution))
	else:
		start(filename, " %s -y -v:q 1" % (grabber))

def stop():
	global proc_ffmpeg
	# Kill remaining ffmpeg instances
	if proc_ffmpeg:
		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
		
def video_duration(filename):
	cmd = get_cmd_ffprobe()
	cmd += " -i %s" % filename
	cmd += " -show_entries format=duration"
	print ("====================================================")
	print ("cmd: %s" % cmd)
	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))
	for d in durations:
		duration = d.split("=")[1]
		print("duration string: %s" % duration)
		print("duration: %f" % float(duration))
		return float(duration)

def wait():
	global proc_ffmpeg
	if proc_ffmpeg:
		(out, error) = proc_ffmpeg.communicate()
		print("output:\n%s" % out)
		print("error:\n%s" % error)
		proc_ffmpeg = None

class renderer:
	pconcat=""
	nconcat=0
	vidfilter=[]
	vidresolution=""
	lastfilename=""
	tmppath=""
	framerate=20
	qfilter=" -v:q 1 "

	def __init__(self):
		self.tmppath = os.path.join("videos", "tmp")
		self.framerate = 20
		
	def add_text(self, filename, duration=5):
		f = open(filename, "r")
		if f:
			content = f.read()
			f.close()
		if content.replace("\n", "") == "":
			return

		params = " -scale %s!" % self.vidresolution.replace(":", "x")
		params += " -font %s -weight 500 -pointsize 75" % font
		params += " -draw \"gravity northwest fill white text 100,650 '%s'\"" % content
		params += " -scale %s!" % self.vidresolution.replace(":", "x")
		image = os.path.splitext(filename)[0] + ".png"
		convert_image(os.path.join("media", "back.png"), image, params)
		self.add_image(image, duration)
		return content
	
	def add_image(self, filename, duration=5, fadein=True):
		print(self.tmppath)
		tmpimage = os.path.join(self.tmppath, os.path.basename(filename) + ".png")
		tmpfilename = os.path.join(self.tmppath, os.path.basename(filename) + ".mkv")
		params = " -scale %s!" % self.vidresolution.replace(":", "x")
		convert_image(filename, tmpimage, params)

		params = " -y -loop 1 -t %d -i %s" % (duration, tmpimage)
		if fadein:
			params += " -filter_complex \"fade=in:0:d=1[v];[v]fade=out:st=%f:d=2\"" % ((duration-2))
		else:
			params += " -filter_complex \"fade=out:st=%f:d=1\"" % ((duration-3))
		params += self.qfilter
		start(tmpfilename, params)
		wait()

		self.pconcat += " -i %s" % (tmpfilename)
		self.nconcat+=1
	
	def add_video(self, filename):
		# fade video temporarily
		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
		start(tmpfilename, params)
		wait()
		
		self.pconcat += " -i %s" % (tmpfilename)
		self.nconcat+=1

	def speedup(self, filename, pts=0.7):
		params = " -i %s -y -filter:v \"setpts=%f*PTS\"" % (self.lastfilename, pts)
		params += self.qfilter
		self.lastfilename = filename
		start(filename, params)
		wait()

	def mux(self, filename, audiofile):
		duration = video_duration(self.lastfilename)
		params = " -i %s -i %s -y" % (self.lastfilename, audiofile)
		params += " -t %d" % (int(duration)+1)
		params += " -af \"afade=out:st=%f:d=3\"" % (duration-3.0)
		params += " -map 0:v:0 -map 1:a:0"
		params += self.qfilter
		self.lastfilename = filename
		start(filename, params)
		wait()

	def filter(self, filter):
		self.vidfilter = filter

	def resolution(self, resolution):
		self.vidresolution = resolution

	def concat(self, filename, audio=False):
		if self.lastfilename != "":
			self.nconcat += 1
			self.pconcat = "-i %s %s " % (self.lastfilename, self.pconcat)

		if audio:
			a=1
		else:
			a=0
		if len(self.vidfilter) > 0:
			params = self.pconcat + " -y -filter_complex \"concat=n=%d:v=1:a=%d,%s\"" % (a, self.nconcat, ",".join(self.vidfilter))
		else:
			params = self.pconcat + " -y -filter_complex concat=n=%d:v=1:a=%d" % (self.nconcat, a)
		self.lastfilename = filename
		params += self.qfilter
		start(filename, params)
		wait()

	def filename(self, filename):
		self.lastfilename = filename

	def framerate(self, framerate):
		self.framerate = framerate

	def tmppath(self, path):
		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
		start(filename, params)
		wait()