forked from TraySimpson/RedditVideoGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
90 lines (77 loc) · 3.18 KB
/
main.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from moviepy.editor import *
import reddit, screenshot, time, subprocess, random, configparser, sys, math
from os import listdir
from os.path import isfile, join
def createVideo():
config = configparser.ConfigParser()
config.read('config.ini')
outputDir = config["General"]["OutputDirectory"]
startTime = time.time()
# Get script from reddit
# If a post id is listed, use that. Otherwise query top posts
if (len(sys.argv) == 2):
script = reddit.getContentFromId(outputDir, sys.argv[1])
else:
postOptionCount = int(config["Reddit"]["NumberOfPostsToSelectFrom"])
script = reddit.getContent(outputDir, postOptionCount)
fileName = script.getFileName()
# Create screenshots
screenshot.getPostScreenshots(fileName, script)
# Setup background clip
bgDir = config["General"]["BackgroundDirectory"]
bgPrefix = config["General"]["BackgroundFilePrefix"]
bgFiles = [f for f in listdir(bgDir) if isfile(join(bgDir, f))]
bgCount = len(bgFiles)
bgIndex = random.randint(0, bgCount-1)
backgroundVideo = VideoFileClip(
filename=f"{bgDir}/{bgPrefix}{bgIndex}.mp4",
audio=False).subclip(0, script.getDuration())
w, h = backgroundVideo.size
def __createClip(screenShotFile, audioClip, marginSize):
imageClip = ImageClip(
screenShotFile,
duration=audioClip.duration
).set_position(("center", "center"))
imageClip = imageClip.resize(width=(w-marginSize))
videoClip = imageClip.set_audio(audioClip)
videoClip.fps = 1
return videoClip
# Create video clips
print("Editing clips together...")
clips = []
marginSize = int(config["Video"]["MarginSize"])
clips.append(__createClip(script.titleSCFile, script.titleAudioClip, marginSize))
for comment in script.frames:
clips.append(__createClip(comment.screenShotFile, comment.audioClip, marginSize))
# Merge clips into single track
contentOverlay = concatenate_videoclips(clips).set_position(("center", "center"))
# Compose background/foreground
final = CompositeVideoClip(
clips=[backgroundVideo, contentOverlay],
size=backgroundVideo.size).set_audio(contentOverlay.audio)
final.duration = script.getDuration()
final.set_fps(backgroundVideo.fps)
# Write output to file
print("Rendering final video...")
bitrate = config["Video"]["Bitrate"]
threads = config["Video"]["Threads"]
outputFile = f"{outputDir}/{fileName}.mp4"
final.write_videofile(
outputFile,
codec = 'mpeg4',
threads = threads,
bitrate = bitrate
)
print(f"Video completed in {time.time() - startTime}")
# Preview in VLC for approval before uploading
if (config["General"].getboolean("PreviewBeforeUpload")):
vlcPath = config["General"]["VLCPath"]
p = subprocess.Popen([vlcPath, outputFile])
print("Waiting for video review. Type anything to continue")
wait = input()
print("Video is ready to upload!")
print(f"Title: {script.title} File: {outputFile}")
endTime = time.time()
print(f"Total time: {endTime - startTime}")
if __name__ == "__main__":
createVideo()