Welcome back to another installment of the "Project" series, where I delve into the technical components of the projects I've undertaken. I apologize for the delays in my posts; the past few weeks have been hectic with school, co-op applications, and just life (haha).
This week, I'll discuss a personal project I made using MoviePy, the YouTube API, and the OpenAI API. If you're unfamiliar with me, please refer to my introductory post. ๐
As a personal project, I worked on a tool called the "Video Content Aggregator". This tool, combining the YouTube API, OpenAI API, and MoviePy, aids in the creation of dynamic video content in the form of YouTube Shorts. I leaned on a mix of contemporary APIs and MoviePy to ensure the tool's functionality and reliability. Here's a quick overview of the technologies used:
APIs: YouTube, OpenAI, Pexels
Language: Python
Video Editing: MoviePy
Code review ๐
Requirements โ๏ธ
The code utilizes several libraries including moviepy, google-api-python-client, openai, gtts, etc. You should ensure that you have these libraries installed, along with their dependencies.
Ensure you have the right API keys and the environment variables are set correctly.
Ensure you have ffmpeg installed as it's a prerequisite for moviepy.
You can check the entire list of requirements at requirements.txt.
OpenAI API ๐ค
The code interacts with the OpenAI API to generate a fact, a title based on the fact, and a main subject noun based on the fact.
These are three distinct interactions with the API: Fact, Title, and Subject noun, which are important components in fetching video data, aggregation, generation, and upload to YouTube.
defgenerate_fact():prompt="Give me around 75 words based on an interesting fact."response=openai.Completion.create(engine="text-davinci-003",prompt=prompt,max_tokens=200)returnresponse.choices[0].text.strip()defgenerate_title(fact):prompt=f"Based on the generated fact, {fact}, return a short title for the video."response=openai.Completion.create(engine="text-davinci-003",prompt=prompt,max_tokens=30)video_title=response.choices[0].text.strip()returnvideo_titledefgenerate_subject_noun(fact):prompt=f"Based on the generated fact, {fact}, return a main subject noun."response=openai.Completion.create(engine="text-davinci-003",prompt=prompt,max_tokens=30)returnresponse.choices[0].text.strip()
Fetch Video API Calls ๐ฅ
The code fetches videos from two sources: Pexels and YouTube.
Each fetch function returns up to three videos based on a keyword derived from the generated fact. The keyword is sourced from the generate_subject_noun(fact).
deftest_fetch_pexels_videos():videos=fetch_pexels_videos("earth")assertvideos,"Failed to fetch videos from Pexels"deftest_fetch_youtube_videos():videos=fetch_youtube_videos("earth")assertvideos,"Failed to fetch videos from YouTube"
Aggregate and Generate Videos ๐ฌ
The code processes the fetched videos and compiles them, adding subtitles and adjusting their lengths. The videos are resized and cropped.
Audio from Google TTS is used as the background for the video.
The code uses YouTube's v3 API to authenticate and upload videos.
The video is uploaded with a title derived from the filename and a pre-defined description.
I should prioritize restructuring the current linear code to be more modular, breaking tasks down into smaller functions. Handling potential errors is essential, especially in cases where video fetching might yield no results or if there's an unexpected response from the OpenAI API. It's also crucial to manage rate limits and any potential hiccups when interfacing with third-party APIs. Given the concurrent nature of the video processing, I must ensure all resources, like video clips, are appropriately closed after use. Lastly, implementing error handling mechanisms, particularly try-except blocks, in the critical sections will be essential to maintain the stability of the program.
Conclusion โณ๏ธ
Thank you for taking the time to read and I would appreciate any suggestions to improve the quality of my blog. Stay tuned for future posts. ๐ค
Check out the YouTube channel!
Check out the project embedded repository below! ๐
Generate engaging video content utilizing the YouTube API, OpenAI API, and MoviePy. Harness the power of AI and Python automation to craft dynamic YouTube Shorts ๐ฌ