3

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.

UPD:

For record video locally to phone storage I use:

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

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());
Goo
  • 1,318
  • 1
  • 13
  • 31
Alex
  • 571
  • 1
  • 8
  • 26
  • 1
    Can your phone playback the recordings? If not I would say your code may be the issue. – ian.shaun.thomas Sep 09 '12 at 21:41
  • I tested my Android application into phone and recorded video successfully played on phone and PC. – Alex Sep 09 '12 at 21:47
  • Updated the post to show how I work with video capture locally on phone and for streaming it to PC. – Alex Sep 09 '12 at 21:55
  • okay, so if you record video with the built in camera application, can any of your desktop video players playback that video? If not, maybe you just need to install MP4 codecs. – ian.shaun.thomas Sep 09 '12 at 22:06
  • Yes, all of my desktop video players playback that (recorded by my phone application) video, without errors or so. Problems only with video, saved from stream. – Alex Sep 09 '12 at 22:10
  • Your code is correct, as you have already established. What's the question here? – user207421 Sep 09 '12 at 23:21
  • My question is: how to save the video stream CORRECT from my phone to desktop HDD (in file) with my desktop Java server, so it can be played by desktop video player(s) without erros? – Alex Sep 10 '12 at 17:06
  • Old topic, but I got the same problem at the moment. Any suggestion? – DavidL Oct 01 '15 at 07:10

1 Answers1

1

Make this comment to flag question as answered: RTMP should be properly encoded and streamed, my simple socket solution is invalid and question is not correct in this sense. Related to How to encode h.264 live stream to RTP packet with Java

Community
  • 1
  • 1
Alex
  • 571
  • 1
  • 8
  • 26