Today we’re going to code a fully automated youtube compilation channel with Python, and possibly make some money doing so 🙂
Videos and channels like these seem pretty simple — just some memes from online compiled together — yet they get a ton of videos and subscribers. This channel has over 401K subscribers and this video has over 22 million views.
However, making videos like this ourselves would be a lot of menial work. We would have to find all the videos, edit it into a compilation, and then post it to youtube. So, why don’t we make a computer do this all for us? Sure, let’s do it. 😌
If it works and keeps up, there’s a good chance with the frequent quality content, our channel can gain subscribers, get big, and possibly make some $$$.
If you just want to see the finished automated youtube channel, check it out here. If you just want to look at some code, here is the Github Repo.
But now, let’s get to it 🙂
For this part, we can make our compilation channel be for any topic: sports highlights, rap videos, TV show clips, etc.
However, I got some inspiration from the successful meme compilation channels on Youtube, so we’re going to make a meme compilation youtube channel. Cool!
Here, we can find memes from Twitter or Instagram. Instagram would be easier because its more centralized — we can simply find the best video memes from meme accounts.
So, the idea is to:
# scrape_videos.py scrapes all the videos from pages we are following
def scrapeVideos(username = "",
password = "",
output_folder = "",
days = 1):
print("Starting Scraping")
L = instaloader.Instaloader()
# Login or load session for loader
L.login(username, password)
profile = instaloader.Profile.from_username(L.context, username)
following = profile.get_followees()
print(following)
for profile in following:
acc = profile.username
looter = ProfileLooter(acc, videos_only=True, template="{id}-{username}-{width}-{height}")
if not looter.logged_in():
looter.login(username, password)
print("Scraping From Account: " + acc)
today = datetime.date.today()
timeframe = (today, today - dateutil.relativedelta.relativedelta(days=days))
numDowloaded = looter.download(output_folder, media_count=30, timeframe=timeframe)
print("Downloaded " + str(numDowloaded) + " videos successfully")
print("")
This function will go through the list of accounts we are following, and then download all the videos they posted in the past 1 day to an output folder.
This part is pretty simple. We have all of our videos in a folder, and we want to edit them into a single compilation. This is not that bad, as we can just put each video one after another. To do this, we can use a Python library called moviepy.
# makeCompilation takes videos in a folder and creates a compilation with max length totalVidLength
def makeCompilation(path = "./",
introName = '',
outroName = '',
totalVidLength = 10*60,
maxClipLength = 20,
minClipLength = 5,
outputFile = "output.mp4"):
allVideos = []
seenLengths = defaultdict(list)
totalLength = 0
for fileName in os.listdir(path):
if isfile(join(path, fileName)) and fileName.endswith(".mp4"):
print(fileName)
filePath = os.path.join(path, fileName)
# Destination path
clip = VideoFileClip(filePath)
clip = clip.resize(width=1920)
clip = clip.resize(height=1080)
duration = clip.duration
print(duration)
if duration <= maxClipLength and duration >= minClipLength:
allVideos.append(clip)
seenLengths[duration].append(fileName)
totalLength += duration
print("Total Length: " + str(totalLength))
random.shuffle(allVideos)
duration = 0
# Add intro vid
videos = []
if introName != '':
introVid = VideoFileClip("./" + introName)
videos.append(introVid)
duration += introVid.duration
description = ""
# Create videos
for clip in allVideos:
timeRange = generateTimeRange(duration, clip.duration)
acc = extractAcc(clip.filename)
description += timeRange + " : @" + acc + "\n"
duration += clip.duration
videos.append(clip)
print(duration)
if duration >= totalVidLength:
# Just make one video
break
# Add outro vid
if outroName != '':
outroVid = VideoFileClip("./" + outroName)
videos.append(outroVid)
finalClip = concatenate_videoclips(videos, method="compose")
audio_path = "/tmp/temoaudiofile.m4a"
#print(description)
# Create compilation
finalClip.write_videofile(outputFile, threads=8, temp_audiofile=audio_path, remove_temp=True, codec="libx264", audio_codec="aac")
return description
This function will look into the specified folder with videos, and combine them into a single video in random order. Now we have our meme compilation!
We now have post it to Youtube automatically. To do this, we have to use the Youtube API. You will have to create a new Youtube Channel and complete the API Quick Start here carefully: https://developers.google.com/youtube/v3/getting-started
Then, you can upload the video like this:
def uploadYtvid(VIDEO_FILE_NAME='',
title='Intro Video!',
description=':) ',
tags=[],
googleAPI=None):
now = datetime.datetime.now()
upload_date_time = datetime.datetime(now.year, now.month, now.day, now.hour, now.minute, int(now.second)).isoformat() + '.000Z'
request_body = {
'snippet': {
'categoryId': 23,
'title': title,
'description': description,
'tags': tags
},
'status': {
'privacyStatus': 'public',
'selfDeclaredMadeForKids': False,
},
'notifySubscribers': False
}
mediaFile = MediaFileUpload(VIDEO_FILE_NAME, chunksize=-1, resumable=True)
response_upload = googleAPI.videos().insert(
part='snippet,status',
body=request_body,
media_body=mediaFile
).execute()
"""
googleAPI.thumbnails().set(
videoId=response_upload.get('id'),
media_body=MediaFileUpload('thumbnail.png')
).execute()
"""
print("Upload Successful!")
if __name__ == "__main__":
uploadYtvid(VIDEO_FILE_NAME='./intro_vid.mp4')
When you check on Youtube, the video should be posted on your channel!
If we run this every day and keep all of our videos on our computer, we will run out of storage eventually. So, we must delete the temporary files we created (which includes the compilation). We can do this with some Python:
# Step 4: Cleanup
print("Removing temp files!")
# Delete all files made:
# Folder videoDirectory
shutil.rmtree(videoDirectory, ignore_errors=True)
# File outputFile
try:
os.remove(outputFile)
except OSError as e: ## if faile,d, report it back to the user ##
print ("Error: %s - %s." % (e.filename, e.strerror))
print("Removed temp files!")
Now, we have all the components we need. We can put it all into one function. This is main.py in the Github:
def routine():
now = datetime.datetime.now()
print(now.year, now.month, now.day, now.hour, now.minute, now.second)
title = "TRY NOT TO LAUGH (BEST Dank video memes) V1"
videoDirectory = "./Memes_" + num_to_month[now.month].upper() + "_" + str(now.year) + "_V" + str(now.day) + "/"
outputFile = "./" + num_to_month[now.month].upper() + "_" + str(now.year) + "_v" + str(now.day) + ".mp4"
#metadataFile = "./metadata-" + num_to_month[now.month].upper() + "_" + str(now.year) + "_v" + str(now.day) + ".txt"
description = ""
print(outputFile)
if not os.path.exists(videoDirectory):
os.makedirs(videoDirectory)
# Step 1: Scrape Videos
print("Scraping Videos...")
scrapeVideos(username = IG_USERNAME,
password = IG_PASSWORD,
output_folder = videoDirectory,
days=1)
print("Scraped Videos!")
description = "Enjoy the memes :) \n\n" \
"like and subscribe for more \n\n" \
# Step 2: Make Compilation
print("Making Compilation...")
makeCompilation(path = videoDirectory,
introName = INTRO_VID,
outroName = OUTRO_VID,
totalVidLength = TOTAL_VID_LENGTH,
maxClipLength = MAX_CLIP_LENGTH,
minClipLength = MIN_CLIP_LENGTH,
outputFile = outputFile)
print("Made Compilation!")
# Step 3: Upload to Youtube
print("Uploading to Youtube...")
uploadYtvid(VIDEO_FILE_NAME=outputFile,
title=title,
description=description,
googleAPI=googleAPI)
print("Uploaded To Youtube!")
# Step 4: Cleanup
print("Removing temp files!")
# Delete all files made:
# Folder videoDirectory
shutil.rmtree(videoDirectory, ignore_errors=True)
# File outputFile
try:
os.remove(outputFile)
except OSError as e: ## if failed, report it back to the user ##
print ("Error: %s - %s." % (e.filename, e.strerror))
print("Removed temp files!")
We can then call:
python3 main.py
When we set our computer to never turn off (I use my really old computer for this), a new compilation video will be posted on our channel every day!
Yeah, pretty cool and has some potential if you ask me.
Follow me for more, and again checkout the Github if you wanna: https://github.com/nathan-149/automated_youtube_channel for the full code. Feel free to follow me on Instagram @not_nang as well.
Have a gooooooood day 🙂