Guides

How to Get Videos from TikTok Using Deno Puppeteer

Mockup a quick video API to prototype your next video app

Written

This guide is for educational purposes, TikTok please dont sue me.

Manually downloading video from TikTok can be a tiresome process, I'm gonna show you how I simplified my workflow using Puppeteer and Deno Runtime.

Share URL from ios is short but redirects

const shortUrl = "https://vm.tiktok.com/ZM8TWr9dr/";

to figure out where it goes you can run this on Deno


const shortUrl = "https://vm.tiktok.com/ZM8TWr9dr/"
try {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto(shortUrl);

  const targetUrl = await page._target._targetInfo.url;
  console.log(targetUrl);
} catch (error) {
  console.log(error);
}

Although we get redirects to another page, but we need to redirects again

We need to figure out how to do this in one go

Now we copy the new address


const targetUrl =
  "https://m.tiktok.com/v/7045493291319594242.html?_d=secCgwIARCbDRjEFSACKAESPgo8toR7%2B6WX1iW6g60W9qYmkS5LT5hDTaEfXmUv2AFeOKpYVKwMA%2F0tqxMLgAH77460toQh5kNJBdmz8UDyGgA%3D&checksum=4a2c5da41251fbd507fab89fe55fbf841baaa0056f4e39c05ac95eac40cf198f&language=en&preview_pb=0&sec_user_id=MS4wLjABAAAAVNy9nKpP9x0GjjMfbXdWQaSs797Q8_1nGRseEipDEC5w6-X4ZxP9QgO8xfXYjkKd&share_app_id=1233&share_item_id=7045493291319594242&share_link_id=2C7530A4-5EC9-497A-888D-CF8FE4A0A512&source=h5_m&timestamp=1640446651&tt_from=copy&u_code=d4e2dcehla9je7&user_id=6653577797574049797&utm_campaign=client_share&utm_medium=ios&utm_source=copy";

  try {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  await page.goto(targetUrl);
  const cleanTarget = await page._frameManager._mainFrame._url;
  console.log(cleanTarget);
  await browser.close();

  //error handling
} catch (error) {
  console.log(error);
}

That should give me the location were the full html site with the video

then we can just save it


const contenturl = "https://www.tiktok.com/@ew.go.awayyyehrhrj/video/7045493291319594242";

try {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  await page.goto(contenturl);
  const html = await page.content();
  const $ = cheerio.load(html);
  const username = $(".author-uniqueId").text();
  console.log(username);
  //
  const video = $(".video-card-one-column > video")[0].attribs.src;
  console.log(video);

  await browser.close();
  //error handling
} catch (error) {
  console.log(error);
}

Test this, it should give us the location of the video.