Search code examples
javaandroidtcpvitamiomp4parser

How can I record a live stream with Android Java?


Right now I am displaying a live stream on a Surface using the vitamio librairy.

I wish to record it in order to watch it later as the feed comes from a drone.

Right now I am periodically saving a series of bitmaps using jcodec :

 SequenceEncoder enc = new SequenceEncoder(new File("filename"));
// GOP size will be supported in 0.2
// enc.getEncoder().setKeyInterval(25);
for(...) {
    BufferedImage image = ... // Obtain an image to encode
    enc.encodeImage(image);
}
enc.finish();

My problems are :

  • The h264 output from jcodec is corrupted (there is a lot of color patches on the frames)
  • I can't record at a very high framerate (I'd like 30fps).

Moreover I still need to find a way to mux it.. I looked into Mp4Parser without much succes.


Solution

  • Why not to use data input stream:

    DataInputStream in = new DataInputStream (recording.getInputStream());
    FileOutputStream videoFile = new FileOutputStream(targetFile);
    int len;
    byte buffer[] = new byte[8192];
    
    while((len = in.read(buffer)) != -1) {
        videoFile.write(buffer, 0, len);
    }
    
    videoFile.close();
    

    Then, you can play the video from file.