18
18
19
19
20
20
class LiveTranscoder :
21
- def __init__ (self ):
21
+ def __init__ (self , log_filename ):
22
22
# initialize config
23
23
# Initialize Blank Configs
24
24
self .config = ConfigObj ()
@@ -27,14 +27,6 @@ def __init__(self):
27
27
self .log = logging .getLogger ("LiveTranscoder" )
28
28
self .log .setLevel (logging .DEBUG )
29
29
log_format = logging .Formatter ("%(asctime)s %(name)s [%(levelname)s]: %(message)s" )
30
- log_filename = "/var/log/streamkit/live_transcoder_%s.log" % \
31
- (datetime .datetime .fromtimestamp (time .time ()).strftime ('%Y-%m-%d_%H_%M_%S' ))
32
- try :
33
- file_handler = logging .FileHandler (log_filename )
34
- file_handler .setFormatter (log_format )
35
- self .log .addHandler (file_handler )
36
- except :
37
- pass
38
30
39
31
# when executing the collector separately, log directly on the output stream
40
32
stream_handler = logging .StreamHandler ()
@@ -43,6 +35,18 @@ def __init__(self):
43
35
44
36
logging .getLogger ('LiveTranscoder' ).addHandler (stream_handler )
45
37
38
+ try :
39
+ file_handler = logging .FileHandler (log_filename )
40
+ file_handler .setFormatter (log_format )
41
+ self .log .addHandler (file_handler )
42
+ self .log .info ("Log Handler added for file [%s]" , log_filename )
43
+ except Exception , e :
44
+ self .log .warn ("Could not create logfile [%s]" )
45
+ self .log .exception (e )
46
+ pass
47
+
48
+
49
+
46
50
def _get_default_config (self , file_name = '/etc/live-transcoder/default_config.json' ):
47
51
config_file = open (file_name )
48
52
cfg = json .load (config_file )
@@ -51,6 +55,8 @@ def _get_default_config(self, file_name='/etc/live-transcoder/default_config.jso
51
55
return cfg
52
56
53
57
def _get_user_config (self , user_config_json ):
58
+ if not user_config_json :
59
+ return None
54
60
try :
55
61
self .log .info ("Loading user-data: %s" , user_config_json )
56
62
config = json .loads (user_config_json )
@@ -112,21 +118,21 @@ def _getTranscodingCmd(self, config):
112
118
sub_commands = []
113
119
for quality in bitrates :
114
120
if int (quality ["bitrate" ]) <= int (config ["bitrate" ]):
115
- sub_cmd_template = """-f flv -c:a copy -c:v libx264 -s %dx%d -x264opts bitrate=%d -rtmp_playpath %s -rtmp_app %s %s """
116
- sub_cmd_template_audio = """-f flv -c:a copy -b:a %dk -c:v libx264 -s %dx%d -x264opts bitrate=%d -rtmp_playpath %s -rtmp_app %s %s """
121
+ sub_cmd_template = """-f flv -c:a copy -c:v %s -s %dx%d -x264opts bitrate=%d -rtmp_playpath %s -rtmp_app %s %s """
122
+ sub_cmd_template_audio = """-f flv -c:a %s -b:a %dk -c:v libx264 -s %dx%d -x264opts bitrate=%d -rtmp_playpath %s -rtmp_app %s %s """
117
123
target_stream = config ["target_stream" ]
118
124
target_stream = target_stream .replace ("$width" , str (quality ["width" ]))
119
125
target_stream = target_stream .replace ("$height" , str (quality ["height" ]))
120
126
target_stream = target_stream .replace ("$bitrate" , str (quality ["bitrate" ]))
121
127
sub_cmd = ''
122
- if "audio_bitrate" in quality :
128
+ if "audio_bitrate" in quality and "audio_codec" in quality :
123
129
sub_cmd = sub_cmd_template_audio % (
124
- quality ["audio_bitrate" ], quality ["width" ], quality ["height" ], quality ["bitrate" ],
130
+ quality ["audio_codec" ], quality [ " audio_bitrate" ], quality ["width" ], quality ["height" ], quality ["bitrate" ],
125
131
target_stream ,
126
132
config ["target_app" ], config ["target_host" ] )
127
133
else :
128
134
sub_cmd = sub_cmd_template % (
129
- quality ["width" ], quality ["height" ], quality ["bitrate" ], target_stream ,
135
+ quality ["video_codec" ], quality [ " width" ], quality ["height" ], quality ["bitrate" ], target_stream ,
130
136
config ["target_app" ], config ["target_host" ] )
131
137
cmd = cmd + sub_cmd
132
138
return cmd
@@ -158,28 +164,34 @@ def startLiveTranscoding(self, user_config_json):
158
164
159
165
max_retries = int (self .config ["max_retries" ])
160
166
max_retries_delay = int (self .config ["max_retries_delay_sec" ])
167
+ cmd_exit_code = None
161
168
for i in range (1 , max_retries + 1 ):
162
169
self .config = self ._updateStreamMetadataInConfig (self .config )
163
-
164
- cmd = self ._getTranscodingCmd (self .config )
165
- self .log .info ("Executing FFmpeg command:\n %s\n " , cmd )
166
-
167
- # start live transcoding
168
- cmd_args = cmd .split ()
169
- self .log .info ("Running command. (run=%d/%d)" , i , max_retries )
170
- r = self ._runTranscodingCommand (cmd_args )
171
- self .log .info ("Transcoding command stopped. (run=%d/%d). Code=%d" , i , max_retries , r )
170
+ if "bitrate" in self . config and "width" in self . config and "height" in self . config :
171
+ cmd = self ._getTranscodingCmd (self .config )
172
+ self .log .info ("Executing FFmpeg command:\n %s\n " , cmd )
173
+
174
+ # start live transcoding
175
+ cmd_args = cmd .split ()
176
+ self .log .info ("Running command. (run=%d/%d)" , i , max_retries )
177
+ cmd_exit_code = self ._runTranscodingCommand (cmd_args )
178
+ self .log .info ("Transcoding command stopped. (run=%d/%d). Code=%d" , i , max_retries , ( cmd_exit_code or - 777 ) )
172
179
time .sleep (max_retries_delay )
173
180
174
181
self .log .info ("Live-Transcoder has completed ! You can now shutdown the instance." )
175
182
176
183
177
- transcoder = LiveTranscoder ()
184
+
178
185
user_config_json = None
186
+ log_file = "/var/log/streamkit/live_transcoder_%s.log" % \
187
+ (datetime .datetime .fromtimestamp (time .time ()).strftime ('%Y-%m-%d_%H_%M_%S' ))
179
188
if __name__ == '__main__' :
180
189
parser = argparse .ArgumentParser ()
181
190
parser .add_argument ('-u' , '--user-config-json' , dest = 'user_config_json' )
191
+ parser .add_argument ('-l' , '--log-file' , dest = 'log_file' )
182
192
args = parser .parse_args ()
183
193
user_config_json = args .user_config_json
194
+ log_file = args .log_file or log_file
184
195
196
+ transcoder = LiveTranscoder (log_file )
185
197
transcoder .startLiveTranscoding (user_config_json )
0 commit comments