Recording Audio and Video with Red5
What’s up people, these last days (and weeks) I’ve been working with Red5, and learning along the way because I had never used it beyond ‘Hello world’ type experiments, and it’s a shame I did not started to play with it earlier, because is so damn cool. Thanks a lot Red5 Team!
Anyway, I’ve seen some folks here and there asking on how to record a live webcam/mic stream using Red5, and some long ago when I searched for that I did not find a straightforward answer (AKA gimme the code :D), so for those fellas that are badass lazy as I am, and that their hopes are to find all the help (AKA gimme the code) on Google, here’s a little example that should get your ass going (if you were too lazy to try this suggestion which you should do if you want to really learn )
Ok, I’ll assume you have everything in order so we can go ahead and get our cameras and microphones:
1 2 3 | var myCam:Camera = Camera.getCamera(); var myMic:Microphone = Microphone.getMicrophone(); myMic.setLoopback(true); |
, then create our net stream and attach the live video/audio to it. Here you could add some validations to verify that you are getting your camera and mic feed as expected (e.g. not null), I will omit it however because my fingers are getting tired.
1 2 3 4 5 6 7 | // Create our net stream, by the way for the sake of brevity let's assume you have all your connection sh!t together var stream:NetStream = new NetStream(myNetConnection); // add a listener so we know when our stuff is ready for recording stream.addEventListener(NetStatusEvent.NET_STATUS, onRecStreamStatus); stream.attachCamera(myCam ); stream.attachAudio(myMic); stream.publish('myStream', 'live'); |
Now the stream status event handler, as you’ll see, when your stream has started to “stream” ha!
then you are ready to record,
1 2 3 4 5 6 7 | private function onRecStreamStatus(e:NetStatusEvent):void { if ( e.info.code == "NetStream.Publish.Start" ) { myConnection.call('startRecordingStream', null); } } |
This line myConnection.call('startRecordingStream', null); invokes the method ’startRecordingStream’ on our Red5 service which is responsible for handling our streams and recording/saving them accordingly… and it could look something like this…is Java by the way
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | //Java public void startRecordingStream(IConnection conn) { String clientId = conn.getClient().getId(); IScope scope = conn.getScope(); String streamName = clientId + "_" + String.valueOf(System.currentTimeMillis()); try { ClientBroadcastStream stream = (ClientBroadcastStream) this.getBroadcastStream(conn.getScope(), "myStream" ); stream.saveAs(streamName, false); // You could, if you wish of course, notify the user that the recording has actually started // You just have to add the recordingStarted public function on your flash application ServiceUtils.invokeOnConnection(conn, "recordingStarted", new Object[]{clientId}); } catch(Exception e) { // You could notify the user that the recording failed for some stupid reason // sometimes things does not go well :) // You just have to add the recordingStarted public function on your flash application ServiceUtils.invokeOnConnection(conn, "recordingFailed", new Object[]{clientId}); } } |
And if you want to have a button to stop recording on your flash app., just throw a button and when is clicked invokes a method on your Red5 service for stopping the recording process. Something like this:
1 2 3 | //AS3 //assuming this is on a click handler or whatever myConnection.call('stopRecordingStream', null); |
1 2 3 4 5 6 7 8 | //Java public void stopRecordingStream(IConnection conn) { String clientId = conn.getClient().getId(); ClientBroadcastStream stream = (ClientBroadcastStream) this.getBroadcastStream(conn.getScope(), "myStream"); stream.stopRecording(); ServiceUtils.invokeOnConnection(conn, "recordingStopped", new Object[]{clientId}); } |
As Dominick pointed out on his blog post, you should get your recorded stream under red5/webapps/YouBadAssApplication/streams.
There are lot of things that have to be done in order to have video/voice application running smoothly, however I hope this helps you in realizing how simple Red5 makes things for us. Let me know if I missed something, if there are other workarounds you have found, or whatever you might wanna say
Cheers!
November 5th, 2009 at
Hey I played with Red5 recording a few months ago but found after doing few recordings the videos has weird pauses and gaps in it when I want to play it back so I had to use FMS3.5 did you experience any of these difficulties?
November 5th, 2009 at
Hi GSpark, nope, I have not experienced issues so far with the recording. I’m using version 0.8 final by the way.
February 12th, 2010 at
It would be nice If somebody could post a tutorial from A to Z about Red5…
February 12th, 2010 at
@Dimitree, yeah I know what you mean - There aren’t a bunch of tutorials/howtows’ outhere, pretty much you have to figure it out on your own or go through the mailing lists which is cumbersome. Or if you find one, it doesn’t cover every aspect of it.
I’m not sure if the Red5 docs on the site are up to date either. Anyway, from my experience, I think that at the beginning, for someone who has never used it before, is kinda overwhelming at some points, specially if you are not that familiar with Java, however once you get it rolling it’s not that hard.
I’ve found this blogs very helpful:
http://mswallace.com/2008/09/30/red5-and-flex-basics-part-1-keep-track-of-users/
http://www.joachim-bauch.de/tutorials/red5/HOWTO-NewApplications.txt
http://sziebert.net
May 5th, 2010 at
Recording through webcam is not being properly. some unwanted frames are being inserted during recording and because of that duration of playback is double(metadata info.duration) as the real recording duration.We are using red5 server. I have given too much time but still i did’t find any solution. Please help me its urgent.