欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

node.js去水印方法實(shí)例分析

 更新時(shí)間:2023年04月15日 09:17:34   作者:Mebius·  
這篇文章主要介紹了node.js去水印方法,結(jié)合實(shí)例形式分析了node.js基于第三方平臺(tái)實(shí)現(xiàn)去水印的相關(guān)交互與操作技巧,需要的朋友可以參考下

一、封裝一個(gè)函數(shù)來(lái)識(shí)別要解析的類型

// 獲取類型
get_type(){
    if(this.url.match(/http[s]?:\/\/v\.douyin\.com\/[^ ]+/) != null){
        console.log("識(shí)別到【dy】鏈接")
        return "dy"
    }
    else if(this.url.match(/http[s]?:\/\/v\.kuaishou.com\/[^ ]+/) != null){
        console.log("識(shí)別到【ks】鏈接")
        return "ks"
    }
    else if(this.url.match(/http[s]?:\/\/xhslink\.com\/[^ ]+/) != null){
        console.log("識(shí)別到【xhs】鏈接")
        return "xhs"
    }
    else{
        console.log("未識(shí)別到鏈接類型,請(qǐng)輸入正確的鏈接")
        return null
    }
}

二、在初始化方法中寫入本實(shí)例共用的數(shù)據(jù)

// 初始化方法
constructor() {
    this.token = "Z1QljZOZiT4NTG"  // token
    // 請(qǐng)求地址數(shù)組對(duì)象
    this.req_urls = {
        dy: "http://api.txapi.cn/v1/parse_short_video/dy",
        ks: "http://api.txapi.cn/v1/parse_short_video/ks",
        xhs: "http://api.txapi.cn/v1/parse_short_video/xhs",
    }
    this.url = ''  // 要解析的地址
    this.type = ''  // 用來(lái)存儲(chǔ)識(shí)別到的類型
}

三、封裝一個(gè)“萬(wàn)能解析”的方法

// 萬(wàn)能解析
parse_video(){
    axios({
        url: this.req_urls[this.type],
        method: 'POST',
        headers: {
            'Content-Type': "application/x-www-form-urlencoded"
        },
        responseType: 'json',
        data: {
            token: this.token,
            url: this.url
        }
    })
    .then(resp => {
        // 校驗(yàn)是否解析成功
        if(resp.data.code != 200 && resp.data.msg != "OK"){
            console.log("解析失敗")
        }
        else{
            // 獲取到解析后的數(shù)據(jù)
            const data = resp.data.data
            console.log(data)
            var type = data.type  // 類型:1視頻 2圖片集
            var title = data.title  // 標(biāo)題
            var cover_url = data.cover_url  // 封面地址
            var video_url = data.video_url  // 無(wú)水印視頻地址
            var imgs = data.imgs  // 無(wú)水印圖片數(shù)組
        }
    })
}

廢話不多說(shuō) 直接上完整代碼??

const axios = require('axios')
class Parse{
    // 初始化方法
    constructor() {
        this.token = "Z1QljZOZiT4NTG"  // token
        // 請(qǐng)求地址數(shù)組對(duì)象
        this.req_urls = {
            dy: "http://api.txapi.cn/v1/parse_short_video/dy",
            ks: "http://api.txapi.cn/v1/parse_short_video/ks",
            xhs: "http://api.txapi.cn/v1/parse_short_video/xhs",
        }
        this.url = ''  // 要解析的地址
        this.type = ''  // 用來(lái)存儲(chǔ)識(shí)別到的類型
    }
    // 萬(wàn)能解析
    parse_video(){
        axios({
            url: this.req_urls[this.type],
            method: 'POST',
            headers: {
                'Content-Type': "application/x-www-form-urlencoded"
            },
            responseType: 'json',
            data: {
                token: this.token,
                url: this.url
            }
        })
        .then(resp => {
            // 校驗(yàn)是否解析成功
            if(resp.data.code != 200 && resp.data.msg != "OK"){
                console.log("解析失敗")
            }
            else{
                // 獲取到解析后的數(shù)據(jù)
                const data = resp.data.data
                console.log(data)
                var type = data.type  // 類型:1視頻 2圖片集
                var title = data.title  // 標(biāo)題
                var cover_url = data.cover_url  // 封面地址
                var video_url = data.video_url  // 無(wú)水印視頻地址
                var imgs = data.imgs  // 無(wú)水印圖片數(shù)組
            }
        })
    }
    // 獲取類型
    get_type(){
        if(this.url.match(/http[s]?:\/\/v\.douyin\.com\/[^ ]+/) != null){
            console.log("識(shí)別到【dy】鏈接")
            return "dy"
        }
        else if(this.url.match(/http[s]?:\/\/v\.kuaishou.com\/[^ ]+/) != null){
            console.log("識(shí)別到【ks】鏈接")
            return "ks"
        }
        else if(this.url.match(/http[s]?:\/\/xhslink\.com\/[^ ]+/) != null){
            console.log("識(shí)別到【xhs】鏈接")
            return "xhs"
        }
        else{
            console.log("未識(shí)別到鏈接類型,請(qǐng)輸入正確的鏈接")
            return null
        }
    }
    // 使用正則區(qū)分要解析的鏈接是哪個(gè)平臺(tái)的【dy、ks、xhs】
    run(url){
        // 1、把url保存給實(shí)例變量【方便后期使用】
        this.url = url
        // 1、獲取類型
        this.type = this.get_type();
        if(!this.type){
            return
        }
        // 2、調(diào)用萬(wàn)能解析
        this.parse_video()
    }
}
if(__filename === process.mainModule.filename) {
    // new一個(gè)Parse對(duì)象
    const p = new Parse()
    // 調(diào)用run方法
    p.run("https://v.douyin.com/hoDBW9H")
    p.run("https://v.kuaishou.com/C75B2q")
    p.run("http://xhslink.com/fKihbj")
}

補(bǔ)充:除了使用axios網(wǎng)絡(luò)請(qǐng)求第三方平臺(tái)交互之外,還可以使用第三方庫(kù)來(lái)實(shí)現(xiàn)去水印功能,例如使用jimp庫(kù),實(shí)例代碼如下:

const Jimp = require('jimp');

// 讀取原圖
Jimp.read('source.png').then(image => {
  // 讀取水印圖
  Jimp.read('watermark.png').then(watermark => {
    // 獲取原圖和水印圖的寬高
    const width = image.bitmap.width;
    const height = image.bitmap.height;
    const wmWidth = watermark.bitmap.width;
    const wmHeight = watermark.bitmap.height;

    // 計(jì)算水印寬高縮放比例
    const scale = width / wmWidth;

    // 縮放水印圖
    watermark.scale(scale);

    // 將水印圖繪制到原圖上
    image.composite(watermark, 0, 0, {
      mode: Jimp.BLEND_SOURCE_OVER,
      opacitySource: 1,
      opacityDest: 1
    });

    // 保存處理后的圖片
    image.write('result.png');
  });
});

相關(guān)文章

最新評(píng)論