Skip to content
This repository has been archived by the owner on Jun 7, 2022. It is now read-only.

Commit

Permalink
Update make_compilation.py
Browse files Browse the repository at this point in the history
Auto/Manual mode in make_compilation Script, 
Auto description generation (TimeStamps, Credits, Caption, etc)
Minor Bug Fixes
  • Loading branch information
sam5epi0l authored Jan 15, 2022
1 parent 94f1161 commit db2a1a0
Showing 1 changed file with 73 additions and 51 deletions.
124 changes: 73 additions & 51 deletions make_compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import random
import shutil
from collections import defaultdict
import json

VideoFileClip.resize = resize

Expand All @@ -16,84 +17,102 @@ def extractAcc(filepath):
except:
return ""

# generateTimeRange converts float seconds to a range of form @MM:SS
def generateTimeRange(duration, clipDuration):
preHour = int(duration / 60)
preMin = int(duration % 60)
preTime = str(preHour // 10) + str(preHour % 10) + ":" + str(preMin // 10) + str(preMin % 10)

duration += clipDuration
postHour = int(duration / 60)
postMin = int(duration % 60)
postTime = str(postHour // 10) + str(postHour % 10) + ":" + str(postMin // 10) + str(postMin % 10)

#return "@" + preTime + " - " + "@" + postTime
return "@" + preTime

# 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"):
outputFile = "output.mp4",
video_source_meta = {},
videoDirectory = "",
description_meta = "",
modeAM = "A"):

allVideos = []
seenLengths = defaultdict(list)
totalLength = 0
duration = 0
videos = []

# Add intro video if included
if introName != '':
introVid = VideoFileClip("./" + introName)
videos.append(introVid)
duration += introVid.duration


for fileName in os.listdir(path):

filePath = join(path, fileName);
filePath = join(path, fileName)
if isfile(filePath) and fileName.endswith(".mp4"):
print(fileName)

if os.stat(filePath).st_size < 5000:
continue

# Destination path
# Destination path
clip = VideoFileClip(filePath)
clip = clip.resize(width=1920)
clip = clip.resize(height=1080)
duration = clip.duration
print(duration)
# Commit
if duration <= maxClipLength and duration >= minClipLength:
print(fileName + " " + str(duration) + " Added")

# add_video in min&max range or ignore errors
def add_video():
allVideos.append(clip)
seenLengths[duration].append(fileName)
totalLength += duration
else:
ignore_error = input("Do You want to ignore errors on max & min Clip Length (Y/n):")
if ignore_error == "n":
print("Stopping Script Due to errors")
Break

if modeAM == "A":
add_video()
elif modeAM == "M":
if duration <= maxClipLength and duration >= minClipLength:
add_video()
else:
print("Continue with errors. RegardLess of Clip Length")
allVideos.append(clip)
seenLengths[duration].append(fileName)
totalLength += duration

print("Total Length: " + str(totalLength))
ignore_error = input("Do you want to ignore Errors in min max Total Video Length?(Y/n)").strip()
if ignore_error != "n":
pass
else:
add_video()

random.shuffle(allVideos)
#Add automated description

for k in range(len(os.listdir(path))):

fileNameJ = fileName.split(".mp4")
fileNameJSON = ''.join(fileNameJ) + ".json"

acc = extractAcc(clip.filename)

# Fix error in TimeStamps
duration_in_min = str(duration).split(".")
duration_in_min.pop()
video_source_meta[f"TimeStamps{k}"] = "00:" + str(duration_in_min[0]) + " : @" + acc + "\n"

video_source_meta[f"profile{k}"] = "Instagram profile:" + " instagram.com/" + acc +'\n'

#extract url & other information about video

f = open(f"{videoDirectory}{fileNameJSON}", "r")
json_d = json.loads(f.read())
f.close()

video_source_meta[f"vido_url{k}"] = "Video URL:" + "instagram.com/tv/" + json_d["shortcode"] + '\n'
video_source_meta[f"Caption{k}"] = json_d["edge_media_to_caption"]["edges"][0]["node"]["text"] + '\n'

description_meta = video_source_meta[f"TimeStamps{k}"] + video_source_meta[f"profile{k}"] + video_source_meta[f"vido_url{k}"] + video_source_meta[f"Caption{k}"]

print(description_meta)

print("Total Length: " + str(totalLength))

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
Expand All @@ -107,16 +126,19 @@ def makeCompilation(path = "./",

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
return description_meta

if __name__ == "__main__":
makeCompilation(path = "/Users/nathanan/Documents/YOUTUBE/AutomatedChannel/Videos/Memes/",
makeCompilation(path = "/home/kali/Documents/YOUTUBE/AutomatedChannel/Videos/Memes/",
introName = "intro_vid.mp4",
outroName = '',
totalVidLength = 10*60,
maxClipLength = 20,
outputFile = "outputseq.mp4")
outputFile = "outputseq.mp4",
video_source_meta = {},
videoDirectory = "",
description_meta = "",
modeAM = "A")

0 comments on commit db2a1a0

Please sign in to comment.