-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_2.py
More file actions
51 lines (44 loc) · 1.72 KB
/
task_2.py
File metadata and controls
51 lines (44 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""Task 2. Module to convert TikTok video to GIF."""
import os
from TikTokApi import TikTokApi
from moviepy.editor import VideoFileClip
# https://www.youtube.com/shorts/NC4G_WnuKYs
# "https://vm.tiktok.com/ZMNqhVbcV/?k=1"
def tiktok_video_to_gif(url):
"""function that converts TikTok video to GIF"""
# Downloading TikTok video to local PC
# Connecting via TikTokApi
with TikTokApi() as api:
print("Downloading video from TikTok.......")
video = api.video(url=url)
video_data = video.bytes()
# Unique video file name using TikTok video id
video_name = video.info()['id']
# Writing video to file
with open(video_name, "wb") as out_file:
out_file.write(video_data)
print("Download completed!")
# Converting downloaded video to GIF
# Using TikTok id for the name of .gif file
gif_name = video_name + ".gif"
video_clip = VideoFileClip(video_name)
video_clip.write_gif(gif_name)
print("Converting video to GIF completed!")
# Removing the video file
os.remove(video_name)
print("Path of the created GIF: " + os.path.abspath(gif_name))
return os.path.abspath(gif_name)
def main():
"""main() function"""
user_url = input("Enter the TikTok video url:\n")
try:
tiktok_video_to_gif(user_url)
# TypeError will occur if the link is not broken, but is not TikTok video link
except TypeError:
print("Entered URL is not TikTok video URL. Below is an example of a supported URL.\nhttps://www.tiktok.com/@therock/video/6829267836783971589"
)
# Exception for broken links
except:
print(
f"Connection error. Please, check the link {user_url} for mistakes.")
main()