How-To: Converting Recorded Files to MP4 for Seamless Playback via Red5 Pro

December 18th, 2015 by Chris Allen

A lot of developers have been asking us detailed questions about recording streams using Red5 Pro. The one thing that comes up most frequently is how to create an MP4 file for easy playback on mobile devices. While you can use our SDK to playback the recorded file as is (Red5 records in FLV format), this requires that the user always use your native app, and sometimes it would just be nice to provide a link to an MP4 for progressive download.

So with that, here’s a quick guide to get you up and running with a custom Red5 application that converts recorded files to MP4 for easy playback. You can find the source for this example on GitHub.

Install ffmpeg

The first step is to install ffmpeg on your server. ffmpeg is a great command line utility to manipulate video files, such as converting between formats like we want to do for this example.

You can find precompiled packages for your platform from ffmpeg’s website, although if you need to convert your recordings to formats that need an extra module on top of ffmpeg, you might need to compile those from scratch. Once you have ffmpeg on your server, just issue a simple command to make sure it’s working. The simplest thing you could do is just called ffmpeg with no params like this: ./ffmpeg . That should output something like this:

FFMPEG_output.png

If you see it output the version number and other info, it means you have a working version installed on your machine. Note that I’m running on my local Mac, but the same concept applies to putting it on any other OS. Also note, for production use, you would want to put the binary in a better place than ~/Downloads , and you would want to update your PATH  to point to the ffmpeg application from anywhere.

 

Start Your Own Red5 Application

Now that you have ffmpeg on your server, the next step is to build your own custom server side application for Red5, extending the live example here. You will use this as the beginning of your custom app that will convert your recorded files to MP4.

 

Overwrite MultithreadedApplicationAdapter.streamBroadcastClose()

Now on to calling ffmpeg from your custom Red5 Application.

In your MultiThreadedApplicationAdapter  class you will now need to override the streamBroadcastClose()  method. As is pretty obvious from the name, this method is called automatically by Red5 Pro once a stream broadcast has finished and closed. Even though the Broadcast is finished, it doesn’t mean that the file has finished being written. So, in order to handle that, we need to create a thread to wait and allow the file writer to finish the flv write-out. Then, we access the file and convert it to an MP4 using ffmpeg.

 

Let’s see what that looks like in code:

1. Overwrite the method.

 

2. Check to make sure there is a file that was recorded.

 

3. Setup some variables to hold the data about the recorded file.

 

4. Create a thread to wait for the FLV file to be written. Five seconds should be plenty of time.

 

5. Next we need to get a reference to the FLV file.

 

6. Make sure that there is a file before continuing.

7.  Get the path to the file on the file system and the path to where you want the MP4 version saved.

 

8. Delete any previous file created, otherwise FFMPEG  will fail to write the file.

 

9. Tell your application where to find FFMPEG . Note in this case we’ve added the app to our PATH so we can call it from anywhere.

10. Create the FFMPEG  command.

 

11. Run the process.

 

12. Read the output from FFMPEG  for errors and other info.

 

13. Create threads for each of these and wait for the process to finish.

 

14. Make sure that the process created the file and that it didn’t log any errors.

 

15. Finally catch any potential exceptions and close out the thread you created.

 

That’s it! You’ve now created an MP4 file from the recording. Rather than typing that out line by line we’ve provided the example project here:

https://github.com/red5pro/red5pro-server-examples/tree/develop/FileConversion

Now that you know how to intercept one of the methods of the MultiThreadedApplicationAdapter  in Red5 Pro you can start to look at other ones you could implement to do custom behavior for your app. Many people who are recording their files want to move them to an Amazon S3 bucket for storage. Adding in a few lines using Amazon’s API to do that wouldn’t be too challenging.

What other things would you want to do with a custom Red5 application? We would love to hear from you.

-Chris

 

Leave a Reply