Skip to content

Commit c1462e2

Browse files
committed
# updated transcoder and dockerfile. transcoder accepts audio_codec and video_codec
1 parent df3be59 commit c1462e2

File tree

4 files changed

+85
-38
lines changed

4 files changed

+85
-38
lines changed

Dockerfile

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# ffmpeg live transcoder
22
#
3-
# VERSION 2.4.3-1.1
3+
# VERSION 2.4.3
44
#
55
# From https://trac.ffmpeg.org/wiki/CompilationGuide/Centos
66
#
@@ -15,4 +15,6 @@ RUN mkdir -p /var/log/streamkit/
1515
# forward request and error logs to docker log collector
1616
RUN ln -sf /dev/stdout /var/log/streamkit/*
1717

18+
VOLUME /var/log/streamkit/
19+
1820
ENTRYPOINT ["python", "/usr/local/live-transcoder/live_transcoder.py"]

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ Usage:
2222
```
2323
docker run ddragosd/ffmpeg-live-transcoder:2.4.3-1.1 \
2424
--user-config-json "`cat /usr/local/live-transcoder-config.json`"
25+
26+
```
27+
28+
To specify a log file:
29+
30+
```
31+
docker run -v /var/log/streamkit:/var/log/streamkit ddragosd/ffmpeg-live-transcoder:2.4.3-1.1 \
32+
--user-config-json "`cat /usr/local/live-transcoder-config.json`" \
33+
--log-file "/var/log/streamkit/ffmpeg-live-transcoding.log"
2534
```
2635

2736

default_config.json

+37-13
Original file line numberDiff line numberDiff line change
@@ -7,70 +7,94 @@
77
{
88
"width": 1440,
99
"height": 1080,
10-
"bitrate": 2200
10+
"bitrate": 2200,
11+
"audio_codec": "copy",
12+
"video_codec": "libx264"
1113
},
1214
{
1315
"width": 960,
1416
"height": 720,
15-
"bitrate": 1000
17+
"bitrate": 1000,
18+
"audio_codec": "copy",
19+
"video_codec": "libx264"
1620
},
1721
{
1822
"width": 640,
1923
"height": 480,
20-
"bitrate": 600
24+
"bitrate": 600,
25+
"audio_codec": "copy",
26+
"video_codec": "libx264"
2127
},
2228
{
23-
"width": 640,
29+
"width": 480,
2430
"height": 360,
2531
"bitrate": 300,
26-
"audio_bitrate": 96
32+
"audio_codec": "libfdk_aac",
33+
"audio_bitrate": 96,
34+
"video_codec": "libx264"
2735
},
2836
{
2937
"width": 320,
3038
"height": 240,
3139
"bitrate": 150,
32-
"audio_bitrate": 96
40+
"audio_codec": "libfdk_aac",
41+
"audio_bitrate": 96,
42+
"video_codec": "libx264"
3343
},
3444
{
3545
"width": 214,
3646
"height": 160,
3747
"bitrate": 64,
38-
"audio_bitrate": 48
48+
"audio_codec": "libfdk_aac",
49+
"audio_bitrate": 48,
50+
"video_codec": "libx264"
3951
}
4052
],
4153
"hd_streams": [
4254
{
4355
"width": 1920,
4456
"height": 1080,
45-
"bitrate": 2400
57+
"bitrate": 2400,
58+
"audio_codec": "copy",
59+
"video_codec": "libx264"
4660
},
4761
{
4862
"width": 1280,
4963
"height": 720,
50-
"bitrate": 1200
64+
"bitrate": 1200,
65+
"audio_codec": "copy",
66+
"video_codec": "libx264"
5167
},
5268
{
5369
"width": 854,
5470
"height": 480,
55-
"bitrate": 600
71+
"bitrate": 600,
72+
"audio_codec": "copy",
73+
"video_codec": "libx264"
5674
},
5775
{
5876
"width": 640,
5977
"height": 360,
6078
"bitrate": 300,
61-
"audio_bitrate": 96
79+
"audio_codec": "libfdk_aac",
80+
"audio_bitrate": 96,
81+
"video_codec": "libx264"
6282
},
6383
{
6484
"width": 426,
6585
"height": 240,
6686
"bitrate": 150,
67-
"audio_bitrate": 96
87+
"audio_codec": "libfdk_aac",
88+
"audio_bitrate": 96,
89+
"video_codec": "libx264"
6890
},
6991
{
7092
"width": 284,
7193
"height": 160,
7294
"bitrate": 10,
73-
"audio_bitrate": 48
95+
"audio_codec": "libfdk_aac",
96+
"audio_bitrate": 48,
97+
"video_codec": "libx264"
7498
}
7599
],
76100
"max_retries": 90,

live_transcoder.py

+36-24
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919

2020
class LiveTranscoder:
21-
def __init__(self):
21+
def __init__(self, log_filename):
2222
# initialize config
2323
# Initialize Blank Configs
2424
self.config = ConfigObj()
@@ -27,14 +27,6 @@ def __init__(self):
2727
self.log = logging.getLogger("LiveTranscoder")
2828
self.log.setLevel(logging.DEBUG)
2929
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
3830

3931
# when executing the collector separately, log directly on the output stream
4032
stream_handler = logging.StreamHandler()
@@ -43,6 +35,18 @@ def __init__(self):
4335

4436
logging.getLogger('LiveTranscoder').addHandler(stream_handler)
4537

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+
4650
def _get_default_config(self, file_name='/etc/live-transcoder/default_config.json'):
4751
config_file = open(file_name)
4852
cfg = json.load(config_file)
@@ -51,6 +55,8 @@ def _get_default_config(self, file_name='/etc/live-transcoder/default_config.jso
5155
return cfg
5256

5357
def _get_user_config(self, user_config_json):
58+
if not user_config_json:
59+
return None
5460
try:
5561
self.log.info("Loading user-data: %s", user_config_json)
5662
config = json.loads(user_config_json)
@@ -112,21 +118,21 @@ def _getTranscodingCmd(self, config):
112118
sub_commands = []
113119
for quality in bitrates:
114120
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 """
117123
target_stream = config["target_stream"]
118124
target_stream = target_stream.replace("$width", str(quality["width"]))
119125
target_stream = target_stream.replace("$height", str(quality["height"]))
120126
target_stream = target_stream.replace("$bitrate", str(quality["bitrate"]))
121127
sub_cmd = ''
122-
if "audio_bitrate" in quality:
128+
if "audio_bitrate" in quality and "audio_codec" in quality:
123129
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"],
125131
target_stream,
126132
config["target_app"], config["target_host"] )
127133
else:
128134
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,
130136
config["target_app"], config["target_host"] )
131137
cmd = cmd + sub_cmd
132138
return cmd
@@ -158,28 +164,34 @@ def startLiveTranscoding(self, user_config_json):
158164

159165
max_retries = int(self.config["max_retries"])
160166
max_retries_delay = int(self.config["max_retries_delay_sec"])
167+
cmd_exit_code = None
161168
for i in range(1, max_retries + 1):
162169
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))
172179
time.sleep(max_retries_delay)
173180

174181
self.log.info("Live-Transcoder has completed ! You can now shutdown the instance.")
175182

176183

177-
transcoder = LiveTranscoder()
184+
178185
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'))
179188
if __name__ == '__main__':
180189
parser = argparse.ArgumentParser()
181190
parser.add_argument('-u', '--user-config-json', dest='user_config_json')
191+
parser.add_argument('-l', '--log-file', dest='log_file')
182192
args = parser.parse_args()
183193
user_config_json = args.user_config_json
194+
log_file = args.log_file or log_file
184195

196+
transcoder = LiveTranscoder(log_file)
185197
transcoder.startLiveTranscoding(user_config_json)

0 commit comments

Comments
 (0)