目 录CONTENT

文章目录
Web

通过接口下载抖音无水印视频

恪晨
2022-10-13 / 0 评论 / 0 点赞 / 37 阅读 / 697 字 / 正在检测是否收录...
温馨提示:
慢慢看,慢慢读,不行就边听歌边读

解析抖音链接,支持获取抖音无法下载的视频,暂不支持图片。

项目文件:http://pan.wangboweb.site/s/priL

主要代码:

/**
 *
 * index.js
 * @author wangbo
 * @since 2022/4/25
 */
const express = require('express')
const request = require('request');

const app = express()
const port = 3000

app.get('/', (req, res) => {
    res.send('Hello World!')
});

let send = {
    'Error:':'查询失败',
    'code':400,
}
app.get('/api/getUrl', async (req, res) => {
    let { url } = req.query;
    url = httpString(url);

    //前端传过来的地址 进行重定向拿到 item_ids 并且返回
    let watermark = await new Promise(resolve=>{
        request(url,(error, response, body) => {
            if (!error && response.statusCode === 200) {
                let href = response.request.href;
                let id = void 0;

                try {
                    id = href.match(/video\/(\S*)\?/)[1];
                } catch (error) {
                    res.json(send)
                    return false;
                }

                resolve(`https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=${id}`);
            }else{
                res.json(send)
            }
        })
    });

    //拿到完整地址返回指定数据
    request(watermark,async (error, response, body)=>{
        if (!error && response.statusCode === 200) {
            let result = JSON.parse(body);
            let data = result.item_list[0];
            //视频url解析
            let video = await videourl(data['video']["play_addr"]["url_list"][0]);
            // 拼接返回指定数据
            res.json({
                'title':data["share_info"]["share_title"],
                'cover':data['video']["origin_cover"]["url_list"][0],
                'video':video,
                'audio':data['music']["play_url"]["uri"],
                'code':200,
            })
        }else{
            res.json(send)
        }
    })
});

//解析视频
const videourl = async (url)=>{
    //截取字符串 wm
    url = url.replace(/wm/g,'');
    return await new Promise(resolve=>{
        request(url,(error, response, body) => {
            resolve(response.request.href)
        })
    })
}

//解析字符串里面的url
const httpString = (s) =>{
    let reg = /(https?|http|ftp|file):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/g;
    try {
        return s.match(reg)[0];
    } catch (error) {
        return null;
    }
}

app.listen(port, () => {
    console.log(`Example app listening on port ${port}`)
})

package.json

{
  "name": "getdouyinurl",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.3",
    "request": "^2.88.2"
  }
}

使用方式:

  • npm run start运行项目,打开http://localhost:3000/api/getUrl?url=https://v.douyin.com/1q2w3e4r
  • url后面的参数为抖音的链接,可以在抖音直接点击复制链接,接口会自动解析url参数
  • 返回参数
{
  "title": "非要妹妹吗?姐姐不行吗",
  "cover": "https://p3-sign.douyinpic.com/tos-cn-p-0015/8c1de6af8c5e4c73b2b0290a5e9c1c3d_1650811125~tplv-dy-360p.jpeg?x-expires=1652061600&x-signature=%2F3Gfl3FTrqGfujDAXggNFf2VE60%3D&from=4257465056&se=false&biz_tag=feed_cover&l=202204251033280102040510710D35F1EE",
  "video": "https://aweme.snssdk.com/aweme/v1/play/?video_id=v0200fg10000c9im1d3c77ub8ai3900g&ratio=720p&line=0",
  "audio": "https://sf3-cdn-tos.douyinstatic.com/obj/ies-music/7090179779231599374.mp3",
  "code": 200
} 

部署

0

评论区