9

In Android, is it possible to record a short clip (ex: an arbitrary 5-10 seconds in the video) from a Video URL (ex: http://www.test.com/video.mp4)?

For example, I'd like to stream a video (from url) in an Activity and allow the ability to capture/record a short clip from it. Perhaps, allow the user to record an arbitrary Start/End time from the video. If so, is there an API to accomplish this? If not, is there an Android library to support this?

Please provide a sample code solution for this.

code
  • 5,294
  • 16
  • 62
  • 113

2 Answers2

4

You can see this link. In short your server has to support downloading. If it does, you can try the following code:

private final int TIMEOUT_CONNECTION = 5000; //5sec
private final int TIMEOUT_SOCKET = 30000; //30sec
private final int BUFFER_SIZE = 1024 * 5; // 5MB

private final int TIMEOUT_CONNECTION = 5000; //5sec
private final int TIMEOUT_SOCKET = 30000; //30sec
private final int BUFFER_SIZE = 1024 * 5; // 5MB

try {
  URL url = new URL("http://....");

  //Open a connection to that URL.
  URLConnection ucon = url.openConnection();
  ucon.setReadTimeout(TIMEOUT_CONNECTION);
  ucon.setConnectTimeout(TIMEOUT_SOCKET);

  // Define InputStreams to read from the URLConnection.
  // uses 5KB download buffer
  InputStream is = ucon.getInputStream();
  BufferedInputStream in = new BufferedInputStream(is, BUFFER_SIZE);
  FileOutputStream out = new FileOutputStream(file);
  byte[] buff = new byte[BUFFER_SIZE];

  int len = 0;
  while ((len = in.read(buff)) != -1)
  {
      out.write(buff,0,len);
  }
} catch (IOException ioe) {
  // Handle the error
} finally {
  if(in != null) {
    try {
      in.close();
    } catch (Exception e) {
      // Nothing you can do
    }
  }
  if(out != null) {
    try {
      out.flush();
      out.close();
    } catch (Exception e) {
      // Nothing you can do
    }
  }
}

If the server doesn't support downloading, there is nothing you can do.

Community
  • 1
  • 1
Kiril Aleksandrov
  • 2,601
  • 20
  • 27
  • Thank you Kiril. How can I specify the seconds into the video? For example, lets say I want to save only 5 seconds in the middle of the video? – code Mar 06 '15 at 02:00
  • I am not sure you can do this. I am not familiar with a certain HTTP header that can provide you with such capability. You can download a specific range of data but you have to find the relation between quality and size for each video. – Kiril Aleksandrov Mar 06 '15 at 06:45
  • Thank you Kiril. I will try to see if there is an http header to let me know the quality... then based on quality I should be able to do some math with the bytes to get the byte/second range... – code Mar 06 '15 at 06:47
  • Yep, I hope there is something that can help you. – Kiril Aleksandrov Mar 06 '15 at 06:59
1

I think you are looking for android.media.projection library. Here is a link to an example for how to use it: MediaProjectionDemo

If you are wanting android's documentation on this library here is a link

Here is an issue tracker that you may want to follow as well mentioning recording video and audo at the same time.

gmaniac
  • 940
  • 1
  • 17
  • 33