Home » Android » Android – Save an rtsp (h264) stream to mp4 file

Android – Save an rtsp (h264) stream to mp4 file

Posted by: admin May 14, 2020 Leave a comment


I am working on a project where I need to

  1. Read input H.264 encoded stream from an IPCamera – I am able to fetch this in through an rtsp url like rtsp://

  2. Display the IPCamera stream – This I am able to do using the

final VideoView vv = (VideoView) findViewById(R.id.video_view_h264);
        MediaController mc = new MediaController(getApplicationContext());
        vv.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            public void onPrepared(MediaPlayer mp) {

  1. Now I want to record this stream to an MP4 file. This is where I am stuck and I am considering the following options

a) MediaRecorder – Based on my google searches I believe for this class the input can only from a device camera. Is there a way to tweak this where I can provide an input from rtsp stream ?

b) MediaCodec API – 4.1 onwards Android has released this low level API with and MediaExtractor and MediaCodec. For this option I think an rtsp stream cannot be used in the following snippet

final String STREAM_URL = "rtsp://";
MediaExtractor mediaExtractor = new MediaExtractor();
mediaExtractor.setDataSource(STREAM_URL); // I get an exception 04-28 18:30:18.914: E/AndroidRuntime(8140): Caused by: java.io.IOException: Failed to instantiate extractor.

c) Can I do a read from the url and store it like a file. How to I convert this stream to an MP4 file ? Any code snippet will be really helpful.

I had also tried to use FFMPEG but the performance was so poor that I dropped this option.

Any inputs on the above three options or any other additional option which I can consider will be greatly appreciated.

Thanks !!

How to&Answers:

It is true that MediaRecorder doesn’t have any support for handling direct streaming content. From a frameworks perspective, there are parser objects as in MediaExtractor and codec objects like MediaCodec, but not writer abstractions (yet).

In AOSP, recordVideo is a command line utility aimed for a simple file based recording. As part of the implementation, an encoder is created and passed to the MPEG4Writer as observed here. You could potentially reuse this code and try to write the incoming stream into the file directly.

For this you could model your streaming input as a MediaSource and pass it to the writer directly. You would have to setup certain basic metaData from the streaming source and pass it to the downstream writer. You may not require to create a new MediaRecorder, if you could model the input source directly.


Got the exact same problem over here. FFmpeg solves the problem and seems very efficient to me but it is hardware dependent, it need to be compiled for the specific arm version of each device’s cpu.

Another approach jcodec (http://jcodec.org/) but haven’t yet figured out how to save a rtsp stream with it. It is a purely java library, but there isn’t much documentation available.

Please tell me if you figure out a way of doing it.