davidteather / TikTok-Api

The Unofficial TikTok API Wrapper In Python
https://davidteather.github.io/TikTok-Api
MIT License
4.84k stars 971 forks source link

Download video failed #1092

Open cg00003 opened 10 months ago

cg00003 commented 10 months ago

async for video in tag.videos(count=10): video_id = video.id video_bytes = api.video(id=video_id).bytes() file_name = video_id + ".mp4" with open(file_name, "wb") as out: out.write(video_bytes)

Abnormal information: TypeError: a bytes-like object is required, not 'coroutine'

BoolmanO commented 10 months ago

You should use:

await video.bytes()

At this moment, the function, unfortunately, is not implemented

sonnguyenhong commented 10 months ago

Do you have any solution to download Tiktok video? @BoolmanO

smoltsbohdan commented 9 months ago

Do you find any solution?@sonnguyenhong

Chdlopezes commented 9 months ago

Same request here. Method to download video is not implemented, so please consider this issue

Coronal-Halo commented 8 months ago

Have you guys found a solution to download videos from Tiktok automatically? Does such an API exist?

juanls1 commented 7 months ago

Did you find any solution in order to download tiktok videos?

VMASPAD commented 4 months ago

IN THE CASE THAT IT BITES YOU TO INSTALL LIBRARIES LIKE PANDAS OR SOME OTHER LIBRARY, JUST INSTALL

Repository Repository

I had to install this (latest)

pip install browser_cookie3
pip install pandas

``I found the solution to the problem, I had the same errors but making some modifications and changing the type and the way in which I obtained the videos, I modified:

add two variables

        user = api.user(username)
        videos = user.videos()

change the for to iterate over the videos found in the account and then another variable


        async for video in videos:
            print(f"Username: {video.author.username}")
            print(f"Video ID: {video.id}")
            print(f"Stats: {video.stats}")

            video_url = f"https://www.tiktok.com/@{username}/video/{video.id}"

sincerely I do not know what differences there are with the other method (speaking of the variable video_url) but well it works and then I leave as this the part that downloads the video from YoutubeDL

and at the end of the code add the variable username in which will start the code that is the user that we want to download the videos, I clarify that this will download all the videos of the user from top to bottom.

if __name__ == "__main__":
    username = 'username'
    asyncio.run(download_user_videos(username))

and regarding the token, honestly I did not use the ms_token and I do not know what it is for hahahahahah xd but the token that worked for me was the "multi_sids" that is the domain tiktok.com SPECIFICALLY THAT one

code complete:

from TikTokApi import TikTokApi
from yt_dlp import YoutubeDL
import asyncio
import os

ms_token = os.environ.get("multi_sids", None) 
ydl_opts = {
    'outtmpl': '%(uploader)s_%(id)s_%(timestamp)s.%(ext)s',
}

async def download_user_videos(username):
    async with TikTokApi() as api:
        await api.create_sessions(ms_tokens=[ms_token], num_sessions=1, sleep_after=3, 
                                  headless=False, suppress_resource_load_types=["image", "media", "font", "stylesheet"])

        user = api.user(username)
        videos = user.videos()

        async for video in videos:
            print(f"Username: {video.author.username}")
            print(f"Video ID: {video.id}")
            print(f"Stats: {video.stats}")

            video_url = f"https://www.tiktok.com/@{username}/video/{video.id}"
            with YoutubeDL(ydl_opts) as ydl:
                ydl.download([video_url])

if __name__ == "__main__":
    username = 'username'
    asyncio.run(download_user_videos(username))

I clarify that the verisions of the libraries I am using are:

Name: TikTokApi
Version: 6.3.0
Name: yt-dlp
Version: 2024.5.27
tomasruizt commented 3 months ago

With this library you can download TIkTok videos in mp4 format: https://github.com/dfreelon/pyktok