c++通过抖音分享链接获取视频长链接的多种实现代码
时间: 2023-08-18 17:08:32 浏览: 226
JAVA实现抖音代码跳舞源码
以下是两种实现方式:
方式一:使用正则表达式
```c++
#include <iostream>
#include <string>
#include <regex>
using namespace std;
string getVideoUrl(string shareUrl) {
regex reg("https://v.douyin.com/(\\w+)/");
smatch match;
if (regex_search(shareUrl, match, reg)) {
string itemId = match[1];
string apiUrl = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=" + itemId;
// 发送网络请求获取视频信息
// ...
return videoUrl;
}
return "";
}
int main() {
string shareUrl = "https://v.douyin.com/JdQeX7B/";
string videoUrl = getVideoUrl(shareUrl);
if (videoUrl.empty()) {
cout << "获取视频链接失败" << endl;
} else {
cout << "视频长链接:" << videoUrl << endl;
}
return 0;
}
```
方式二:使用第三方库
```c++
#include <iostream>
#include <string>
#include <curl/curl.h>
#include <rapidjson/document.h>
using namespace std;
size_t writeCallback(char* ptr, size_t size, size_t nmemb, void* userdata) {
string* response = reinterpret_cast<string*>(userdata);
response->append(ptr, size * nmemb);
return size * nmemb;
}
string getVideoUrl(string shareUrl) {
CURL* curl = curl_easy_init();
if (curl) {
// 设置请求URL
string apiUrl = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?url=" + shareUrl;
curl_easy_setopt(curl, CURLOPT_URL, apiUrl.c_str());
// 设置请求头
struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, "User-Agent: okhttp/3.12.1");
headers = curl_slist_append(headers, "Connection: keep-alive");
headers = curl_slist_append(headers, "Host: www.iesdouyin.com");
headers = curl_slist_append(headers, "Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7");
headers = curl_slist_append(headers, "Accept-Encoding: gzip, deflate, br");
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
// 设置响应处理函数
string responseBody;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseBody);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
// 发送网络请求
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
cout << "网络请求失败:" << curl_easy_strerror(res) << endl;
return "";
}
// 解析响应JSON
rapidjson::Document doc;
doc.Parse(responseBody.c_str());
if (!doc.IsObject() || doc.HasParseError()) {
cout << "解析响应失败" << endl;
return "";
}
if (doc.HasMember("item_list") && doc["item_list"].IsArray()) {
const rapidjson::Value& itemList = doc["item_list"];
if (itemList.Size() > 0 && itemList[0].IsObject()) {
const rapidjson::Value& video = itemList[0]["video"];
if (video.IsObject() && video.HasMember("play_addr") && video["play_addr"].IsObject()) {
const rapidjson::Value& playAddr = video["play_addr"];
if (playAddr.HasMember("url_list") && playAddr["url_list"].IsArray()) {
const rapidjson::Value& urlList = playAddr["url_list"];
if (urlList.Size() > 0 && urlList[0].IsString()) {
string videoUrl = urlList[0].GetString();
return videoUrl;
}
}
}
}
}
return "";
}
return "";
}
int main() {
string shareUrl = "https://v.douyin.com/JdQeX7B/";
string videoUrl = getVideoUrl(shareUrl);
if (videoUrl.empty()) {
cout << "获取视频链接失败" << endl;
} else {
cout << "视频长链接:" << videoUrl << endl;
}
return 0;
}
```
以上代码仅供参考,具体实现方式还需要根据实际情况进行修改。
阅读全文