Save video stream from Socket to File

All your problems with Audio, Video and Images.

Save video stream from Socket to File

Postby AlexyFrost » Sun Sep 09, 2012 11:15 pm

Hi, guys!

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):

Code: Select all
// 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();


For record video locally to phone storage I use:

Code: Select all
//targetFile - new File object, represents a file on phone SD card

myMediaRecorder.setOutputFile(targetFile);


And for streaming it to PC (without errors handling to simplify)

Code: Select all
ParcelFileDescriptor pfd = null;
Socket socket = null;
String hostname = "my IP";
int port = 8081;

socket = new Socket(InetAddress.getByName(hostname), port);
pfd = ParcelFileDescriptor.fromSocket(socket);

myMediaRecorder.setOutputFile(pfd.getFileDescriptor());


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.
AlexyFrost
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Sep 09, 2012 11:07 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Google [Bot] and 6 guests