node.js怎么去水印

前端开发   发布日期:2025年02月19日   浏览次数:179

这篇文章主要介绍“node.js怎么去水印”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“node.js怎么去水印”文章能帮助大家解决问题。

一、封装一个函数来识别要解析的类型

  1. // 获取类型
  2. get_type(){
  3. if(this.url.match(/http[s]?://v.douyin.com/[^ ]+/) != null){
  4. console.log("识别到【dy】链接")
  5. return "dy"
  6. }
  7. else if(this.url.match(/http[s]?://v.kuaishou.com/[^ ]+/) != null){
  8. console.log("识别到【ks】链接")
  9. return "ks"
  10. }
  11. else if(this.url.match(/http[s]?://xhslink.com/[^ ]+/) != null){
  12. console.log("识别到【xhs】链接")
  13. return "xhs"
  14. }
  15. else{
  16. console.log("未识别到链接类型,请输入正确的链接")
  17. return null
  18. }
  19. }

二、在初始化方法中写入本实例共用的数据

  1. // 初始化方法
  2. constructor() {
  3. this.token = "Z1QljZOZiT4NTG" // token
  4. // 请求地址数组对象
  5. this.req_urls = {
  6. dy: "http://api.txapi.cn/v1/parse_short_video/dy",
  7. ks: "http://api.txapi.cn/v1/parse_short_video/ks",
  8. xhs: "http://api.txapi.cn/v1/parse_short_video/xhs",
  9. }
  10. this.url = '' // 要解析的地址
  11. this.type = '' // 用来存储识别到的类型
  12. }

三、封装一个“万能解析”的方法

  1. // 万能解析
  2. parse_video(){
  3. axios({
  4. url: this.req_urls[this.type],
  5. method: 'POST',
  6. headers: {
  7. 'Content-Type': "application/x-www-form-urlencoded"
  8. },
  9. responseType: 'json',
  10. data: {
  11. token: this.token,
  12. url: this.url
  13. }
  14. })
  15. .then(resp => {
  16. // 校验是否解析成功
  17. if(resp.data.code != 200 && resp.data.msg != "OK"){
  18. console.log("解析失败")
  19. }
  20. else{
  21. // 获取到解析后的数据
  22. const data = resp.data.data
  23. console.log(data)
  24. var type = data.type // 类型:1视频 2图片集
  25. var title = data.title // 标题
  26. var cover_url = data.cover_url // 封面地址
  27. var video_url = data.video_url // 无水印视频地址
  28. var imgs = data.imgs // 无水印图片数组
  29. }
  30. })
  31. }

废话不多说 直接上完整代码????

  1. const axios = require('axios')
  2. class Parse{
  3. // 初始化方法
  4. constructor() {
  5. this.token = "Z1QljZOZiT4NTG" // token
  6. // 请求地址数组对象
  7. this.req_urls = {
  8. dy: "http://api.txapi.cn/v1/parse_short_video/dy",
  9. ks: "http://api.txapi.cn/v1/parse_short_video/ks",
  10. xhs: "http://api.txapi.cn/v1/parse_short_video/xhs",
  11. }
  12. this.url = '' // 要解析的地址
  13. this.type = '' // 用来存储识别到的类型
  14. }
  15. // 万能解析
  16. parse_video(){
  17. axios({
  18. url: this.req_urls[this.type],
  19. method: 'POST',
  20. headers: {
  21. 'Content-Type': "application/x-www-form-urlencoded"
  22. },
  23. responseType: 'json',
  24. data: {
  25. token: this.token,
  26. url: this.url
  27. }
  28. })
  29. .then(resp => {
  30. // 校验是否解析成功
  31. if(resp.data.code != 200 && resp.data.msg != "OK"){
  32. console.log("解析失败")
  33. }
  34. else{
  35. // 获取到解析后的数据
  36. const data = resp.data.data
  37. console.log(data)
  38. var type = data.type // 类型:1视频 2图片集
  39. var title = data.title // 标题
  40. var cover_url = data.cover_url // 封面地址
  41. var video_url = data.video_url // 无水印视频地址
  42. var imgs = data.imgs // 无水印图片数组
  43. }
  44. })
  45. }
  46. // 获取类型
  47. get_type(){
  48. if(this.url.match(/http[s]?://v.douyin.com/[^ ]+/) != null){
  49. console.log("识别到【dy】链接")
  50. return "dy"
  51. }
  52. else if(this.url.match(/http[s]?://v.kuaishou.com/[^ ]+/) != null){
  53. console.log("识别到【ks】链接")
  54. return "ks"
  55. }
  56. else if(this.url.match(/http[s]?://xhslink.com/[^ ]+/) != null){
  57. console.log("识别到【xhs】链接")
  58. return "xhs"
  59. }
  60. else{
  61. console.log("未识别到链接类型,请输入正确的链接")
  62. return null
  63. }
  64. }
  65. // 使用正则区分要解析的链接是哪个平台的【dy、ks、xhs】
  66. run(url){
  67. // 1、把url保存给实例变量【方便后期使用】
  68. this.url = url
  69. // 1、获取类型
  70. this.type = this.get_type();
  71. if(!this.type){
  72. return
  73. }
  74. // 2、调用万能解析
  75. this.parse_video()
  76. }
  77. }
  78. if(__filename === process.mainModule.filename) {
  79. // new一个Parse对象
  80. const p = new Parse()
  81. // 调用run方法
  82. p.run("https://v.douyin.com/hoDBW9H")
  83. p.run("https://v.kuaishou.com/C75B2q")
  84. p.run("http://xhslink.com/fKihbj")
  85. }

补充:除了使用axios网络请求第三方平台交互之外,还可以使用第三方库来实现去水印功能,例如使用jimp库,实例代码如下:

  1. const Jimp = require('jimp');
  2. // 读取原图
  3. Jimp.read('source.png').then(image => {
  4. // 读取水印图
  5. Jimp.read('watermark.png').then(watermark => {
  6. // 获取原图和水印图的宽高
  7. const width = image.bitmap.width;
  8. const height = image.bitmap.height;
  9. const wmWidth = watermark.bitmap.width;
  10. const wmHeight = watermark.bitmap.height;
  11. // 计算水印宽高缩放比例
  12. const scale = width / wmWidth;
  13. // 缩放水印图
  14. watermark.scale(scale);
  15. // 将水印图绘制到原图上
  16. image.composite(watermark, 0, 0, {
  17. mode: Jimp.BLEND_SOURCE_OVER,
  18. opacitySource: 1,
  19. opacityDest: 1
  20. });
  21. // 保存处理后的图片
  22. image.write('result.png');
  23. });
  24. });

以上就是node.js怎么去水印的详细内容,更多关于node.js怎么去水印的资料请关注九品源码其它相关文章!