Simple TikTok 🎬 video using Python

Whether I’m building a little script, a web app, or doing machine learning, Python is always a handy little language to have in my toolbox.

As is the “Pythonic” way, there’s a package for everything and anything in Python, and video is no different.


This content originally appeared on DEV Community and was authored by Kevin Naidoo

Whether I'm building a little script, a web app, or doing machine learning, Python is always a handy little language to have in my toolbox.

As is the "Pythonic" way, there's a package for everything and anything in Python, and video is no different.

In this quick guide, I'll show you how to make a simple TikTok short video with just a few lines of code.

Getting started

To get started we'll need 3 things:

Building our "movie maker" script

First, we need to load our ".gif" file:

from moviepy.editor import VideoFileClip, AudioFileClip
video_clip = VideoFileClip("./background.gif")

Next, let's load our audio file:

bg_music = AudioFileClip("./piano.mp3")

Finally, we add our sound to the GIF:

video_clip = video_clip.set_audio(bg_music)
video_clip.write_videofile("./video.mp4", codec='libx264', fps=24)
video_clip.close()
bg_music.close()

And Voila! You should now have a video file ("video.mp4") in the current directory with our final TikTok short.

Here's the full script:

from moviepy.editor import VideoFileClip, AudioFileClip

video_clip = VideoFileClip("./background.gif")
bg_music = AudioFileClip("./piano.mp3")
video_clip = video_clip.set_audio(bg_music)
video_clip.write_videofile("./video.mp4", codec='libx264', fps=24)

video_clip.close()
bg_music.close()

You can learn more about "moviepy" here: https://pypi.org/project/moviepy/

Tip: To shorten the audio so that it fits the video length:

bg_music = AudioFileClip("./piano.mp3") \
    .audio_loop(duration=video_clip.duration)


This content originally appeared on DEV Community and was authored by Kevin Naidoo


Print Share Comment Cite Upload Translate Updates
APA

Kevin Naidoo | Sciencx (2024-07-08T15:22:14+00:00) Simple TikTok 🎬 video using Python. Retrieved from https://www.scien.cx/2024/07/08/simple-tiktok-%f0%9f%8e%ac-video-using-python/

MLA
" » Simple TikTok 🎬 video using Python." Kevin Naidoo | Sciencx - Monday July 8, 2024, https://www.scien.cx/2024/07/08/simple-tiktok-%f0%9f%8e%ac-video-using-python/
HARVARD
Kevin Naidoo | Sciencx Monday July 8, 2024 » Simple TikTok 🎬 video using Python., viewed ,<https://www.scien.cx/2024/07/08/simple-tiktok-%f0%9f%8e%ac-video-using-python/>
VANCOUVER
Kevin Naidoo | Sciencx - » Simple TikTok 🎬 video using Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/08/simple-tiktok-%f0%9f%8e%ac-video-using-python/
CHICAGO
" » Simple TikTok 🎬 video using Python." Kevin Naidoo | Sciencx - Accessed . https://www.scien.cx/2024/07/08/simple-tiktok-%f0%9f%8e%ac-video-using-python/
IEEE
" » Simple TikTok 🎬 video using Python." Kevin Naidoo | Sciencx [Online]. Available: https://www.scien.cx/2024/07/08/simple-tiktok-%f0%9f%8e%ac-video-using-python/. [Accessed: ]
rf:citation
» Simple TikTok 🎬 video using Python | Kevin Naidoo | Sciencx | https://www.scien.cx/2024/07/08/simple-tiktok-%f0%9f%8e%ac-video-using-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.