溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

node.js怎么去水印

發(fā)布時(shí)間:2023-04-15 10:51:16 來(lái)源:億速云 閱讀:112 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹“node.js怎么去水印”的相關(guān)知識(shí),小編通過(guò)實(shí)際案例向大家展示操作過(guò)程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“node.js怎么去水印”文章能幫助大家解決問(wè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
    }
}

二、在初始化方法中寫(xiě)入本實(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)于“node.js怎么去水印”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI