How to capture/record clip from Video URL in Android and save to phone
Asked Answered
B

2

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.

Benildas answered 8/12, 2014 at 21:1 Comment(3)
What does "record a short clip... from a Video URL" mean?Zawde
Sorry I updated the original post to clarify.Benildas
@android-user, may I ask you to accept my answer if it solved your problem?Digit
D
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.

Digit answered 19/12, 2014 at 11:37 Comment(4)
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?Benildas
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.Digit
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...Benildas
Yep, I hope there is something that can help you.Digit
A
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.

Archaize answered 18/12, 2014 at 15:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.