在Android上从Youtube Rtsp URL下载3gp视频

时间:2013-09-02 08:43:37

标签: android ios android-youtube-api

我正在尝试从Youtube的rtsp网址下载3gp视频文件并将其保存到我的外部存储中,但似乎我只使用HTTP网址的方法。

我收到了这个警告:

09-02 10:32:40.877: WARN/System.err(7988): java.net.MalformedURLException: Unknown protocol: rtsp

我的下载方法如下:

public static void downloadFromURL(String url, File cacheDir, String fileName) throws IOException {
        long startTime = System.currentTimeMillis();
        String TAG = "DL";
        if (url != null) {
            if (fileName.contains("/"))
                fileName = fileName.replace("/", "-");
            url = url.replace(" ", "%20");
            URL mUrl = new URL(url);

            URLConnection ucon = mUrl.openConnection();

            ucon.setReadTimeout(TIMEOUT_CONNECTION);
            ucon.setConnectTimeout(TIMEOUT_SOCKET);

            File f = new File(cacheDir, fileName);
            InputStream is = ucon.getInputStream();
            BufferedInputStream inStream = new BufferedInputStream(is, BUFFER_SIZE);
            FileOutputStream outStream = new FileOutputStream(f);
            byte[] buff = new byte[BUFFER_SIZE];


            int len;
            while ((len = inStream.read(buff)) != -1) {
                outStream.write(buff, 0, len);
            }
            // clean up
            outStream.flush();
            outStream.close();
            inStream.close();

            android.util.Log.d(TAG, "Download completed in " + ((System.currentTimeMillis() - startTime) / 1000) + " sec");
        }
    }

仅供参考我在带有Wi-Fi连接的Nexus 7上进行测试。 如果有人能解决我遇到的问题,请告诉我。

更新

无论如何,我发现这些Objective-C片段可以满足我的需求,但我无法将其转换为Java代码:

NSString *urlString = @"http://www.youtube.com/get_video_info?&video_id=7ubt8AWa7SU"; //7ubt8AWa7SU gylfmQgtMJc
    NSURL *infoUrl = [NSURL URLWithString:urlString];
    NSError *error = NULL;
    NSString *info = [NSString stringWithContentsOfURL:infoUrl encoding:NSUTF8StringEncoding error:&error];
    if (info == nil)
    {
        NSLog(@"Error: %@", [error description]);
        return;
    }
    NSArray *urlComponents = [info componentsSeparatedByString:@"&"];
    NSArray *itemComponents;
    NSString *urlEncodedFmtStreamMap = NULL;
    for (NSString *item in urlComponents)
    {
        itemComponents = [item componentsSeparatedByString:@"="];
        if (itemComponents)
        {
            NSString *first = [itemComponents objectAtIndex:0];
            if ([first isEqualToString: @"url_encoded_fmt_stream_map"])
            {
                urlEncodedFmtStreamMap = [itemComponents objectAtIndex:1];
                break;
            }
        }
    }
    NSString *type = NULL;
    NSString *urlSeg1 = NULL;
    NSString *sig = NULL;
    if (urlEncodedFmtStreamMap)
    {
        NSArray *formats = [[urlEncodedFmtStreamMap stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] componentsSeparatedByString:@","];
        for (NSString *item in formats)
        {
            type = NULL;
            urlSeg1 = NULL;
            sig = NULL;
            NSArray *pairs = [item componentsSeparatedByString:@"&"];
            for (NSString *pair in pairs)
            {
                NSArray *varComps = [pair componentsSeparatedByString:@"="];
                NSString *varName = [varComps objectAtIndex: 0];
                NSString *varValue = [[varComps objectAtIndex: 1] stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
                if ([varName isEqualToString: @"type"])
                {
                    NSArray *typeSegments = [varValue componentsSeparatedByString: @";"];
                    type = [typeSegments objectAtIndex:0];
                }
                else if ([varName isEqualToString: @"url"])
                {
                    urlSeg1 = [varValue stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
                }
                else if ([varName isEqualToString: @"sig"])
                {
                    sig = varValue;
                }
            }
            if ([type isEqualToString:@"video/mp4"] && urlSeg1 && sig)
            {
                self.videoUrl = [[urlSeg1 stringByAppendingString: @"&signature="] stringByAppendingString: sig];
                break;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

我认为没有一种简单的方法可以做到这一点。获取RTSP流并不像HTTP GET请求那么简单。视频和音频数据包作为RTP发送,本质上是带有额外数据的UDP数据包,然后您可以使用RTSP和RTCP(您可以忽略)进行服务器/客户端通信。

我建议查看MediaPlayer和Stagefright源代码,特别是在他们设置不同数据源并查看是否可以提取部分RTSP握手和RTP会话设置的情况下。