├── .gitignore ├── README.md ├── Screenshots ├── Screenshot.png ├── Streaming-Quality.png ├── Twitch_Audio00.png └── Twitch_Audio01.png ├── twitch_avconv.sh ├── twitch_ffmpeg.sh └── twitch_key /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | twitch_tmp 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Twitch Streamer For Linux 2 | ========================= 3 | 4 | This is a script dedicated to stream to Twitch.tv. it also has Webcam support! 5 | **AVCONV script is DEPRECATED, due libav drama, etc...** 6 | 7 | Tested on: 8 | * Linux: Ubuntu 13.04 9 | * CPU: AMD Athlon(tm) 64 X2 Dual Core Processor 4200+ × 2 10 | * Ram: 2 GB 11 | * Arch: 64bit 12 | * GPU: Radeon X1300 PCI (ATI RV515) 13 | * Bandwith: ~70 byte/s 14 | * Mem Used: ~192 MB (2 Threads) 15 | 16 | ![Streaming Quality](https://raw.github.com/wargio/Twitch-Streamer-Linux/master/Screenshots/Streaming-Quality.png) 17 | 18 | Authors: 19 | -------- 20 | 21 | * Giovanni Dante Grazioli 22 | 23 | ### Contributors 24 | 25 | * İlteriş Eroğlu (linuxgemini) 26 | * yofreke 27 | * Vilsol 28 | 29 | How to 30 | ------ 31 | 32 | * Go to http://www.twitch.tv/broadcast/ , click on the **Show Key** button and copy and paste the key inside the twitch_key file or inside the ~/.twitch_key 33 | * To save the Streaming, go to http://twitch.tv/settings/videos and check **Archive Broadcasts - Automatically archive my broadcasts** 34 | * Open now the twitch.sh and edit the settings 35 | 36 | Value Example Description 37 | ------------------ ------------------------ --------------------------------------------------------------------------------------------------------- 38 | OUTRES "1280x720" Twitch Output Resolution ("1920x1080" should be the maximum resolution) 39 | FPS "24" Frame per Seconds (Suggested 24, 25, 30 or 60) 40 | THREADS "4" Change this if you have a good CPU (Suggested 4 threads, Max 6 threads) 41 | QUALITY "medium" or "veryfast" Streaming Quality (ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow, placebo) 42 | WEBCAM "/dev/video1" WebCam chardevice under /dev 43 | WEBCAM_WH "320:240" WebCam Width end Height in the Output 44 | WEBCAM_XY "10:10" WebCam Position if empty then it will set the standard position 45 | SERVER "live" or "live-fra" Twitch Server list at http://bashtech.net/twitch/ingest.php 46 | CBR "1000k" to "3000k" Constant bitrate. Increase this to get a better pixel quality (Twitch suggest between 1000k to 3000k) 47 | ALWAYS_FULLSCREEN "false" or "true" Change this to 'true' if you want to go always on FULLSCREEN, this will disable the output. 48 | SUPPRESS_OUTPUT "false" or "true" Change this to 'true' if you want to hide your STREAM_KEY, for security purpose. This will not affect 49 | the ALWAYS_FULLSCREEN option. ALWAYS_FULLSCREEN will always disable the output. 50 | FILE_VIDEO "My_stream.flv" File name to redirect the stream if there's the -save arg (go to the How To to see how it works) 51 | SET_XY "10,100" Position of the Window on the screen (X,Y) and will be used only if the -coords option is called. 52 | SET_INRES "1024x600" Window size (WxH) and will be used only if the -coords option is called. 53 | AUDIO_RATE "44100" Twitch Audio Rate. Twitch itself, says must be 2 channels with 44100 as rate; so DO NOT CHANGE IT! 54 | KEY_FRAME "2" Twitch Video Key Frame. Twitch itself, says must be 2, so DO NOT CHANGE IT! 55 | 56 | * Open the game that you want to stream and set window mode. 57 | * Open a terminal, browse to the twitch script directory and run the script 58 | 59 | $ ./twitch_ffmpeg.sh 60 | 61 | * Avconv script is deprecated. It can give you errors 62 | * Click with your Mouse on the game window 63 | * Now you should be live (check on your channel). 64 | * You can set the Audio settings (select the Null Output on pavucontrol) 65 | * To stop the stream, click on the terminal and press CTRL+C 66 | * **BE CAREFUL ON SHOWING THE TERMINAL SINCE FFMPEG PRINTS ON THE TERMINAL THE KEY** 67 | * For suggestion or bugfix, please write to me on github. (I love suggestions! <3 ) 68 | * Please do NOT write for support on my BLOG. post bug reports on http://github.com/wargio/Twitch-Streamer-Linux ! 69 | 70 | Additional How to 71 | ----------------- 72 | 73 | * You can run the scripts with some arguments (you can use a combination of these): 74 | 75 | Value Description 76 | ------------------ ------------------------------------------------------------------------------- 77 | -h Display the usage screen 78 | -fullscreen Run the script in FULLSCREEN mode 79 | -window Run the script in WINDOW mode 80 | -coords Set the screen resolution, by using SET_XY SET_INRES defined inside the script 81 | -save Save the video to the file FILE_VIDEO instead of streaming it 82 | -quiet Disables most of the outputs (The Twitch KEY will be hided) 83 | 84 | 85 | Setup Audio (with pavucontrol): 86 | ------------------------------- 87 | * My microphone is `Turtle Beach PLa Headset` and my game is `Syobon Action (Cat Mario)` 88 | * Open the game you want to stream and exec the script 89 | * Now open `pavucontrol` 90 | * Under `Playback`: 91 | ![Screenshot from pavucontrol](https://raw.github.com/wargio/Twitch-Streamer-Linux/master/Screenshots/Twitch_Audio00.png) 92 | * Under `Recording`: 93 | ![Screenshot from pavucontrol](https://raw.github.com/wargio/Twitch-Streamer-Linux/master/Screenshots/Twitch_Audio01.png) 94 | * Done (For `ffmpeg/avconv` setting, `Monitor of MicAudio` or `Monitor of GameAudio`. it's the same). 95 | * If you don't see any `Monitor of MicAudio` or `Monitor of GameAudio`, but only `Monitor of null output`, use it, don't worry. 96 | 97 | Dependencies: 98 | ------------- 99 | These dependencies are the name of the packages that you need. The libs names can change from distro to distro!. 100 | 101 | x11-utils pulseaudio-utils libfaac-dev libmp3lame-dev libv4l-dev libx264-dev libpulse-dev librtmp-dev libasound2-dev X11proto-xext-dev libxext-dev 102 | 103 | Grab the latest `ffmpeg` from: 104 | 105 | git clone git://source.ffmpeg.org/ffmpeg.git ffmpeg 106 | 107 | Build it with the following build options: 108 | 109 | ./configure --disable-ffplay --disable-ffprobe --disable-ffserver --enable-libfaac --enable-libmp3lame --enable-libv4l2 --enable-libx264 --enable-x11grab --enable-libpulse --enable-librtmp --enable-gpl --enable-nonfree --disable-yasm --extra-libs="-lasound" && make 110 | 111 | ### Suggested: 112 | 113 | pavucontrol 114 | 115 | FAQ 116 | --- 117 | * How do i choose the bitrate? 118 | 119 | The optimal bitrate can be calculated in this way: `bitrate = Width*Height/144`, 120 | an example `720*480/144 = 2400 (k)` but if you get a bitrate above 5000(k) 121 | like `1920*1080/144=2073456(k = ~2073 M)` choose 5000k, because 6000k is 122 | DVD quality. 123 | 124 | * I see some errors after `Stopping Audio (Don't worry if you see errors here)`, should i worry about this? 125 | 126 | No, you don't. They are supposed to show up. 127 | 128 | * Which is the Webcam standard position on the screen? 129 | 130 | The standard position is: (ScreenWidth - WebcamWidth - 10):10 (Upper Right corner) 131 | Keep in mind that the coordinates start from 0:0 (Upper Left corner) to Width:Height (Bottom Right corner) 132 | 133 | * How i can test the output before going to livestream? 134 | 135 | Use the `-save` arg for the script: `./twitch_ffmpeg -save` or `./twitch_avconv -save` 136 | in this way you can see how it looks like and change things if they are not ok. 137 | 138 | * How i can save the output instead of livestreaming? 139 | Use the `-save` arg for the script: `./twitch_ffmpeg -save` or `./twitch_avconv -save` 140 | 141 | * Why the avconv script is deprecated? 142 | It's deprecated due libav drama. Please use ffmpeg. 143 | 144 | 145 | Screenshot: 146 | ----------- 147 | 148 | ![Screenshot from twitch.tv](https://raw.github.com/wargio/Twitch-Streamer-Linux/master/Screenshots/Screenshot.png) 149 | -------------------------------------------------------------------------------- /Screenshots/Screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wargio/Twitch-Streamer-Linux/66f6e4ad4c926d57337ff396fbde198b75f131e7/Screenshots/Screenshot.png -------------------------------------------------------------------------------- /Screenshots/Streaming-Quality.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wargio/Twitch-Streamer-Linux/66f6e4ad4c926d57337ff396fbde198b75f131e7/Screenshots/Streaming-Quality.png -------------------------------------------------------------------------------- /Screenshots/Twitch_Audio00.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wargio/Twitch-Streamer-Linux/66f6e4ad4c926d57337ff396fbde198b75f131e7/Screenshots/Twitch_Audio00.png -------------------------------------------------------------------------------- /Screenshots/Twitch_Audio01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wargio/Twitch-Streamer-Linux/66f6e4ad4c926d57337ff396fbde198b75f131e7/Screenshots/Twitch_Audio01.png -------------------------------------------------------------------------------- /twitch_avconv.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | # Copyright (c) 2013-2014, Giovanni Dante Grazioli (deroad) 4 | 5 | # ================================================ OPTIONS ===================================================== 6 | # Add the AVCONV ABSOLUTE PATH "/path/to/ffmpeg" 7 | AVCONV_PATH="avconv" 8 | 9 | # Streaming Options 10 | OUTRES="1280x720" # Twitch Output Resolution 11 | FPS="24" # Frame per Seconds (Suggested 24, 25, 30 or 60) 12 | THREADS="4" # Change this if you have a good CPU (Suggested 4 threads, Max 6 threads) 13 | QUALITY="ultrafast" # ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow, placebo 14 | CBR="1000k" # Constant bitrate (CBR) Increase this to get a better pixel quality (1000k - 3000k for twitch) 15 | 16 | # Webcam Options 17 | WEBCAM="/dev/video1" # WebCam device 18 | WEBCAM_WH="320:240" # WebCam Width end Height 19 | WEBCAM_XY="" # WebCam Position (in pixel) example: "10:10", if "" (empty) then it will set the standard position 20 | 21 | # File to save if you do not want to stream 22 | FILE_VIDEO="my.flv" # File name 23 | 24 | # STREAM KEY 25 | # You can find YOUR key here: http://www.twitch.tv/broadcast/ (Show Key button) 26 | # Save your key inside the twitch_key file 27 | # Or make a global file named ".twitch_key" in your home directory (~/.twitch_key) 28 | 29 | # Twitch Server list http://bashtech.net/twitch/ingest.php 30 | SERVER="live-fra" # EU server 31 | 32 | # Change this to 'true' if you want to go always on FULLSCREEN, this will disable the output. 33 | ALWAYS_FULLSCREEN=false 34 | 35 | # Change this to 'true' if you want to hide your STREAM_KEY, for security purpose (this will disable most of the output). 36 | # This will not affect the ALWAYS_FULLSCREEN option. ALWAYS_FULLSCREEN will always disable the output. 37 | SUPPRESS_OUTPUT=false 38 | 39 | # Twitch says it MUST have a 44100 rate, please do not change it unless you know what you are doing. 40 | AUDIO_RATE="44100" 41 | 42 | 43 | # ============================================== END OPTIONS =================================================== 44 | # The following values are changed automatically, so do not change them 45 | TOPXY="0,0" # Position of the Window (You don't need to change this) 46 | INRES="0x0" # Game Resolution (You don't need to change this) 47 | LOGLEVEL_ARG="" # LogLevel, for security purpose (You don't need to change this) 48 | SCREEN_SETUP=0 # Do not change this. it's used for the args. 49 | STREAM_SAVE=0 # Do not change this. it's used for the args. 50 | # ================================================= CHECKS ===================================================== 51 | # To see the output. 52 | ECHO_LOG="" 53 | 54 | # checks to avoid a false "true" where it checks for the webcam 55 | if [ -z "$WEBCAM" ]; then 56 | ECHO_LOG=$ECHO_LOG"\nYour Webcam has been disabled because there isn't a WEBCAM in the options" 57 | WEBCAM="/dev/no-webcam" 58 | elif [ -z "$WEBCAM_WH" ]; then 59 | # checks to avoid a fail on loading the Webcam 60 | ECHO_LOG=$ECHO_LOG"\nYour Webcam has been disabled because there isn't a WEBCAM_WH in the options" 61 | WEBCAM="/dev/no-webcam" 62 | fi 63 | # Find stream key 64 | if [ -f ~/.twitch_key ]; then 65 | ECHO_LOG=$ECHO_LOG"\nUsing global twitch key located in home directory" 66 | STREAM_KEY=$(cat ~/.twitch_key) 67 | else 68 | if [ -f ./twitch_key ]; then 69 | ECHO_LOG=$ECHO_LOG"\nUsing twitch key located in current running directory" 70 | STREAM_KEY=$(cat ./twitch_key) 71 | else 72 | echo "Could not locate ~/.twitch_key or twitch_key" 73 | exit 1 74 | fi 75 | fi 76 | 77 | 78 | # Find script name 79 | SCRIPT_NAME=${0##*/} 80 | 81 | # Suppress output for security purpose 82 | if [ $SUPPRESS_OUTPUT = true ]; then 83 | ECHO_LOG=$ECHO_LOG"\nOutput blocked!" 84 | LOGLEVEL_ARG="-loglevel 0" 85 | fi 86 | 87 | # checks to avoid fails 88 | if [ -z "$SERVER" ]; then 89 | SERVER="live" 90 | fi 91 | if [ -z "$OUTRES" ]; then 92 | OUTRES="1280x720" 93 | fi 94 | if [ -z "$FPS" ]; then 95 | FPS="30" 96 | GOP="60" 97 | GOPMIN="30" 98 | else 99 | GOP=$(($FPS*2)) 100 | GOPMIN=$FPS 101 | fi 102 | if [ -z "$THREADS" ]; then 103 | THREADS="4" 104 | fi 105 | if [ -z "$QUALITY" ]; then 106 | QUALITY="fast" 107 | fi 108 | if [ -z "$CBR" ]; then 109 | CBR="1000k" 110 | fi 111 | if [ -z "$ALWAYS_FULLSCREEN" ]; then 112 | ALWAYS_FULLSCREEN=false 113 | fi 114 | if [ -z "$AUDIO_RATE" ]; then 115 | AUDIO_RATE="44100" 116 | fi 117 | if [ -z "$LOGLEVEL_ARG" ]; then 118 | LOGLEVEL_ARG="" 119 | fi 120 | if [ -z "$SUPPRESS_OUTPUT" ]; then 121 | SUPPRESS_OUTPUT=false 122 | fi 123 | if [ -z "$FILE_VIDEO" ]; then 124 | FILE_VIDEO="video.flv" 125 | fi 126 | if [ ! $SCREEN_SETUP -eq 0 ]; then 127 | SCREEN_SETUP=0 128 | fi 129 | if [ ! $STREAM_SAVE -eq 0 ]; then 130 | STREAM_SAVE=0 131 | fi 132 | if [ -z "$STREAM_KEY" ]; then 133 | ECHO_LOG=$ECHO_LOG"\nSTREAM_KEY not set or there is a problem with it... Aborting." 134 | ECHO_LOG=$ECHO_LOG"\nCheck if the path to the file or the streaming key is correctly set." 135 | exit 1 136 | fi 137 | 138 | # ================================================= CODE ======================================================= 139 | # DO NOT CHANGE THE CODE! 140 | MODULE_LOAD1="" 141 | MODULE_LOAD2="" 142 | APP_RETURN="" 143 | 144 | checkFileExists(){ 145 | if [ -f $FILE_VIDEO ]; then 146 | i=0 147 | TMP="OLD_"$i"_"$FILE_VIDEO 148 | echo "$FILE_VIDEO already exists! finding a new name for the old file" 149 | while [ -f $TMP ] 150 | do 151 | i=$((i+1)) 152 | TMP="OLD_"$i"_"$FILE_VIDEO 153 | echo "$TMP already exists" 154 | done 155 | echo "The old stream $FILE_VIDEO has been renamed as $TMP" 156 | echo "The new stream will be saved into $FILE_VIDEO" 157 | mv $FILE_VIDEO $TMP 158 | fi 159 | } 160 | 161 | streamWebcam(){ 162 | echo "Webcam found!!" 163 | echo "You should be online! Check on http://twitch.tv/ (Press CTRL+C to stop)" 164 | echo " " 165 | if [ -z "$WEBCAM_XY" ]; then 166 | # checks to avoid a fail on loading the Webcam 167 | #standard position is: main_w - overlay_w - 10:10 168 | WEBCAM_XY="$(($(echo $INRES | awk -F"x" '{ print $1 }') - $(echo $WEBCAM_WH | awk -F":" '{ print $1 }') - 10)):10" 169 | echo "There isn't a WEBCAM_XY in the options, i'll generate the standard one ($WEBCAM_XY)" 170 | fi 171 | $AVCONV_PATH -f x11grab -s $INRES -framerate "$FPS" -i :0.0+$TOPXY -f alsa -i pulse -f flv -ac 2 -ar $AUDIO_RATE -vcodec libx264 -g $GOP -keyint_min $GOPMIN -b $CBR -minrate $CBR -maxrate $CBR -pix_fmt yuv420p -s $OUTRES -preset $QUALITY -tune film -acodec libmp3lame -threads $THREADS -vf "movie=$WEBCAM:f=video4linux2, scale=$WEBCAM_WH , setpts=PTS-STARTPTS [WebCam]; [in] setpts=PTS-STARTPTS [Screen]; [Screen][WebCam] overlay=$WEBCAM_XY [out]" -strict normal -bufsize $CBR $LOGLEVEL_ARG "rtmp://$SERVER.twitch.tv/app/$STREAM_KEY" 172 | APP_RETURN=$? 173 | } 174 | 175 | streamNoWebcam(){ 176 | echo "Webcam NOT found!! ("$WEBCAM")" 177 | echo "You should be online! Check on http://twitch.tv/ (Press CTRL+C to stop)" 178 | echo " " 179 | $AVCONV_PATH -f x11grab -s $INRES -framerate "$FPS" -i :0.0+$TOPXY -f alsa -i pulse -f flv -ac 2 -ar $AUDIO_RATE -vcodec libx264 -g $GOP -keyint_min $GOPMIN -b:v $CBR -minrate $CBR -maxrate $CBR -pix_fmt yuv420p -s $OUTRES -preset $QUALITY -tune film -acodec libmp3lame -threads $THREADS -strict normal -bufsize $CBR $LOGLEVEL_ARG "rtmp://$SERVER.twitch.tv/app/$STREAM_KEY" 180 | APP_RETURN=$? 181 | } 182 | 183 | saveStreamWebcam(){ 184 | echo "Webcam found!!" 185 | echo "You should be online! Check on http://twitch.tv/ (Press CTRL+C to stop)" 186 | echo " " 187 | if [ -z "$WEBCAM_XY" ]; then 188 | # checks to avoid a fail on loading the Webcam 189 | #standard position is: main_w - overlay_w - 10:10 190 | WEBCAM_XY="$(($(echo $INRES | awk -F"x" '{ print $1 }') - $(echo $WEBCAM_WH | awk -F":" '{ print $1 }') - 10)):10" 191 | echo "There isn't a WEBCAM_XY in the options, i'll generate the standard one ($WEBCAM_XY)" 192 | fi 193 | $AVCONV_PATH -f x11grab -s $INRES -framerate "$FPS" -i :0.0+$TOPXY -f alsa -i pulse -f flv -ac 2 -ar $AUDIO_RATE -vcodec libx264 -g $GOP -keyint_min $GOPMIN -b $CBR -minrate $CBR -maxrate $CBR -pix_fmt yuv420p -s $OUTRES -preset $QUALITY -tune film -acodec libmp3lame -threads $THREADS -vf "movie=$WEBCAM:f=video4linux2, scale=$WEBCAM_WH , setpts=PTS-STARTPTS [WebCam]; [in] setpts=PTS-STARTPTS [Screen]; [Screen][WebCam] overlay=$WEBCAM_XY [out]" -strict normal -bufsize $CBR $LOGLEVEL_ARG $FILE_VIDEO 194 | APP_RETURN=$? 195 | } 196 | 197 | saveStreamNoWebcam(){ 198 | echo "Webcam NOT found!! ("$WEBCAM")" 199 | echo "You should be online! Check on http://twitch.tv/ (Press CTRL+C to stop)" 200 | echo " " 201 | $AVCONV_PATH -f x11grab -s $INRES -framerate "$FPS" -i :0.0+$TOPXY -f alsa -i pulse -f flv -ac 2 -ar $AUDIO_RATE -vcodec libx264 -g $GOP -keyint_min $GOPMIN -b:v $CBR -minrate $CBR -maxrate $CBR -pix_fmt yuv420p -s $OUTRES -preset $QUALITY -tune film -acodec libmp3lame -threads $THREADS -strict normal -bufsize $CBR $LOGLEVEL_ARG $FILE_VIDEO 202 | APP_RETURN=$? 203 | } 204 | 205 | doDefaults(){ 206 | if [ $ALWAYS_FULLSCREEN = true ]; then 207 | echo "[+] ALWAYS_FULLSCREEN is ON. Going to fullscreen!" 208 | echo "[+] Output blocked!" 209 | TOPXY="0,0" 210 | INRES=$(xwininfo -root | awk '/geometry/ {print $2}'i | sed -e 's/\+[0-9]//g') 211 | LOGLEVEL_ARG="-loglevel 0" 212 | SUPPRESS_OUTPUT=true 213 | else 214 | echo "[+] Click, with the mouse, on the Window that you want to Stream" 215 | rm -f twitch_tmp 2> /dev/null 216 | xwininfo -stats >> twitch_tmp 217 | TOPXY=$(cat twitch_tmp | awk 'FNR == 8 {print $4}')","$(cat twitch_tmp | awk 'FNR == 9 {print $4}') 218 | INRES=$(cat twitch_tmp | awk 'FNR == 12 {print $2}')"x"$(cat twitch_tmp | awk 'FNR == 13 {print $2}') 219 | rm -f twitch_tmp 2> /dev/null 220 | echo " " 221 | fi 222 | } 223 | 224 | loadModule(){ 225 | MODULE_LOAD1=$(pactl load-module module-null-sink sink_name=GameAudio sink_properties=device.description="GameAudio") # For Game Audio 226 | MODULE_LOAD2=$(pactl load-module module-null-sink sink_name=MicAudio sink_properties=device.description="MicAudio") # For Mic Audio 227 | pactl load-module module-device-manager >> /dev/null 228 | pactl load-module module-loopback sink=GameAudio >> /dev/null 229 | pactl load-module module-loopback sink=MicAudio >> /dev/null 230 | } 231 | 232 | unloadModule(){ 233 | echo "Stopping Audio (Don't worry if you see errors here)" 234 | pactl unload-module $MODULE_LOAD1 235 | pactl unload-module $MODULE_LOAD2 236 | pactl unload-module module-device-manager 237 | pactl unload-module module-null-sink 238 | pactl unload-module module-loopback 239 | echo "Exit!" 240 | } 241 | 242 | showUsage(){ 243 | echo "usage:" 244 | echo " "$SCRIPT_NAME" [options]" 245 | echo " -h | show usage screen" 246 | echo " -fullscreen | enable the fullscreen" 247 | echo " and disable the output" 248 | echo " -window | enable the window mode" 249 | echo " -save | save the video to a file" 250 | echo " instead of streaming it" 251 | echo " -quiet | disables most of the outputs" 252 | } 253 | 254 | 255 | echo " " 256 | echo "Twitch Streamer for Linux ("$SCRIPT_NAME")" 257 | echo "Copyright (c) 2013 - 2014, Giovanni Dante Grazioli (deroad)" 258 | 259 | echo "This script is DEPRECATED. if it does not work, use the ffmpeg one." 260 | 261 | # To be sure to unload everything 262 | trap "unloadModule; exit" SIGHUP SIGINT SIGTERM 263 | echo -e $ECHO_LOG 264 | 265 | if [ $# -ge 1 ]; then 266 | for ARG in "$@" 267 | do 268 | if [ $ARG == "-h" ]; then 269 | showUsage 270 | exit 1 271 | elif [ $ARG == "-fullscreen" ]; then 272 | if [ ! $SCREEN_SETUP -eq 0 ]; then 273 | continue 274 | fi 275 | echo "[+] Going to fullscreen! Output blocked!" 276 | TOPXY="0,0" 277 | INRES=$(xwininfo -root | awk '/geometry/ {print $2}'i | sed -e 's/\+[0-9]//g') 278 | LOGLEVEL_ARG="-loglevel 0" 279 | SUPPRESS_OUTPUT=true 280 | SCREEN_SETUP=1 281 | elif [ $ARG == "-window" ]; then 282 | if [ $SCREEN_SETUP -eq 1 ]; then 283 | continue 284 | fi 285 | echo "[+] Click, with the mouse, on the Window that you want to Stream" 286 | rm -f twitch_tmp 2> /dev/null 287 | xwininfo -stats >> twitch_tmp 288 | TOPXY=$(cat twitch_tmp | awk 'FNR == 8 {print $4}')","$(cat twitch_tmp | awk 'FNR == 9 {print $4}') 289 | INRES=$(cat twitch_tmp | awk 'FNR == 12 {print $2}')"x"$(cat twitch_tmp | awk 'FNR == 13 {print $2}') 290 | rm -f twitch_tmp 2> /dev/null 291 | SCREEN_SETUP=2 292 | elif [ $ARG == "-save" ]; then 293 | if [ ! $STREAM_SAVE -eq 0 ]; then 294 | continue 295 | fi 296 | echo "[+] Saving the video into $FILE_VIDEO instead of stream to Twitch.tv" 297 | STREAM_SAVE=1 298 | elif [ $ARG == "-quiet" ]; then 299 | if [ $SUPPRESS_OUTPUT = true ]; then 300 | continue 301 | fi 302 | echo "[+] Quiet Mode" 303 | LOGLEVEL_ARG="-loglevel 0" 304 | SUPPRESS_OUTPUT=true 305 | else 306 | echo "[+] Unknown param:" $ARG 307 | showUsage 308 | exit 1 309 | fi 310 | done 311 | elif [ $# -eq 0 ]; then 312 | doDefaults 313 | SCREEN_SETUP=1; 314 | else 315 | # You should never get here.. but who knows.. 316 | echo "[+] There are some unknown params, please check what you wrote!" 317 | echo "[+] Params: " $@ 318 | showUsage 319 | exit 1 320 | fi 321 | echo " " 322 | # Setup 323 | echo "Please setup the Audio Output to sink null (something like 'pavucontrol')" 324 | # if you see errors here, please report on github 325 | loadModule 326 | 327 | # Disable trap 328 | trap - SIGHUP SIGINT SIGTERM 329 | # Checks if the screen got a setup 330 | if [ $SCREEN_SETUP -eq 0 ]; then 331 | # if not then, get the defaults 332 | doDefaults 333 | fi 334 | # Checks if the webcam is loaded 335 | if [ $STREAM_SAVE -eq 1 ]; then 336 | checkFileExists 337 | if [ -c $WEBCAM ]; then 338 | saveStreamWebcam 339 | else 340 | saveStreamNoWebcam 341 | fi 342 | else 343 | if [ -c $WEBCAM ]; then 344 | streamWebcam 345 | else 346 | streamNoWebcam 347 | fi 348 | fi 349 | # Checks if any error returned 350 | if [ $APP_RETURN -eq 1 ]; then 351 | if [ $SUPPRESS_OUTPUT = true ]; then 352 | echo "[+] Something went wrong. check the log without FULLSCREEN or SUPPRESS_OUTPUT options" 353 | fi 354 | fi 355 | echo " " 356 | 357 | # Closing.. 358 | unloadModule 359 | 360 | -------------------------------------------------------------------------------- /twitch_ffmpeg.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | # Copyright (c) 2013-2014, Giovanni Dante Grazioli (deroad) 4 | 5 | # ================================================ OPTIONS ===================================================== 6 | # Add the FFMPEG ABSOLUTE PATH "/path/to/ffmpeg" 7 | FFMPEG_PATH="ffmpeg" 8 | 9 | # Streaming Options 10 | OUTRES="1280x720" # Twitch Output Resolution 11 | FPS="24" # Frame per Seconds (Suggested 24, 25, 30 or 60) 12 | THREADS="4" # Change this if you have a good CPU (Suggested 4 threads, Max 6 threads) 13 | QUALITY="ultrafast" # ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow, placebo 14 | CBR="1000k" # Constant bitrate (CBR) Increase this to get a better pixel quality (1000k - 3000k for twitch) 15 | 16 | # Webcam Options 17 | WEBCAM="/dev/video1" # WebCam device 18 | WEBCAM_WH="320:240" # WebCam Width end Height 19 | WEBCAM_XY="" # WebCam Position (in pixel) example: "10:10", if "" (empty) then it will set the standard position 20 | 21 | # File to save if you do not want to stream 22 | FILE_VIDEO="my.flv" # File name 23 | 24 | # STREAM KEY 25 | # You can find YOUR key here: http://www.twitch.tv/broadcast/ (Show Key button) 26 | # Save your key inside the twitch_key file 27 | # Or make a global file named ".twitch_key" in your home directory (~/.twitch_key) 28 | 29 | # Twitch Server list http://bashtech.net/twitch/ingest.php 30 | SERVER="live-fra" # EU server 31 | 32 | # These will be used only if the -coords option is called. 33 | SET_XY="0,0" # Position of the Window on the screen (X,Y) 34 | SET_INRES="0x0" # Window size (WxH) 35 | 36 | # Change this to 'true' if you want to go always on FULLSCREEN, this will disable the output. 37 | ALWAYS_FULLSCREEN=false 38 | 39 | # Change this to 'true' if you want to hide your STREAM_KEY, for security purpose (this will disable most of the output). 40 | # This will not affect the ALWAYS_FULLSCREEN option. ALWAYS_FULLSCREEN will always disable the output. 41 | SUPPRESS_OUTPUT=false 42 | 43 | # Twitch says it MUST have a 44100 rate, please do not change it unless you know what you are doing. 44 | AUDIO_RATE="44100" 45 | # Twitch says it MUST be 2, please do not change it unless you know what you are doing. 46 | KEY_FRAME="2" 47 | 48 | # ============================================== END OPTIONS =================================================== 49 | # The following values are changed automatically, so do not change them 50 | TOPXY="0,0" # Position of the Window (You don't need to change this) 51 | INRES="0x0" # Game Resolution (You don't need to change this) 52 | LOGLEVEL_ARG="" # LogLevel, for security purpose (You don't need to change this) 53 | SCREEN_SETUP=0 # Do not change this. it's used for the args. 54 | STREAM_SAVE=0 # Do not change this. it's used for the args. 55 | # ================================================= CHECKS ===================================================== 56 | # To see the output. 57 | ECHO_LOG="" 58 | 59 | # checks to avoid a false "true" where it checks for the webcam 60 | if [ -z "$WEBCAM" ]; then 61 | ECHO_LOG=$ECHO_LOG"\nYour Webcam has been disabled because there isn't a WEBCAM in the options" 62 | WEBCAM="/dev/no-webcam" 63 | elif [ -z "$WEBCAM_WH" ]; then 64 | # checks to avoid a fail on loading the Webcam 65 | ECHO_LOG=$ECHO_LOG"\nYour Webcam has been disabled because there isn't a WEBCAM_WH in the options" 66 | WEBCAM="/dev/no-webcam" 67 | fi 68 | # Find stream key 69 | if [ -f ~/.twitch_key ]; then 70 | ECHO_LOG=$ECHO_LOG"\nUsing global twitch key located in home directory" 71 | STREAM_KEY=$(cat ~/.twitch_key) 72 | else 73 | if [ -f ./twitch_key ]; then 74 | ECHO_LOG=$ECHO_LOG"\nUsing twitch key located in current running directory" 75 | STREAM_KEY=$(cat ./twitch_key) 76 | else 77 | echo "Could not locate ~/.twitch_key or twitch_key" 78 | exit 1 79 | fi 80 | fi 81 | 82 | 83 | # Find script name 84 | SCRIPT_NAME=${0##*/} 85 | 86 | # Suppress output for security purpose 87 | if [ $SUPPRESS_OUTPUT = true ]; then 88 | ECHO_LOG=$ECHO_LOG"\nOutput blocked!" 89 | LOGLEVEL_ARG="-loglevel 0" 90 | fi 91 | 92 | # checks to avoid fails 93 | if [ -z "$SERVER" ]; then 94 | SERVER="live" 95 | fi 96 | if [ -z "$OUTRES" ]; then 97 | OUTRES="1280x720" 98 | fi 99 | if [ -z "$FPS" ]; then 100 | FPS="30" 101 | GOP="60" 102 | GOPMIN="30" 103 | else 104 | GOP=$(($FPS*2)) 105 | GOPMIN=$FPS 106 | fi 107 | if [ -z "$THREADS" ]; then 108 | THREADS="4" 109 | fi 110 | if [ -z "$QUALITY" ]; then 111 | QUALITY="fast" 112 | fi 113 | if [ -z "$CBR" ]; then 114 | CBR="1000k" 115 | fi 116 | if [ -z "$ALWAYS_FULLSCREEN" ]; then 117 | ALWAYS_FULLSCREEN=false 118 | fi 119 | if [ -z "$AUDIO_RATE" ]; then 120 | AUDIO_RATE="44100" 121 | fi 122 | if [ -z "$LOGLEVEL_ARG" ]; then 123 | LOGLEVEL_ARG="" 124 | fi 125 | if [ -z "$SUPPRESS_OUTPUT" ]; then 126 | SUPPRESS_OUTPUT=false 127 | fi 128 | if [ -z "$FILE_VIDEO" ]; then 129 | FILE_VIDEO="video.flv" 130 | fi 131 | if [ ! $SCREEN_SETUP -eq 0 ]; then 132 | SCREEN_SETUP=0 133 | fi 134 | if [ ! $STREAM_SAVE -eq 0 ]; then 135 | STREAM_SAVE=0 136 | fi 137 | if [ -z "$STREAM_KEY" ]; then 138 | ECHO_LOG=$ECHO_LOG"\nSTREAM_KEY not set or there is a problem with it... Aborting." 139 | ECHO_LOG=$ECHO_LOG"\nCheck if the path to the file or the streaming key is correctly set." 140 | exit 1 141 | fi 142 | 143 | # ================================================= CODE ======================================================= 144 | # DO NOT CHANGE THE CODE! 145 | MODULE_LOAD1="" 146 | MODULE_LOAD2="" 147 | APP_RETURN="" 148 | 149 | checkFileExists(){ 150 | if [ -f $FILE_VIDEO ]; then 151 | i=0 152 | TMP="OLD_"$i"_"$FILE_VIDEO 153 | echo "$FILE_VIDEO already exists! finding a new name for the old file" 154 | while [ -f $TMP ] 155 | do 156 | i=$((i+1)) 157 | TMP="OLD_"$i"_"$FILE_VIDEO 158 | echo "$TMP already exists" 159 | done 160 | echo "The old stream $FILE_VIDEO has been renamed as $TMP" 161 | echo "The new stream will be saved into $FILE_VIDEO" 162 | mv $FILE_VIDEO $TMP 163 | fi 164 | } 165 | 166 | streamWebcam(){ 167 | echo "Webcam found!!" 168 | echo "You should be online! Check on http://twitch.tv/ (Press CTRL+C to stop)" 169 | echo " " 170 | if [ -z "$WEBCAM_XY" ]; then 171 | # checks to avoid a fail on loading the Webcam 172 | #standard position is: main_w - overlay_w - 10:10 173 | WEBCAM_XY="$(($(echo $INRES | awk -F"x" '{ print $1 }') - $(echo $WEBCAM_WH | awk -F":" '{ print $1 }') - 10)):10" 174 | echo "There isn't a WEBCAM_XY in the options, i'll generate the standard one ($WEBCAM_XY)" 175 | fi 176 | $FFMPEG_PATH -f x11grab -s $INRES -framerate "$FPS" -i :0.0+$TOPXY -f alsa -i pulse -f flv -ac 2 -ar $AUDIO_RATE -vcodec libx264 -filter:v fps="$FPS" -force_key_frames "expr:gte(t,n_forced*$KEY_FRAME)" -g $GOP -keyint_min $GOPMIN -b $CBR -minrate $CBR -maxrate $CBR -pix_fmt yuv420p -s $OUTRES -preset $QUALITY -tune film -acodec libmp3lame -threads $THREADS -vf "movie=$WEBCAM:f=video4linux2, scale=$WEBCAM_WH , setpts=PTS-STARTPTS [WebCam]; [in] setpts=PTS-STARTPTS [Screen]; [Screen][WebCam] overlay=$WEBCAM_XY [out]" -strict normal -bufsize $CBR $LOGLEVEL_ARG "rtmp://$SERVER.twitch.tv/app/$STREAM_KEY" 177 | APP_RETURN=$? 178 | } 179 | 180 | streamNoWebcam(){ 181 | echo "Webcam NOT found!! ("$WEBCAM")" 182 | echo "You should be online! Check on http://twitch.tv/ (Press CTRL+C to stop)" 183 | echo " " 184 | $FFMPEG_PATH -f x11grab -s $INRES -framerate "$FPS" -i :0.0+$TOPXY -f alsa -i pulse -f flv -ac 2 -ar $AUDIO_RATE -vcodec libx264 -filter:v fps="$FPS" -force_key_frames "expr:gte(t,n_forced*$KEY_FRAME)" -g $GOP -keyint_min $GOPMIN -b:v $CBR -minrate $CBR -maxrate $CBR -pix_fmt yuv420p -s $OUTRES -preset $QUALITY -tune film -acodec libmp3lame -threads $THREADS -strict normal -bufsize $CBR $LOGLEVEL_ARG "rtmp://$SERVER.twitch.tv/app/$STREAM_KEY" 185 | APP_RETURN=$? 186 | } 187 | 188 | saveStreamWebcam(){ 189 | echo "Webcam found!!" 190 | echo "You should be online! Check on http://twitch.tv/ (Press CTRL+C to stop)" 191 | echo " " 192 | if [ -z "$WEBCAM_XY" ]; then 193 | # checks to avoid a fail on loading the Webcam 194 | #standard position is: main_w - overlay_w - 10:10 195 | WEBCAM_XY="$(($(echo $INRES | awk -F"x" '{ print $1 }') - $(echo $WEBCAM_WH | awk -F":" '{ print $1 }') - 10)):10" 196 | echo "There isn't a WEBCAM_XY in the options, i'll generate the standard one ($WEBCAM_XY)" 197 | fi 198 | $FFMPEG_PATH -f x11grab -s $INRES -framerate "$FPS" -i :0.0+$TOPXY -f alsa -i pulse -f flv -ac 2 -ar $AUDIO_RATE -vcodec libx264 -force_key_frames "expr:gte(t,n_forced*$KEY_FRAME)" -g $GOP -keyint_min $GOPMIN -b $CBR -minrate $CBR -maxrate $CBR -pix_fmt yuv420p -s $OUTRES -preset $QUALITY -tune film -acodec libmp3lame -threads $THREADS -vf "movie=$WEBCAM:f=video4linux2, scale=$WEBCAM_WH , setpts=PTS-STARTPTS [WebCam]; [in] setpts=PTS-STARTPTS [Screen]; [Screen][WebCam] overlay=$WEBCAM_XY [out]" -strict normal -bufsize $CBR $LOGLEVEL_ARG $FILE_VIDEO 199 | APP_RETURN=$? 200 | } 201 | 202 | saveStreamNoWebcam(){ 203 | echo "Webcam NOT found!! ("$WEBCAM")" 204 | echo "You should be online! Check on http://twitch.tv/ (Press CTRL+C to stop)" 205 | echo " " 206 | $FFMPEG_PATH -f x11grab -s $INRES -framerate "$FPS" -i :0.0+$TOPXY -f alsa -i pulse -f flv -ac 2 -ar $AUDIO_RATE -vcodec libx264 -filter:v fps="$FPS" -force_key_frames "expr:gte(t,n_forced*$KEY_FRAME)" -g $GOP -keyint_min $GOPMIN -b:v $CBR -minrate $CBR -maxrate $CBR -pix_fmt yuv420p -s $OUTRES -preset $QUALITY -tune film -acodec libmp3lame -threads $THREADS -strict normal -bufsize $CBR $LOGLEVEL_ARG $FILE_VIDEO 207 | APP_RETURN=$? 208 | } 209 | 210 | doDefaults(){ 211 | if [ $ALWAYS_FULLSCREEN = true ]; then 212 | echo "[+] ALWAYS_FULLSCREEN is ON. Going to fullscreen!" 213 | echo "[+] Output blocked!" 214 | TOPXY="0,0" 215 | INRES=$(xwininfo -root | awk '/geometry/ {print $2}'i | sed -e 's/\+[0-9]//g') 216 | LOGLEVEL_ARG="-loglevel 0" 217 | SUPPRESS_OUTPUT=true 218 | else 219 | echo "[+] Click, with the mouse, on the Window that you want to Stream" 220 | rm -f twitch_tmp 2> /dev/null 221 | xwininfo -stats >> twitch_tmp 222 | TOPXY=$(cat twitch_tmp | awk 'FNR == 8 {print $4}')","$(cat twitch_tmp | awk 'FNR == 9 {print $4}') 223 | INRES=$(cat twitch_tmp | awk 'FNR == 12 {print $2}')"x"$(cat twitch_tmp | awk 'FNR == 13 {print $2}') 224 | rm -f twitch_tmp 2> /dev/null 225 | echo " " 226 | fi 227 | } 228 | 229 | loadModule(){ 230 | MODULE_LOAD1=$(pactl load-module module-null-sink sink_name=GameAudio sink_properties=device.description="GameAudio") # For Game Audio 231 | MODULE_LOAD2=$(pactl load-module module-null-sink sink_name=MicAudio sink_properties=device.description="MicAudio") # For Mic Audio 232 | pactl load-module module-device-manager >> /dev/null 233 | pactl load-module module-loopback sink=GameAudio >> /dev/null 234 | pactl load-module module-loopback sink=MicAudio >> /dev/null 235 | } 236 | 237 | unloadModule(){ 238 | echo "Stopping Audio (Don't worry if you see errors here)" 239 | pactl unload-module $MODULE_LOAD1 240 | pactl unload-module $MODULE_LOAD2 241 | pactl unload-module module-device-manager 242 | pactl unload-module module-null-sink 243 | pactl unload-module module-loopback 244 | echo "Exit!" 245 | } 246 | 247 | showUsage(){ 248 | echo "usage:" 249 | echo " "$SCRIPT_NAME" [options]" 250 | echo " -h | show usage screen" 251 | echo " -fullscreen | enable the fullscreen" 252 | echo " and disable the output" 253 | echo " -window | enable the window mode" 254 | echo " -coords | set the screen resolution" 255 | echo " by using SET_XY SET_INRES" 256 | echo " defined inside the script" 257 | echo " -save | save the video to a file" 258 | echo " instead of streaming it" 259 | echo " -quiet | disables most of the outputs" 260 | } 261 | 262 | 263 | echo " " 264 | echo "Twitch Streamer for Linux ("$SCRIPT_NAME")" 265 | echo "Copyright (c) 2013 - 2014, Giovanni Dante Grazioli (deroad)" 266 | # To be sure to unload everything 267 | trap "unloadModule; exit" SIGHUP SIGINT SIGTERM 268 | echo -e $ECHO_LOG 269 | if [ $# -ge 1 ]; then 270 | for ARG in "$@" 271 | do 272 | if [ $ARG == "-h" ]; then 273 | showUsage 274 | exit 1 275 | elif [ $ARG == "-fullscreen" ]; then 276 | if [ ! $SCREEN_SETUP -eq 0 ]; then 277 | continue 278 | fi 279 | echo "[+] Going to fullscreen! Output blocked!" 280 | TOPXY="0,0" 281 | INRES=$(xwininfo -root | awk '/geometry/ {print $2}'i | sed -e 's/\+[0-9]//g') 282 | LOGLEVEL_ARG="-loglevel 0" 283 | SUPPRESS_OUTPUT=true 284 | SCREEN_SETUP=1 285 | elif [ $ARG == "-window" ]; then 286 | if [ $SCREEN_SETUP -eq 1 ]; then 287 | continue 288 | fi 289 | echo "[+] Click, with the mouse, on the Window that you want to Stream" 290 | rm -f twitch_tmp 2> /dev/null 291 | xwininfo -stats >> twitch_tmp 292 | TOPXY=$(cat twitch_tmp | awk 'FNR == 8 {print $4}')","$(cat twitch_tmp | awk 'FNR == 9 {print $4}') 293 | INRES=$(cat twitch_tmp | awk 'FNR == 12 {print $2}')"x"$(cat twitch_tmp | awk 'FNR == 13 {print $2}') 294 | rm -f twitch_tmp 2> /dev/null 295 | SCREEN_SETUP=2 296 | elif [ $ARG == "-coords" ]; then 297 | if [ $SCREEN_SETUP -eq 1 ]; then 298 | continue 299 | elif [ "$SET_INRES" != "0x0" ]; then 300 | TOPXY=$SET_XY 301 | INRES=$SET_INRES 302 | echo "[+] Using pre-defined screen coords (X,Y = $TOPXY) (WxH = $INRES)." 303 | SCREEN_SETUP=2 304 | else 305 | echo "[+] Could not use pre-defined screen coords, because 'SET_INRES' equals '0x0'." 306 | fi 307 | elif [ $ARG == "-save" ]; then 308 | if [ ! $STREAM_SAVE -eq 0 ]; then 309 | continue 310 | fi 311 | echo "[+] Saving the video into $FILE_VIDEO instead of stream to Twitch.tv" 312 | STREAM_SAVE=1 313 | elif [ $ARG == "-quiet" ]; then 314 | if [ $SUPPRESS_OUTPUT = true ]; then 315 | continue 316 | fi 317 | echo "[+] Quiet Mode" 318 | LOGLEVEL_ARG="-loglevel 0" 319 | SUPPRESS_OUTPUT=true 320 | else 321 | echo "[+] Unknown param:" $ARG 322 | showUsage 323 | exit 1 324 | fi 325 | done 326 | elif [ $# -eq 0 ]; then 327 | doDefaults 328 | SCREEN_SETUP=1; 329 | else 330 | # You should never get here.. but who knows.. 331 | echo "[+] There are some unknown params, please check what you wrote!" 332 | echo "[+] Params: " $@ 333 | showUsage 334 | exit 1 335 | fi 336 | echo " " 337 | # Setup 338 | echo "Please setup the Audio Output to sink null (something like 'pavucontrol')" 339 | # if you see errors here, please report on github 340 | loadModule 341 | 342 | # Disable trap 343 | trap - SIGHUP SIGINT SIGTERM 344 | # Checks if the screen got a setup 345 | if [ $SCREEN_SETUP -eq 0 ]; then 346 | # if not then, get the defaults 347 | doDefaults 348 | fi 349 | # Checks if the webcam is loaded 350 | if [ $STREAM_SAVE -eq 1 ]; then 351 | checkFileExists 352 | if [ -c $WEBCAM ]; then 353 | saveStreamWebcam 354 | else 355 | saveStreamNoWebcam 356 | fi 357 | else 358 | if [ -c $WEBCAM ]; then 359 | streamWebcam 360 | else 361 | streamNoWebcam 362 | fi 363 | fi 364 | # Checks if any error returned 365 | if [ $APP_RETURN -eq 1 ]; then 366 | if [ $SUPPRESS_OUTPUT = true ]; then 367 | echo "[+] Something went wrong. check the log without FULLSCREEN or SUPPRESS_OUTPUT options" 368 | fi 369 | fi 370 | echo " " 371 | 372 | # Closing.. 373 | unloadModule 374 | 375 | -------------------------------------------------------------------------------- /twitch_key: -------------------------------------------------------------------------------- 1 | live_xxxxxxxx_yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 2 | --------------------------------------------------------------------------------