How to download videos?

How to download videos?

Endpoints for getting posts such as Get feed posts or the Get video information endpoint return the video(s) information in the JSON response object. 

You can download the video file using the link at the downloadAddr or playAddr field.

Example response:
JSON video response object

However if you try to directly play the video link in your browser, it might look something like this:



Or like this:



That's because TikTok links have an expiration time which makes them invalid after a certain date, and also some video links require valid cookies and headers in order to open them.
 


Save the video

You can save the videos locally before expiration or upload them somewhere else (e.g AWS S3).

In order to access the video link successfully, you must make a GET request to the link using a HTTP client library and set the right headers for the request, which are included in the JSON object response ($other -> videoLinkHeaders): 



For your convenience, there is a simple method for downloading and saving videos implemented in TikAPI's JavaScript & Python Library which does this automatically.

Example in JavaScript (the 'saveVideo' method is available on the response object):

  1. import TikAPI from 'tikapi';

    const api = TikAPI("myAPIKey");

    (async function(){
        try{
            let response = await api.public.video({
                id: "7109178205151464746"
            });

            console.log(response.json);
           
            response.saveVideo(response.json.itemInfo.itemStruct.video.downloadAddr, "video.mp4")
        }
        catch(err){
            console.log(err?.statusCode, err?.message, err?.json)
        }  
    })();

Example in Python (the 'save_video' method is available on the response object):

  1. from tikapi import TikAPI, ValidationException, ResponseException

    api = TikAPI("myAPIKey")

    try:
        response = api.public.video(
            id="7109178205151464746"
        )

        json: dict = response.json()
       
        print(json)

        response.save_video(json['itemInfo']['itemStruct']['video']['downloadAddr'], 'video.mp4')

    except ValidationException as e:
        print(e, e.field)

    except ResponseException as e:
        print(e, e.response.status_code)


    • Related Articles

    • How can I stream Live videos?

      You can easily get the Stream Key and Server parameters using the Start Live endpoint if you meet certain requirements by TikTok. The account has at least 1000 followers and Live streaming is enabled Third-Party Live streaming is enabled ...