Ejemplo n.º 1
0
  // ------------------------
  // Handler for timer
  // ------------------------
  public void actionPerformed(ActionEvent e) {

    // if the current image nb is less than the length of the video
    if (imagenb < VIDEO_LENGTH) {
      // update current imagenb
      imagenb++;

      try {
        // get next frame to send from the video, as well as its size
        int image_length = video.getnextframe(buf);

        // Builds an RTPpacket object containing the frame
        RTPpacket rtp_packet =
            new RTPpacket(MJPEG_TYPE, imagenb, imagenb * FRAME_PERIOD, buf, image_length);

        // get to total length of the full rtp packet to send
        int packet_length = rtp_packet.getlength();

        // retrieve the packet bitstream and store it in an array of bytes
        byte[] packet_bits = new byte[packet_length];
        rtp_packet.getpacket(packet_bits);

        // send the packet as a DatagramPacket over the UDP socket
        senddp = new DatagramPacket(packet_bits, packet_length, ClientIPAddr, RTP_dest_port);
        RTPsocket.send(senddp);

        // System.out.println("Send frame #"+imagenb);
        // print the header bitstream
        rtp_packet.printheader();

        // update GUI
        label.setText("Send frame #" + imagenb);
      } catch (Exception ex) {
        System.out.println("Exception caught: " + ex);
        System.exit(0);
      }
    } else {
      // if we have reached the end of the video file, stop the timer
      timer.stop();
    }
  }
  public static String calculateYouTubeUrl(
      String pYouTubeFmtQuality, boolean pFallback, String pYouTubeVideoId)
      throws IOException, ClientProtocolException, UnsupportedEncodingException {

    String lUriStr = null;
    HttpClient lClient = new DefaultHttpClient();

    HttpGet lGetMethod = new HttpGet(YoutubeUrlIDs.YOUTUBE_VIDEO_INFORMATION_URL + pYouTubeVideoId);

    HttpResponse lResp = null;

    lResp = lClient.execute(lGetMethod);

    ByteArrayOutputStream lBOS = new ByteArrayOutputStream();
    String lInfoStr = null;

    lResp.getEntity().writeTo(lBOS);
    lInfoStr = new String(lBOS.toString("UTF-8"));

    String[] lArgs = lInfoStr.split("&");
    Map<String, String> lArgMap = new HashMap<String, String>();
    for (int i = 0; i < lArgs.length; i++) {
      String[] lArgValStrArr = lArgs[i].split("=");
      if (lArgValStrArr != null) {
        if (lArgValStrArr.length >= 2) {
          lArgMap.put(lArgValStrArr[0], URLDecoder.decode(lArgValStrArr[1]));
        }
      }
    }

    // Find out the URI string from the parameters

    // Populate the list of formats for the video
    String lFmtList = URLDecoder.decode(lArgMap.get("fmt_list"));
    ArrayList<Format> lFormats = new ArrayList<Format>();
    if (null != lFmtList) {
      String lFormatStrs[] = lFmtList.split(",");

      for (String lFormatStr : lFormatStrs) {
        Format lFormat = new Format(lFormatStr);
        lFormats.add(lFormat);
      }
    }

    // Populate the list of streams for the video
    String lStreamList = lArgMap.get("url_encoded_fmt_stream_map");
    if (null != lStreamList) {
      String lStreamStrs[] = lStreamList.split(",");
      ArrayList<VideoStream> lStreams = new ArrayList<VideoStream>();
      for (String lStreamStr : lStreamStrs) {
        VideoStream lStream = new VideoStream(lStreamStr);
        lStreams.add(lStream);
      }

      // Search for the given format in the list of video formats
      // if it is there, select the corresponding stream
      // otherwise if fallback is requested, check for next lower format
      int lFormatId = Integer.parseInt(pYouTubeFmtQuality);

      Format lSearchFormat = new Format(lFormatId);
      while (!lFormats.contains(lSearchFormat) && pFallback) {
        int lOldId = lSearchFormat.getId();
        int lNewId = getSupportedFallbackId(lOldId);

        if (lOldId == lNewId) {
          break;
        }
        lSearchFormat = new Format(lNewId);
      }

      int lIndex = lFormats.indexOf(lSearchFormat);
      if (lIndex >= 0) {
        VideoStream lSearchStream = lStreams.get(lIndex);
        lUriStr = lSearchStream.getUrl();
      }
    }
    // Return the URI string. It may be null if the format (or a fallback format if enabled)
    // is not found in the list of formats for the video
    return lUriStr;
  }
  /** Parsing videos from youtube. This is : Script#2 */
  public static ArrayList<Video> func_get_video_script2(String pYouTubeVideoId)
      throws IOException, ClientProtocolException, UnsupportedEncodingException {

    ArrayList<Video> lVideoList = new ArrayList<Video>();

    HttpClient lClient = new DefaultHttpClient();

    HttpGet lGetMethod =
        new HttpGet(
            YoutubeUrlIDs.YOUTUBE_VIDEO_INFORMATION_URL
                + pYouTubeVideoId
                + "&el=embedded&gl=US&hl=en&eurl="
                + URLEncoder.encode("https://youtube.googleapis.com/v/" + pYouTubeVideoId, "UTF-8")
                + "&asv=3&sts=1588");

    HttpResponse lResp = null;

    lResp = lClient.execute(lGetMethod);

    ByteArrayOutputStream lBOS = new ByteArrayOutputStream();
    String lInfoStr = null;

    lResp.getEntity().writeTo(lBOS);
    lInfoStr = new String(lBOS.toString("UTF-8"));

    String[] lArgs = lInfoStr.split("&");
    Map<String, String> lArgMap = new HashMap<String, String>();
    for (int i = 0; i < lArgs.length; i++) {
      String[] lArgValStrArr = lArgs[i].split("=");
      if (lArgValStrArr != null) {
        if (lArgValStrArr.length >= 2) {
          lArgMap.put(lArgValStrArr[0], URLDecoder.decode(lArgValStrArr[1]));
        }
      }
    }

    // Find out the URI string from the parameters

    // Populate the list of formats for the video
    String lFmtList = URLDecoder.decode(lArgMap.get("fmt_list"));
    ArrayList<Format> lFormats = new ArrayList<Format>();
    if (null != lFmtList) {
      String lFormatStrs[] = lFmtList.split(",");

      for (String lFormatStr : lFormatStrs) {
        Format lFormat = new Format(lFormatStr);
        lFormats.add(lFormat);
      }
    }

    // Populate the list of streams for the video
    String lStreamList = lArgMap.get("url_encoded_fmt_stream_map");
    if (null != lStreamList) {
      String lStreamStrs[] = lStreamList.split(",");
      ArrayList<VideoStream> lStreams = new ArrayList<VideoStream>();
      for (String lStreamStr : lStreamStrs) {
        VideoStream lStream = new VideoStream(lStreamStr);
        lStreams.add(lStream);
      }

      // -----------------------------------------------------
      // ----
      HashMap<Integer, String> typeMap = new HashMap<Integer, String>();

      typeMap.put(13, "Low : 176x144 | 3GP");
      typeMap.put(17, "Low : 176x144 | 3GP");
      typeMap.put(36, "Medium : 320x240 | 3GP");

      typeMap.put(5, "Medium : 640x360 | FLV");
      typeMap.put(6, "Medium : 640x360 | FLV");

      typeMap.put(34, "Medium : 640x360 | FLV");
      typeMap.put(35, "High : 854x480 | FLV");

      typeMap.put(43, "Medium : 640x360 | WEBM");
      typeMap.put(44, "High : 854x480 | WEBM");
      typeMap.put(45, "HD : 1280x720 | WEBM");
      typeMap.put(46, "Full HD 3D : 1920x1020 | WEBM");

      typeMap.put(18, "Medium :  480x360 | MP4");
      typeMap.put(22, "HD :  1280x720 | MP4");

      typeMap.put(37, "Full HD : 1920x1080 | MP4");
      typeMap.put(38, "Full HD 3D : 3072x1080 | MP4");
      typeMap.put(33, "4K : 4096x230 | MP4");

      for (Format lformat : lFormats) {
        Video lvid = new Video();
        int lIndex = lFormats.indexOf(lformat);
        int id = lformat.getId();
        // get video quality type string..
        String ltype = typeMap.get(id);

        if (lIndex >= 0) {
          VideoStream lSearchStream = lStreams.get(lIndex);

          String lUriString = Uri.decode(Uri.parse(lSearchStream.getUrl()).toString());

          lvid.type = ltype;
          lvid.url = lUriString;
          if (lvid.type != null && lvid.url != null) {
            lVideoList.add(lvid);
          }
        }
      }
    }

    return lVideoList;
  }
  /** Parsing videos from youtube. Script#1. */
  public static ArrayList<Video> func_get_video_script1(String web_source) throws Exception {
    ArrayList<Video> video_list = new ArrayList<Video>();

    String[] lArgs = web_source.split("&");
    Map<String, String> lArgMap = new HashMap<String, String>();
    for (int i = 0; i < lArgs.length; i++) {
      String[] lArgValStrArr = lArgs[i].split("=");
      if (lArgValStrArr != null) {
        if (lArgValStrArr.length >= 2) {
          lArgMap.put(lArgValStrArr[0], URLDecoder.decode(lArgValStrArr[1]));
        }
      }
    }

    // Find out the URI string from the parameters

    // Populate the list of formats for the video
    String lFmtList = URLDecoder.decode(lArgMap.get("fmt_list"));
    ArrayList<Format> lFormats = new ArrayList<Format>();
    if (null != lFmtList) {
      String lFormatStrs[] = lFmtList.split(",");

      for (String lFormatStr : lFormatStrs) {
        Format lFormat = new Format(lFormatStr);
        lFormats.add(lFormat);
      }
    }

    // Populate the list of streams for the video
    String lStreamList = lArgMap.get("url_encoded_fmt_stream_map");
    if (null != lStreamList) {
      String lStreamStrs[] = lStreamList.split(",");
      ArrayList<VideoStream> lStreams = new ArrayList<VideoStream>();
      for (String lStreamStr : lStreamStrs) {
        VideoStream lStream = new VideoStream(lStreamStr);
        lStreams.add(lStream);
      }

      // -----------------------------------------------------
      // ----
      HashMap<Integer, String> typeMap = new HashMap<Integer, String>();

      typeMap.put(13, "Low : 176x144 | 3GP");
      typeMap.put(17, "Low : 176x144 | 3GP");
      typeMap.put(36, "Medium : 320x240 | 3GP");

      typeMap.put(5, "Medium : 640x360 | FLV");
      typeMap.put(6, "Medium : 640x360 | FLV");

      typeMap.put(34, "Medium : 640x360 | FLV");
      typeMap.put(35, "High : 854x480 | FLV");

      typeMap.put(43, "Medium : 640x360 | WEBM");
      typeMap.put(44, "High : 854x480 | WEBM");
      typeMap.put(45, "HD : 1280x720 | WEBM");
      typeMap.put(46, "Full HD 3D : 1920x1020 | WEBM");

      typeMap.put(18, "Medium :  480x360 | MP4");
      typeMap.put(22, "HD :  1280x720 | MP4");

      typeMap.put(37, "Full HD : 1920x1080 | MP4");
      typeMap.put(38, "Full HD 3D : 3072x1080 | MP4");
      typeMap.put(33, "4K : 4096x230 | MP4");

      for (Format lformat : lFormats) {
        Video lvid = new Video();
        int lIndex = lFormats.indexOf(lformat);
        int id = lformat.getId();
        // get video quality type string..
        String ltype = typeMap.get(id);

        if (lIndex >= 0) {
          VideoStream lSearchStream = lStreams.get(lIndex);

          String lUriString = Uri.decode(Uri.parse(lSearchStream.getUrl()).toString());

          lvid.type = ltype;
          lvid.url = lUriString;
          if (lvid.type != null && lvid.url != null) {
            video_list.add(lvid);
          }
        }
      }
    }

    return video_list;
  }