我正在尝试从 Youtube 的 rtsp URL 下载 3gp 视频文件并将其保存到我的外部存储中,但我的方法似乎只适用于 HTTP URL。
我收到了这个警告:
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;
}
}
}