/
FMEcmd.py
309 lines (283 loc) · 10.5 KB
/
FMEcmd.py
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
###############################################################################
# Developper: Francois Schnell - ULP Multimedia
# GPL v2
#
# A script to automate FlashMediaEnocder v2(FMEcmd.exe - commnande line)
# from Python without needing to open the FlashMediaEncoder GUI first
#
# The script will generate its own profile file "flv_startup.xml" in the same
# folder
#
# Note: Since version 2.5 of Flash Media Encoder, videoDeviceName,audioDeviceName
# can be the full name or the input number
#
# Flash Media Ecoder URL
# http://www.adobe.com/products/flashmediaserver/flashmediaencoder/
###############################################################################
import os,subprocess, codecs, time,wx,sys
if sys.platform=="win32":
import winsound #for tests
from pywinauto import application,findwindows
class FMEcmd(object):
"""Command FlashMediaEncoder FMDcmd.exe from Python"""
def __init__(self,videoDeviceName,audioDeviceName,flvPath,liveParams,externalProfile,usage="video",live=False,pathData="C:\\"):
print "### Now in FMEcmd\nreceived videoDeviceName",videoDeviceName,"audioDeviceName",audioDeviceName
print "externalProfile is ",externalProfile
print "flvPath is ", flvPath
self.flvPath=flvPath
self.usage=usage
#self.logsDir=os.environ["USERPROFILE"]+"\\audiovideocours"
self.pathData=pathData
self.logsDir=pathData
print "Flash Media Live Encoder logs will be here: ", self.logsDir
if usage=="video":
videoSource=u"""
<video>
<device>"""+videoDeviceName+u"""</device>
<crossbar_input>0</crossbar_input>
<frame_rate>15.00</frame_rate>
<size>
<width>320</width>
<height>240</height>
</size>
</video>"""
videoEncode=u"""
<video>
<format>VP6</format>
<datarate>500;</datarate>
<outputsize>320x240;</outputsize>
<advanced>
<keyframe_frequency>5 Seconds</keyframe_frequency>
<quality>Good Quality - Good Framerate</quality>
<noise_reduction>None</noise_reduction>
<datarate_window>Medium</datarate_window>
<cpu_usage>Dedicated</cpu_usage>
</advanced>
<autoadjust>
<enable>false</enable>
<maxbuffersize>1</maxbuffersize>
<dropframes>
<enable>false</enable>
</dropframes>
<degradequality>
<enable>false</enable>
<minvideobitrate></minvideobitrate>
<preservepfq>false</preservepfq>
</degradequality>
</autoadjust>
</video>"""
elif usage=="audio":
videoSource=""
videoEncode=""
if live==True and usage=="audio":
outputFile=""
else:
outputFile="""<file>
<path>"""+flvPath+u"""</path>
</file>"""
self.profileHead=u"""<?xml version="1.0" encoding="UTF-16"?>
<flashmedialiveencoder_profile>
<preset>
<name>Medium Bandwidth (300 Kbps)</name>
<description></description>
</preset>
<capture>"""+\
videoSource+"""
<audio>
<device>""" +audioDeviceName+u"""</device>
<crossbar_input>0</crossbar_input>
<sample_rate>22050</sample_rate>
<channels>1</channels>
<input_volume>75</input_volume>
</audio>
</capture>
<encode>"""\
+videoEncode +\
"""
<audio>
<format>Mp3</format>
<datarate>48</datarate>
</audio>
</encode>
<restartinterval>
<days></days>
<hours></hours>
<minutes></minutes>
</restartinterval>
<reconnectinterval>
<attempts></attempts>
<interval></interval>
</reconnectinterval>"""
self.profileOutput=u"""<output>
"""+liveParams+outputFile+"""
</output>"""
self.profileTail=u"""<metadata>
<entry>
<key>author</key>
<value></value>
</entry>
<entry>
<key>copyright</key>
<value></value>
</entry>
<entry>
<key>description</key>
<value></value>
</entry>
<entry>
<key>keywords</key>
<value></value>
</entry>
<entry>
<key>rating</key>
<value></value>
</entry>
<entry>
<key>title</key>
<value></value>
</entry>
</metadata>
<preview>
<video>
<input>
<zoom>100%</zoom>
</input>
<output>
<zoom>100%</zoom>
</output>
</video>
<audio></audio>
</preview>
<log>
<level>100</level>
<directory>"""+self.logsDir+"""</directory>
</log>
</flashmedialiveencoder_profile>
"""
# check if an external profile exist and take profileHead in this case
if externalProfile== True:
print "Reading external profile"
fP=codecs.open("startup.xml",encoding="utf-16-le")
#fP=open("startup.xml","r")
exProfile=u""
for line in fP:
exProfile+=line
#print repr(exProfile)
self.profileHead=exProfile.split(u"<output>")[0]
#print self.profileHead
fP.close()
self.profile=self.profileHead+self.profileOutput+self.profileTail
fileOut=open(self.pathData+"/flv_startup.xml","wb")
fileOut.write(self.profile.encode("UTF-16"))
fileOut.close
def record(self):
""" Launch FMEcmd.exe with the given profile. """
if sys.platform=="win32":
winsound.Beep(500,50)
FME1='C:/Program Files/Adobe/Flash Media Live Encoder 3.1/FMLEcmd.exe'
FME2='C:/Program Files/Adobe/Flash Media Live Encoder 3.2/FMLEcmd.exe'
FMEwin7='C:/Program Files (x86)/Adobe/Flash Media Live Encoder 3.2/FMLEcmd.exe'
FMEwin7_31='C:/Program Files (x86)/Adobe/Flash Media Live Encoder 3.1/FMLEcmd.exe'
FMEwin7_32='C:/Program Files (x86)/Adobe/Flash Media Live Encoder 3.2/FMLEcmd.exe'
if os.path.exists(FME1):
print ">>> Detected",FME1
FME=FME1
elif os.path.exists(FME2):
print ">>> Detected",FME2
FME=FME2
elif os.path.exists(FMEwin7_31):
print ">>> Detected",FMEwin7_31
FME=FMEwin7_31
elif os.path.exists(FMEwin7_32):
print ">>> Detected",FMEwin7_32
FME=FMEwin7_32
else:
print "!!! No FME found !!!"
if sys.platform=="darwin":
FME='/Applications/Adobe/Flash Media Live Encoder 3.2/CommandLineFMLE/FMLECmd'
try:
subprocess.Popen(["%s"%FME,"/d","/P",self.pathData+"/flv_startup.xml"])
print "Ordered sent to FMLE to begin recording:",FME
except:
try:
subprocess.Popen(["%s"%FMEwin7,"/d","/P",self.pathData+"/flv_startup.xml"])
print "Ordered sent to FMLE to begin recording: ",FMEwin7
except:
try:
subprocess.Popen(["FMLEcmd.exe","/d","/P",self.pathData+"/flv_startup.xml"])
print "!!! after record second Try !!!"
except:
print "Couldn't find C:\Program Files\Adobe\Flash Media Live Encoder 3.1\FMLEcmd.exe"
caption="Audiovideocours Error Message"
text="Problem while launching Flash Media Encoder.\n\n Is C:\Program Files\Adobe\Flash Media Live Encoder 3.2\FMLEcmd.exe exists?"+\
"\n If not install Flash Media Live Encoder 3.2"
dialog=wx.MessageDialog(None,message=text,caption=caption,
style=wx.OK|wx.ICON_INFORMATION)
dialog.ShowModal()
time.sleep(4)
if sys.platform=="win32":
print "trying to minimize FMLEcmd.exe DOS window in task bar"
try:
appA = application.Application()
appA.connect_(title_re = r".*FMLEcmd.exe")
appA.window_(title_re = r".*FMLEcmd.exe").Minimize()
except:
for i in range(3):
winsound.Beep(500,100)
time.sleep(0.2)
print "Couldn't find and minimize DOS window"
caption="Audiovideocours Error Message"
text="Impossible d'acceder a la source video ou Audio qui est peut-etre deja en cours d'utilisation.\n Merci de redemarrer le PC puis de relancer l'enregistrement."
dialog=wx.MessageDialog(None,message=text,caption=caption,
style=wx.OK|wx.ICON_INFORMATION)
dialog.ShowModal()
def stop(self,FMLEpid):
"""Kill the FlashMediaEncoder"""
print 'On windows ordering: FMLEcmd.exe /s "%s" ' % FMLEpid
if sys.platform=="win32":
FME1='C:/Program Files/Adobe/Flash Media Live Encoder 3.1/FMLEcmd.exe'
FME2='C:/Program Files/Adobe/Flash Media Live Encoder 3.2/FMLEcmd.exe'
FMEwin7='C:/Program Files (x86)/Adobe/Flash Media Live Encoder 3.2/FMLEcmd.exe'
FMEwin7_31='C:/Program Files (x86)/Adobe/Flash Media Live Encoder 3.1/FMLEcmd.exe'
FMEwin7_32='C:/Program Files (x86)/Adobe/Flash Media Live Encoder 3.2/FMLEcmd.exe'
if os.path.exists(FME1):
FME=FME1
elif os.path.exists(FME2):
FME=FME2
elif os.path.exists(FMEwin7_31):
FME=FMEwin7_31
elif os.path.exists(FMEwin7_32):
FME=FMEwin7_32
else:
print "!!! No FME found !!!"
if sys.platform=="darwin":
FME='/Applications/Adobe/Flash Media Live Encoder 3.2/CommandLineFMLE/FMLECmd'
try:
subprocess.Popen(["%s"%FME,"/s","%s" % FMLEpid])
except:
try:
subprocess.Popen(["%s"%FMEwin7,"/s","%s" % FMLEpid])
except:
try:
subprocess.Popen(["FMLEcmd.exe","/s","%s" % FMLEpid])
except:
print "Problem while stopping Flash Media Encoder"
caption="Audiovideocours Error Message"
text="Problem while stopping Flash Media Encoder.\n\nYou may have to stop 'FMEcmd' process manually."
dialog=wx.MessageDialog(None,message=text,caption=caption,
style=wx.OK|wx.ICON_INFORMATION)
dialog.ShowModal()
if __name__=="__main__":
import time
#You must give the exact name of the device by looking either in Windows or
# in the GUI version of Flash Media Encoder
videoDeviceName="Philips ToUcam Pro Camera; Video"
audioDeviceName="Appareil photo Philips ToUcam F"
#FMEcmd doesn't take relative filename path
#if so the video is saved by default in the video foler of the OS "My videos"
flvPath=r"C:\Documents and Settings\franz\Bureau\newsample.flv"
# Example: record a 30 seconds video
flv=FMEcmd(videoDeviceName,audioDeviceName,flvPath,liveParams="",externalProfile=False)
flv.record()
time.sleep(30)
flv.stop()