Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# NGINX-RTMP-BACKUP
This is a set of shell scripts that allows you to implement elementary backups for your RTMP streams.
## Prerequisites
You have to have [avconv](https://libav.org/avconv.html) (or [ffmpeg](https://www.ffmpeg.org/)) and [nginx](https://nginx.ru/en/) with [nginx-rtmp-module](https://github.com/arut/nginx-rtmp-module) installed on your machine.
You have to have [gstreamer](https://gstreamer.freedesktop.org) ( or [avconv](https://libav.org/avconv.html) or [ffmpeg](https://www.ffmpeg.org/)) and [nginx](https://nginx.ru/en/) with [nginx-rtmp-module](https://github.com/sergey-dryabzhinsky/nginx-rtmp-module) installed on your machine.

## Installation
1. Download files from this repo (or clone it locally);
Expand All @@ -26,8 +26,8 @@ Default is `out`.
`true` or `false`. If set to `true`, the main stream will be pushed to out stream each time it recovers. If set to `false`, once the out stream switches to backup, it will stay there.
Default is `true`.
* `RUNNER`
`avconv` or `ffmpeg`. Defines which program will push streams.
Default is `avconv`.
`gst` or `avconv` or `ffmpeg`. Defines which program will push streams.
Default is `gst`.
* `NGINX_USER`
A username nginx workers runs under. Required for setting right permissions for logs and pids folders.
Default is `nobody`.
Expand Down Expand Up @@ -56,14 +56,14 @@ Basically, you need to create three applications, one accepting the main stream,
# Enable live streaming.
live on;

# This will prevent avconv/ffmpeg from hanging when stream ends.
# This will prevent gst/avconv/ffmpeg from hanging when stream ends.
# We will kill it from scripts anyway, but just in case.
play_restart on;

# We need `out` app to have access from localhost.
allow play 127.0.0.1;
# You may want this in case not to allow anyone to watch streams from this point.
deny play all;
# However, we need `out` app to have access.
allow play 127.0.0.1;

# That's where the magic starts.
# Do not forget to change paths.
Expand All @@ -83,8 +83,8 @@ Basically, you need to create three applications, one accepting the main stream,
application backup {
live on;
play_restart on;
deny play all;
allow play 127.0.0.1;
deny play all;

# When stream stops publishing,
# call backup_publish_done.sh and pass a streamname to it.
Expand All @@ -102,9 +102,9 @@ For example, if you have specified the following names for nginx-rtmp apps:
`BACKUP_STREAM_APPNAME="backup"`
`OUT_STREAM_APPNAME="out"`,
and then sent your streams to `rtmp://your.domain/main/test` and `rtmp://your.domain/backup/test`, you can watch the output stream at `rtmp://your.domain/out/test`.
When switching between streams, you may see a slight delay, as avconv/ffmpeg needs time to run.
When switching between streams, you may see a slight delay, as gst/avconv/ffmpeg needs time to run.
### Logs
All logs are stored at `/var/log/nginx-rtmp/backup`.
Logs for avconv/ffmpeg are stored under the names `main_$streamname.log` and `backup_$streamname.log`, where `$streamname` is the RTMP key you send your stream to.
Logs for gst/avconv/ffmpeg are stored under the names `main_$streamname.log` and `backup_$streamname.log`, where `$streamname` is the RTMP key you send your stream to.
Logs for scripts are stored in the subdirectory `scripts` named after the scripts themselves.

6 changes: 3 additions & 3 deletions config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
# with true it will change back, with false -- stay on backup stream
MAIN_STREAM_PRIORITY="true"

# [avconv|ffmpeg] defines which program will push your stream
# use avconv if not sure
RUNNER="avconv"
# [gst|avconv|ffmpeg] defines which program will push your stream
# use ffmpeg if not sure
RUNNER="gst"

# nginx rtmp application name for main stream
MAIN_STREAM_APPNAME="main"
Expand Down
2 changes: 1 addition & 1 deletion main_publish_done.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exec > "$LOGS_FOLDER/scripts/main_publish_done.log" 2>&1
parse_argv "$@"

# Stop pushing main stream.
# In case stopping avconv/ffmpeg needs some time, use a loop.
# In case stopping gst/avconv/ffmpeg needs some time, use a loop.
# Otherwise pushing backup may be denied with 'already publishing' error
while is_running main; do
kill main
Expand Down
10 changes: 5 additions & 5 deletions nginx_example.conf
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ rtmp {
# Enable live streaming.
live on;

# This will prevent avconv/ffmpeg from hanging when stream ends.
# This will prevent gst/avconv/ffmpeg from hanging when stream ends.
# We will kill it from scripts anyway, but just in case.
play_restart on;

# We need `out` app to have access from localhost.
allow play 127.0.0.1;
# You may want this in case not to allow anyone to watch streams from this point.
deny play all;
# However, we need `out` app to have access.
allow play 127.0.0.1;

# That's where the magic starts.
# Do not forget to change paths.
Expand All @@ -42,12 +42,12 @@ rtmp {
application backup {
live on;
play_restart on;
deny play all;
allow play 127.0.0.1;
deny play all;

# When stream stops publishing,
# call backup_publish_done.sh and pass a streamname to it.
exec_publish_done /usr/local/share/nginx-rtmp-backup/backup_publish_done.sh $name;
}
}
}
}
34 changes: 24 additions & 10 deletions utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,31 @@ push_stream() { # Starts pushing stream

LOGFILE="$LOGS_FOLDER/${appname}_${STREAMNAME}.log"

assert_one_of RUNNER avconv ffmpeg
assert_one_of RUNNER gst avconv ffmpeg

nohup "$RUNNER" \
-re -i "rtmp://localhost/$appname/$STREAMNAME" \
-c copy -f flv \
"rtmp://localhost/$OUT_STREAM_APPNAME/$STREAMNAME" \
\
</dev/null \
>"$LOGFILE" \
2>&1 \
&
if [ "$RUNNER" = "gst" ]; then
nohup gst-launch-1.0 \
rtmpsrc location="rtmp://localhost/$appname/$STREAMNAME" do-timestamp=true ! queue ! flvdemux name=demux \
flvmux name=mux \
demux.video ! queue ! mux.video \
demux.audio ! queue ! mux.audio \
mux.src ! queue ! rtmpsink location="rtmp://localhost/$OUT_STREAM_APPNAME/$STREAMNAME" \
\
</dev/null \
>"$LOGFILE" \
2>&1 \
&
else
nohup "$RUNNER" \
-re -i "rtmp://localhost/$appname/$STREAMNAME" \
-c copy -f flv \
"rtmp://localhost/$OUT_STREAM_APPNAME/$STREAMNAME" \
\
</dev/null \
>"$LOGFILE" \
2>&1 \
&
fi

echo $! > "$(pid_for "$stream_kind")"
}