-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay-stream.sh
executable file
·120 lines (108 loc) · 3.65 KB
/
play-stream.sh
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
#!/usr/bin/env bash
if [ -x /snap/bin/obs-studio.ffplay ]; then
PLAYER="/snap/bin/obs-studio.ffplay"
else
PLAYER=""
fi
LAUNCHER=$(basename $0 .sh)
STAMP=$(date +"%C%j-%H%M%S")
LOG_LEVEL="fatal"
# Network settings
IP_PROTO="srt"
IP_PORT="4864"
IP_ADDR="0.0.0.0"
function usage {
echo
echo "Usage"
echo " ${LAUNCHER} [--ip 192.168.0.1] [--player [ffplay|mpv] [--port 4864] [--protocol srt|tcp|udp] [--help]"
echo
echo "You can also pass optional parameters"
echo " --ip : Set the IP address to play from."
echo " --player : Set the player. [ffplay|mpv]"
echo " --port : Set the tcp/udp port to connect to."
echo " --protocol : Set the protocol to play over. [srt|tcp|udp]"
echo " --help : This help."
echo
exit 1
}
function cleanup_trap() {
CLEANUP_PLAYER=$(killall ${PLAYER} 2>/dev/null)
exit
}
# TODO - validate the inputs
# Check for optional parameters
while [ $# -gt 0 ]; do
case "${1}" in
-ip|--ip)
IP_ADDR="$2"
shift
shift;;
-player|--player)
PLAYER="$2"
shift
shift;;
-port|--port)
IP_PORT="$2"
shift
shift;;
-protocol|--protocol)
IP_PROTO="$2"
shift
shift;;
-h|--h|-help|--help)
usage;;
*)
echo "ERROR! \"${1}\" is not a supported parameter."
usage;;
esac
done
TEST_PLAYER=$(which "${PLAYER}")
if [ $? -eq 1 ]; then
echo "ERROR! Could not find ${PLAYER}. Quitting."
exit 1
fi
if [ "${IP_PROTO}" != "srt" ] && [ "${IP_PROTO}" != "tcp" ] && [ "${IP_PROTO}" != "udp" ]; then
echo "ERROR! Unknown IP protocol: ${IP_PROTO}. Quitting."
exit 1
fi
case ${IP_PROTO} in
srt) STREAM_OPTIONS="?mode=listener";;
tcp) STREAM_OPTIONS="?listen";;
udp) STREAM_OPTIONS="";;
esac
# Do we have nvdec capable hardware?
TEST_NVENC=$(nvidia-smi -q | grep Encoder | wc -l)
TEST_CUVID=$(${PLAYER} -hide_banner -decoders | grep h264_cuvid | wc -l)
if [ ${TEST_NVENC} -ge 1 ] && [ ${TEST_CUVID} -ge 1 ]; then
DECODER="h264_cuvid"
THREADS=1
else
DECODER="h264"
THREADS=2
fi
# Call cleanup_trap() function on Ctrl+C
trap "cleanup_trap" SIGINT SIGTERM
if [ "${LAUNCHER}" == "play-stream" ]; then
# Run the player in an infinite loop so is it always listening. Exit with Ctrl+C.
while true; do
case ${PLAYER} in
/snap/bin/obs-studio.ffplay|/usr/bin/ffplay|ffplay)
WIN_TITLE="${LAUNCHER} - ffplay"
# Play a video stream with low latency
# - https://stackoverflow.com/questions/16658873/how-to-minimize-the-delay-in-a-live-streaming-with-ffmpeg
echo "Playing: ${IP_PROTO}://${IP_ADDR}:${IP_PORT}${STREAM_OPTIONS}"
${PLAYER} -hide_banner -threads ${THREADS} -loglevel ${LOG_LEVEL} -stats \
-probesize 2M -fflags nobuffer+nofillin+fastseek+flush_packets+igndts -flags low_delay -sync ext -framedrop -noinfbuf -strict experimental -window_title "${WIN_TITLE}" -vcodec ${DECODER} -i "${IP_PROTO}://${IP_ADDR}:${IP_PORT}${STREAM_OPTIONS}"
;;
mpv)
WIN_TITLE="${LAUNCHER} - mpv"
mpv --no-cache --untimed --profile=low-latency --demuxer-lavf-buffersize=1 --swapchain-depth=1 --title="${WIN_TITLE}" "${IP_PROTO}://${IP_ADDR}:${IP_PORT}${STREAM_OPTIONS}"
;;
esac
done
elif [ "${LAUNCHER}" == "record-stream" ]; then
echo "Recording: ${IP_PROTO}://${IP_ADDR}:${IP_PORT}${STREAM_OPTIONS}"
# Record a video stream in a Matroska container.
ffmpeg -hide_banner -threads ${THREADS} -loglevel ${LOG_LEVEL} -stats \
-probesize 2M -fflags nobuffer+nofillin+fastseek+flush_packets+igndts -flags low_delay -sync ext -framedrop -noinfbuf -strict experimental -i ${IP_PROTO}://${IP_ADDR}:${IP_PORT}${STREAM_OPTIONS} -c:a copy -c:v copy "${LAUNCHER}-${STAMP}.mkv"
fi