How To: Stream Mac Desktop to Apple TV

Update 2: I've been doing some research about this not working on Mac OSX Lion. Check out my latest post on this topic.


Update: It seems like this wont work if you have Mac OS X Lion. I will investigate this issue as soon I can.


First of all, you will need VLC or libvlc installed on your computer. This is used to capture your screen. Second, you should not be afraid to play around in the Terminal.app. The commands I will give you will not destroy your computer. Hopefully. Third, an Apache server. I'm using MAMP. Last but not least, a Mac and a Apple TV (of course).

The first thing we need to do is installing MAMP or an Apache server. I will not give acrash course how to install MAMP today. Just follow the installations guide here.

When the installation is finished, we must add some MIME-types to the Apache config file. Use Finder and browse to /Applications/MAMP/conf/apache/mime.types and add the following two lines at the bottom of the file. This is used to tell the server it can handle the streaming media types we will use.

application/x-mpegURL m3u8
video/MP2T ts

Open Finder again and browse to your http-root folder in your web server. It's /Applications/MAMP/htdocs. Create a sub folder, I named my /stream because... this is where the stream will be located.

Fire up your MAMP server by launching the Mamp.app!

Tip for lazy peoples: Open preferences and navigate to Ports. Change the Apache Port from 8080 to 80. By doing this you don't have to write e.g: http://localhost:8080/ to access your web server, just type http://localhost/. Press OK and we're done with MAMP.

Now download VLC at videolan.org and drag it to your Applications folder.

It's time for some magic Start the Terminal. Copy the command below and paste in Terminal, close your eyes like the computer is going to explode... and press Enter.

/Applications/VLC.app/Contents/MacOS/VLC -vv screen:// :screen-fps=30 :screen-caching=100 --intf=rc '--sout=#transcode{vcodec=h264,vb=1024,width=1280,height=720}:duplicate{dst=std{access=file,mux=ts,dst=-}}' | mediastreamsegmenter -f /Applications/MAMP/htdocs/stream -s 3 -t 2 -D

The Terminal will write a lot of messages, don't be afraid. It's supposed to happen. It stops for awhile and it will start to write something like this every second:

Jul 20 2011 22:28:37.930: start-code non-alignment - lost 98
Jul 20 2011 22:28:37.930: start-code non-alignment - lost 98
Jul 20 2011 22:28:37.931: video pid set at 44
Jul 20 2011 22:28:38.726: Finalized /Applications/MAMP/htdocs/stream/fileSequence0.ts
Jul 20 2011 22:28:38.727: file bitrate 66768 is new max
Jul 20 2011 22:28:38.727: received 153506 written bytes 17108 skipped 3008 pat/pmt skipped 3008
Jul 20 2011 22:28:40.759: Finalized /Applications/MAMP/htdocs/stream/fileSequence1.ts
Jul 20 2011 22:28:40.759: file bitrate 362526 is new max
Jul 20 2011 22:28:40.759: received 96564 written bytes 92496 skipped 3008 pat/pmt skipped 3008
Jul 20 2011 22:28:42.671: Finalized /Applications/MAMP/htdocs/stream/fileSequence2.ts
Jul 20 2011 22:28:42.671: wrote initial index file

Which means the stream is live! For best result, lower your resolutions on your desktop to 1280 by 720. The snippet allows you to stream high resolutions as well, if you want to customize and stream another resolution, just change width=,height= to what ever you want. To terminate the stream just press Ctrl+C or exit the Terminal.

But how do we get this to the Apple TV?! We just need to download one more app. It's AirFlick by Erica Sadun. Open the application and you will see an input field with the label "Movie URL or File Path". Now get your local IP (something like: 192.168.x.x) by either writing ifconfig in the Terminal or navigate to your Network Settings in your System Preferences. Copy your IP and put the following line to the AirFlick input field and press the big play button. Note: If you use a local ip, both your Apple TV and your computer need to be on the same network.

http://{insert-your-ipaddress-here}/stream/prog_index.m3u8

Just wait and see how your Apple TV will play the stream. There will be a delay of about 5-7 seconds.

One more thing, this is optional.

If you want to be able to play the stream on a iOS device as well create a index.html file in your htdocs/steam folder and paste the following code. This will embed a video player with the new HTML5 video tag.

<video width="100%" height="100%" controls>
    <source src="prog_index.m3u8" />
</video>

Open up Safari on your iOS device and navigate to the url below. This also works over 3G, but watch out. This eats up your internet limit.

http://{insert-your-ipaddress-here}/stream/

I really hope this works for you. I will gladly take any questions! Thanks for reading.