[tiktok] add 'audio' option (#7060)

This commit is contained in:
Mike Fährmann
2025-02-26 20:51:23 +01:00
parent 13c3fa45f7
commit 5e87aee32d
4 changed files with 41 additions and 10 deletions

View File

@@ -4316,6 +4316,16 @@ Description
and click ``Create Token``. and click ``Create Token``.
extractor.tiktok.audio
----------------------
Type
``bool``
Default
``true``
Description
Download audio tracks using |ytdl|.
extractor.tiktok.videos extractor.tiktok.videos
----------------------- -----------------------
Type Type

View File

@@ -595,6 +595,7 @@
}, },
"tiktok": "tiktok":
{ {
"audio" : true,
"videos": true, "videos": true,
"user": { "user": {

View File

@@ -22,11 +22,14 @@ class TiktokExtractor(Extractor):
root = "https://www.tiktok.com" root = "https://www.tiktok.com"
cookies_domain = ".tiktok.com" cookies_domain = ".tiktok.com"
def _init(self):
self.audio = self.config("audio", True)
self.videos = self.config("videos", True)
def avatar(self): def avatar(self):
return "" return ""
def items(self): def items(self):
videos = self.config("videos", True)
# We assume that all of the URLs served by urls() come from the same # We assume that all of the URLs served by urls() come from the same
# author. # author.
downloaded_avatar = not self.avatar() downloaded_avatar = not self.avatar()
@@ -50,8 +53,6 @@ class TiktokExtractor(Extractor):
post["user"] = user = author["uniqueId"] post["user"] = user = author["uniqueId"]
post["date"] = text.parse_timestamp(post["createTime"]) post["date"] = text.parse_timestamp(post["createTime"])
original_title = title = post["desc"] original_title = title = post["desc"]
if not title:
title = "TikTok photo #{}".format(post["id"])
if not downloaded_avatar: if not downloaded_avatar:
avatar_url = author["avatarLarger"] avatar_url = author["avatarLarger"]
@@ -62,7 +63,11 @@ class TiktokExtractor(Extractor):
downloaded_avatar = True downloaded_avatar = True
yield Message.Directory, post yield Message.Directory, post
ytdl_media = False
if "imagePost" in post: if "imagePost" in post:
if not original_title:
title = "TikTok photo #{}".format(post["id"])
img_list = post["imagePost"]["images"] img_list = post["imagePost"]["images"]
for i, img in enumerate(img_list, 1): for i, img in enumerate(img_list, 1):
url = img["imageURL"]["urlList"][0] url = img["imageURL"]["urlList"][0]
@@ -78,19 +83,23 @@ class TiktokExtractor(Extractor):
}) })
yield Message.Url, url, post yield Message.Url, url, post
elif videos: if self.audio and "music" in post:
if not original_title: ytdl_media = "audio"
title = "TikTok video #{}".format(post["id"])
elif self.video and "video" in post:
ytdl_media = "video"
else: else:
self.log.info("%s: Skipping post", tiktok_url) self.log.info("%s: Skipping post", tiktok_url)
if videos: if ytdl_media:
if not original_title:
title = "TikTok {} #{}".format(ytdl_media, post["id"])
post.update({ post.update({
"type" : "video", "type" : ytdl_media,
"image" : None, "image" : None,
"filename" : "", "filename" : "",
"extension" : "mp4", "extension" : "mp3" if ytdl_media == "audio" else "mp4",
"title" : title, "title" : title,
"num" : 0, "num" : 0,
"img_id" : "", "img_id" : "",

View File

@@ -124,8 +124,19 @@ __tests__ = (
"#comment" : "www.tiktokv.com link: many photos with audio", "#comment" : "www.tiktokv.com link: many photos with audio",
"#category" : ("", "tiktok", "post"), "#category" : ("", "tiktok", "post"),
"#class" : tiktok.TiktokPostExtractor, "#class" : tiktok.TiktokPostExtractor,
"#options" : {"audio": True},
"#pattern" : PATTERN_WITH_AUDIO, "#pattern" : PATTERN_WITH_AUDIO,
"#options" : {"videos": True}, "#count" : 17,
},
{
"#url" : "https://www.tiktokv.com/share/video/7240568259186019630",
"#comment" : "www.tiktokv.com link: many photos with audio disabled",
"#category" : ("", "tiktok", "post"),
"#class" : tiktok.TiktokPostExtractor,
"#options" : {"audio": False},
"#pattern" : PATTERN,
"#count" : 16,
}, },
{ {