Skip to content

Instantly share code, notes, and snippets.

@dyindude
Forked from joshdvir/twitch_proxy.md
Created June 11, 2018 19:38
Show Gist options
  • Save dyindude/a412cfa38001503de76ffa7962086650 to your computer and use it in GitHub Desktop.
Save dyindude/a412cfa38001503de76ffa7962086650 to your computer and use it in GitHub Desktop.

Livestreaming via PS4 / Xbox to a local RTMP server

Live stream from your PS4 / Xbox to a local computer running an RTMP server by intercepting the twitch.tv stream.

Requirements

  • DD-WRT enabled Router (or router with iptables compatibility)
  • nix Envirment
  • nginx with the nginx-rtmp-module

Find the Twitch Ingest IP range

In your console run

dig live.twitch.tv

Result should look like:

; <<>> DiG 9.9.7-P3 <<>> live.twitch.tv
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 44228
;; flags: qr rd ra; QUERY: 1, ANSWER: 17, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;live.twitch.tv.			IN	A

;; ANSWER SECTION:
live.twitch.tv.		60	IN	CNAME	live-sfo.twitch.tv.
live-sfo.twitch.tv.	60	IN	A	192.108.239.86
live-sfo.twitch.tv.	60	IN	A	23.160.0.82
live-sfo.twitch.tv.	60	IN	A	192.108.239.80
live-sfo.twitch.tv.	60	IN	A	192.108.239.84
live-sfo.twitch.tv.	60	IN	A	192.108.239.82
live-sfo.twitch.tv.	60	IN	A	23.160.0.81
live-sfo.twitch.tv.	60	IN	A	192.108.239.81
live-sfo.twitch.tv.	60	IN	A	23.160.0.90
live-sfo.twitch.tv.	60	IN	A	192.108.239.83
live-sfo.twitch.tv.	60	IN	A	23.160.0.85
live-sfo.twitch.tv.	60	IN	A	23.160.0.86
live-sfo.twitch.tv.	60	IN	A	23.160.0.84
live-sfo.twitch.tv.	60	IN	A	23.160.0.83
live-sfo.twitch.tv.	60	IN	A	23.160.0.80
live-sfo.twitch.tv.	60	IN	A	192.108.239.85
live-sfo.twitch.tv.	60	IN	A	23.160.0.89

;; Query time: 45 msec
;; SERVER: 172.16.0.1#53(172.16.0.1)
;; WHEN: Fri Nov 24 22:57:47 GMT 2017
;; MSG SIZE  rcvd: 322

Look at the IP addresses and decide what is the IP range, in this case:

23.160.0.0/16
192.108.0.0/16

IPTables

To capture the outgoing RTMP stream, I'm using NAT redirection on the Twitch.tv IP range for port 1935. This will cause normal twitch streaming to break.

iptables -t nat -A PREROUTING -d {IP range from the section above}/16 -p tcp --dport 1935 -j DNAT --to-destination <local_ip>:1935
iptables -t nat -A POSTROUTING -j MASQUERADE

List PREROUTING & POSTROUTING rules

iptables -t nat -v -L PREROUTING -n --line-number # PREROUTING only
iptables -t nat -v -L POSTROUTING -n --line-number # POSTROUTING only

iptables -t nat -v -L -n --line-number # PREROUTING & POSTROUTING both

Delete rules

iptables -t nat -D POSTROUTING {number-here} # Delete POSTROUTING based on rule number

iptables -t nat -D PREROUTING {number-here} # Delete PREROUTING based on rule number

You can ssh into your DD-WRT enabled router and add these rules, any future requests will be redirected.

nginx-rtmp-module

Here is the config for the rtmp module and the bash script I use to convert from FLV to MP4.

rtmp {
	server {
		listen 1935;
		application app {
			live on;
			recorder storage {
				record all;
				record_path /home/user/gameplay;
				record_unique on;
				exec_record_done /home/user/gameplay/.encode.sh $path $basename;
			}
		}
	}
}
#!/bin/bash
/usr/local/bin/ffmpeg -i $1 -vcodec copy -acodec copy -threads 1 /Users/phelps/gameplay/${date +"%m-%d-%y %r"}.mp4
rm $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment