forked from wargio/Twitch-Streamer-Linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitch.sh
executable file
·125 lines (106 loc) · 5.08 KB
/
twitch.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
121
122
123
124
#! /bin/bash
# Copyright (c) 2013, Giovanni Dante Grazioli (deroad)
# ================================================ OPTIONS =====================================================
# Streaming Options
OUTRES="1280x720" # Twitch Output Resolution
FPS="30" # Frame per Seconds (Suggested 24, 25, 30 or 60)
THREADS="4" # Change this if you have a good CPU (Suggested 4 threads, Max 6 threads)
QUALITY="medium" # ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow, placebo
CRF_VAL="23" # Is the quality of the encoding. 0 (lossless) and 51 (bad quality). 23 is medium quality
CBR="400k" # Constant bitrate (CBR) Increase this to get a better pixel quality (500k ok 1000k very good)
# Webcam Options
WEBCAM="/dev/video1" # WebCam device
WEBCAM_WH="320:240" # WebCam Width end Height
# You can find YOUR key here: http://www.twitch.tv/broadcast/ (Show Key button)
# Save your key inside the twitch_key file
STREAM_KEY=$(cat twitch_key)
# Twitch Server list http://bashtech.net/twitch/ingest.php
SERVER="live-fra" # EU server
# ============================================== END OPTIONS ===================================================
# The following values are changed automatically, so do not change them
TOPXY="0,0" # Position of the Window (You don't need to change this)
INRES="0x0" # Game Resolution (You don't need to change this)
# ================================================= CHECKS =====================================================
# checks to avoid a false "true" where it checks for the webcam
if [ -z "$WEBCAM" ]; then
echo "Your Webcam has been disabled because there isn't a WEBCAM in the options"
WEBCAM="/dev/no-webcam"
else
# checks to avoid a fail on loading the Webcam
if [ -z "$WEBCAM_WH" ]; then
echo "Your Webcam has been disabled because there isn't a WEBCAM_WH in the options"
WEBCAM="/dev/no-webcam"
fi
fi
# checks to avoid fails
if [ -z "$SERVER" ]; then
SERVER="live"
fi
if [ -z "$OUTRES" ]; then
OUTRES="1280x720"
fi
if [ -z "$FPS" ]; then
FPS="30"
fi
if [ -z "$THREADS" ]; then
THREADS="4"
fi
if [ -z "$QUALITY" ]; then
QUALITY="fast"
fi
if [ -z "$CBR" ]; then
CBR="400k"
fi
if [ -z "$CRF_VAL" ]; then
CRF_VAL="23"
fi
if [ -z "$STREAM_KEY" ]; then
echo "STREAM_KEY not set or there is a problem with it... Aborting."
echo "Check if the path to the file or the streaming key is correctly set."
exit 1
fi
# ================================================= CODE =======================================================
# DO NOT CHANGE THE CODE!
streamWebcam(){
echo "Webcam found!!"
echo "You should be online! Check on http://twitch.tv/ (Press CTRL+C to stop)"
avconv -f x11grab -s $INRES -r "$FPS" -i :0.0+$TOPXY -f alsa -ac 2 -i pulse -vcodec libx264 -s $OUTRES -b $CBR -minrate $CBR -maxrate $CBR -preset $QUALITY -tune film -crf $CRF_VAL -qscale:v 1 -threads:v $THREADS -acodec libmp3lame -ar 44100 -threads $THREADS -qscale:a 1 -bufsize 512k -vf "movie=$WEBCAM:f=video4linux2, scale=$WEBCAM_WH , setpts=PTS-STARTPTS [WebCam]; [in] setpts=PTS-STARTPTS, [WebCam] overlay=main_w-overlay_w-10:10 [out]" -force_key_frames 2 -b $CBR -minrate $CBR -maxrate $CBR -g 2 -keyint_min 2 -f flv "rtmp://$SERVER.twitch.tv/app/$STREAM_KEY"
}
streamNoWebcam(){
echo "Webcam NOT found!! ("$WEBCAM")"
echo "You should be online! Check on http://twitch.tv/ (Press CTRL+C to stop)"
avconv -f x11grab -s $INRES -r "$FPS" -i :0.0+$TOPXY -f alsa -ac 2 -i pulse -vcodec libx264 -s $OUTRES -b $CBR -minrate $CBR -maxrate $CBR -preset $QUALITY -tune film -crf $CRF_VAL -qscale:v 1 -threads:v $THREADS -acodec libmp3lame -ar 44100 -threads $THREADS -qscale:a 1 -bufsize 512k -force_key_frames 2 -b $CBR -minrate $CBR -maxrate $CBR -g 2 -keyint_min 2 -f flv "rtmp://$SERVER.twitch.tv/app/$STREAM_KEY"
}
echo "Twitch Streamer for Linux"
echo "Copyright (c) 2013, Giovanni Dante Grazioli (deroad)"
echo " "
# Get Game Window
echo "Click, with the mouse, on the Window that you want to Stream"
rm -f twitch_tmp 2> /dev/null
xwininfo -stats >> twitch_tmp
TOPXY=$(cat twitch_tmp | awk 'FNR == 8 {print $4}')","$(cat twitch_tmp | awk 'FNR == 9 {print $4}')
INRES=$(cat twitch_tmp | awk 'FNR == 12 {print $2}')"x"$(cat twitch_tmp | awk 'FNR == 13 {print $2}')
rm -f twitch_tmp 2> /dev/null
echo " "
# Setup
echo "Please setup the Audio Output (something like 'pavucontrol')"
# if you see errors here, please report on github
MODULE_LOAD1=$(pactl load-module module-null-sink sink_name=GameAudio) # For Game Audio
MODULE_LOAD2=$(pactl load-module module-null-sink sink_name=MicAudio ) # For Mic Audio
pactl load-module module-loopback sink=GameAudio >> /dev/null
pactl load-module module-loopback sink=MicAudio >> /dev/null
echo " "
# Checks if the webcam is loaded
if [ -c $WEBCAM ]; then
streamWebcam
else
streamNoWebcam
fi
echo " "
# Closing..
echo "Stopping Audio (Don't worry if you see errors here)"
pactl unload-module $MODULE_LOAD1
pactl unload-module $MODULE_LOAD2
pactl unload-module module-null-sink
pactl unload-module module-loopback
echo "Exit!"