In this guide I’ll show you how to stream live HDV/DV video to your iphone using a linux box (Ubuntu 9.10) with firewire input running vlc/ffmpeg and a Imac with OSX 10.6.2 running mediastreamsegmenter and apache2.
Start out with the iPhone streaming media overview. Without understanding this document you’ll have a hard time getting things working.
First things first, you need to have a working Ubuntu 9.10 machine. I’m using a small footprint 2.4Ghz Core2Duo machine with PCI firewire 400 card in it. For video input I’m using a Canon HV30 set to HDV mode (1080i/60) connected via firewire.
Next you need to follow the instructions on this page (steps 0-5) to get a working ffmpeg with x264 and aac encoding. Without this working you’re not going anywhere….sorry. If you’re trying this on a different Ubuntu installation follow the other links to get a working ffmpeg setup.
Then install vlc using “sudo apt-get install vlc“. I used vlc as my encoder frontend as I understand it better than ffmpeg. You can use just straight ffmpeg as well if you can figure out how to get it to encode the live HDV stream over firewire.
You’ll also need dvgrab utility. Install it using “sudo apt-get install dvgrab“.
Now we want to make sure the internal firewire module is working so type this command and see if you get a vlc window with the camera output in it (make sure you turn the camera ON and hook it up first).
sudo dvgrab -f hdv -noavc -nostop -|vlc -
You have to use sudo under ubuntu to get proper access to the firewire device. The above command runs dvgrab with hdv format and makes sure that 1394 AV/Device control is turned off (this way you can be in Camera mode and get a live feed). The nostop switch prevents dvgrab from sending stop commands to the camera everytime you stop it via Ctrl-C, which I though was a good thing. The last dash forces dvgrab to output to stdout, which we’ll then pipe into vlc (the dash for vlc tells it to use stdin as input).
Next we need to create a media stream out of our linux box and ship it over UDP to the Imac. The vlc command below gets the job done. Remember you’re sudo’ing and need to provide the password after you enter the command.
sudo dvgrab -f hdv -noavc -nostop -|vlc - --sout '#transcode{vcodec=h264,vb=256,venc=x264{aud,profile=baseline,level=30,keyint=30,bframes=0,ref=1,nocabac},acodec=mp4a,ab=64,scale=0.25,deinterlace,width=320,height=240}:duplicate{dst=std{access=udp,mux=ts,dst=192.168.1.97:1234}}'
The IP address toward the end of the command is the IP of the Imac machine receiving the stream. Port 1234 is arbitrary. The stream is comprised of h.264 video @ 256K and AAC audio @ 64K. Those elementary streams are then packaged in mpeg2 transport stream before being shipped to the Imac. This is the standard way of doing HTML5 video (from what I understand).
So now we can go over to the mac and see if we receive the video stream. For that just run VLC for OSX and open UDP network port on port 1234 (udp://). If things are working nicely you should see a 320×240 video from you HDV camera on the Imac.
Now that we have the video on the mac, we need to use the “mediastreamsegmenter” command line tool to create HTML5 video stream out of it. mediastreamsegmenter listens on a UDP port for incoming transport stream chops it (by default) into 10 sec. “mini” transport stream files and writes these mini files to wherever you tell it. This location is important since it needs to be accessible to your webserver. Remember, at the end of the chain (day), the webserver is doing all the heavy lifting of delivering the mini transport stream files to your iphone. mediastreamsegmenter also produces a file of type .m3u8
which is basically a live updated playlist.
Something you might not know is that apple ships standard OSX with apache builtin. All you have to do is use the following command to get it started.
apachectl start
Now point your browser on the mac to localhost and see if it loads a page. Now that apache is working we need to modify it so it knows how to deal with .ts and .m3u8 files. This involves adding a couple of lines to /etc/apache2/httpd.conf
AddType application/x-mpegURL .m3u8
AddType video/MP2T .ts
and /etc/apache2/mime.types
.m3u8 application/x-mpegURL
.ts video/MP2T
Next we need to restart apache
apachectl restart
By default OSX apache is setup to load it’s documents from /Library/WebServer/Documents, so I created a directory called “stream” to contain the media stuff (.ts files and .m3u8 file) and put the following into the index.html file in /Library/WebServer/Documents.
<html>
<head>
<title>Video Test</title>
<meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
</head>
<body style="background-color:#FFFFFF; ">
<center>
<video width='320' height='240' src="prog_index.m3u8" controls autoplay> </video>
</center>
</body>
</html>
Now that we’ve got all the prep done on the mac, we issue the following command from terminal window to get the mediastreamsegmenter going (details can be found by using man mediastreamsegmenter).
mediastreamsegmenter -b http://192.168.1.97/stream -f /Library/WebServer/Documents/stream 192.168.1.64:1234
Here -b specifies the base of the URL that will be encoded into the .m3u8 file (this is the IP address of your Imac, stream is the folder in /Library/WebServer/Documents/ where the mini .ts files and the .m3u8 file are). The -f switch specifies the output directory for the mini .ts files and the .m3u8 file. and the last IP address:port is from your Linux box.
Now you should be able to open up your browser on your iphone/ipod touch and punch in http://192.168.1.97 (assuming the Imac is reachable from your phone) and see the streaming video (You might have to turn on “Plugins” feature under settings/safari on your device. Mine was turned off and drove me crazy until I figured it out). If Plugins is turned off, the index.html page will load, but no video.
Hopefully there is enough meat here to get you guys started……btw. I hear the following command (or variations of) can be used on linux side (instead of vlc). I haven’t tried it and can’t confirm if it works.
ffmpeg -i <in file> -f mpegts -acodec libmp3lame -ar 48000 -ab 64k -s 320×240 -vcodec libx264 -b 96k -flags +loop -cmp +chroma -partitions +parti4×4+partp8×8+partb8×8 -subq 5 -trellis 1 -refs 1 -coder 0 -me_range 16 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt 200k -maxrate 96k -bufsize 96k -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 30 -aspect 320:240 -g 30 -async 2 <output file>
Some excellent information can be found on Carson McDonald’s blog:
One response to “How to stream live HDV/DV to iphone…..”
Great post! I just got my iPhone and am still trying to figure out how the darn thing works :) thanks for posting!