Skip to content

Commit

Permalink
Added a video download function for nonslideshows
Browse files Browse the repository at this point in the history
  • Loading branch information
Russell-Newton committed Nov 19, 2023
1 parent b1c6e47 commit ca5cc9d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
6 changes: 6 additions & 0 deletions docs/source/users/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ Download Videos and Slideshows
If all you want to do is download a video or slideshow from TikTok, go no further. Because slideshows are saved as
images with a sound, you'll need to join these images together with the sound. I'd suggest using `ffmpeg`_ for this:

.. note::
The ``download()`` function on videos can be used to download videos to a file. It doesn't work for slideshows.
It requires the ``yt-dlp`` package to be installed, which can be installed with ``pip install yt-dlp`` or
``pip install tiktokapipy[download]``.


.. code-block:: py
import asyncio
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ docs = [
"sphinx-tabs",
"mock",
]
download = [
"yt-dlp"
]

[project.urls]
"Homepage" = "https://github.com/Russell-Newton/TikTokPy"
Expand Down
40 changes: 38 additions & 2 deletions src/tiktokapipy/models/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from typing import Any, ForwardRef, List, Optional, Union

from playwright.async_api import BrowserContext as AsyncBrowserContext
from pydantic import Field, computed_field
from pydantic import AliasChoices, Field, computed_field
from tiktokapipy import TikTokAPIError
from tiktokapipy.models import CamelCaseModel, TitleCaseModel
from tiktokapipy.util.deferred_collectors import (
Expand Down Expand Up @@ -101,7 +101,7 @@ class ImagePost(CamelCaseModel):


class LightVideo(CamelCaseModel):
id: int = Field(alias="id")
id: int = Field(validation_alias=AliasChoices("cid", "uid", "id"))
stats: VideoStats
create_time: datetime

Expand All @@ -115,6 +115,8 @@ class Video(LightVideo):
digged: bool
item_comment_status: int
author: Union[LightUser, str]
image_post: Optional[ImagePost] = None
"""The images in the video if the video is a slideshow"""

@computed_field(repr=False)
@property
Expand Down Expand Up @@ -170,6 +172,40 @@ def creator(self) -> Union[DeferredUserGetterAsync, DeferredUserGetterSync]:
def url(self) -> str:
return video_link(self.id)

def download(self) -> str:
"""
Downloads this video, returning the relative filepath where it was stored.
Requires yt-dlp installed (``pip install yt-dlp``
or ``pip install tiktokapipy[download]``)
"""
if self.image_post:
raise TikTokAPIError(
"The download function isn't available for slideshows."
)

try:
import yt_dlp
except ImportError:
raise TikTokAPIError(
"You don't have youtube_dl installed! "
"Please install with `pip install yt-dlp` or "
"`pip install tiktokapipy[download]"
)

downloaded_file = ""

class GetFileNamePP(yt_dlp.postprocessor.PostProcessor):
def run(self, info):
nonlocal downloaded_file
downloaded_file = info["filename"]
return [], info

with yt_dlp.YoutubeDL() as ydl:
ydl.add_post_processor(GetFileNamePP())
ydl.download([video_link(self.id)])

return downloaded_file


del Challenge, LightChallenge, Comment, LightUser, User, UserStats
from tiktokapipy.models.challenge import Challenge, LightChallenge # noqa E402
Expand Down

0 comments on commit ca5cc9d

Please sign in to comment.