视频地址获取
大家好,又见面了,我是你们的朋友全栈君。 首先需要获取视频的videoId,直接请求该url,然后match匹配:
Pattern pattern = Pattern.compile("videoId: \'(.+)\'");
Matcher matcher = pattern.matcher(response);
if (matcher.find()) {
String videoId = matcher.group(1);
Log.i(TAG,videoId);
}
获得videoId后开始获取r参数与s参数:
String URL_VIDEO="/video/urls/v/1/toutiao/mp4/%s?r=%s";
String HOST_VIDEO = "http://i.snssdk.com";
String r = getRandom();
CRC32 crc32 = new CRC32();
String s = String.format(URL_VIDEO, videoId, r);
//进行crc32加密。
crc32.update(s.getBytes());
String crcString = crc32.getValue() + "";
//2.生成获取视频地址的url
String url = HOST_VIDEO + s + "&s=" + crcString;
Log.i(TAG,url);
private String getRandom() {
Random random = new Random();
StringBuilder result = new StringBuilder();
for (int i = 0; i < 16; i++) {
result.append(random.nextInt(10));
return result.toString();
}
接下来访问生成的url,即可获得类似下面的json:
箭头所示的main_url即为我们需要的视频播放真实地址,但其实Base64编码的,解码即可得到真实地址:
System.err.println( new String(Base64.getUrlDecoder()
.decode("main_url的值")));
视频地址有有效时间,意味每次播放都需要判断地址是否准确!
bak:关于videoId的获取:
open fun test(url: String): String? {
var client = OkHttpClient().newBuilder()
.followRedirects(false)
.followSslRedirects(false)
.build();
var mediaType = MediaType.parse("application/x-www-form-urlencoded");
var body = RequestBody.create(mediaType,
"checkCookiesEnabled=true&checkMobileDevice=false&checkStandaloneMode=false&checkTabletDevice=false&portalAccountUsername=username&portalAccountPassword=password");
var request = Request.Builder()
.url(url)
.post(body)
.addHeader("content-type", "application/x-www-form-urlencoded")
.addHeader("cache-control", "no-cache")