forked from chris0piper/DotaClipzVideoCreator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
video_editer.py
144 lines (109 loc) · 5.59 KB
/
video_editer.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import time
import subprocess
import logging
import moviepy.editor as mp
from moviepy.editor import *
import os
import ffmpeg
import string
from PIL import Image, ImageDraw
import urllib.request
import cv2
import numpy as np
import requests
class VideoEditer:
def __init__(self):
logging.basicConfig(filename='logs/editor.log', filemode='w', format='%(asctime)s %(levelname)-8s %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
logging.getLogger().setLevel(logging.INFO)
def createClip(self, timestamp, fileId):
videoFileName = 'rawVideos/video.mp4'
audioFileName = 'rawVideos/audio.mp4'
videoClipFileName = 'videoclips/videoClip{}.mp4'.format(fileId)
audioClipFileName = 'audioclips/audioClip{}.wav'.format(fileId)
start = time.time()
fullVideo = VideoFileClip(videoFileName)
fullAudio = AudioFileClip(audioFileName)
# SPLIT THE VIDEO
videoClip = fullVideo.subclip(timestamp[0], timestamp[1])
audioClip = fullAudio.subclip(timestamp[0], timestamp[1])
# ADD THE LOGO
# load the logo at the correct length
logo = (mp.ImageClip("logos/DotaClipsLogoXWide.jpg")
.set_duration(videoClip.duration)
.resize(height=65)
.set_pos(("right","top")))
videoClip = mp.CompositeVideoClip([videoClip, logo])
# ADD AUDIO TO CLIP
videoClip = videoClip.set_audio(audioClip)
# save this subclip
videoClip.write_videofile(videoClipFileName)
# This tests to see if its a valid mp4 after these changes
# error will be caught outside of this method
vid = cv2.VideoCapture(videoClipFileName)
end = time.time()
logging.info('Created {} in {} seconds'.format(videoClipFileName, int(end - start)))
def combineClips(self, finalFilename):
# write all files in dir to .txt
with open('videoclips/sorted.txt', 'w') as f:
filenames = os.listdir('videoclips/')
for filename in filenames:
if('sorted' in filename):
continue
f.write('file {}\n'.format(filename))
f.close()
# combine all the files in the dir
os.chdir('/home/ec2-user/workspace/Twitch2Youtube/videoclips/')
combineFiles = ['ffmpeg -f concat -i sorted.txt -c copy \"/home/ec2-user/workspace/Twitch2Youtube/finishedVideos/{}\"'.format(finalFilename)]
subprocess.run(combineFiles, shell=True)
os.chdir('/home/ec2-user/workspace/Twitch2Youtube/')
def createThumbnail(self, urlLeft, urlRight, gameNumber, outputFileName):
# download the team logos
img_data = requests.get(urlLeft).content
with open('thumbnailCreation/leftTeam.png', 'wb') as handler:
handler.write(img_data)
img_data = requests.get(urlRight).content
with open('thumbnailCreation/rightTeam.png', 'wb') as handler:
handler.write(img_data)
#Read the three images
image1 = Image.open('thumbnailCreation/leftTeam.png')
image2 = Image.open('thumbnailCreation/rightTeam.png')
background = Image.open('logos/background{}.jpeg'.format(gameNumber))
size1 = image1.size
size2 = image2.size
background_size = background.size
# get two points from each line in the X shape of the background to center/size the team logos perfectly
topLeftPoint1 = [880, 0]
topLeftPoint2 = [background_size[0] - 1925,background_size[1]]
botLeftPoint1 = [880,background_size[1]]
botLeftPoint2 = [background_size[0] - 1925,0]
topRightPoint1 = [background_size[0] - 880, 0]
topRightPoint2 = [1925, background_size[1]]
botRightPoint1 = [background_size[0] - 880, background_size[1]]
botRightPoint2 = [1925, 0]
# y2-y1 / x2-x1
topLeftm = (topLeftPoint2[1] - topLeftPoint1[1]) / (topLeftPoint2[0] - topLeftPoint1[0])
botLeftm = (botLeftPoint2[1] - botLeftPoint1[1]) / (botLeftPoint2[0] - botLeftPoint1[0])
topRightm = (topRightPoint2[1] - topRightPoint1[1]) / (topRightPoint2[0] - topRightPoint1[0])
botRightm = (botRightPoint2[1] - botRightPoint1[1]) / (botRightPoint2[0] - botRightPoint1[0])
# b = y - (m * x)
topLeftB = topLeftPoint2[1] - (topLeftm * topLeftPoint2[0])
botLeftB = botLeftPoint2[1] - (botLeftm * botLeftPoint2[0])
topRightB = topRightPoint2[1] - (topRightm * topRightPoint2[0])
botRightB = botRightPoint2[1] - (botRightm * botRightPoint2[0])
# ratioTop height/width
ratioLeftImage = size1[1]/size1[0]
ratioRightImage = size2[1]/size2[0]
leftX = (botLeftB - topLeftB) / (ratioLeftImage + topLeftm - botLeftm)
topLeftY = (topLeftm * leftX) + topLeftB
image1 = image1.resize((int(leftX), int(leftX * ratioLeftImage))).convert("RGBA")
# this works because the shape is mirrored
maxImageWidth = (botLeftB - topLeftB) / (ratioRightImage + topLeftm - botLeftm)
rightX = background_size[0] - maxImageWidth
topRightY = (topRightm * rightX) + topRightB
image2 = image2.resize((int(maxImageWidth), int(maxImageWidth * ratioRightImage))).convert("RGBA")
# create white background and past the newly sized logos. Then add background on top
new_image = Image.new('RGB',background_size, (250,250,250))
new_image.paste(background, (0,0))
new_image.paste(image1,(0,int(topLeftY)), image1)
new_image.paste(image2,(int(rightX),int(topRightY)), image2)
new_image.save(outputFileName,"jpeg")