Java - Save video stream from Socket to File

Posted by Alex on Stack Overflow See other posts from Stack Overflow or by Alex
Published on 2012-09-09T21:34:28Z Indexed on 2012/09/09 21:37 UTC
Read the original article Hit count: 391

Filed under:
|
|
|
|

I use my Android application for streaming video from phone camera to my PC Server and need to save them into file on HDD. So, file created and stream successfully saved, but the resulting file can not play with any video player (GOM, KMP, Windows Media Player, VLC etc.) - no picture, no sound, only playback errors.

I tested my Android application into phone and may say that in this instance captured video successfully stored on phone SD card and after transfer it to PC played witout errors, so, my code is correct.

In the end, I realized that the problem in the video container: data streamed from phone in MP4 format and stored in *.mp4 files on PC, and in this case, file may be incorrect for playback with video players. Can anyone suggest how to correctly save streaming video to a file?

There is my code that process and store stream data (without errors handling to simplify):

// getOutputMediaFile() returns a new File object

DataInputStream in = new DataInputStream (server.getInputStream());
FileOutputStream videoFile = new FileOutputStream(getOutputMediaFile());
int len;
byte buffer[] = new byte[8192];

while((len = in.read(buffer)) != -1) {
    videoFile.write(buffer, 0, len);
}

videoFile.close();
server.close();

Also, I would appreciate if someone will talk about the possible "pitfalls" in dealing with the conservation of media streams.

Thank you, I hope for your help!

Alex.

© Stack Overflow or respective owner

Related posts about java

Related posts about android