Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload video from a(n Android) phone as it is being recorded

I'm trying to write an app that uploads videos from Android phones as they are being recorded by reading from .mp4 file and uploading the bytes as they are written to the file. The problem is that, as far as I can tell, the moov atom and some other assorted data do not seem to be written to file until the video recording has finished and the video file has been closed. Is there any way to process the video file and add these metadata on the server-side assuming either

  1. The full video file has been uploaded (but without the moov atom or any other data that does not get written on the first pass)
  2. Only part of the video has been uploaded (e.g. the first 10 seconds) and I want to convert this into a valid mp4 file containing the uploaded segment of video.

I've seen links like http://www.mattakis.com/blog/kisg/20090708/broadcasting-video-with-android-without-writing-to-the-file-system but they gloss over the problem by simply stating

So the received stream will have to be fixed up after the recording is finished, or the raw video / audio frames have to be processed by the server.

without explaining exactly how to go about doing that.

like image 783
velocipedestrian Avatar asked Apr 22 '12 11:04

velocipedestrian


People also ask

How do I upload a video from my gallery android?

1) Select the video file from the gallery. Create a global int private static final int SELECT_VIDEO = 3; -- it doesn't matter what number you use, so long as that's to one you check for later on. Then, use an intent to select a video. Intent intent = new Intent(); intent.

How long can an Android phone record video?

To prevent your phone from overheating, higher resolution videos have a 10-minute recording limit, but you can choose a lower resolution to extend your recording time.


1 Answers

As you've noticed, the MP4 format can be difficult to use in such situations. I suspect the linked blog post doesn't go into detail about the "fixing up" because it can be quite involved. In addition to writing the missing size field of the mdat box, you'll need to generate the ftyp and moov boxes. If you really need an end-to-end MP4 solution, ISO 14496-12 and ISO 14496-14 will tell you more than you ever wanted to know about how to build these data structures.

However, you might find that a much more elegant solution is to use a format that is actually suited for real-time processing. In other words, on the Android side, remux the video stream into a real-time format and send that to the server. On the server side, you then have great flexibility for processing the video: You can remux the whole video back into MP4, you can slice-and-dice, make 10-second chunks, or whatever. The open-source Sipdroid project contains some code that demonstrates remuxing live video into RTP. (You may prefer a reliable transmission format -- RTP over TCP, or whatever -- the principle is the same.)

like image 173
David Simmons Avatar answered Sep 28 '22 17:09

David Simmons