How do I record video to a local disk in AIR?

Jim OHalloran picture Jim OHalloran · Jan 21, 2010 · Viewed 9k times · Source

I'm trying to record a webcam's video and audio to a FLV file stored on the users local hard disk. I have a version of this code working which uses NetConnection and NetStream to stream the video over a network to a FMS (Red5) server, but I'd like to be able to store the video locally for low bandwidth/flaky network situations. I'm using FLex 3.2 and AIR 1.5, so I don't believe there should be any sandbox restrictions which prevent this from occurring.

Things I've seen:

  • FileStream - Allows reading.writing local files but no .attachCamera and .attachAudio methids for creating a FLV.
  • flvrecorder - Produces screen grabs from the web cam and creates it's own flv file. Doesn't support Audio. License prohibits commercial use.
  • SimpleFLVWriter.as - Similar to flvrecorder without the wierd license. Doesn't support audio.
  • This stackoverflow post - Which demonstrates the playback of a video from local disk using a NetConnection/NetStream.

Given that I have a version already which uses NetStream to stream to the server I thought the last was most promising and went ahead and put together this demo application. The code compiles and runs without errors, but I don't have a FLV file on disk which the stop button is clicked. -

<mx:Script>
    <![CDATA[

        private var _diskStream:NetStream;
        private var _diskConn:NetConnection;
        private var _camera:Camera;
        private var _mic:Microphone; 

        public function cmdStart_Click():void {
            _camera = Camera.getCamera();
            _camera.setQuality(144000, 85);
            _camera.setMode(320, 240, 15);
            _camera.setKeyFrameInterval(60);

            _mic = Microphone.getMicrophone();

            videoDisplay.attachCamera(_camera);

            _diskConn = new NetConnection();
            _diskConn.connect(null);

            _diskStream = new NetStream(_diskConn);
            _diskStream.client = this;
            _diskStream.attachCamera(_camera);
            _diskStream.attachAudio(_mic);
            _diskStream.publish("file://c:/test.flv", "record");

        }

        public function cmdStop_Click() {
            _diskStream.close();
            videoDisplay.close();
        }

    ]]>
</mx:Script>    
    <mx:VideoDisplay x="10" y="10" width="320" height="240" id="videoDisplay" />
    <mx:Button x="10" y="258" label="Start" click="cmdStart_Click()" id="cmdStart"/>
    <mx:Button x="73" y="258" label="Stop" id="cmdStop" click="cmdStop_Click()"/>

</mx:WindowedApplication>

It seems to me that there's either something wrong with the above code which is preventing it from working, or NetStream just can't be abused in this wany to record video.

What I'd like to know is, a) What (if anything) is wrong with the code above? b) If NetStream doesn't support recording to disk, are there any other alternatives which capture Audio AND Video to a file on the users local hard disk?

Thanks in advance!

Answer

walv picture walv · Jan 12, 2012

My solution was to embed Red5 into AIR. Sharing with you my article

http://mydevrecords.blogspot.com/2012/01/local-recording-in-adobe-air-using-red5.html

In general, the solution is to embed free media server Red5 into AIR like an asset. So, the server will be present in AIR application folder. Then, through the NativeProcess, you can run Red5 and have its instance in memory. As result, you can have local video recording without any network issues.