1

I have a function for getting no watermarked video url from TikTok, but it don't work anymore.

How to get TikTok nowatermark video url, if I have video id?

def get_tiktok_video_nowatermark(url):
   headers = {
       "method": "GET",
       "accept-encoding": "utf-8",
       "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"
   }
   req = requests.get(url, headers=headers).text

   video_data = json.loads(re.findall(u"<script id=\"__NEXT_DATA__\" type=\"application/json\" crossorigin=\"anonymous\">(.*?)</script><script crossorigin=\"anonymous\" nomodule=", req)[0])

   watermark_url = video_data["props"]["pageProps"]["videoData"]["itemInfos"]["video"]["urls"][0]

   watermark_video = str(requests.get(watermark_url, headers=headers).content)

   position = int(re.search(r"vid:", watermark_video).start())

   nowatermark_url = "https://api2.musical.ly/aweme/v1/playwm/?video_id=" + watermark_video[position+4:position+36]

   return nowatermark_url
trauer
  • 11
  • 1
  • 2

1 Answers1

4
def get_tiktok_video_nowatermark(url):
   headers = {
       "method": "GET",
       "accept-encoding": "utf-8",
       "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"
   }
   req = requests.get(url, headers=headers).text

   video_data = json.loads(re.findall(u"<script id=\"__NEXT_DATA__\" type=\"application/json\" crossorigin=\"anonymous\">(.*?)</script><script crossorigin=\"anonymous\" nomodule=", req)[0])

   watermark_url = video_data["props"]["pageProps"]["videoData"]["itemInfos"]["video"]["urls"][0]

   watermark_video = str(requests.get(watermark_url, headers=headers).content)

   position = int(re.search(r"vid:", watermark_video).start())

   nowatermark_url = "https://api.tiktokv.com/aweme/v1/play/?video_id=" + watermark_video[position+4:position+36] + "&vr_type=0&is_play_url=1&source=PackSourceEnum_PUBLISH&media_type=4&ratio=default&improve_bitrate=1"

   return nowatermark_url

Also, make sure you request nonwatermark_url without any User agent header, else you will get empty result. Better off if you can make head request to the url and determine the final url which can be passed directly to the browser to play or download.

Sovit Tamrakar
  • 331
  • 1
  • 3
  • Any updates/new ways for getting (Video ID) for videos that don't include "vid:" in them? – Ibrahim Sep 25 '20 at 01:59
  • 2
    The only way right now is to use X-Gorgon & X-Khronos that is used by TikTok mobile apps to sign requests, which is quite complex for newer videos. There are few API services that can deliver non watermarked video id & urls.. you might want to look at https://rapidapi.com/wppressapi-wppressapi-default/api/tiktok-no-watermark1 – Sovit Tamrakar Sep 27 '20 at 04:50
  • Thank you Sovit! – Ibrahim Sep 28 '20 at 18:51