How to stream a camcorder’s stream over a network by creating a valid video using Android?

I am trying to record camcorder recordings to a network stream, see the following snippet:

ParcelFileDescriptor fd = ParcelFileDescriptor.fromSocket(socket); this.camera.lock(); // this.camera refers to an android.hardware.Camera instance. this.camera.unlock(); this.mediaRecorder = new MediaRecorder(); this.mediaRecorder.setCamera(this.camera); this.mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); this.mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); this.mediaRecorder.setProfile(CamcorderProfile.get(0, CamcorderProfile.QUALITY_HIGH)); this.mediaRecorder.setOutputFile(fd.getFileDescriptor()); this.mediaRecorder.prepare(); this.mediaRecorder.start(); 

Then I try to capture it on the other side of the connection: with Socket.getInputStream() I read the bytes, and yes, the bytes come in. The byte is actually written over the network.

But when you try to save it to a file using the FileOutputStream class and open the file using VLC, it is simply impossible to read.

How can I end up making the file “playable”, which means it's a valid video file?

Edit:

When I use this.mediaRecorder.setOutputFile("/sdcard/out.mp4"); , I can open the video file from Android, and it will play successfully. However, when I use this.mediaRecorder.setOutputFile(fd.getFileDescriptor()); , he will not.

+6
source share
1 answer

The problem may be due to the fact that you do not install everything in your camcorder profile.

Instead of this:

 this.mediaRecorder.setProfile(CamcorderProfile.get(0, CamcorderProfile.QUALITY_HIGH)); 

try for example the following:

 CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH); profile.fileFormat = MediaRecorder.OutputFormat.MPEG_4; profile.videoCodec = MediaRecorder.VideoEncoder.MPEG_4_SP; this.mediaRecorder.setProfile(profile); 

By the way, are you trying to transfer this video after it is finished recording or in real time when receiving data from your camcorder?

EDIT:

If I remember correctly, the mpeg4 file format does not have a footer, so there is no need to “wait for the whole file” ... BUT: this type of file has a structure that you cannot break if you want to read This. Therefore, a problem may arise if you send raw bytes of video that were prepared in the mpeg4 file format, and you want to open the file after you receive a random number of these bytes. You can be almost sure that you will not be able to get the correct enough bytes of your video.

I would advise you:

  • Check this. For example: send some video to the other side and use some kind of software that allows you to check the file structure. Perhaps one of them will tell you if your file has the correct structure. I can try to link some tools, but I have not tried any of them myself, so you may need to find something else (if none of them work well enough). You can try for example this or which .

  • If step 1. confirms that your file is being cut out in the wrong place, you can try to control it somehow. For example, telling the recipient about the locations of the file, which when used to cut it will not lead to the destruction of the structure (it will open the file).

  • An alternative could be an attempt to send some kind of "raw" video frames. And by "raw" I mean some low-level data from the camera that you could send and apply a specific codec on the receiving side. I have never tried to do this, so you may need to check if this is possible (get this data type).

+4
source

All Articles