Skip to content

Commit 5068615

Browse files
authored
fix(cuda): use correct hwaccel arg, add configuration via env in entrypoint (#89)
1 parent a71c7e3 commit 5068615

File tree

3 files changed

+49
-16
lines changed

3 files changed

+49
-16
lines changed

Dockerfile.cuda

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ ENV HTTP_PORT 80
140140
ENV HTTPS_PORT 443
141141
ENV RTMP_PORT 1935
142142

143+
# Set default options.
144+
ENV SINGLE_STREAM ""
145+
ENV MAX_MUXING_QUEUE_SIZE ""
146+
ENV ANALYZEDURATION ""
147+
143148
RUN apt update && apt install -y --no-install-recommends \
144149
ca-certificates \
145150
curl \
@@ -158,13 +163,13 @@ COPY --from=build-ffmpeg /usr/lib/x86_64-linux-gnu/libfdk-aac.so.1 /usr/lib/x86_
158163

159164
# Add NGINX path, config and static files.
160165
ENV PATH "${PATH}:/usr/local/nginx/sbin"
161-
ADD nginx-cuda.conf /etc/nginx/nginx.conf.template
162166
RUN mkdir -p /opt/data && mkdir /www
167+
ADD nginx-cuda.conf /etc/nginx/nginx.conf.template
168+
ADD entrypoint.cuda.sh /opt/entrypoint.sh
169+
RUN chmod gu+x /opt/entrypoint.sh
163170
ADD static /www/static
164171

165172
EXPOSE 1935
166173
EXPOSE 80
167174

168-
CMD envsubst "$(env | sed -e 's/=.*//' -e 's/^/\$/g')" < \
169-
/etc/nginx/nginx.conf.template > /etc/nginx/nginx.conf && \
170-
nginx
175+
CMD /opt/entrypoint.sh

entrypoint.cuda.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
if [ ! -v "${MAX_MUXING_QUEUE_SIZE}" ]; then
5+
MAX_MUXING_QUEUE_SIZE_ARG="-max_muxing_queue_size ${MAX_MUXING_QUEUE_SIZE} "
6+
fi
7+
8+
if [ ! -v "${ANALYZEDURATION}" ]; then
9+
ANALYZEDURATION_ARG="-analyzeduration ${ANALYZEDURATION} "
10+
fi
11+
12+
quality1=('480' '256k' '64k' 'low' '448000')
13+
quality2=('720' '768k' '128k' 'mid' '448000')
14+
quality3=('960' '1240k' '128k' 'high' '1152000')
15+
quality4=('1280' '1920k' '128k' 'hd720' '2048000')
16+
17+
if [ -v ${SINGLE_STREAM} ]; then
18+
qualities=(quality1 quality2 quality3 quality4)
19+
else
20+
qualities=(quality4)
21+
fi
22+
23+
output_execpush="/usr/local/bin/ffmpeg $ANALYZEDURATION_ARG-async 1 -vsync -1 -hwaccel cuda -hwaccel_output_format cuda -c:v h264_cuvid -i rtmp://localhost:1935/stream/\$name "
24+
output_hlsvariants=""
25+
for quality in "${qualities[@]}"; do
26+
declare -n qualitylist=$quality
27+
output_execpush="$output_execpush"$'\n\t\t'"-c:v h264_nvenc -c:a aac -b:v ${qualitylist[1]} -b:a ${qualitylist[2]} -vf \"scale_npp=${qualitylist[0]}:trunc(ow/a/2)*2\" -zerolatency 1 $MAX_MUXING_QUEUE_SIZE_ARG-f flv rtmp://localhost:1935/hls/\$name_${qualitylist[3]}"
28+
output_hlsvariants=$'\n\t\t'"hls_variant _${qualitylist[3]} BANDWIDTH=${qualitylist[4]};"$'\n'"${output_hlsvariants}"
29+
done
30+
31+
export EXECPUSH="$output_execpush"
32+
export HLSVARIANTS="$output_hlsvariants"
33+
34+
envsubst "$(env | sed -e 's/=.*//' -e 's/^/\$/g')" < \
35+
/etc/nginx/nginx.conf.template > /etc/nginx/nginx.conf
36+
37+
nginx

nginx-cuda.conf

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,7 @@ rtmp {
1414
application stream {
1515
live on;
1616

17-
exec ffmpeg -hwaccel cuvid -c:v h264_cuvid -i rtmp://localhost:1935/stream/$name
18-
-c:a libfdk_aac -b:a 128k -c:v h264_nvenc -b:v 2500k -f flv -g 30 -r 30 -s 1280x720 -preset superfast -profile:v baseline rtmp://localhost:1935/hls/$name_720p2628kbs
19-
-c:a libfdk_aac -b:a 128k -c:v h264_nvenc -b:v 1000k -f flv -g 30 -r 30 -s 854x480 -preset superfast -profile:v baseline rtmp://localhost:1935/hls/$name_480p1128kbs
20-
-c:a libfdk_aac -b:a 128k -c:v h264_nvenc -b:v 750k -f flv -g 30 -r 30 -s 640x360 -preset superfast -profile:v baseline rtmp://localhost:1935/hls/$name_360p878kbs
21-
-c:a libfdk_aac -b:a 128k -c:v h264_nvenc -b:v 400k -f flv -g 30 -r 30 -s 426x240 -preset superfast -profile:v baseline rtmp://localhost:1935/hls/$name_240p528kbs
22-
-c:a libfdk_aac -b:a 64k -c:v h264_nvenc -b:v 200k -f flv -g 15 -r 15 -s 426x240 -preset superfast -profile:v baseline rtmp://localhost:1935/hls/$name_240p264kbs;
17+
exec ${EXECPUSH};
2318
}
2419

2520
application hls {
@@ -31,11 +26,7 @@ rtmp {
3126
hls_path /opt/data/hls;
3227
hls_nested on;
3328

34-
hls_variant _720p2628kbs BANDWIDTH=2628000,RESOLUTION=1280x720;
35-
hls_variant _480p1128kbs BANDWIDTH=1128000,RESOLUTION=854x480;
36-
hls_variant _360p878kbs BANDWIDTH=878000,RESOLUTION=640x360;
37-
hls_variant _240p528kbs BANDWIDTH=528000,RESOLUTION=426x240;
38-
hls_variant _240p264kbs BANDWIDTH=264000,RESOLUTION=426x240;
29+
${HLSVARIANTS}
3930
}
4031
}
4132
}
@@ -57,7 +48,7 @@ http {
5748

5849
# Uncomment these lines to enable SSL.
5950
# Update the ssl paths with your own certificate and private key.
60-
51+
6152
# listen ${HTTPS_PORT} ssl;
6253
# ssl_certificate /opt/certs/example.com.crt;
6354
# ssl_certificate_key /opt/certs/example.com.key;

0 commit comments

Comments
 (0)