Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 632
code Avatar asked Dec 08 '14 21:12

code


People also ask

How do you screenshot a video on Android?

Important: These steps work on phones running Android 12 and up, on most screens that allow you to scroll. Open the screen that you want to capture. Press the Power and Volume down buttons at the same time. At the bottom, tap Capture more.

Can u screen record on android?

Android Screen Recorder Pull down the notification shade from the top of the screen to view your quick settings options. Tap the Screen Recorder icon and give permission to the device to record the screen (you might have to edit the default icons that appear). Determine what sound, if any, you want recorded.


1 Answers

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.

like image 167
Kiril Aleksandrov Avatar answered Oct 21 '22 14:10

Kiril Aleksandrov