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

8k views Asked by At

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
1

There are 1 answers

3
Sovit Tamrakar On
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.