欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

抖音视频去水印小程序+tp5+mysql

程序员文章站 2022-04-11 15:51:38
...

抖音视频去水印小程序+tp5+mysql

小程序端代码
wxml

<view class="page-body">
  <view></view>
  <form bindsubmit="formSubmit">
    <view class="section">
      <textarea class="intro" name="url" placeholder="请粘贴链接(禁止商用)" model:value="{{chosen}}" />
    </view>
    <view class="button-sp-area">
      <button class="mini-btn" type="primary" form-type='submit' size="mini">提交</button>
      <button class="mini-btn" type="default" form-type='reset' size="mini">重置</button>
    </view>
  </form>
  <view bindtap="copy">{{str.linkurl}}</view>
  <view class="juzhong">
    <navigator url="/pages/viewlist/viewlist" hover-class="navigator-hover">前往列表页</navigator>
  </view>
</view>

js

formSubmit(e) {
    const value = e.detail.value.url
    //console.log(app.globalData.url)
    var reg = /https:\/\/v.douyin.com/
    if (!reg.test(value)) {
      wx.showToast({
        title: '提交链接不正确',
        icon: 'none',
        duration: 2000
      })
      this.setData({
        chosen: ''
      })
      return false;
    }
    //var value = "5.8 Ag:/ 抓到你了 小家伙%高跟鞋 %高跟鞋卡点 %抓到你了  https://v.douyin.com/eVnWmy2/ 复製此lian接,打開Dou搜索,直接观kan视频!";

    var first = value.indexOf('https') //找到第一次出现下划线的位置
    var str_after = value.substring(first) //取first后的字符
    var last = str_after.lastIndexOf('/') //取/最后一次出现的字符
    var str_before = str_after.substring(0, last) //取/前的字符
    console.log(str_before)
    const _this = this;
    wx.request({
      url: app.globalData.url + '/home/Index/videoadd',
      method: 'POST',
      data: {
        url: str_before
      },
      header: {
        "Content-Type": "json"
      },
      success: function (res) {
        console.log(res.data)
        if (res.data.code == 200) {
          // _this.setData({
          //   //str: res.data
            
          // })
          wx.showLoading({
            title: '下载中',
          })
          let fileName = res.data.title;
          wx.downloadFile({
            url: res.data.linkurl,
            
            filePath: wx.env.USER_DATA_PATH + '/' + fileName + '.mp4',
            success: res => {
              console.log(res);
              let filePath = res.filePath;
              wx.saveVideoToPhotosAlbum({
                filePath,
                success: file => {
                  wx.showToast({
                    title: '下载成功',
                    icon: 'success',
                    duration: 2000
                  })
                  let fileMgr = wx.getFileSystemManager();
                  fileMgr.unlink({
                    filePath: wx.env.USER_DATA_PATH + '/' + fileName + '.mp4',
                    success: function (r) {
      
                    },
                  })
                },
                fail: err => {
                  console.log(err)
                  if (err.errMsg === 'saveVideoToPhotosAlbum:fail auth deny') {
                    wx.showModal({
                      title: '提示',
                      content: '需要您授权保存相册',
                      showCancel: false,
                      success: data => {
                        wx.openSetting({
                          success(settingdata) {
                            if (settingdata.authSetting['scope.writePhotosAlbum']) {
                              wx.showModal({
                                title: '提示',
                                content: '获取权限成功,再次点击下载即可保存',
                                showCancel: false,
                              })
                            } else {
                              wx.showModal({
                                title: '提示',
                                content: '获取权限失败,将无法保存到相册哦~',
                                showCancel: false,
                              })
                            }
                          },
                        })
                      }
                    })
                  }
                }
              })
            }
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail: function (error) {
        // that.toast("请下拉刷新在试下")
        callBack("400")
      }
    })
  },

PHP

public function videoadd()
    {
        if (Request::instance()->isPOST())
        {
            header("Content-Type: text/json;charset=utf-8");
            $url = Request::instance()->post('url');
            if(empty($url)){
                exit(json_encode(array('code'=>400,'msg'=>'链接错误请重试')));
            }else{
                $res = getParams($url);
                if($res == false){
                    exit(json_encode(array('code'=>400,'msg'=>'此视频已存在请前往列表下载')));
                }
            }


        }
    }
    function getParams($url){
    $UserAgent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
    $curl      = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_ENCODING, '');
    curl_setopt($curl, CURLOPT_USERAGENT, $UserAgent);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);
    $data = curl_exec($curl);
    curl_close($curl);
//     echo $data;
    preg_match('/video\/(.*?)\/.*?mid=(.*?)&/i', $data, $result);
    // print_r($result);
    sleep(1);
//    halt($result);
    getItemInfo($result[1],$result[2]);
}

function getItemInfo($item_id, $mid) {
    $find = Db::name('videolist')->where('item_id',$item_id)->find();
    if(!empty($find)){
        return false;
    }
    $url = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=".$item_id;
    $headers =[
        'authority'=> 'www.iesdouyin.com',
        'user-agent'=> 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1',
        'content-type'=> 'application/x-www-form-urlencoded',
        'accept'=> '*/*',
        'referer'=> 'https://www.iesdouyin.com/share/video/'.$item_id.'/?region=CN&mid='.$mid.'&u_code=15b9142gf&titleType=title&utm_source=copy_link&utm_campaign=client_share&utm_medium=android&app=aweme',
        'accept-language'=> 'zh-CN,zh;q=0.9,en-GB;q=0.8,en;q=0.7'
    ];
    $UserAgent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 3.5.21022; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
    $curl      = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_HEADER, 0);//返回response头部信息
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_USERAGENT, $UserAgent);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);
    $data = curl_exec($curl);
    // print_r($data);
    // die;
    curl_close($curl);
    $json=json_decode($data,true);
    // print_r($json['item_list'][0]["video"]["play_addr"]["url_list"][0]);
    $name=$json['item_list'][0]["share_info"]["share_title"];
    $video=$json['item_list'][0]["video"]["play_addr"]["url_list"][0];
    $video=str_replace("playwm","play",$video);

    download($name,$video);
    //返回本地路径地址
    $time = date('Ymd',time());
    $re=str_replace(" ",'',$name);
    $arr = array(
        "#" => "",
        "@" => "",
        "$" => "",
        "&" => "",
        "*" => "",
        "," => "",
        "?" => "",
        "," => "",
    );
    $reS=strtr($re,$arr);
    $str =  DS .'uploads'. DS.$time. DS.$reS.'.mp4';
    //获取当前域名
    $request = Request::instance();
    $domain = $request->domain();
    $lujing = str_replace('\\','/',$domain.DS.$str);
    videoAdd($reS,$item_id,$mid,$str);

    exit(json_encode(array('code'=>200,'linkurl'=>$lujing,'title'=>$reS)));
}

function download($name,$video){

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $video);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1");
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    $data = curl_exec($curl);
    curl_close($curl);
    //日期文件夹
    $time = date('Ymd',time());
    $path = ROOT_PATH."public". DS .'uploads'. DS.$time;
    if (!file_exists($path)){
        mkdir($path);
    }
    $re=str_replace(" ",'',$name);
    $arr = array(
        "#" => "",
        "@" => "",
        "$" => "",
        "&" => "",
        "*" => "",
        "," => "",
        "?" => "",
        "," => "",
    );
    $str=strtr($re,$arr);
    $news_path = $path. DS . $str . '.mp4';
    //保存到本地
    file_put_contents($news_path, $data);
}

代码包去抖音视频水印.zip