-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gate-Engine.py
186 lines (164 loc) · 7.79 KB
/
Gate-Engine.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#######################################################
# Gated Video Editor #
#######################################################
# Built by Sabian Hibbs #
# MIT LICENCE #
#######################################################
from contextlib import closing
from PIL import Image
from audiotsm import phasevocoder
from audiotsm.io.wav import WavReader, WavWriter
from scipy.io import wavfile
from pytube import YouTube
from shutil import copyfile, rmtree
from pytube import YouTube
import numpy as np
import re, subprocess, os, argparse, math
# This program is free software: you can redistribute it and/or modify
def downloadFile(url):
name = YouTube(url).streams.first().download()
newname = name.replace(' ','_')
os.rename(name,newname)
return newname
# it under the terms of the GNU General Public License as published by
def getMaxVolume(s):
maxv = float(np.max(s))
minv = float(np.min(s))
return max(maxv,-minv)
# the Free Software Foundation, either version 3 of the License, or
def copyFrame(inputFrame,outputFrame):
src = TEMP_FOLDER+"/frame{:06d}".format(inputFrame+1)+".jpg"
dst = TEMP_FOLDER+"/newFrame{:06d}".format(outputFrame+1)+".jpg"
if not os.path.isfile(src):
return False
copyfile(src, dst)
if outputFrame%20 == 19:
print(str(outputFrame+1)+" time-altered frames saved.")
return True
# (at your option) any later version.
def inputToOutputFilename(filename):
dotIndex = filename.rfind(".")
return filename[:dotIndex]+"_ALTERED"+filename[dotIndex:]
# This program is distributed in the hope that it will be useful,
def createPath(s):
try:
os.mkdir(s)
except OSError:
assert False, "Creation of the directory %s failed. Temp folder already exists?"
# but WITHOUT ANY WARRANTY; without even the implied warranty of
def deletePath(s): # Dangerous! Watch out!
try:
rmtree(s,ignore_errors=False)
except OSError:
print ("Deletion of the directory %s failed" % s)
print(OSError)
# args = parser.parse_args()
parser = argparse.ArgumentParser(description='Check Documentation for more info.')
parser.add_argument('--input_file', type=str, help='')
parser.add_argument('--url', type=str, help='Check Documentation for more info.')
parser.add_argument('--output_file', type=str, default="", help="Check Documentation for more info.")
parser.add_argument('--silent_threshold', type=float, default=0.03, help="Check Documentation for more info.")
parser.add_argument('--sounded_speed', type=float, default=1.00, help="Check Documentation for more info.")
parser.add_argument('--silent_speed', type=float, default=5.00, help="Check Documentation for more info.")
parser.add_argument('--frame_margin', type=float, default=1, help="Check Documentation for more info.")
parser.add_argument('--sample_rate', type=float, default=44100, help="Check Documentation for more info.")
parser.add_argument('--frame_rate', type=float, default=30, help="Check Documentation for more info.")
parser.add_argument('--frame_quality', type=int, default=3, help="Check Documentation for more info.")
args = parser.parse_args()
frameRate = args.frame_rate
SAMPLE_RATE = args.sample_rate
SILENT_THRESHOLD = args.silent_threshold
FRAME_SPREADAGE = args.frame_margin
NEW_SPEED = [args.silent_speed, args.sounded_speed]
if args.url != None:
INPUT_FILE = downloadFile(args.url)
else:
INPUT_FILE = args.input_file
URL = args.url
FRAME_QUALITY = args.frame_quality
assert INPUT_FILE != None , "No input file specified."
if len(args.output_file) >= 1:
OUTPUT_FILE = args.output_file
else:
OUTPUT_FILE = inputToOutputFilename(INPUT_FILE)
TEMP_FOLDER = "TEMP"
AUDIO_FADE_ENVELOPE_SIZE = 400
createPath(TEMP_FOLDER)
command = "ffmpeg -i "+INPUT_FILE+" -qscale:v "+str(FRAME_QUALITY)+" "+TEMP_FOLDER+"/frame%06d.jpg -hide_banner"
subprocess.call(command, shell=True)
command = "ffmpeg -i "+INPUT_FILE+" -ab 160k -ac 2 -ar "+str(SAMPLE_RATE)+" -vn "+TEMP_FOLDER+"/audio.wav"
subprocess.call(command, shell=True)
command = "ffmpeg -i "+TEMP_FOLDER+"/input.mp4 2>&1"
f = open(TEMP_FOLDER+"/params.txt", "w")
subprocess.call(command, shell=True, stdout=f)
sampleRate, audioData = wavfile.read(TEMP_FOLDER+"/audio.wav")
audioSampleCount = audioData.shape[0]
maxAudioVolume = getMaxVolume(audioData)
f = open(TEMP_FOLDER+"/params.txt", 'r+')
pre_params = f.read()
f.close()
params = pre_params.split('\n')
for line in params:
m = re.search('Stream #.*Video.* ([0-9]*) fps',line)
if m is not None:
frameRate = float(m.group(1))
samplesPerFrame = sampleRate/frameRate
audioFrameCount = int(math.ceil(audioSampleCount/samplesPerFrame))
hasLoudAudio = np.zeros((audioFrameCount))
# Find all the audio frames that have sound in them
for i in range(audioFrameCount):
start = int(i*samplesPerFrame)
end = min(int((i+1)*samplesPerFrame),audioSampleCount)
audiochunks = audioData[start:end]
maxchunksVolume = float(getMaxVolume(audiochunks))/maxAudioVolume
if maxchunksVolume >= SILENT_THRESHOLD:
hasLoudAudio[i] = 1
chunks = [[0,0,0]]
shouldIncludeFrame = np.zeros((audioFrameCount))
for i in range(audioFrameCount):
start = int(max(0,i-FRAME_SPREADAGE))
end = int(min(audioFrameCount,i+1+FRAME_SPREADAGE))
shouldIncludeFrame[i] = np.max(hasLoudAudio[start:end])
if (i >= 1 and shouldIncludeFrame[i] != shouldIncludeFrame[i-1]): # Did we flip?
chunks.append([chunks[-1][1],i,shouldIncludeFrame[i-1]])
chunks.append([chunks[-1][1],audioFrameCount,shouldIncludeFrame[i-1]])
chunks = chunks[1:]
outputAudioData = np.zeros((0,audioData.shape[1]))
outputPointer = 0
lastExistingFrame = None
# Go through each chunk and alter the speed
for chunk in chunks:
audioChunk = audioData[int(chunk[0]*samplesPerFrame):int(chunk[1]*samplesPerFrame)]
sFile = TEMP_FOLDER+"/tempStart.wav"
eFile = TEMP_FOLDER+"/tempEnd.wav"
wavfile.write(sFile,SAMPLE_RATE,audioChunk)
with WavReader(sFile) as reader:
with WavWriter(eFile, reader.channels, reader.samplerate) as writer:
tsm = phasevocoder(reader.channels, speed=NEW_SPEED[int(chunk[2])])
tsm.run(reader, writer)
_, alteredAudioData = wavfile.read(eFile)
leng = alteredAudioData.shape[0]
endPointer = outputPointer+leng
outputAudioData = np.concatenate((outputAudioData,alteredAudioData/maxAudioVolume))
if leng < AUDIO_FADE_ENVELOPE_SIZE:
outputAudioData[outputPointer:endPointer] = 0
else:
premask = np.arange(AUDIO_FADE_ENVELOPE_SIZE)/AUDIO_FADE_ENVELOPE_SIZE
mask = np.repeat(premask[:, np.newaxis],2,axis=1) # Repeat for stereo
outputAudioData[outputPointer:outputPointer+AUDIO_FADE_ENVELOPE_SIZE] *= mask
outputAudioData[endPointer-AUDIO_FADE_ENVELOPE_SIZE:endPointer] *= 1-mask
startOutputFrame = int(math.ceil(outputPointer/samplesPerFrame))
endOutputFrame = int(math.ceil(endPointer/samplesPerFrame))
for outputFrame in range(startOutputFrame, endOutputFrame):
inputFrame = int(chunk[0]+NEW_SPEED[int(chunk[2])]*(outputFrame-startOutputFrame))
didItWork = copyFrame(inputFrame,outputFrame)
if didItWork:
lastExistingFrame = inputFrame
else:
copyFrame(lastExistingFrame,outputFrame)
outputPointer = endPointer
# Write the altered audio and video to a new file
wavfile.write(TEMP_FOLDER+"/audioNew.wav",SAMPLE_RATE,outputAudioData)
command = "ffmpeg -framerate "+str(frameRate)+" -i "+TEMP_FOLDER+"/newFrame%06d.jpg -i "+TEMP_FOLDER+"/audioNew.wav -strict -2 "+OUTPUT_FILE
subprocess.call(command, shell=True)
deletePath(TEMP_FOLDER)